[C++-sig] [Boost::Python] Registered class not recognized when returned by reference.

Eugenio Bargiacchi svalorzen at gmail.com
Thu Jan 7 05:43:30 EST 2016


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20160107/24f58dea/attachment.html>


More information about the Cplusplus-sig mailing list