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 位元無號整數
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
s 和ctypes.Union
s 的類別名稱將單一元素
ctypes.Union
s 轉換為單一元素ctypes.Structure
s插入填充欄位
範例
轉換簡單的 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 接受任何 path-like 物件。
- 參數:
- libnamepath-like
程式庫名稱,可以 ‘lib’ 作為前綴,但沒有副檔名。
- loader_pathpath-like
程式庫可以找到的位置。
- 傳回:
- 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 和旗標資訊的
_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
的別名。