simplest c python api callback example

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Spens

    simplest c python api callback example

    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.setpy thonHandler(cal lback1)

    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_CallObje ct(pythonHandle r, args);
    }

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

    //makes these functions available by importing cregister in python
    static struct PyMethodDef cregister_metho ds[] = {
    {"setpythonHand ler", Register_python Handler},
    {NULL, NULL}
    };

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

    int main(){
    while (1){
    PyObject *arglist;
    arglist = Py_BuildValue(" (si)", "c code", 5);
    Py_XINCREF(pyth onHandler);
    Py_XINCREF(argl ist);
    PyEval_CallObje ct(pythonHandle r, arglist);
    Py_XDECREF(pyth onHandler);
    Py_XDECREF(argl ist);
    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_Ma lloc () 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 ?? ()





Working...