interrogate & callbacks

Is there a way to have callbacks (C++ to Python) using interrogate?

The only thing I can up right now is to have published C++ functions which accept PyObject* as argument, store the PyObject* and then call them by hand (I mean using the Python C++ API, with argument C++ to Python conversion and everything).


I don’t think interrogate provides any means of doing that.

However, it is interesting that you succeeded in doing it manually. (Ie, using the python 2.5 API.) I might actually be interested in borrowing that trick.

We usually use the event system to “call” from C++ into python — ie, the C++ code sends an event “foo”, and then a DirectObject somewhere accepts the “foo” and invokes a method. Of course, this doesn’t work for all cases - sometimes you really need a hook, not an event.

Thanks for the answer. I will dig through the event system, though I am not 100% sure that this will work in my case, since I (think) I need an immediate return value from the callback. But perhaps I can find workarounds.

Calling Python functions from C++ isn’t very hard, and works for all Python version. Python functions are PyObject* like everything else in Python (instances, types, integers, tuples, …). In principle you just have to call them using PyEval_CallObject. The Python documentation explains this far better than I ever would be able to:

Another way is to write an PyTypeObject in C++, derive from this type on the Python layer, and then call member methods from C++ using PyObject_CallMethod( self, “method_name”, /argument-format and args/ ). This is my preferred way. I can give a short example if you want.


We do occasionally use this method in Panda code to make Python callbacks from C++. In particular, we use it in direct/src/dcparser and direct/src/distributed.

Though in general, it is also true that we try to avoid doing this in the core Panda codebase, since we like to imagine that we support other languages than Python, and we don’t want to bind core Panda too intimately with Python.