[SciPy-user] passings arrays into C extension

mike cantor mcantor at stanford.edu
Fri Dec 2 16:29:01 CST 2005


Thanks for the response

newarr = PyArray_From_OF(array, CARRAY_FLAGS);
dptr = (int *)PyArray_DATA(newarr);

looks like it would do the trick.  Is there a reason to use PyArray_From_OF 
vs PyArray_ContiguousFromObject?

Thanks,-mike



dptr = (int *)PyArray_DATA(newarr);



At 01:15 PM 12/2/2005, you wrote:
>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
>
>_______________________________________________
>SciPy-user mailing list
>SciPy-user at scipy.net
>http://www.scipy.net/mailman/listinfo/scipy-user




More information about the SciPy-user mailing list