[Numpy-discussion] wrapping question

Todd Miller jmiller at stsci.edu
Fri Oct 7 07:51:50 CDT 2005


usr01873 at prolocation.net wrote:

>>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?
>Thanks,
>  
>
1. yes
2. yes
3. PyArray_Return is mostly important if you want rank-0 arrays 
converted into equivalent Python scalars.   Otherwise I think it's not 
really needed and is commonly omitted.
4. When you call PyArray_FromDims(),  the returned array has one 
reference.   If the array is a temporary you want to deallocate at 
extension function exit,  then DECREF;  if you want to return the 
array,  don't DECREF.

Todd

>Joris
>
>/*
>(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
>>
>>
>>    
>>
>
>
>
>
>-------------------------------------------------------
>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