It is possible to pass a Python callback to C-extensions?

Discussion in 'Python' started by Pierre Rouleau, Aug 20, 2003.

  1. I have a GUI application written in Python (with WxPython) which uses
    some low level classes also written in Python. These low level Python
    classes are given a pointer to some GUI Python object. The low level
    Python classes callback some of the methods of the GUI object. All
    works fine.

    Now I want to convert the low level Python classes to C++. I will be
    using SWIG to create the marshaling so the top level GUI, written in
    Python stays that way.

    The problem: is it possible to continue using call backs? The C++ code
    would need to call the Python methods back. Is there an easy way to do
    this?

    This is a little different from extending or embedding. I have a Python
    program that uses Python extensions written in C++. This C++ code would
    need to pass some information back to the Python code (and currently,
    the all-round Python code uses call backs).

    My quick review of the SWIG documentation did not help me find a
    solution to this.

    Thanks in advance for any help.

    /Pierre
     
    Pierre Rouleau, Aug 20, 2003
    #1
    1. Advertising

  2. Pierre Rouleau

    Robin Becker Guest

    In article <OKM0b.2109$>, Pierre Rouleau
    <> writes
    >I have a GUI application written in Python (with WxPython) which uses
    >some low level classes also written in Python. These low level Python
    >classes are given a pointer to some GUI Python object. The low level
    >Python classes callback some of the methods of the GUI object. All
    >works fine.
    >
    >Now I want to convert the low level Python classes to C++. I will be
    >using SWIG to create the marshaling so the top level GUI, written in
    >Python stays that way.
    >
    >The problem: is it possible to continue using call backs? The C++ code
    >would need to call the Python methods back. Is there an easy way to do
    >this?
    >

    certainly you can call back into python, I have some typical code that
    looks like

    PyObject *callback;
    ........
    PyObject *arglist;
    PyObject *result;

    arglist = Py_BuildValue("(s)",buf);
    result = PyEval_CallObject(callback, arglist);
    Py_DECREF(arglist);
    if(result){
    Py_DECREF(result);
    /*success*/
    }
    else {
    /*handle error*/
    }

    Of course you need to get the definition of callback somehow. But that
    is easy via argument passing or obtaining a global function.

    >This is a little different from extending or embedding. I have a Python
    >program that uses Python extensions written in C++. This C++ code would
    >need to pass some information back to the Python code (and currently,
    >the all-round Python code uses call backs).
    >
    >

    ......
    >Thanks in advance for any help.
    >
    >/Pierre
    >


    --
    Robin Becker
     
    Robin Becker, Aug 20, 2003
    #2
    1. Advertising

  3. Sure. Explanation by example:

    Python code:

    #v+
    def myfunc(): pass

    foobar.register_callback(myfunc)
    #v-

    C code:

    #v+
    PyObject* register_callback(FooType* self, PyObject* args, PyObject**
    kwargs)
    {
    /* ParseTupleAndKeywords */
    /* PyCallable_Check */
    self->callback = ...
    }

    ....
    function_result = PyObject_CallObject(callback, calling_args);
    ....
    #v-

    No idea about SWIG, but I do this a lot in the PySQLite code using
    Python's raw C API, which you could use as an example:

    http://cvs.sourceforge.net/cgi-bin/...rev=HEAD&content-type=text/vnd.viewcvs-markup

    HTH,

    -- Gerhard
     
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=, Aug 20, 2003
    #3
    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. André
    Replies:
    2
    Views:
    502
    André
    Jun 23, 2006
  2. Harri Pesonen
    Replies:
    6
    Views:
    525
    David Abrahams
    Sep 3, 2003
  3. Replies:
    4
    Views:
    291
    Kenny McCormack
    Feb 21, 2006
  4. Replies:
    1
    Views:
    538
    mlimber
    Jun 7, 2006
  5. JBT
    Replies:
    2
    Views:
    164
    Grant Edwards
    Sep 24, 2012
Loading...

Share This Page