ctypes 外部函数接口 (numpy.ctypeslib)#
- numpy.ctypeslib.as_array(obj, shape=None)[源代码]#
从 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)[源代码]#
从 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)[源代码]#
将 dtype 转换为 ctypes 类型。
- 参数:
- dtypedtype
要转换的 dtype
- 返回:
- ctype
一个 ctype 标量、联合、数组或结构
- 引发:
- NotImplementedError
如果转换不可能
备注
此函数在任一方向上都不能无损地进行往返转换。
np.dtype(as_ctypes_type(dt))将插入填充字段
将字段按偏移量排序
丢弃字段标题
as_ctypes_type(np.dtype(ctype))将丢弃
ctypes.Structures 和ctypes.Unions 的类名将单元素
ctypes.Unions 转换为单元素ctypes.Structures插入填充字段
示例
转换简单 dtype
>>> dt = np.dtype('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)[源代码]#
可以使用以下方式加载库
>>> 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)[源代码]#
数组检查 restype/argtypes。
ndpointer 实例用于在 restype 和 argtype 规范中描述 ndarray。这种方法比使用例如
POINTER(c_double)更灵活,因为可以指定多项限制,这些限制会在调用 ctypes 函数时进行验证。这些限制包括数据类型、维数、形状和标志。如果给定的数组不满足指定的限制,将引发TypeError。- 参数:
- dtype数据类型,可选
数组数据类型。
- ndimint, optional
数组的维度数。
- shapetuple of ints, optional
数组形状。
- flagsstr or tuple of str
数组标志;可以是一个或多个:
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#
一个
ctypes有符号整数类型,大小与numpy.intp相同。根据平台的不同,它可以是
c_int、c_long或c_longlong的别名。