How to register a destructor for a numpy array allocated by C?

I want to allocate numbers for a numpy array in C / C ++ and pass them to python as a numpy array. What can I do with PyArray_SimpleNewFromData .

The problem is that I also want to register a function that should be called from Python when the numpy array reference count reaches zero, and this will cause some destructor semantics on the C side ... Here is a pseudo-example that I need:

 float* arr; PyObject* np_arr; void (*destructor)(float* arr);
 // ... C-allocate array on arr, ...
 // ...
 // ... initialize destructor with some suitable value, and then:
 np_arr = /* ... create the array to wrap arr, 
             and to use destructor on some meaningful way ... */

is there an easy way to do this?

+5
source share
1 answer

There is no easy way as such, however I think this url will answer your question

http://blog.enthought.com/python/numpy-arrays-with-pre-allocated-memory/

+1