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

P

Pierre Rouleau

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
 
R

Robin Becker

Pierre Rouleau said:
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.
 
?

=?ISO-8859-1?Q?Gerhard_H=E4ring?=

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
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top