Discussion:
[C++-sig] Magic boost conversion unicorn hook?
Ken Seehart (kseehart) [CONT - Type 2]
2015-09-24 22:36:02 UTC
Permalink
I have a python class that wraps a boost.python object. I am using a boost.python library function that requires the raw boost.python object. I want to be able to pass the python object to the boost api function.

Is there a hook available on the python side that instructs boost to perform a conversion?

Hypothetically, it would be something like this:

class EggWrapper(object):
def __init__(self, egg):
object.__setattr__(self, '_egg', egg)

def __getattribute__(self, name):
egg = object.__getattribute__(self, '_egg')

if name=='_egg':
return egg

if name in EggWrapper.__dict__:
return object.__getattribute__(self, name)

return getattr(egg, name)

def __setattr__(self, name, value):
egg = object.__getattribute__(self, '_egg')
setattr(egg, name, value)

def magic_boost_conversion_unicorn_hook(self):
'this is automatically called by boost to convert arguments'
egg = object.__getattribute__(self, '_egg')
return egg


import myboostlib

egg = EggWrapper(myboostlib.getEgg())

myboostlib.spam(egg)


Where the signature for spam is: spam(class boost::python::api::object)
And myboostlib.getEgg() returns a boost::python::api::object

- I do not have the ability to modify the library or any C/C++ code, the solution must be entirely on the python side.

- I can't change the calling convention. The wrapped object must be used (i.e. I can't just say myboostlib.spam(egg._egg), though that is the desired result). So the solution should be incorporated into the EggWrapper class.

- I'm only interested in solutions that reflect expert knowledge of Boost.Python, since I believe I have already considered all the trivial solutions.

So my question is, does magic_boost_conversion_unicorn_hook() exist, and if so, how is it spelled?


Thanks,
Ken
Stefan Seefeld
2015-09-25 15:20:48 UTC
Permalink
Post by Ken Seehart (kseehart) [CONT - Type 2]
I have a python class that wraps a boost.python object. I am using a
boost.python library function that requires the raw boost.python
object. I want to be able to pass the python object to the boost api
function.
I don't entirely understand what you are saying. What do you mean by
"boost.python object", and what by "boost.python library function" ?

Casting from Python to C++ types is typically done automatically by
Boost.Python.

To give an example:

You typcally start with a C++ class such as

class Foo {...};

which is exposed to Python via the usual magic. Likewise, you may have a
C++ function taking "Foo" arguments (by value or by reference), which
equally can be exposed to Python.

In Python, you can manipulate objects of type "Foo" (including
subclassing Foo), and pass them to these functions, and Boost.Python
will take care of the required conversion.

Could you please outline in a little more detail what it is that you are
attempting to do ? It sounds to me like you might be missing some
fundamental aspects of what Boost.Python does, as explicit type
conversion is rarely necessary, in particular from the Python side.

Regards,
Stefan
--
...ich hab' noch einen Koffer in Berlin...
Francesco Biscani
2015-09-26 17:30:53 UTC
Permalink
Post by Stefan Seefeld
I don't entirely understand what you are saying. What do you mean by
"boost.python object", and what by "boost.python library function" ?
I think he has a C++ class that was exposed to Python, and which is
included as a member of a pure Python class. I think he would like to be
able to "implicitly convert" said pure Python class to the type of the
exposed C++ class, so that it can be used directly as an argument to
exposed C++ functions that expect the exposed C++ class as argument.

Maybe a possible solution would be to register a Python converter for your
wrapping class? This blog post was helpful to me when I had to learn how to
write custom to/from Python converters:

http://misspent.wordpress.com/2009/09/27/how-to-write-boost-python-converters

Hope that helps! Cheers,

Francesco.

Loading...