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('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数据类型,可选
- 数组数据类型。 
- ndim整数,可选
- 数组维度数。 
- 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#
- 一个 - ctypes有符号整数类型,其大小与- numpy.intp相同。- 根据平台的不同,它可以是 - c_int、- c_long或- c_longlong的别名。