ctypeslib.pyi 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. # NOTE: Numpy's mypy plugin is used for importing the correct
  2. # platform-specific `ctypes._SimpleCData[int]` sub-type
  3. import ctypes
  4. from ctypes import c_int64 as _c_intp
  5. from _typeshed import StrOrBytesPath
  6. from collections.abc import Iterable, Sequence
  7. from typing import (
  8. Literal as L,
  9. Any,
  10. TypeAlias,
  11. TypeVar,
  12. Generic,
  13. overload,
  14. ClassVar,
  15. )
  16. import numpy as np
  17. from numpy import (
  18. ndarray,
  19. dtype,
  20. generic,
  21. byte,
  22. short,
  23. intc,
  24. long,
  25. longlong,
  26. ubyte,
  27. ushort,
  28. uintc,
  29. ulong,
  30. ulonglong,
  31. single,
  32. double,
  33. longdouble,
  34. void,
  35. )
  36. from numpy._core._internal import _ctypes
  37. from numpy._core.multiarray import flagsobj
  38. from numpy._typing import (
  39. # Arrays
  40. NDArray,
  41. _ArrayLike,
  42. # Shapes
  43. _Shape,
  44. _ShapeLike,
  45. # DTypes
  46. DTypeLike,
  47. _DTypeLike,
  48. _VoidDTypeLike,
  49. _BoolCodes,
  50. _UByteCodes,
  51. _UShortCodes,
  52. _UIntCCodes,
  53. _ULongCodes,
  54. _ULongLongCodes,
  55. _ByteCodes,
  56. _ShortCodes,
  57. _IntCCodes,
  58. _LongCodes,
  59. _LongLongCodes,
  60. _SingleCodes,
  61. _DoubleCodes,
  62. _LongDoubleCodes,
  63. )
  64. __all__ = ["load_library", "ndpointer", "c_intp", "as_ctypes", "as_array", "as_ctypes_type"]
  65. # TODO: Add a proper `_Shape` bound once we've got variadic typevars
  66. _DType = TypeVar("_DType", bound=dtype[Any])
  67. _DTypeOptional = TypeVar("_DTypeOptional", bound=None | dtype[Any])
  68. _SCT = TypeVar("_SCT", bound=generic)
  69. _FlagsKind: TypeAlias = L[
  70. 'C_CONTIGUOUS', 'CONTIGUOUS', 'C',
  71. 'F_CONTIGUOUS', 'FORTRAN', 'F',
  72. 'ALIGNED', 'A',
  73. 'WRITEABLE', 'W',
  74. 'OWNDATA', 'O',
  75. 'WRITEBACKIFCOPY', 'X',
  76. ]
  77. # TODO: Add a shape typevar once we have variadic typevars (PEP 646)
  78. class _ndptr(ctypes.c_void_p, Generic[_DTypeOptional]):
  79. # In practice these 4 classvars are defined in the dynamic class
  80. # returned by `ndpointer`
  81. _dtype_: ClassVar[_DTypeOptional]
  82. _shape_: ClassVar[None]
  83. _ndim_: ClassVar[None | int]
  84. _flags_: ClassVar[None | list[_FlagsKind]]
  85. @overload
  86. @classmethod
  87. def from_param(cls: type[_ndptr[None]], obj: NDArray[Any]) -> _ctypes[Any]: ...
  88. @overload
  89. @classmethod
  90. def from_param(cls: type[_ndptr[_DType]], obj: ndarray[Any, _DType]) -> _ctypes[Any]: ...
  91. class _concrete_ndptr(_ndptr[_DType]):
  92. _dtype_: ClassVar[_DType]
  93. _shape_: ClassVar[tuple[int, ...]]
  94. @property
  95. def contents(self) -> ndarray[_Shape, _DType]: ...
  96. def load_library(libname: StrOrBytesPath, loader_path: StrOrBytesPath) -> ctypes.CDLL: ...
  97. c_intp = _c_intp
  98. @overload
  99. def ndpointer(
  100. dtype: None = ...,
  101. ndim: int = ...,
  102. shape: None | _ShapeLike = ...,
  103. flags: None | _FlagsKind | Iterable[_FlagsKind] | int | flagsobj = ...,
  104. ) -> type[_ndptr[None]]: ...
  105. @overload
  106. def ndpointer(
  107. dtype: _DTypeLike[_SCT],
  108. ndim: int = ...,
  109. *,
  110. shape: _ShapeLike,
  111. flags: None | _FlagsKind | Iterable[_FlagsKind] | int | flagsobj = ...,
  112. ) -> type[_concrete_ndptr[dtype[_SCT]]]: ...
  113. @overload
  114. def ndpointer(
  115. dtype: DTypeLike,
  116. ndim: int = ...,
  117. *,
  118. shape: _ShapeLike,
  119. flags: None | _FlagsKind | Iterable[_FlagsKind] | int | flagsobj = ...,
  120. ) -> type[_concrete_ndptr[dtype[Any]]]: ...
  121. @overload
  122. def ndpointer(
  123. dtype: _DTypeLike[_SCT],
  124. ndim: int = ...,
  125. shape: None = ...,
  126. flags: None | _FlagsKind | Iterable[_FlagsKind] | int | flagsobj = ...,
  127. ) -> type[_ndptr[dtype[_SCT]]]: ...
  128. @overload
  129. def ndpointer(
  130. dtype: DTypeLike,
  131. ndim: int = ...,
  132. shape: None = ...,
  133. flags: None | _FlagsKind | Iterable[_FlagsKind] | int | flagsobj = ...,
  134. ) -> type[_ndptr[dtype[Any]]]: ...
  135. @overload
  136. def as_ctypes_type(dtype: _BoolCodes | _DTypeLike[np.bool] | type[ctypes.c_bool]) -> type[ctypes.c_bool]: ...
  137. @overload
  138. def as_ctypes_type(dtype: _ByteCodes | _DTypeLike[byte] | type[ctypes.c_byte]) -> type[ctypes.c_byte]: ...
  139. @overload
  140. def as_ctypes_type(dtype: _ShortCodes | _DTypeLike[short] | type[ctypes.c_short]) -> type[ctypes.c_short]: ...
  141. @overload
  142. def as_ctypes_type(dtype: _IntCCodes | _DTypeLike[intc] | type[ctypes.c_int]) -> type[ctypes.c_int]: ...
  143. @overload
  144. def as_ctypes_type(dtype: _LongCodes | _DTypeLike[long] | type[ctypes.c_long]) -> type[ctypes.c_long]: ...
  145. @overload
  146. def as_ctypes_type(dtype: type[int]) -> type[c_intp]: ...
  147. @overload
  148. def as_ctypes_type(dtype: _LongLongCodes | _DTypeLike[longlong] | type[ctypes.c_longlong]) -> type[ctypes.c_longlong]: ...
  149. @overload
  150. def as_ctypes_type(dtype: _UByteCodes | _DTypeLike[ubyte] | type[ctypes.c_ubyte]) -> type[ctypes.c_ubyte]: ...
  151. @overload
  152. def as_ctypes_type(dtype: _UShortCodes | _DTypeLike[ushort] | type[ctypes.c_ushort]) -> type[ctypes.c_ushort]: ...
  153. @overload
  154. def as_ctypes_type(dtype: _UIntCCodes | _DTypeLike[uintc] | type[ctypes.c_uint]) -> type[ctypes.c_uint]: ...
  155. @overload
  156. def as_ctypes_type(dtype: _ULongCodes | _DTypeLike[ulong] | type[ctypes.c_ulong]) -> type[ctypes.c_ulong]: ...
  157. @overload
  158. def as_ctypes_type(dtype: _ULongLongCodes | _DTypeLike[ulonglong] | type[ctypes.c_ulonglong]) -> type[ctypes.c_ulonglong]: ...
  159. @overload
  160. def as_ctypes_type(dtype: _SingleCodes | _DTypeLike[single] | type[ctypes.c_float]) -> type[ctypes.c_float]: ...
  161. @overload
  162. def as_ctypes_type(dtype: _DoubleCodes | _DTypeLike[double] | type[float | ctypes.c_double]) -> type[ctypes.c_double]: ...
  163. @overload
  164. def as_ctypes_type(dtype: _LongDoubleCodes | _DTypeLike[longdouble] | type[ctypes.c_longdouble]) -> type[ctypes.c_longdouble]: ...
  165. @overload
  166. def as_ctypes_type(dtype: _VoidDTypeLike) -> type[Any]: ... # `ctypes.Union` or `ctypes.Structure`
  167. @overload
  168. def as_ctypes_type(dtype: str) -> type[Any]: ...
  169. @overload
  170. def as_array(obj: ctypes._PointerLike, shape: Sequence[int]) -> NDArray[Any]: ...
  171. @overload
  172. def as_array(obj: _ArrayLike[_SCT], shape: None | _ShapeLike = ...) -> NDArray[_SCT]: ...
  173. @overload
  174. def as_array(obj: object, shape: None | _ShapeLike = ...) -> NDArray[Any]: ...
  175. @overload
  176. def as_ctypes(obj: np.bool) -> ctypes.c_bool: ...
  177. @overload
  178. def as_ctypes(obj: byte) -> ctypes.c_byte: ...
  179. @overload
  180. def as_ctypes(obj: short) -> ctypes.c_short: ...
  181. @overload
  182. def as_ctypes(obj: intc) -> ctypes.c_int: ...
  183. @overload
  184. def as_ctypes(obj: long) -> ctypes.c_long: ...
  185. @overload
  186. def as_ctypes(obj: longlong) -> ctypes.c_longlong: ...
  187. @overload
  188. def as_ctypes(obj: ubyte) -> ctypes.c_ubyte: ...
  189. @overload
  190. def as_ctypes(obj: ushort) -> ctypes.c_ushort: ...
  191. @overload
  192. def as_ctypes(obj: uintc) -> ctypes.c_uint: ...
  193. @overload
  194. def as_ctypes(obj: ulong) -> ctypes.c_ulong: ...
  195. @overload
  196. def as_ctypes(obj: ulonglong) -> ctypes.c_ulonglong: ...
  197. @overload
  198. def as_ctypes(obj: single) -> ctypes.c_float: ...
  199. @overload
  200. def as_ctypes(obj: double) -> ctypes.c_double: ...
  201. @overload
  202. def as_ctypes(obj: longdouble) -> ctypes.c_longdouble: ...
  203. @overload
  204. def as_ctypes(obj: void) -> Any: ... # `ctypes.Union` or `ctypes.Structure`
  205. @overload
  206. def as_ctypes(obj: NDArray[np.bool]) -> ctypes.Array[ctypes.c_bool]: ...
  207. @overload
  208. def as_ctypes(obj: NDArray[byte]) -> ctypes.Array[ctypes.c_byte]: ...
  209. @overload
  210. def as_ctypes(obj: NDArray[short]) -> ctypes.Array[ctypes.c_short]: ...
  211. @overload
  212. def as_ctypes(obj: NDArray[intc]) -> ctypes.Array[ctypes.c_int]: ...
  213. @overload
  214. def as_ctypes(obj: NDArray[long]) -> ctypes.Array[ctypes.c_long]: ...
  215. @overload
  216. def as_ctypes(obj: NDArray[longlong]) -> ctypes.Array[ctypes.c_longlong]: ...
  217. @overload
  218. def as_ctypes(obj: NDArray[ubyte]) -> ctypes.Array[ctypes.c_ubyte]: ...
  219. @overload
  220. def as_ctypes(obj: NDArray[ushort]) -> ctypes.Array[ctypes.c_ushort]: ...
  221. @overload
  222. def as_ctypes(obj: NDArray[uintc]) -> ctypes.Array[ctypes.c_uint]: ...
  223. @overload
  224. def as_ctypes(obj: NDArray[ulong]) -> ctypes.Array[ctypes.c_ulong]: ...
  225. @overload
  226. def as_ctypes(obj: NDArray[ulonglong]) -> ctypes.Array[ctypes.c_ulonglong]: ...
  227. @overload
  228. def as_ctypes(obj: NDArray[single]) -> ctypes.Array[ctypes.c_float]: ...
  229. @overload
  230. def as_ctypes(obj: NDArray[double]) -> ctypes.Array[ctypes.c_double]: ...
  231. @overload
  232. def as_ctypes(obj: NDArray[longdouble]) -> ctypes.Array[ctypes.c_longdouble]: ...
  233. @overload
  234. def as_ctypes(obj: NDArray[void]) -> ctypes.Array[Any]: ... # `ctypes.Union` or `ctypes.Structure`