ctypes 外部函数接口 (numpy.ctypeslib)#
- numpy.ctypeslib.as_array(obj, shape=None)[source]#
从 ctypes 数组或 POINTER 创建一个 numpy 数组。
该 numpy 数组与 ctypes 对象共享内存。
如果从 ctypes POINTER 转换,则必须提供 shape 参数。如果从 ctypes 数组转换,则 shape 参数会被忽略。
示例
转换 ctypes 整数数组
>>> 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)
转换 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)
- numpy.ctypeslib.as_ctypes(obj)[source]#
从 numpy 数组创建并返回一个 ctypes 对象。实际上,任何暴露了 __array_interface__ 的对象都可以被接受。
示例
从推断出的 int
np.array创建 ctypes 对象>>> 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]
从显式的 8 位无符号 int
np.array创建 ctypes 对象>>> 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]
- numpy.ctypeslib.as_ctypes_type(dtype)[source]#
将 dtype 转换为 ctypes 类型。
- 参数:
- dtypedtype
要转换的 dtype
- 返回:
- ctype
ctype 标量、联合体、数组或结构体
- 引发:
- NotImplementedError
如果转换不可行
备注
此函数在任一方向上均无法实现无损往返。
np.dtype(as_ctypes_type(dt))将插入填充字段
重新排序字段以按偏移量排序
丢弃字段标题
as_ctypes_type(np.dtype(ctype))将丢弃
ctypes.Structure和ctypes.Union的类名将单元素
ctypes.Union转换为单元素ctypes.Structure插入填充字段
示例
转换简单的 dtype
>>> dt = np.dtype(np.int8) >>> ctype = np.ctypeslib.as_ctypes_type(dt) >>> ctype <class 'ctypes.c_byte'>
转换结构化 dtype
>>> dt = np.dtype([('x', 'i4'), ('y', 'f4')]) >>> ctype = np.ctypeslib.as_ctypes_type(dt) >>> ctype <class 'struct'>
- numpy.ctypeslib.load_library(libname, loader_path)[source]#
可以通过以下方式加载库
>>> lib = ctypes.cdll[<full_path_name>]
但需要考虑跨平台因素,例如库文件扩展名,此外 Windows 只会加载它找到的具有该名称的第一个库。NumPy 提供 load_library 函数作为便利。
在 1.20.0 版本中变更: 允许 libname 和 loader_path 接受任何 路径类对象。
- 参数:
- libname路径类对象
库的名称,可以带有“lib”前缀,但不能带有扩展名。
- loader_path路径类对象
库所在的位置。
- 返回:
- ctypes.cdll[libpath]库对象
一个 ctypes 库对象
- 引发:
- OSError
如果没有具有预期扩展名的库,或者库已损坏且无法加载。
- numpy.ctypeslib.ndpointer(dtype=None, ndim=None, shape=None, flags=None)[source]#
数组检查 restype/argtypes。
ndpointer 实例用于在 restypes 和 argtypes 规范中描述 ndarray。这种方法比使用
POINTER(c_double)等更灵活,因为可以指定多个限制,并在调用 ctypes 函数时进行验证。这些限制包括数据类型、维度数量、形状和标志。如果给定的数组不满足指定的限制,则会引发TypeError。- 参数:
- dtype数据类型,可选
数组数据类型。
- ndimint, optional
数组的维度数。
- shape整数元组,可选
数组形状。
- flags字符串或字符串元组
数组标志;可以是以下一种或多种
C_CONTIGUOUS / C / CONTIGUOUS
F_CONTIGUOUS / F / FORTRAN
OWNDATA / O
WRITEABLE / W
ALIGNED / A
WRITEBACKIFCOPY / X
- 返回:
- klassndpointer 类型对象
一种类型对象,是一个包含 dtype、ndim、shape 和 flags 信息的
_ndtpr实例。
- 引发:
- TypeError
如果给定的数组不满足指定的限制。
示例
>>> clib.somefunc.argtypes = [np.ctypeslib.ndpointer(dtype=np.float64, ... ndim=1, ... flags='C_CONTIGUOUS')] ... >>> clib.somefunc(np.array([1, 2, 3], dtype=np.float64)) ...
- class numpy.ctypeslib.c_intp#
一种与
numpy.intp大小相同的ctypes有符号整数类型。根据平台的不同,它可以是
c_int、c_long或c_longlong的别名。