Registering a python function in C

Matimus mccredie at gmail.com
Thu Aug 30 19:27:03 EDT 2007


On Aug 30, 2:21 pm, fernando <fernandofariajun... at gmail.com> wrote:
> Could someone post an example on how to register a python function as
> a callback in a C function? It expects a pointer to PyObject... how do
> I expose that? Basically, the signature of the function is
> foo(PyObject* obj), where obj is the callback function... It's not
> exactly extending or embedding, I've looked at those examples but they
> don't really show how to do this...
>
> Thanks for the help!

It most definitely is extending or embedding, it just doesn't follow
the examples provided. I can't be too specific because I don't know
what your environment is like. As in, have you already initialized
python and all of that? Do you have access to _any_ PyObject*
variables?

Here is something that _might_ help (not tested):
[code]
PyObject* mod;
PyObject* func;

mod = PyImport_ImportModule("mymodule");
func = PyObject_GetAttrString( mod, "myfunc") ;
foo(func);
[/code]

It might be useful to look at the "Python/C API Reference Manual", not
just the extending and embedding stuff.

http://docs.python.org/api/api.html

Matt




More information about the Python-list mailing list