array of arrays question

2005-08-30 Thread Meo
Hi,

Here is what I'm doing in python 2.4.1 :

>>> aOfa=[[[]]*3]*4
>>> aOfa
[[[], [], []], [[], [], []], [[], [], []], [[], [], []]]
>>> aOfa[3][2].append(1)
>>> aOfa
[[[1], [1], [1]], [[1], [1], [1]], [[1], [1], [1]], [[1], [1], [1]]]

Ok, so there is something I didn't understand about python's arrays.

The result I was expecting was :

[[[], [], []], [[], [], []], [[], [], []], [[], [], [1]]]

Somebody understand what's going on here?

Thank

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: array of arrays question

2005-08-30 Thread Meo
yes,

thank you for your help

-- 
http://mail.python.org/mailman/listinfo/python-list


embedded python doesn't like socket.accept() and SegFaults

2008-08-02 Thread Riccardo Di Meo

Hi everyone,

I'm practicing with embedding python into C code and i have encountered 
a very strange problem: I'm unable to call the "accept" method of a 
(correctly created) server socket without receiving a "Segmentation 
fault" (inside the PyObject_CallMethod).


My code  to be correct (at least it's correct enough for me to 
call .getsockname(),  .fileno() and other methods without problems), I'm 
pretty new to this thing though, therefore I'm confident I'm doing 
something very dumb.


Here is the C code:

-- extending.c
#include 

PyObject *new_server(long port)
{
 PyObject *pName,*pModule;
 PyObject *pFunction,*pArg;
 PyObject *pTuple,*pValue;

 // Import the module
 pName = PyString_FromString("extending");
 pModule = PyImport_Import(pName);
 Py_DECREF(pName);

 // Get the function
 pFunction = PyObject_GetAttrString(pModule,
"server_socket");

 // Module not needed anymore
 Py_DECREF(pModule);

 // Build the arguments
 pArg=PyInt_FromLong(port);
 pTuple=PyTuple_New(1);
 PyTuple_SET_ITEM(pTuple,0,pArg);

 // Call the function
 pValue = PyObject_CallObject(pFunction,
  pTuple);

 // Release the references
 Py_DECREF(pFunction);
 Py_DECREF(pTuple);

 if(pValue==NULL)
   printf("Error: server socket not created!\n");

 return pValue;
}

PyObject *accept(PyObject *server)
{
 PyObject *pValue;

 // Code fails here (it does NOT return NULL: just crashes!)
 // Note that other calls work fine (e.g. fileno, getsockname ecc)
 pValue = PyObject_CallMethod(server,
  "accept",
  NULL);
 return pValue;
}

int main(int argc,char *argv[])
{
 PyObject *server,*connection;

 // Boot python
 Py_Initialize();
 PySys_SetArgv(argc, argv);

 // Create the server
 server=new_server(23000);

 // Print it
 PyObject_Print(server,stderr,0);
 fprintf(stderr,"\n");

 // Wait for a connection
 connection=accept(server);

 // See what we got
 PyObject_Print(connection,stderr,0);
 fprintf(stderr,"\n");

 // We are done, hint the gc.
 Py_DECREF(connection);
 Py_DECREF(server);

 Py_Finalize();
 return 0;
}
--

and this is the python script:

-- extending.py
import socket

def server_socket(port):
   s=socket.socket()
   s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,True)
   s.bind(("0.0.0.0",port))
   s.listen(10)
   return s


as already mentioned, replacing the "accept" string with "fileno" or 
something else doesn't crash the interpreter.


Another thing worth mentioning, is that even inserting a s.accept() call 
in the python script (before the return) makes the bug appear (it 
doesn't seems related to my use of the PyObject_CallMethod function, then).


I have tried posting the problem  in IRC, searching google (no good 
matches) and debugging the code (however I'm afraid i don't have the 
python-lib with debugging  syms. compiled in, therefore it was quite a 
useless attempt...).


Any help about the code would be appreciated (even unrelated to the 
issue at hand: im quite new to this "embedding thing" and therefore  i 
gladly accept hints).


Thank you for your attention,
Riccardo Di Meo

PS: I'm also new to Usenet: is it fine to post the code in the body of 
the mail as i did (since it was small, i dared: however I'd like to know 
the correct etiquette)?


--
http://mail.python.org/mailman/listinfo/python-list


Re: embedded python doesn't like socket.accept() and SegFaults

2008-08-02 Thread Riccardo Di Meo

Solved (with the help of the guys on #python on freenode).

Long story short: i forgot the static in the function definitions and 
the libc's "accept" got replaced with mine...




Riccardo Di Meo wrote:

Hi everyone,

I'm practicing with embedding python into C code and i have 
encountered a very strange problem: I'm unable to call the "accept" 
method of a (correctly created) server socket without receiving a 
"Segmentation fault" (inside the PyObject_CallMethod).


My code  to be correct (at least it's correct enough for me to 
call .getsockname(),  .fileno() and other methods without problems), 
I'm pretty new to this thing though, therefore I'm confident I'm doing 
something very dumb.




--
http://mail.python.org/mailman/listinfo/python-list