[Numpy-discussion] Wrapping C array

Todd Miller jmiller at stsci.edu
Wed May 18 09:39:20 EDT 2005


On Wed, 2005-05-18 at 12:18, Carlos Pita wrote:
> The documentation for PyArray_FromDimsAndData
> that I quoted in my previous post seems to suggest
> that a copy is done. But browsing the web

I'd appreciate it if you could send me a reference to where that is
because it is stale.

> numarray cvs I find out that its implementation
> first creates a PyBuffer from the passed data:
> 
>   buf = PyBuffer_FromReadWriteMemory(data, nelements * descr->elsize);
>   a = NA_NewAllFromBuffer( nd, dimensions, descr->type_num, buf, 
> 				 0, descr->elsize, NA_ByteOrder(), 1, 1);
> 
> Then, from the NA_NewAllFromBuffer doc (in numarray
> manual):
> 
>   It is possible to create a Python buffer object from
>   an array of C data and then construct a numarray using
>   this function which refers to the C data without
>   making a copy.
> 
> So you both are right. I guess creating a
> PyBuffer as above and then calling
> NA_NewAllFromBuffer is the best way
> to do what I was asking for in numarray
> (that is, without recurring to the
> numeric simulation API).
> 
> Am I right?

Not in my opinion, for two reasons:

1.  PyArray_FromDimsAndData is easier to use,  internalizing the buffer
creation step.

2.  These days,  my recommendation is to use the Numeric compatibility
layer because it gives the best portability,  to Numeric now and to
Numeric3 in the future.   Most of the NA_ routines exist to facilitate
the implementation of numarray itself and because we didn't always know
a compatibility layer was possible or desirable.  

Of course you're free to use the NA_ routines and we're not planning on
removing them,  but I think the Numeric compatible routines are a better
choice.

Regards,
Todd









More information about the NumPy-Discussion mailing list