[SciPy-user] passings arrays into C extension
Travis Oliphant
oliphant.travis at ieee.org
Fri Dec 2 16:15:36 EST 2005
mike cantor wrote:
>Hi,
>
>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 */
Py_DECREF(newarr);
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
int(array.__array_data__[0],0)
will give you an integer that is a pointer to your data.
-Travis
More information about the SciPy-User
mailing list