[C++-sig] Re: Automation of Python object retrieval

Pierre Barbier de Reuille pierre.barbier at cirad.fr
Sun Mar 28 11:22:42 CEST 2004

Sorry if I wasn't clear, but the problem is quite complex from my point
of view and I have problems to express it clearly.

Le dim 28/03/2004 à 07:33, David Abrahams a écrit :
> Pierre Barbier de Reuille <pierre.barbier at cirad.fr> writes:
> > I have a problem with the boost::python::object contruction. I'd want to
> > know if there is a way to automate the retrieval of the existing Python
> > object when I hold a Pointer on the C++ base class and that the class is
> > extended in Python. Here's an example of what I do now :
> >
> <snip big example with no mention of boost::python::object>
> > ======================
> >
> > All the equality tests fails because the other in detected as a Base
> > object and not a Derived object !!!
> detected where?

When a function returns a base class pointer, there is a mechanism to
detect the dynamic type of the pointer (whether it's the base class or
any Derived class exported in Python). But if you just construct a
boost::python::object from a pointer, the Python type of the
boost::python::object will be the type of the pointer (whatever the
dynamic type of the object could be).

> > But, if I replace the BaseWrap::operator== by :
> <snip some workaround>
> > I get the wanted behaviour. But I wonder if there is a cleaner method to
> > do that ? 
> Sorry, I stared at your example for 5 minutes and couldn't figure out
> what you were after.  Is there a reason that Holder can't hold a
> shared_ptr<Base> or a boost::python::object?

No, there's no reasons. In the code I use, I export the classes with
smart pointers ... but it does not resolve anything. My goal is the

I have an object pointed by a Base pointer. But the object's dynamic
type could be (and in practive it WILL be) a class derived from Base and
most probably the derivation occured in Python. What I want is :

 - if the dynamic type of the object is a Python class derived from Base
: construct the boost::python::object that holds the PyObject* given to
the class wrapper when constructed
 - if the dynamic type of the object is a C++ class derived from Base
and exported in Python using Boost : construct the boost::python::object
referencing the dynamic type of the object

The method I wrote do that, but it's not very clean. I hope I'm clear
enough here too :-/

Pierre Barbier de Reuille

INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP
Botanique et Bio-informatique de l'Architecture des Plantes
TA40/PSII, Boulevard de la Lironde

tel   : (33) 4 67 61 65 77    fax   : (33) 4 67 61 56 68 

More information about the Cplusplus-sig mailing list