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