Problem on Python/C++

Martin von Loewis loewis at informatik.hu-berlin.de
Sun Sep 30 13:15:16 EDT 2001


clio <wangye1 at auburn.edu> writes:

> The problem is when I compile, it return a error in set_pymethod that it 
> can not convert the PyObject * to int (*())().

This is very tricky. You cannot use a Python object as a C function
pointer, nbecause it isn't one. Instead, you need to give a C function
as a function pointer, which will do PyObject_CallFunction, or the like.
You need to write this function in C; I doubt SWIG will generate
anything for you that could help. The code might look like

int the_callback()
{
  PyObject_CallFunction(obj,""); /* no args */
}

Now, the question is where the_callback should get its arguments
from. If it is indeed a callback, then it better allows some callback
data:

int the_callback(void *cb_data)
{
  PyObject *obj = cb_data;
  PyObject_CallFunction(obj, "");
}

You should then use the_callback as the function pointer.

Regards,
Martin




More information about the Python-list mailing list