[Numpy-discussion] numpy CAPI questions

Lane Brooks lbrooks@MIT....
Sun Oct 19 14:28:49 CDT 2008


I am using the numpy CAPI to write an extension module that returns a 
numpy Array from an imaging data source.  I collect the image into a 
buffer that I allocate. I then create numpy Array using the 
PyArray_New(..) function and pass it the buffer.  I then set the 
NPY_OWNDATA flag on the Array because I want the Array to deallocate the 
buffer when it is deleted.  Is that the correct way to do it?  The code 
snippet below is what I wrote, and it seems to be working just fine, but 
I wanted to verify that I am doing things correctly.



uint16_t *data;

// malloc data and fill it with some image data...

img = PyArray_New(&PyArray_Type, 2, dims, NPY_UINT16, NULL, data, 2, 
NPY_C_CONTIGUOUS | NPY_WRITEABLE | NPY_ALIGNED, NULL);
PyArray_UpdateFlags((PyArrayObject *) img, NPY_OWNDATA | 
NPY_C_CONTIGUOUS | NPY_WRITEABLE | NPY_ALIGNED);
return Py_BuildValue("O", img);



Here are my questions:
1. Does NPY_OWNDATA mean that the object will deallocate the memory when 
the object is deleted?  The manual seems to indicate that as such but it 
is not explicitly stated.

2. Is my reference counting correct?  Do I need to call the 
PyArray_INCREF() on img?

Thanks,
Lane Brooks


More information about the Numpy-discussion mailing list