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

Francois Ostiguy ostiguy at fnal.gov
Wed Aug 18 21:01:50 CEST 2004

> That's not a wrapper class; it's a smart pointer.

Dave  =

I apologize for the the poor wording. One might argue that the
particular smart pointer in my example is a "wrapper" around a regular
pointer. I agree that "wrapper class", especially in the context of
the boost.python library, has a more precise meaning.

Let me try to ask my questions differently.

Q1 -

Your explanation of how the conversion is handled is

"So the conversion is
 handled by the make_function call that created py_get_existing.
 However, you need class_<A...> in order to create the Python class
 object corresponding to A."

When make_function() converts a c++ A* into a python object "A",
"conversion" implies

(1) knowing what the python type A interface is, which is why class_<A,..>
    is needed.

(2) deciding what will be held in that object. In my case, the return
    value policy implies that I want the object to hold a dumb

What determines the valid held types ? Do I understand correctly that A*, A&,
auto_ptr<A> are automatically valid held types and therefore explicitly specifying
A* is uncessary ? In other words, the only held types that need to be explicitly
specified are those that are not already supported ?

Q2 -

When I do

class<A, boost:noncopyable>("A", no_init);
class<B,SmartPointer<B>, boost:noncopyable>("B", no_init);

A* a;
B* b;

object PythonInstanceofA ( a );                    //   error [1]
object PythonInstanceofB ( SmartPointer<B>(b) );   //   OK    [2]

according to your previous explanations:

[1] "fails because there is an attempt to copy "a" directly into the
     A object held by PythonInstanceofA"

object is generic object. By what mechanism would it know prior
to construction  that it should be holding an instance of A ?
I would think that  PythonInstanceofA  can only acquire its identity as a
type "A" by infering it from the argument in the constructor. So I do not
understand the statement "copy "a" directly into the A object held by

[2] "succeeds because Boost.Python recognizes that it's a wrapper class
     that contains a PyObject* referring to a Python object, and
     just extracts that pointer"

 Which "wrapper" class are you refering to ? SmartPointer<T> ?
 Does class<B, SmartPointer<B>, boost:noncopyable> register a
 converter from SmartPointer<B> to Python ?

Thank you in advance. Your patience is greatly appreciated ;-)


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