[SciPy-user] passings arrays into C extension
oliphant.travis at ieee.org
Fri Dec 2 15:15:36 CST 2005
mike cantor wrote:
>What is the blessed way to pass SciPy arrays into C extension code (that
>is, to convert Array PthonObjs into a C int* or other array type).
What would you like the equivalent of?
You can access the data attribute (which is the pointer to raw memory as)
dptr = (<yourtype> *)PyArray_DATA(array)
You should be aware of PyArray_FLAGS(array) however because the data
might be unaligned, not in machine-byte order, or not writeable, or not
contiguous in memory. If you need a contiguous chunk of "behaved memory"
you can use:
/* This doesn't make sure you have an integer array */
newarr = PyArray_From_OF(array, CARRAY_FLAGS);
dptr = (int *)PyArray_DATA(newarr);
/* code that uses dptr */
If you need the "misbehaved" memory to receive the updates after the
copy is made use
CARRAY_FLAGS | UPDATEIFCOPY in the PyArray_From_OF(...) macro then when
newarr is DECREF'd it will update the contents of array.
If you are talking about passing something to the ctypes module, then
will give you an integer that is a pointer to your data.
More information about the SciPy-user