[Numpy-discussion] wrapping question

usr01873 at prolocation.net usr01873 at prolocation.net
Fri Oct 7 07:27:28 EDT 2005

> usr01873 at prolocation.net wrote:
>> Hi all,
>> I'm using the numarray package. I would like to use the numarray C API
>> to create a new array object and fill it with data without copying.
>> Also, I
> The numarray C-API has a compatibility layer which supports most Numeric
> C-API functionality.   So,  using numarray,  you can write most
> Numeric/Numeric3/scipy_core-like code in C.  For historical reasons
> numarray also has a wider set of native APIs (i.e. NA_vNewArray()),  but
> since numarray is likely to be replaced by scipy_core when it matures,
> it's wisest to use the compatibility API.
>> would like numarray to take care of freeing the data once the numarray
>> object is destructed. I've managed to find some related posts in the
>> mailing list archive, but it is really unclear which of these posts are
>>  still accurate and what the current 'recommended' approach is. See:
>> http://sourceforge.net/mailarchive/message.php?msg_id=11788304
>> http://sourceforge.net/mailarchive/message.php?msg_id=2494535
>> Currently, I first create a new PyArrayObject that is large enough to
>> hold the data: array =
>> NA_vNewArray(NULL,__numarray_type,tmp_num_dims,tmp_dims);
>> then I pass array->data to a C function that reads the data from disk.
>> Any
>> comments on this approach?
> That should work fine;  'array' owns the data and will free it when
> 'array' is destructed.
> A better approach,  however,  is to use numarray's PyArray_FromDims()
> which is the Numeric/Numeric3/scipy_core API.
>> Also, I was wondering if numarray frees the
>> data using this approach (because it also allocates the memory in the
>> first place within the NA_vNewArray function.)
> Yes, numarray will free the data allocated internally by
> NA_vNewArray().  numarray will also free the data allocated by
> PyArray_FromDims().

Thanx for the reply! Just to be sure I understand this correctly: to
conform to the Numeric/Numeric3/scipy api I could/should do the following:

1. allocate a new array using PyArray_FromDims()
2. pass the ->data pointer to a C routine that reads the data
3. use PyArray_Return to return the array to Python
4. No DECREF's are necessary?

Also, PyArray_FromDims() will not copy the data, right?


(From http://stsdas.stsci.edu/numarray/numarray-1.3.html/node58.html)
PyObject* PyArray_FromDims(int nd, int *dims, int type)
    This function will allocate a new numarray.
    An array created with PyArray_FromDims can be used as a temporary or
    returned using PyArray_Return.

    Used as a temporary, calling Py_DECREF deallocates it.

> Regards,
> Todd
> -------------------------------------------------------
> This SF.Net email is sponsored by:
> Power Architecture Resource Center: Free content, downloads, discussions,
> and more. http://solutions.newsforge.com/ibmarch.tmpl
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion

More information about the NumPy-Discussion mailing list