extracting a c++ object from a PyObject
This is a follow-up question to a recent thread. Dave Abrahams provided the following code to extract a c++ object from a PyObject void f(PyObject* p) { handle<> ph(borrowed(p)); MyObj* op = extract<MyObj*>(object(ph)); } this works beautifully, but appenrently fails in the case where MyObj is a simple function. That is typedef void (fcntptr*)(); void f(PyObject* p) { handle<> ph(borrowed(p)); fcntptr op = extract<fcntptr>(object(ph)); } results in the following compilation error message. /usr/local/include/boost/type_traits/add_cv.hpp:34: `const volatile' qualifiers cannot be applied to `void ()()' As always, any help/insight is greatly appreciated. -Francois ---------------------------------------------------------------------------- Dr. Jean-Francois OSTIGUY voice: (630) 840-2231 Beam Physics Dept MS220 FAX: (630) 840-6039 Fermi National Accelerator Laboratory email: ostiguy@fnal.gov Batavia IL 60510-0500 WWW:www-ap.fnal.gov/~ostiguy
Francois Ostiguy <ostiguy@fnal.gov> writes:
This is a follow-up question to a recent thread. Dave Abrahams provided the following code to extract a c++ object from a PyObject
void f(PyObject* p) {
handle<> ph(borrowed(p)); MyObj* op = extract<MyObj*>(object(ph));
}
this works beautifully, but appenrently fails in the case where MyObj is a simple function. That is
typedef void (fcntptr*)();
void f(PyObject* p) {
handle<> ph(borrowed(p)); fcntptr op = extract<fcntptr>(object(ph));
}
results in the following compilation error message.
/usr/local/include/boost/type_traits/add_cv.hpp:34: `const volatile' qualifiers cannot be applied to `void ()()'
As always, any help/insight is greatly appreciated.
The compilation error occurs because Boost.Python isn't set up to convert Python objects to function pointers. It's not a crazy idea to do so if, for example, you have a Python object which actually contains a C++ function pointer in the first place, but that's not implemented. If you're trying to convert arbitrary Python functions to C++ function pointers... Can you figure out how to turn an arbitrary Python object into a C++ function pointer at runtime, using just the Python 'C' API? If the C++ function pointed to always does the same thing regardless of the source Python object, you can... but that's not a very interesting case. If you want the C++ function to do something different based on the Python function passed (e.g. call the Python object), where would the code for that C++ function come from? -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (2)
-
David Abrahams -
Francois Ostiguy