[C++-sig] to_python / from_python guidance?

Alexis H. Rivera-Rios ahrivera at yahoo.com
Tue Aug 2 20:18:20 CEST 2005


Hi,

I'm trying to expose a class that uses a third party
library: gmtl.  The gmtl library has already bindings
for python.  I want to be able to do this:

struct Test
{
	gmtl::Vec3d x;
	gmtl::Point3d y;
	Test()
		: x(0,0,0),y(0,0,0) {}
	Test(const Test &other)
		: x(other.x),
		y(other.y){}

	Test& operator=(const Test& other)
	{
		if (this==&other)
			return *this;

		x = other.x;
		y = other.y;
	}	
};

Im exposing the class like this:

 class_< Test >("Test", init<  >())
        .def(init< const Test& >())
        .def_readwrite("x", &Test::x)
        .def_readwrite("y", &Test::y)  	
    ;

But this fails:
>>> import gmtl
>>> from PyStatATA import *
>>> test = Test()
>>> test.x = gmtl.Vec3d(0,0,1)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
Boost.Python.ArgumentError: Python argument types in
    None.None(Test, Vec3d)
did not match C++ signature:
    None(struct Test {lvalue}, class
gmtl::Vec<double,3>)
>>> test.y = gmtl.Point3d(0,0,1)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
Boost.Python.ArgumentError: Python argument types in
    None.None(Test, Point3d)
did not match C++ signature:
    None(struct Test {lvalue}, class
gmtl::Point<double,3>)
>>> my = test.y
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: No Python class registered for C++ class
class gmtl::Point<double,3>
>>> mx = test.x
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: No Python class registered for C++ class
class gmtl::Vec<double,3>

>From my understanding, to solve this I need to define
the to and from python converters.  But the examples
assume I have headers to the python type of
gmtl::Vec3d and gmtl::Point3d.  In my case, I don't
have that.   

What is the best approach to solve this?

Thanks,
Alexis

Programming Tutorial:
In Python: To do this, do this
In Perl: To do this, do this or this or this or this...
In C: To do this, do this, but be careful
In C++: To do this, do this, but don't do this, be careful of this, watch out for this, and whatever you do, don't do this

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the Cplusplus-sig mailing list