[Numpy-discussion] Owndata flag

Dag Sverre Seljebotn d.s.seljebotn at astro.uio.no
Fri Dec 16 12:38:20 EST 2011


On 12/16/2011 04:16 PM, Fabrice Silva wrote:
> Le vendredi 16 décembre 2011 à 15:33 +0100, Gregor Thalhammer a écrit :
>>> Even better: the addendum!
>>> http://blog.enthought.com/python/numpy/simplified-creation-of-numpy-arrays-from-pre-allocated-memory/
>>>
>>> Within cython:
>>> cimport numpy
>>> numpy.set_array_base(my_ndarray,  PyCObject_FromVoidPtr(pointer_to_Cobj, some_destructor))
>>>
>>> Seems OK.
>>> Any objections about that ?
>>
>> This is ok, but CObject is deprecated as of Python 3.1, so it's not portable to Python 3.2.
>
> My guess is then that the PyCapsule object is the way to go...
>

Another way: With recent NumPy you should be able to do something like 
this in Cython

cdef class SomeBufferWrapper:
     ...
     def __getbuffer__(self, ...): ...
     def __releasebuffer__(self, ...): ..

arr = np.asarray(SomeBufferWrapper(buf))

and then __releasebuffer__ will be called then `arr` goes out of use. 
See Cython docs.

Dag



More information about the NumPy-Discussion mailing list