[Numpy-discussion] NumPy array from ctypes data

Andrew McMurry andrew.mcmurry at astro.uio.no
Mon Feb 26 06:13:14 EST 2007


How can I create a NumPy array which is backed by ctypes data?

ie create a NumPy array from a ctypes one without any data copying?

So that changing an element of the NumPy array will alter the  
underlying ctypes array.  I have C structures that contain numerical  
data and would like to access that data through the NumPy array  
interface.

I have tried the following:

PyObject *make_dbl_array(double *data, int n)
{
   npy_intp dim = n;
   return PyArray_SimpleNewFromData(1, &dim, NPY_DOUBLE, (void *)data);
}

But when called via ctypes (with restype set to ctypes.py_object) it  
gives a bus error.

	Andrew




More information about the NumPy-Discussion mailing list