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 标量,联合,数组或结构体
- Raises:
- 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 采用任何 path-like object .
- 参数:
- libnamepath-like
库的名称,可以有 ‘lib’ 作为前缀,但没有扩展名.
- loader_pathpath-like
可以找到库的位置.
- 返回:
- ctypes.cdll[libpath]库对象
一个 ctypes 库对象
- Raises:
- OSError
如果没有具有预期扩展名的库,或者库有缺陷且无法加载.
- numpy.ctypeslib.ndpointer(dtype=None, ndim=None, shape=None, flags=None)[源代码]#
数组检查 restype/argtypes.
ndpointer 实例用于描述 restypes 和 argtypes 规范中的 ndarray. 这种方法比使用例如
POINTER(c_double)更灵活,因为可以指定多个限制,这些限制在调用 ctypes 函数时进行验证. 这些限制包括数据类型,维度数,形状和标志. 如果给定的数组不满足指定的限制,则会引发TypeError.- 参数:
- dtypedata-type,可选
数组数据类型.
- ndimint, optional
数组维度的数量.
- shape整数元组,可选
数组形状.
- flagsstr 或 str 元组
数组标志;可以是以下一个或多个:
C_CONTIGUOUS / C / CONTIGUOUS
F_CONTIGUOUS / F / FORTRAN
OWNDATA / O
WRITEABLE / W
ALIGNED / A
WRITEBACKIFCOPY / X
- 返回:
- klassndpointer 类型对象
一个类型对象,它是包含 dtype,ndim,shape 和 flags 信息的
_ndtpr实例.
- Raises:
- 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的别名.