Create a numpy array from a ctypes array or POINTER.
The numpy array shares the memory with the ctypes object.
The shape parameter must be given if converting from a ctypes POINTER. The shape parameter is ignored if converting from a ctypes array
Converting a ctypes integer array:
>>> import ctypes >>> ctypes_array = (ctypes.c_int * 5)(0, 1, 2, 3, 4) >>> np_array = np.ctypeslib.as_array(ctypes_array) >>> np_array array([0, 1, 2, 3, 4], dtype=int32)
Converting a ctypes POINTER:
>>> import ctypes >>> buffer = (ctypes.c_int * 5)(0, 1, 2, 3, 4) >>> pointer = ctypes.cast(buffer, ctypes.POINTER(ctypes.c_int)) >>> np_array = np.ctypeslib.as_array(pointer, (5,)) >>> np_array array([0, 1, 2, 3, 4], dtype=int32)
Create and return a ctypes object from a numpy array. Actually anything that exposes the __array_interface__ is accepted.
Create ctypes object from inferred int np.array:
>>> inferred_int_array = np.array([1, 2, 3]) >>> c_int_array = np.ctypeslib.as_ctypes(inferred_int_array) >>> type(c_int_array) <class 'c_long_Array_3'> >>> c_int_array[:] [1, 2, 3]
Create ctypes object from explicit 8 bit unsigned int np.array :
>>> exp_int_array = np.array([1, 2, 3], dtype=np.uint8) >>> c_int_array = np.ctypeslib.as_ctypes(exp_int_array) >>> type(c_int_array) <class 'c_ubyte_Array_3'> >>> c_int_array[:] [1, 2, 3]
Convert a dtype into a ctypes type.
The dtype to convert
A ctype scalar, union, array, or struct
If the conversion is not possible
This function does not losslessly round-trip in either direction.
np.dtype(as_ctypes_type(dt)) will:
as_ctypes_type(np.dtype(ctype)) will:
ctypes.Structures and ctypes.Unionsctypes.Unions into single-element ctypes.StructuresConverting a simple dtype:
>>> dt = np.dtype('int8')
>>> ctype = np.ctypeslib.as_ctypes_type(dt)
>>> ctype
<class 'ctypes.c_byte'>
Converting a structured dtype:
>>> dt = np.dtype([('x', 'i4'), ('y', 'f4')])
>>> ctype = np.ctypeslib.as_ctypes_type(dt)
>>> ctype
<class 'struct'>
It is possible to load a library using
>>> lib = ctypes.cdll[<full_path_name>]
But there are cross-platform considerations, such as library file extensions, plus the fact Windows will just load the first library it finds with that name. NumPy supplies the load_library function as a convenience.
Changed in version 1.20.0: Allow libname and loader_path to take any path-like object.
Name of the library, which can have ‘lib’ as a prefix, but without an extension.
Where the library can be found.
A ctypes library object
If there is no library with the expected extension, or the library is defective and cannot be loaded.
Array-checking restype/argtypes.
An ndpointer instance is used to describe an ndarray in restypes and argtypes specifications. This approach is more flexible than using, for example, POINTER(c_double), since several restrictions can be specified, which are verified upon calling the ctypes function. These include data type, number of dimensions, shape and flags. If a given array does not satisfy the specified restrictions, a TypeError is raised.
Array data-type.
Number of array dimensions.
Array shape.
Array flags; may be one or more of:
A type object, which is an _ndtpr instance containing dtype, ndim, shape and flags information.
If a given array does not satisfy the specified restrictions.
>>> clib.somefunc.argtypes = [np.ctypeslib.ndpointer(dtype=np.float64, ... ndim=1, ... flags='C_CONTIGUOUS')] ... >>> clib.somefunc(np.array([1, 2, 3], dtype=np.float64)) ...
A ctypes signed integer type of the same size as numpy.intp.
Depending on the platform, it can be an alias for either c_int, c_long or c_longlong.
© 2005–2024 NumPy Developers
Licensed under the 3-clause BSD License.
https://numpy.org/doc/2.4/reference/routines.ctypeslib.html