I am trying to expose a class (Child) with 2 members (val and parent) using boost.python. Both are public and parent is of type Child. The compilation gives no errors but my python wrapper brings the following when I try to access parent:
TypeError: No to_python (by-value) converter found for C++ type: class
I am pretty new to C++, python and therefore boost.python. I am probably missing something pretty obvious but I can't quite see what's wrong here.
I am using Visual Studio Community 2017 and python2.7 (both 64bit) on Windows 7.
Here are the different pieces of code:
class Child {
public:
Child();
double val;
Child* parent;
};
#include "Child.h"
Child::Child()
{
val = 0.0;
}
#include <boost/python.hpp>
#include "Child.h"
BOOST_PYTHON_MODULE(WrapPyCpp)
{
boost::python::class_<Child>("Child")
.def_readwrite("val", &Child::val)
.def_readwrite("parent", &Child::parent)
;
}
import WrapPyCpp
class_test = WrapPyCpp.Child()
class_test.val = 1.0
class_test_parent = WrapPyCpp.Child()
class_test.parent = class_test_parent
print class_test_parent.val
print dir(class_test)
print class_test.val
print class_test.parent.val
And when I launch Wrapper.py, the last line throws an exception:
C:\Users\Visual Studio 2017\Projects\MinExBoostPython2\x64\Debug>Wrapper.py
0.0
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__instance_size__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'parent', 'val']
1.0
Traceback (most recent call last):
File "C:\Users\Visual Studio 2017\Projects\MinExBoostPython2\x64\Debug\
Wrapper.py", line 12, in <module>
print class_test.parent.val
TypeError: No to_python (by-value) converter found for C++ type: class Child * __ptr64
C:\Users\Visual Studio 2017\Projects\MinExBoostPython2\x64\Debug>
I expect to access parent and its member val. So far as I understand the error, I imagine there is a problem with how I expose parent which is declared as a pointer. Am I correct?
I used .def_readwrite("parent", &Child::parent)
because it is a public member of Child but I am not sure that it is the right way to access it.
I tried to find informations in the boost.python documentation and took a look at
TypeError: No to_python (by-value) converter found for C++ type
and
Boost.Python call by reference : TypeError: No to_python (by-value) converter found for C++ type:
but if the solution for my problem is in there, I did not get it.
Anyway, I would be very grateful if anyone could help me to correct my mistake and explain me why I am wrong!
Thanks!
I figured out that, replacing the line
boost::python::class_<Child>("Child")
by
boost::python::class_<Child, Child*>("Child")
in Wrapper.cpp solved my problem. The result is then as expected:
C:\Users\Visual Studio 2017\Projects\MinExBoostPython2\x64\Debug>Wrapper.py
0.0
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__instance_size__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'parent', 'val']
1.0
0.0
C:\Users\Visual Studio 2017\Projects\MinExBoostPython2\x64\Debug>
Hope it helps someone!