Cross-Module Dependencies in Boost Python

Suppose I have two python boost modules that are defined as follows. Module A:

class SomeClass { public: SomeClass() {} ~SomeClass() {} }; BOOST_PYTHON_MODULE(A) { class_<SomeClass>("SomeClass"); } 

And module B:

 class AnotherClass { public: AnotherClass() {} ~AnotherClass() {} void func(SomeClass& sp) {} }; BOOST_PYTHON_MODULE(B) { class_<AnotherClass>("AnotherClass") .def("func", &AnotherClass::func) ; } 

Module B has a dependency on module A (i.e. uses SomeClass from module A). Now I am running the following python script:

 import A import B obj1 = A.SomeClass() obj2 = B.AnotherClass() obj2.func(obj1) 

I get the following error:

 Traceback (most recent call last): File "C:\bladiebla\script.py", line 8, in <module> obj2.func(obj1) ArgumentError: Python argument types in AnotherClass.func(AnotherClass, SomeClass) did not match C++ signature: func(class AnotherClass {lvalue}, class SomeClass) 

Python does not seem to automatically translate classes between modules. Does anyone have an idea how to solve this?

+6
c ++ python boost
source share
2 answers

I just recently started messing around with Boost.Python and had the same problem.

Review section 6 of the following document:

http://www.boost.org/doc/libs/1_47_0/libs/python/doc/building.html

6.1 - Dynamic Binary

The library contains a type conversion registry. Because one registry is shared among all extension modules, instances of the class exposed to Python in one dynamically loaded extension module can be passed to functions open in another such module .

I used a static binary and got the same type of errors as you. As soon as I switched to the dynamic binary, it compiled and works fine.

+6
source share

Depending on your last answer and the updated error message in your question, I think the problem may be that the use of your BOOST_PYTHON_MODULE may be wrong (based on what I saw in other examples of its use). Try something like this and see if this helps:

Module A:

 class SomeClass { public: SomeClass() {} ~SomeClass() {} }; BOOST_PYTHON_MODULE(A) { boost::python::class_<SomeClass>("SomeClass"); } 

And module B:

 class AnotherClass { public: AnotherClass() {} ~AnotherClass() {} void func(SomeClass& sp) {} }; BOOST_PYTHON_MODULE(B) { boost::python::class_<AnotherClass>("AnotherClass") .def("func", &AnotherClass::func) ; } 

Note the insertion of the prefix " boost::python:: " into the class_<...> statement in each of the two BOOST_PYTHON_MODULE .

0
source share

All Articles