Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross module dependencies in Boost Python

Suppose I have two boost python 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. it uses SomeClass from module A). Now, I execute 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)

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

like image 787
Arjan Avatar asked Dec 14 '10 12:12

Arjan


1 Answers

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

Check out section 6 of the following doc:

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

6.1 - The Dynamic Binary

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

I was using the static binary and got the same type of error you were getting. Once I changed to the dynamic binary, it compiled and ran fine.

like image 133
Fat Elvis Avatar answered Sep 20 '22 00:09

Fat Elvis