[C++-sig] Re: creating an instance of a class_<> object in c++ and exporting it to python

Francois Ostiguy ostiguy at fnal.gov
Tue Aug 17 17:27:31 CEST 2004


> >> As Mike and I wrote, just do this:
> >>
> >>   object PythonInstanceOfA  = py_get_existing();
> >>
> >
> > Unfortunately, that does not seem to do quite what I want.
> > I constructed a simple complete toy example to demonstrate.
> >

Dave, Mike, and all the others that took the time to reply to my posts:

Your patience is greatly appreciated.

I tried too many things ... and I should have tested my toy example more
carefully before posting it. The bottom line is that by following your
instructions I got my example to work just as expected. Although I think
I understand what it going on, I would be interested in your feedback.

Tell me if I got this right:

I understand that

object PythonInstanceOfA  = py_get_existing();

[ or alternatively, object PythonInstanceOfA( py_get_existing() ); ]

creates python object wrapper and initializes it with the the object
returned by  py_get_existing(). Since neither py_get_existing() or
PythonInstanceOfA explicitly know anything about the "A" object
exported type, I presume this means that instantiating

class_< A, A*, boost:noncopyable>

registers a type converter from A* to Python and it is through this
converter that the object becomes an instance of the Python type A.

Does this mean that only one Python type can/should be declared as
holder of a  A* ?  In other words, is it possible to declare two Python
types, say A1 and A2, both holding a A* but with different
exported interfaces ?

Finally, the  py_get_existing() gymnastics was required because

object PythonInstanceOfA( a ) ; // a is a raw ptr to A

does not work. But, apparently

object PythonInstanceOfA( aw ) ; // aw is an instance of a wrapper
                                    class which defines get_pointer()
does work.

Can you briefly explain what is happening ? At what point is the type
conversion occuring ? My reading of the documentation led me into thinking
that although the get_pointer() method is not defined, conversion from raw
pointers (and references) should automatically be supported.

Again, Thanks !

-Francois

----------------------------------------------------------------------------
Dr. Jean-Francois OSTIGUY                              voice: (630) 840-2231
Beam Physics Dept MS220                                  FAX: (630) 840-6039
Fermi National Accelerator Laboratory                email: ostiguy at fnal.gov
Batavia IL 60510-0500                           WWW:www-ap.fnal.gov/~ostiguy




More information about the Cplusplus-sig mailing list