It is possible to pass a Python callback to C-extensions?
Gerhard Häring
gh at ghaering.de
Wed Aug 20 12:28:12 EDT 2003
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/viewcvs.cgi/pysqlite/pysqlite/_sqlite.c?rev=HEAD&content-type=text/vnd.viewcvs-markup
HTH,
-- Gerhard
More information about the Python-list
mailing list