| Store | Cart

[C++-sig] extracting a c++ object from a PyObject

From: David Abrahams <d...@boost-consulting.com>
Thu, 27 Feb 2003 15:08:59 -0500
Francois Ostiguy <ostiguy at 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

Recent Messages in this Thread
Francois Ostiguy Feb 27, 2003 06:47 pm
David Abrahams Feb 27, 2003 08:08 pm
Messages in this thread