Magic boost conversion unicorn hook?
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
On 24.09.2015 18:36, Ken Seehart (kseehart) [CONT - Type 2] wrote:
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...
On 25 September 2015 at 17:20, Stefan Seefeld <stefan@seefeld.name> wrote:
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-converter... Hope that helps! Cheers, Francesco.
participants (3)
-
Francesco Biscani -
Ken Seehart (kseehart) [CONT - Type 2] -
Stefan Seefeld