Boost logo

Boost Users :

Subject: [Boost-users] [Boost::Python]
From: Eugenio Bargiacchi (svalorzen_at_[hidden])
Date: 2016-01-03 06:59:48


Hello everyone,

I have a member function with signature

 const Matrix2D & foo const ();

Which returns an internal data structure. Matrix2D is

 using Matrix2D = Eigen::Matrix<double, Eigen::Dynamic,
Eigen::Dynamic, Eigen::RowMajor | Eigen::AutoAlign>;

I have explicitly set a to_python converter for Matrix2D, and I have
exported the function with a call policy of return_internal_reference.
However, when this function is called from Python I get

TypeError: No Python class registered for C++ class
Eigen::Matrix<double, -1, -1, 1, -1, -1>

This happens also if the reference is not const. At the same time, if I
modify the call policy to return_value_policy<copy_const_reference>()
Python can decode the matrix just fine. The same holds true if I return the
Matrix2D by value. However I really need to have a reference to the matrix
in order store it into other classes.

How can I make Python see the data structure through the reference?

The converter in case it is needed:

boost::python::list toPythonList(const double * data, size_t num) {
    boost::python::list list;
    for (size_t i = 0; i < num; ++i)
        list.append(data[i]);

    return list;}
struct Matrix2DToPython{
    static PyObject* convert(Matrix2D const & m)
    {
        boost::python::list list;
        for (int i = 0; i < m.rows(); ++i)
        {
            list.append(toPythonList(m.row(i).data(), m.cols()));
        }

        return boost::python::incref(list.ptr());
    }};

Thanks,

Eugenio



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net