ctypeslib.pyi 7.8 KB

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