[Numpy-discussion] How to tell if PyArray_FromAny has copied data?

Zachary Pincus zpincus at stanford.edu
Sun Mar 12 17:04:19 CST 2006

Hi folks,

I need to figure out how to tell if PyArray_FromAny() has made a copy  
of the data that was provided to it.

I assume that if the returned PyObject has a different address than  
the input object, then a copy was made somewhere along the line. Is  
this true? Is there a better way to tell?

Before anyone asks, here's why I need to know if a copy was made. I'm  
writing a command to turn a numpy array into an image object suitable  
for sending to this 3rd party medical imaging library I've wrapped.  
In some cases, it is desirable to share the data buffer between an  
array object and an image object (and the user is responsible for  
ensuring that the lifetime of the image object doesn't exceed that of  
the array object). However, if PyArray_FromAny() has made a copy,  
then it is returning an entirely new object that the user will never  
see and so I'll need to handle the memory management differently  
(e.g. steal the data buffer from the new array and let the image  
object manage it).

Alternately, I guess I could just inspect the input array object to  
see if it is exactly what I want, and branch on that. But running  
things through PyArray_FromAny seems easier, if it is possible.


More information about the Numpy-discussion mailing list