From: Travis O. <oli...@ee...> - 2006-10-17 16:22:21
|
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 |