Reputation: 211
Documentation of PyBindGen says that it is easy to write custom typehandlers for your own data types. However, I didn't find any good examples of writing them. Examples, which are supplied with pybindgen are too basic to understand the details and the documentation is unfortunately almost non-existing. What I need exactly is:
Is there any complete and well-documented example of adding custom typehandler to pybindgen? May be somebody did this for your own project and may share an approach?
An example of my current binding to better understand what I need. Done with Boost.python currently:
PyObject* System_getXYZ(System* s, int ind, int fr){
CREATE_PYARRAY_1D(p,3)
MAP_EIGEN_TO_PYARRAY(v,Vector3f,p)
v = s->XYZ(ind,fr);
return boost::python::incref(p);
}
...
class_<System, boost::noncopyable>("System", init<>())
...
.def("getXYZ", &System_getXYZ)
...
;
Method System::XYZ() return an Eigen::Vector3f object. It have to be converted to numpy array without copying its data. It's important because the whole idea is about efficiency. Currently it is done by means of ugly macros:
#define MAP_EIGEN_TO_PYARRAY(_matr,_T,_obj_ptr) \
if(!PyArray_Check(_obj_ptr)) throw pteros::Pteros_error("NumPy array expected!"); \
if(PyArray_TYPE(_obj_ptr)!=PyArray_FLOAT) throw pteros::Pteros_error("float NumPy array expected!"); \
Eigen::Map<_T> _matr((float*) PyArray_DATA(_obj_ptr), \
(PyArray_DIM((PyArrayObject*)_obj_ptr,0)==PyArray_Size(_obj_ptr)) ? PyArray_DIM(
(PyArray_DIM((PyArrayObject*)_obj_ptr,0)==PyArray_Size(_obj_ptr)) ? 1 : PyArray_
#define CREATE_PYARRAY_1D(_ptr_obj, _dim1) \
PyObject* _ptr_obj; \
{ \
npy_intp _sz_dim1[1];\
_sz_dim1[0] = _dim1; \
_ptr_obj = PyArray_SimpleNew(1, _sz_dim1, PyArray_FLOAT); \
}
It works, but I need to write a wrapper function for every single method, which takes Eigen objects, which is a nightmare to maintain. That's why I want to try PyBindGen and create a convertor there, which adds all the boilerplate code from these macros automatically.
Btw, boost.python converters doesn't help here since they force copying the data.
Upvotes: 3
Views: 296