Accessing data buffers in numpy scalars

Travis Oliphant oliphant at ee.byu.edu
Tue Oct 17 11:22:21 CDT 2006


Francesc Altet wrote:

>Hi,
>
>I'm looking for an easy way to access the data area of the numpy scalars no 
>matter its type.  I've seen that numpy/arrayscalars.h define a structure for 
>each scalar type, so I'd guess that it will not be possible to find a general 
>way for accessing the data buffer for each type. So, I've decided to look for 
>a workaround and I've devised a couple of possibilities:
>  
>
This problem showed up in writing NumPy several times.

One solution might be to use

PyArray_ScalarAsCtype(PyObject *scalar, void *ctypeptr)

which will copy the data into the area pointed to by ctypeptr (unless 
you have a "flexible scalar" in which case only a pointer to the 
data-area will be copied).


>2.- Fetch the buffer in scalartype.data and use the buffer protocol in order 
>to access the pointer to data in memory. However, I lack experience in buffer 
>protocol, so suggestions for achieving this are welcome.
>  
>
This will also work.   A read-only buffer protocol is exported by all 
the scalars.

scalar.data will return a buffer object.

Or you can use the Python C-API

const char *buffer;
Py_ssize_t buflen;

PyObject_AsReadBuffer(scalar, (const void **)&buffer, &buflen)

to retrieve a pointer to the data in buffer and the size of the data in 
buflen.


-Travis






-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642




More information about the Numpy-discussion mailing list