[C++-sig] Re: extracting C++ objects

James Mastro jmastro at rochester.rr.com
Mon Jul 19 01:22:17 CEST 2004


At 06:24 PM 7/18/2004, you wrote:

>Jim, I think you'd better try posting in plaintext.

Oops. You'd think C++ comments showing up in red in Eudora would have been 
a good clue that something was up. This should be OK. Sorry about that.

My problem is I can't extract a C++ object out of a Python object. MyPoint 
is a simple class storing doubles for x and y. SomeObject is an object 
onscreen, location is its location. Here's the Python:

import JimsModule       #where the wrapped MyPoint is

class SomeObject:

     def __init__(self):
         self.num = 942
         self.location = JimsModule.MyPoint( 3.2, -4.7 )

     def getPt(self):
         return self.location

     def getX(self):
         return self.location.x

Here's the C++:

handle<>        (PyImport_ImportModule( "SomeObject" ) );       // 
SomeObject class is in SomeObject.py module
handle<>        module( borrowed( PyImport_AddModule( "SomeObject" ) ) );
handle<>        the_namespace(borrowed( PyModule_GetDict(module.get()) ));
object          the_class( borrowed( 
PyDict_GetItemString(the_namespace.get(), "SomeObject") ));

object  obj = the_class();

handle<> a( PyObject_CallMethod( obj.ptr(), "getX", NULL ) );
double  val = extract<double>( a.get() );                       // returns 3.2
int     num = extract<int>(obj.attr("num"));            // returns 942

So I can extract POD fine. But what about getting my C++ MyPoint object out?

extract<MyPoint&> ck(obj.attr("location"));
if ( ck.check() ) {
     MyPoint&    myPt = extract<MyPoint&>(obj.attr("location"));
}

ck.check() returns false, and ignoring it and attempting the extraction 
anyway 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". Using the getPt method with call_method, etc, gave 
me the same error. Trying to get a MyPoint object, or a MyPoint* out gives 
me similar errors.

How can I pull my C++ object out of the Python object?

-jim




More information about the Cplusplus-sig mailing list