PyCFunction_New() ?

Scott Deerwester scott at deerwester.org
Thu Sep 16 05:19:59 EDT 2004


Is it possible to create a Python-callable object, dynamically, in C/C++? I
have a GUI app in C++, with the app logic in Python, called from the
GUI via the Python C API. I need be able to call a callback function
in C/C++ from the Python, in response to an event (socket, whatever...)
that the Python is aware of. Callbacks in Python, called from C/C++
are very straightforward, but I haven't been able to find any examples
of the converse.

So I want to be able to do something like:

PyCFunction *myCallback(PyObject *ob, PyObject *args) { ... }

{
  PyObject *somePythonObject;
  ...
  PyObject *myCallbackObject = PyFunction_New(myCallback, ...);
  PyObject_CallMethod(somePythonObject, "setCallback", myCallbackObject);
  ...
}

Even better would be:

PyCFunction *MyClass::pyCallback(PyObject *ob, PyObject *args) { ... }

MyClass::MyClass()
{
  ...
  PyObject_CallMethod(somePythonObject, "setCallback", this->pyCallback);
  ...
}

Any help greatly appreciated!



More information about the Python-list mailing list