I achieved to expose some C++ classes to python with the help of boost. I defined two distinct modules linked statically to my application. I can use the classes defined in those module, derived them, etc.
BOOST_PYTHON_MODULE(module1)
{
class_<MyClass, boost::noncopyable>("MyClass", no_init)
.enable_pickling();
}
However, i can’t pickle them because of an error not related to pickle. The __ module __ attribute is incorrect for my classes. Thus, pickle fail to get the class back. If my two python modules are “module1” and “module2”, and module1 defines a class names MyClass, the following code :
print(module1.MyClass.__name__)
print(module1.MyClass.__module__)
pickle.dumps(module1.MyClass,0)
will output
MyClass
module2
Traceback (most recent call last):
File "main.py", line 23, in <module>
pickle.dumps(module1.MyClass,0)
_pickle.PicklingError: Can't pickle <class 'module2.MyClass'>: attribute lookup
module2.MyClass failed
It proves that the __ module __ attribute isn’t filled properly for this class. I can’t find a workarround. I didn’t find any people having a similar problem.
Thanks for any help or suggestion.
According to other discussions, there is currently no ideal solution to this problem. However, there is a way to “manually” solve the problem by setting __ module __ field in the module initialisation.
This way, pickle won’t fail to identify the module where the class is defined.