<div dir="ltr"><p>Hello everyone,<br></p><p>I have a member function with signature</p>
<pre><code><span> </span><span>const</span><span> </span><span>Matrix2D</span><span> </span><span>&</span><span> foo </span><span>const</span><span> </span><span>();</span></code></pre>
<p>Which returns an internal data structure. <code>Matrix2D</code> is</p>
<pre><code><span> </span><span>using</span><span> </span><span>Matrix2D</span><span> </span><span>=</span><span> </span><span>Eigen</span><span>::</span><span>Matrix</span><span><</span><span>double</span><span>,</span><span> </span><span>Eigen</span><span>::</span><span>Dynamic</span><span>,</span><span> </span><span>Eigen</span><span>::</span><span>Dynamic</span><span>,</span><span> </span><span>Eigen</span><span>::</span><span>RowMajor</span><span> </span><span>|</span><span> </span><span>Eigen</span><span>::</span><span>AutoAlign</span><span>>;</span></code></pre>
<p>I have explicitly set a <code>to_python</code> converter for <code>Matrix2D</code>, and I have exported the function with a call policy of <code>return_internal_reference</code>. However, when this function is called from Python I get</p>
<pre><code><span>TypeError</span><span>:</span><span> </span><span>No</span><span> </span><span>Python</span><span> </span><span>class</span><span> registered </span><span>for</span><span> C</span><span>++</span><span> </span><span>class</span><span> </span><span>Eigen</span><span>::</span><span>Matrix</span><span><</span><span>double</span><span>,</span><span> </span><span>-</span><span>1</span><span>,</span><span> </span><span>-</span><span>1</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>-</span><span>1</span><span>,</span><span> </span><span>-</span><span>1</span><span>></span></code></pre>
<p>This happens also if the reference is not <code>const</code>. At the same time, if I modify the call policy to <code>return_value_policy<copy_const_reference>()</code> Python can decode the matrix just fine. The same holds true if I return the <code>Matrix2D</code> by value. However I really need to have a reference to the matrix in order store it into other classes.</p>
<p>How can I make Python see the data structure through the reference?</p>
<p>The converter in case it is needed:</p>
<pre><code><span>boost</span><span>::</span><span>python</span><span>::</span><span>list toPythonList</span><span>(</span><span>const</span><span> </span><span>double</span><span> </span><span>*</span><span> data</span><span>,</span><span> </span><span>size_t</span><span> num</span><span>)</span><span> </span><span>{</span><span>
boost</span><span>::</span><span>python</span><span>::</span><span>list list</span><span>;</span><span>
</span><span>for</span><span> </span><span>(</span><span>size_t</span><span> i </span><span>=</span><span> </span><span>0</span><span>;</span><span> i </span><span><</span><span> num</span><span>;</span><span> </span><span>++</span><span>i</span><span>)</span><span>
list</span><span>.</span><span>append</span><span>(</span><span>data</span><span>[</span><span>i</span><span>]);</span><span>
</span><span>return</span><span> list</span><span>;</span><span>
</span><span>}</span><span>
</span><span>struct</span><span> </span><span>Matrix2DToPython</span><span>
</span><span>{</span><span>
</span><span>static</span><span> </span><span>PyObject</span><span>*</span><span> convert</span><span>(</span><span>Matrix2D</span><span> </span><span>const</span><span> </span><span>&</span><span> m</span><span>)</span><span>
</span><span>{</span><span>
boost</span><span>::</span><span>python</span><span>::</span><span>list list</span><span>;</span><span>
</span><span>for</span><span> </span><span>(</span><span>int</span><span> i </span><span>=</span><span> </span><span>0</span><span>;</span><span> i </span><span><</span><span> m</span><span>.</span><span>rows</span><span>();</span><span> </span><span>++</span><span>i</span><span>)</span><span>
</span><span>{</span><span>
list</span><span>.</span><span>append</span><span>(</span><span>toPythonList</span><span>(</span><span>m</span><span>.</span><span>row</span><span>(</span><span>i</span><span>).</span><span>data</span><span>(),</span><span> m</span><span>.</span><span>cols</span><span>()));</span><span>
</span><span>}</span><span>
</span><span>return</span><span> boost</span><span>::</span><span>python</span><span>::</span><span>incref</span><span>(</span><span>list</span><span>.</span><span>ptr</span><span>());</span><span>
</span><span>}</span><span>
</span><span>};<br><font face="arial,helvetica,sans-serif"><br>Thanks,<br></font></span></code></pre><pre><code><span><font face="arial,helvetica,sans-serif">Eugenio</font></span></code></pre></div>