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

  • Thread starter Riccardo Di Meo
  • Start date
R

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 <seems> 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 <python2.5/Python.h>

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)?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top