[C++-sig] Problem with PyMethodDef and non-static functions
seefeld at sympatico.ca
Thu Sep 28 20:50:20 CEST 2006
Jon Kristensen wrote:
> Hello list!
> I'm using Python/C to be able to have simple Python scripts in my
> application. I was free of problems until I switched from using static
> and global functions to using non-static class methods:
> python.cpp:101: error: argument of type ‘PyObject* (Python::)(PyObject*,
> PyObject*)’ does not match ‘PyObject* (*)(PyObject*, PyObject*)’
> After some time I understood that this was a problem that a little
> tweaking around wouldn't solve. So I want to ask you guys if you can
> describe how to get around this. I wouldn't want to add any additional
> dependencies such as SWIG or Boost. Please also note that I'm not that
> experienced with Python.
As you have found out, ordinary pointers and pointers to members are
fundamentally incompatible with each other, i.e. there is no way to
cast from one to the other.
You have to implement a wrapper around your method call. Something like:
PyObject *my_method(PyObject *self, PyObject *args)
CxxType *object = extract_object_from_self(self);
int arg = extract_arg(args);
int result = object->func(arg);
But of course, all this forward and backward extraction / conversion
business makes the above highly inconvenient and non-scalable, so you may
indeed want to give boost.python a try as your project grows. :-)
...ich hab' noch einen Koffer in Berlin...
More information about the Cplusplus-sig