[C++-sig] Re: Problem using wrapped void* in Python

David Abrahams dave at boost-consulting.com
Mon Dec 1 12:01:09 CET 2003

Kor de Jong <k.dejong at geog.uu.nl> writes:

> Hi Ralf,
> Thank you for your reply.
> Ralf W. Grosse-Kunstleve wrote:
>>> So, what I am looking for is a way to tell Boost.Python how to
>>> convert from void* to Python and back. I have tried various
>>> things, like the opaque_pointer_converter and the
>>> to_python_converter, but I end up with code which I don't
>>> understand anymore and/or which doesn't compile. I would be very
>>> grateful if one of you could provide me with a bit of code to
>>> get me going again.
>> Recently Niall Douglas added this to the Boost.Python FAQ:
>> http://cci.lbl.gov/~rwgk/shortcuts/boost/libs/python/doc/v2/faq.html#voidptr
>> Does this help in your case?
> I see how this works in the case of converting void* function
> arguments. This will be useful for me too. But in my current case I
> am wrapping a struct which has a void* datamember. Accessing the
> enum/int/string datamembers of the struct from Python goes fine,
> but accessing the void* datamember is a problem and gives:
> TypeError: No to_python (by-value) converter found for C++ type: Pv
> Again, I don't want to access the actual data pointed to by the
> pointer, I just want to pass it along to wrapped functions.
> I might be missing something obvious, but the docs and mailinglist
> archives didn't help me further.
> Does anyone have something to say about converting void* to Python and back?

If you want it to appear to be an attribute in Python, just write a
getter (and optional setter) function using the "opaque void_*"
technique, and use class_<...>.add_property("name", getter, setter) to
expose it.

Dave Abrahams
Boost Consulting

More information about the Cplusplus-sig mailing list