Sending a python argument to a Boost.Python function

kenseehart at gmail.com kenseehart at gmail.com
Wed Sep 23 15:02:07 EDT 2015


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








More information about the Python-list mailing list