Hello all, I want to export a class 'A' from an external library to python. In python I need to extend the class and I need to tell the library that it will 'own' the object.
I have tried the example in: http://wiki.python.org/moin/boost.python/HowTo (chapter "ownership of C++ object") And executing the code I send in attachment I get: $ python test1.py Traceback (most recent call last): File "test1.py", line 19, in <module> main() File "test1.py", line 15, in main print(a.get_int()) Boost.Python.ArgumentError: Python argument types in A.get_int(B) did not match C++ signature: get_int(A {lvalue}) Can you tell me what I am missing in the code? Thank you in advance PS: Sorry quality of the Makefile. Regards Tiago
Makefile
Description: Binary data
// c++ -o test1 test1.cpp -lboost_python -I/usr/include/python2.7 -lpython2.7 #include <memory> #include <boost/python.hpp> using namespace std; using namespace boost::python; class A { public: A() {} virtual ~A() {} int get_int() { return 22; } }; class AWrap: public A { public: PyObject *self; AWrap(PyObject* self_): self(self_) { Py_INCREF(self); } ~AWrap() { Py_DECREF(self); } }; class Owner { public: A* a; Owner(): a(0) {} virtual ~Owner() { if (a!=0) delete a;} void set_A(A* a_) { a=a_; } }; void set_A(Owner& self, auto_ptr<A> a_) { self.set_A(a_.get()); a_.release(); } BOOST_PYTHON_MODULE(_test1) { class_<A, auto_ptr<AWrap>, boost::noncopyable >("A") .def("get_int", &A::get_int) ; implicitly_convertible<auto_ptr<AWrap>, auto_ptr<A> >(); class_<Owner>("Owner") .def("set_A", &set_A) ; }
test1.py
Description: Binary data
_______________________________________________ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig