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)) 将:

示例

转换一个简单的数据类型:

>>> dt = np.dtype('int8')
>>> ctype = np.ctypeslib.as_ctypes_type(dt)
>>> ctype
<class 'ctypes.c_byte'>

转换一个结构化的数据类型:

>>> 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 .

参数:
libname类似路径

库的名称,可以有“lib”作为前缀,但没有扩展名.

loader_path类似路径

可以在哪里找到库.

返回:
ctypes.cdll[libpath]库对象

一个 ctypes 库对象

提出:
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,可选

数组数据类型.

ndim整数,可选

数组维度的数量.

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 实例.

提出:
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_longc_longlong 的别名.