simplest c python api callback example

Discussion in 'Python' started by Tim Spens, Jun 26, 2008.

  1. Tim Spens

    Tim Spens Guest

    The following is a simple complete example using the c python api to generate callbacks from c to python. But when I run the c code I get a segfault in PyInt_FromLong () (see below). Most of this example code was taken from pg 1478 of the 3rd edition python o'reilly book. I cannot see what I'm doing wrong here to get this segfault? Please help.

    //-------------------python-code-------------------------//
    #! /usr/bin/env python
    #######################################################
    # register for and handle event callbacks from C;
    # compile C code, and run with 'python register.py'
    #######################################################

    import time

    #
    # C calls these Python functions;
    # handle an event, return a result
    #
    def callback1(label, count):
    return 'callback1 called with args: label-%s and count-%i' % (label, count)
    #
    # Python calls a C extension module
    # to register handlers, trigger events
    #

    import cregister
    cregister.setpythonHandler(callback1)

    print '\nwaiting for callback pythonHandler to be called:'
    while 1:
    time.sleep(1)

    //-------------------compiler commands--------------//
    gcc -Wall -fno-strict-aliasing -g -I /usr/include/python2.5 -c cregister.c;gcc -g -Wall -I /usr/include/python2.5 -L /usr/local/lib -lpython2.5 cregister.c -o cregister;gcc -shared -fPIC -g -Wall -I /usr/include/python2.5 -L /usr/lib -lpython2.5 -o cregister.so cregister.o

    //-------------------c-code-------------------------//
    #include <Python.h>
    #include <stdlib.h>

    /***********************************************/
    /* 1) code to route events to Python object */
    /* note that we could run strings here instead */
    /***********************************************/

    //python handlers
    //keep Python object in C
    static PyObject *pythonHandler = NULL;

    void Route_Event(){
    PyObject *args;
    // call Python handler
    args = Py_BuildValue("(si)", "c code", 5);
    PyEval_CallObject(pythonHandler, args);
    }

    /*****************************************************/
    /* 2) python extension module to register handlers */
    /* python imports this module to set handler objects */
    /*****************************************************/
    static PyObject *
    Register_pythonHandler(PyObject *self, PyObject *args){
    // save Python callable object
    Py_XDECREF(pythonHandler); //called before?
    PyArg_Parse(args, "O", &pythonHandler); //one argument?
    Py_XINCREF(pythonHandler); //add a reference
    Py_INCREF(Py_None); //return 'None': success
    return Py_None;
    }

    //makes these functions available by importing cregister in python
    static struct PyMethodDef cregister_methods[] = {
    {"setpythonHandler", Register_pythonHandler},
    {NULL, NULL}
    };

    // this is called by Python on first "import cregister"
    void initcregister(){
    (void) Py_InitModule("cregister", cregister_methods);
    }

    int main(){
    while (1){
    PyObject *arglist;
    arglist = Py_BuildValue("(si)", "c code", 5);
    Py_XINCREF(pythonHandler);
    Py_XINCREF(arglist);
    PyEval_CallObject(pythonHandler, arglist);
    Py_XDECREF(pythonHandler);
    Py_XDECREF(arglist);
    sleep(1);
    }
    }

    Program received signal SIGSEGV, Segmentation fault.
    [Switching to Thread 0xb7c1c6b0 (LWP 13699)]
    0xb7e0bcfb in PyInt_FromLong () from /usr/lib/libpython2.5.so.1.0
    (gdb) bt
    #0 0xb7e0bcfb in PyInt_FromLong () from /usr/lib/libpython2.5.so.1.0
    #1 0xb7e8cae6 in ?? () from /usr/lib/libpython2.5.so.1.0
    #2 0x00000005 in ?? ()
    #3 0x00000006 in ?? ()
    #4 0xbf89a378 in ?? ()
    #5 0xb7e9d095 in _PyObject_GC_Malloc () from /usr/lib/libpython2.5.so.1.0
    #6 0xb7e8d1dd in ?? () from /usr/lib/libpython2.5.so.1.0
    #7 0x00000002 in ?? ()
    #8 0x08048320 in ?? ()
    #9 0x72d6dafa in ?? ()
    #10 0x00000029 in ?? ()
    #11 0xbf89a474 in ?? ()
    #12 0xbf89a478 in ?? ()
    #13 0x00000000 in ?? ()
     
    Tim Spens, Jun 26, 2008
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Francois De Serres

    (Win32 API) callback to Python, threading hiccups

    Francois De Serres, Jul 5, 2005, in forum: Python
    Replies:
    5
    Views:
    751
    Gregory Bond
    Jul 8, 2005
  2. Kenneth McDonald
    Replies:
    2
    Views:
    659
    John J. Lee
    Dec 29, 2006
  3. stef
    Replies:
    2
    Views:
    305
  4. Tim Spens
    Replies:
    1
    Views:
    1,090
    Matimus
    Jun 27, 2008
  5. Replies:
    3
    Views:
    381
Loading...

Share This Page