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

David Abrahams dave at boost-consulting.com
Tue Aug 17 15:01:51 CEST 2004


Francois Ostiguy <ostiguy at fnal.gov> writes:

>>
>> 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.
>
> Note the following:
>
> (1) creating python_object_a forces an undesired instantiation of
>     a type A (i.e. in addition to the the existing instance a)

So don't do that.  It's not needed and it wasn't in the code Mike
posted.  Please just read his directions very carefully and don't add
anything else.

> (2) if the interface defines a python object without
>      an __init__ function, the result is a runtime error:
>      "Object cannot by created from python."

The result of what?

> (3) if you compile the example, here is what you get when the
>     module is imported from python
>
>
>   Python 2.3.3 (#1, Jul  7 2004, 13:43:38)
>   [GCC 3.3.3] on linux2
>   Type "help", "copyright", "credits" or "license" for more information.
>   >>> from inject import *
>   >>> a
>   <inject.A object at 0xb733b10c>
>   >>> dir()
>   ['A', '__builtins__', '__doc__', '__name__', 'a']
>   >>> dir (a)
>   ['A', '__class__', '__delattr__', '__dict__', '__doc__',
>   '__getattribute__', '__hash__', '__init__', '__instance_size__',
>   '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
>   '__setattr__', '__str__', '__weakref__']
>   >>>
>
>   Note that the object instance "a" has been injected. However, for a
>   reason that escapes me, the method "dummy" which is
>   part of the specified interface is not correctly exported.

Because you called it "A", not "dummy".

>   Again, my goal is to wrap an existing instance of A, without
>   having to create a spurious object (basically without having to
>   call the A constructor more than once).
>
>   Comments, suggestions ???

Read and follow directions more carefully.  Sorry to be blunt, but
that is the best I can do.

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com




More information about the Cplusplus-sig mailing list