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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Riccardo Di Meo

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

    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 "Segmentati on
    fault" (inside the PyObject_CallMe thod).

    My code <seemsto 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(lon g port)
    {
    PyObject *pName,*pModule ;
    PyObject *pFunction,*pAr g;
    PyObject *pTuple,*pValue ;

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

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

    // Module not needed anymore
    Py_DECREF(pModu le);

    // Build the arguments
    pArg=PyInt_From Long(port);
    pTuple=PyTuple_ New(1);
    PyTuple_SET_ITE M(pTuple,0,pArg );

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

    // Release the references
    Py_DECREF(pFunc tion);
    Py_DECREF(pTupl e);

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

    return pValue;
    }

    PyObject *accept(PyObjec t *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_CallMe thod(server,
    "accept",
    NULL);
    return pValue;
    }

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

    // Boot python
    Py_Initialize() ;
    PySys_SetArgv(a rgc, argv);

    // Create the server
    server=new_serv er(23000);

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

    // Wait for a connection
    connection=acce pt(server);

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

    // We are done, hint the gc.
    Py_DECREF(conne ction);
    Py_DECREF(serve r);

    Py_Finalize();
    return 0;
    }
    ----------

    and this is the python script:

    ---------- extending.py
    import socket

    def server_socket(p ort):
    s=socket.socket ()
    s.setsockopt(so cket.SOL_SOCKET ,socket.SO_REUS EADDR,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_CallMe thod 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)?

Working...