numpy.ndarray.ctypes#

attribute

ndarray.ctypes#

An object to simplify the interaction of the array with the ctypesmodule.

This attribute creates an object that makes it easier to use arrayswhen calling shared libraries with the ctypes module. The returnedobject has, among others, data, shape, and strides attributes (seeNotes below) which themselves return ctypes objects that can be usedas arguments to a shared library.

Parameters:
None
Returns:
cPython object

Possessing attributes data, shape, strides, etc.

Notes

Below are the public attributes of this object which were documentedin “Guide to NumPy” (we have omitted undocumented public attributes,as well as documented private attributes):

_ctypes.data

A pointer to the memory area of the array as a Python integer.This memory area may contain data that is not aligned, or not incorrect byte-order. The memory area may not even be writeable.The array flags and data-type of this array should be respectedwhen passing this attribute to arbitrary C-code to avoid troublethat can include Python crashing. User Beware! The value of thisattribute is exactly the same as:self._array_interface_['data'][0].

Note that unlikedata_as, a reference won’t be kept to the array:code likectypes.c_void_p((a+b).ctypes.data) will result in apointer to a deallocated array, and should be spelt(a+b).ctypes.data_as(ctypes.c_void_p)

_ctypes.shape

(c_intp*self.ndim): A ctypes array of length self.ndim wherethe basetype is the C-integer corresponding todtype('p') on thisplatform (seec_intp). This base-type could bectypes.c_int,ctypes.c_long, orctypes.c_longlong depending onthe platform. The ctypes array contains the shape ofthe underlying array.

_ctypes.strides

(c_intp*self.ndim): A ctypes array of length self.ndim wherethe basetype is the same as for the shape attribute. This ctypesarray contains the strides information from the underlying array.This strides information is important for showing how many bytesmust be jumped to get to the next element in the array.

_ctypes.data_as(obj)[source]

Return the data pointer cast to a particular c-types object.For example, callingself._as_parameter_ is equivalent toself.data_as(ctypes.c_void_p). Perhaps you want to usethe data as a pointer to a ctypes array of floating-point data:self.data_as(ctypes.POINTER(ctypes.c_double)).

The returned pointer will keep a reference to the array.

_ctypes.shape_as(obj)[source]

Return the shape tuple as an array of some other c-typestype. For example:self.shape_as(ctypes.c_short).

_ctypes.strides_as(obj)[source]

Return the strides tuple as an array of some otherc-types type. For example:self.strides_as(ctypes.c_longlong).

If the ctypes module is not available, then the ctypes attributeof array objects still returns something useful, but ctypes objectsare not returned and errors may be raised instead. In particular,the object will still have theas_parameter attribute which willreturn an integer equal to the data attribute.

Examples

>>>importnumpyasnp>>>importctypes>>>x=np.array([[0,1],[2,3]],dtype=np.int32)>>>xarray([[0, 1],       [2, 3]], dtype=int32)>>>x.ctypes.data31962608 # may vary>>>x.ctypes.data_as(ctypes.POINTER(ctypes.c_uint32))<__main__.LP_c_uint object at 0x7ff2fc1fc200> # may vary>>>x.ctypes.data_as(ctypes.POINTER(ctypes.c_uint32)).contentsc_uint(0)>>>x.ctypes.data_as(ctypes.POINTER(ctypes.c_uint64)).contentsc_ulong(4294967296)>>>x.ctypes.shape<numpy._core._internal.c_long_Array_2 object at 0x7ff2fc1fce60> # may vary>>>x.ctypes.strides<numpy._core._internal.c_long_Array_2 object at 0x7ff2fc1ff320> # may vary
On this page