[C++-sig] Retrieving lvalues using custom converters (numpy 1-D converters attached)

Ravi lists_ravi at lavabit.com
Sat Oct 4 17:03:36 CEST 2008


On Friday 03 October 2008 19:49:29 Neal Becker wrote:
> > This, too, remains a puzzle to me. Do shared_ptr conversions not work for
> > objects with custom to_python converters?
>
> I realize now, this isn't a problem of boost::python.  The problem is the
> design of numpy.  numpy can adopt external storage, but has no way to
> really manage it.  It _could_ use the C 'free' function on it, and although
> that _might_ work, it can't do the 'right' thing, which is call your choice
> of deallocation function (like boost::shared_ptr could, for example).

Never say never. Attached is code, along with tests that handles both return-
by-value and returning by shared pointer; returning existing reference was 
already handled by the code posted earlier. Unfortunately, you need to make an 
extra function call in the module initialization code to ensure that the new 
python type for handling smart pointer deletion is initialized. See the 
example. As a bonus, this version handles to_python conversion of ublas 
matrices as well.

Note: Numpy has a bug which prevents return_by_value from working for 
row_major matrices.

Regards,
Ravi

-------------- next part --------------
A non-text attachment was scrubbed...
Name: decco.cc
Type: text/x-c++src
Size: 3664 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20081004/52d4e8f5/attachment.cc>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: numpy.hpp
Type: text/x-c++hdr
Size: 9278 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20081004/52d4e8f5/attachment.hpp>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: numpyregister.hpp
Type: text/x-c++hdr
Size: 16428 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20081004/52d4e8f5/attachment-0001.hpp>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: decco_example.py
Type: text/x-python
Size: 889 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20081004/52d4e8f5/attachment.py>


More information about the Cplusplus-sig mailing list