[Numpy-discussion] C extension in new numpy: help to port from numarray

Travis Oliphant oliphant.travis at ieee.org
Wed May 10 11:35:04 EDT 2006


Sebastian Haase wrote:
> On Wednesday 10 May 2006 10:05, Travis Oliphant wrote:
>   
>> Sebastian Haase wrote:
>>     
>>> One additional question:
>>> is  PyArray_FromDimsAndData creating a copy ?
>>> I have very large image data and cannot afford copies :-(
>>>       
>> No,  it uses the data as the memory space for the array (but you have to
>> either manage that memory area yourself or reset the OWNDATA flag to get
>> NumPy to delete it for you on array deletion).
>>
>> -Travis
>>     
>
> Thanks for the reply.
> Regarding "setting the OWNDATA flag":  
> How does NumPy know if it should call free (C code) or delete [] (C++ code) ?
>   
It doesn't.  

It always uses _pya_free  which is a macro that is defined to either 
system free or Python's memory-manager equivalent.  It should always be 
paired with _pya_malloc.    Yes, you can have serious problems by mixing 
memory allocators.

In other-words, unless you know what you are doing it is unwise to set 
the OWNDATA flag for data that was defined elsewhere.  

My favorite method is to simply let NumPy create the memory for you 
(e.g. use PyArray_SimpleNew).  Then, you won't have trouble. 

If that method is not possible, then the next best thing to do is to 
define a simple Python Object that uses reference counting to manage the 
memory for you.   Then, you point array->base to that object so that 
it's reference count gets decremented when the array disappears.   The 
simple Python Object defines it's tp_deallocate function to call the 
appropriate free.


-Travis






More information about the NumPy-Discussion mailing list