[Numpy-discussion] NumPy array from ctypes data

Andrew McMurry andrew.mcmurry@astro.uio...
Mon Feb 26 05:13:14 CST 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