<html>
<body>
Hello again list. I'm back in the boost.python game after a number of
months off, this time on Win XP.<br><br>
I want to be able to create Python instances from C++. These Python
instances hold wrapped C++ instances that I want to extract. For example,
I have a class called SomeObject that holds a wrapped C++ class called
MyPoint which is it's location onscreen. MyPoint is a class holding a
double for both x and y. Here's the Python code:<br><br>
<br>
<font face="Arial, Helvetica">import
JimsModule<x-tab> </x-tab>#where
the wrapped MyPoint is<br><br>
class SomeObject:<br><br>
def __init__(self):<br>
self.location =
JimsModule.MyPoint( 3.2, -4.7 )<br>
<br>
def getPt(self):<br>
return self.location<br>
<br>
def getX(self):<br>
return
self.location.x<br><br>
<br>
In C++ I have the following:<br><br>
handle<><x-tab> </x-tab>(PyImport_ImportModule(
"SomeObject" )
);<x-tab> </x-tab>//<x-tab> </x-tab>SomeObject
class is in SomeObject.py module<br>
handle<><x-tab> </x-tab>module(
borrowed( PyImport_AddModule( "SomeObject" ) ) );<br>
handle<><x-tab> </x-tab>the_namespace(borrowed(
PyModule_GetDict(module.get()) ));<br>
object<x-tab> </x-tab><x-tab> </x-tab>the_class(
borrowed( PyDict_GetItemString(the_namespace.get(),
"SomeObject") ));<br><br>
object obj = the_class();<br>
double<x-tab> </x-tab> x = call_method<double>(
obj.ptr(), "getX" );<x-tab> </x-tab>// returns 3.2
as it should.<br><br>
handle<> a( PyObject_CallMethod( obj.ptr(), "getX", NULL
) );<br>
</font><font face="Arial, Helvetica" color="#0000FF">double</font><font face="Arial, Helvetica">
val =
extract<</font><font face="Arial, Helvetica" color="#0000FF">double</font><font face="Arial, Helvetica">>(
a.get()
);<x-tab> </x-tab><x-tab> </x-tab><x-tab> </x-tab>//
Also returns 3.2<br><br>
I can extract POD fine. But what about getting my C++ MyPoint object
out?<br><br>
handle<> h( PyObject_CallMethod( obj.ptr(), "getPt", NULL
) );<br>
MyPoint& pt = extract<MyPoint&>( h.get() );<br><br>
This throws an exception saying "TypeError: No registered converter
was able to extract a C++ reference to type class MyPoint from this
Python object of type MyPoint". Obviously I am doing something
wrong, but I haven't a clue what.The PyObject* returned from
PyObject_CallMethod is OK if I print it out. But extracting the C++
object throws. I tried extracting a MyPoint object and a MyPoint* with
similar results.<br><br>
How can I pull my C++ object out of the Python object?<br><br>
-jim</font></body>
<br>
</html>