[Cython] canonical way of converting numpy arrays to C arrays

Robert Kern robert.kern at gmail.com
Mon Oct 13 18:02:59 CEST 2008


Dag Sverre Seljebotn wrote:

> Comment: The only "shady" part here is "contarr.data", which accesses 
> implementation details of NumPy arrays. I'm guessing that this will 
> never change, but I once planned to make a generic cython function 
> "cython.buffer.bufptr" which would return the same pointer but it could 
> be acquired through the buffer API.

The canonical way to avoid accessing .data directly is to use the PyArray_DATA() 
macro (returns a void*). Similarly, dimensions and strides can be accessed via 
PyArray_DIMS() and PyArray_STRIDES().

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco



More information about the Cython-dev mailing list