massive update, probably broken
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-pyright / node_modules / pyright / dist / typeshed-fallback / stdlib / ctypes / __init__.pyi
1 import sys
2 from _typeshed import ReadableBuffer, WriteableBuffer
3 from typing import (
4     Any,
5     Callable,
6     ClassVar,
7     Generic,
8     Iterable,
9     Iterator,
10     Mapping,
11     Optional,
12     Sequence,
13     Tuple,
14     Type,
15     TypeVar,
16     Union as _UnionT,
17     overload,
18 )
19
20 if sys.version_info >= (3, 9):
21     from types import GenericAlias
22
23 _T = TypeVar("_T")
24 _DLLT = TypeVar("_DLLT", bound=CDLL)
25 _CT = TypeVar("_CT", bound=_CData)
26
27 RTLD_GLOBAL: int
28 RTLD_LOCAL: int
29 DEFAULT_MODE: int
30
31 class CDLL(object):
32     _func_flags_: ClassVar[int]
33     _func_restype_: ClassVar[_CData]
34     _name: str
35     _handle: int
36     _FuncPtr: Type[_FuncPointer]
37     if sys.version_info >= (3, 8):
38         def __init__(
39             self,
40             name: str | None,
41             mode: int = ...,
42             handle: int | None = ...,
43             use_errno: bool = ...,
44             use_last_error: bool = ...,
45             winmode: int | None = ...,
46         ) -> None: ...
47     else:
48         def __init__(
49             self, name: str | None, mode: int = ..., handle: int | None = ..., use_errno: bool = ..., use_last_error: bool = ...
50         ) -> None: ...
51     def __getattr__(self, name: str) -> _NamedFuncPointer: ...
52     def __getitem__(self, name: str) -> _NamedFuncPointer: ...
53
54 if sys.platform == "win32":
55     class OleDLL(CDLL): ...
56     class WinDLL(CDLL): ...
57
58 class PyDLL(CDLL): ...
59
60 class LibraryLoader(Generic[_DLLT]):
61     def __init__(self, dlltype: Type[_DLLT]) -> None: ...
62     def __getattr__(self, name: str) -> _DLLT: ...
63     def __getitem__(self, name: str) -> _DLLT: ...
64     def LoadLibrary(self, name: str) -> _DLLT: ...
65     if sys.version_info >= (3, 9):
66         def __class_getitem__(cls, item: Any) -> GenericAlias: ...
67
68 cdll: LibraryLoader[CDLL]
69 if sys.platform == "win32":
70     windll: LibraryLoader[WinDLL]
71     oledll: LibraryLoader[OleDLL]
72 pydll: LibraryLoader[PyDLL]
73 pythonapi: PyDLL
74
75 class _CDataMeta(type):
76     # By default mypy complains about the following two methods, because strictly speaking cls
77     # might not be a Type[_CT]. However this can never actually happen, because the only class that
78     # uses _CDataMeta as its metaclass is _CData. So it's safe to ignore the errors here.
79     def __mul__(cls: Type[_CT], other: int) -> Type[Array[_CT]]: ...  # type: ignore
80     def __rmul__(cls: Type[_CT], other: int) -> Type[Array[_CT]]: ...  # type: ignore
81
82 class _CData(metaclass=_CDataMeta):
83     _b_base: int
84     _b_needsfree_: bool
85     _objects: Mapping[Any, int] | None
86     @classmethod
87     def from_buffer(cls: Type[_CT], source: WriteableBuffer, offset: int = ...) -> _CT: ...
88     @classmethod
89     def from_buffer_copy(cls: Type[_CT], source: ReadableBuffer, offset: int = ...) -> _CT: ...
90     @classmethod
91     def from_address(cls: Type[_CT], address: int) -> _CT: ...
92     @classmethod
93     def from_param(cls: Type[_CT], obj: Any) -> _CT | _CArgObject: ...
94     @classmethod
95     def in_dll(cls: Type[_CT], library: CDLL, name: str) -> _CT: ...
96
97 class _CanCastTo(_CData): ...
98 class _PointerLike(_CanCastTo): ...
99
100 _ECT = Callable[[Optional[Type[_CData]], _FuncPointer, Tuple[_CData, ...]], _CData]
101 _PF = _UnionT[Tuple[int], Tuple[int, str], Tuple[int, str, Any]]
102
103 class _FuncPointer(_PointerLike, _CData):
104     restype: Type[_CData] | Callable[[int], Any] | None
105     argtypes: Sequence[Type[_CData]]
106     errcheck: _ECT
107     @overload
108     def __init__(self, address: int) -> None: ...
109     @overload
110     def __init__(self, callable: Callable[..., Any]) -> None: ...
111     @overload
112     def __init__(self, func_spec: tuple[str | int, CDLL], paramflags: Tuple[_PF, ...] = ...) -> None: ...
113     @overload
114     def __init__(self, vtlb_index: int, name: str, paramflags: Tuple[_PF, ...] = ..., iid: pointer[c_int] = ...) -> None: ...
115     def __call__(self, *args: Any, **kwargs: Any) -> Any: ...
116
117 class _NamedFuncPointer(_FuncPointer):
118     __name__: str
119
120 class ArgumentError(Exception): ...
121
122 def CFUNCTYPE(
123     restype: Type[_CData] | None, *argtypes: Type[_CData], use_errno: bool = ..., use_last_error: bool = ...
124 ) -> Type[_FuncPointer]: ...
125
126 if sys.platform == "win32":
127     def WINFUNCTYPE(
128         restype: Type[_CData] | None, *argtypes: Type[_CData], use_errno: bool = ..., use_last_error: bool = ...
129     ) -> Type[_FuncPointer]: ...
130
131 def PYFUNCTYPE(restype: Type[_CData] | None, *argtypes: Type[_CData]) -> Type[_FuncPointer]: ...
132
133 class _CArgObject: ...
134
135 # Any type that can be implicitly converted to c_void_p when passed as a C function argument.
136 # (bytes is not included here, see below.)
137 _CVoidPLike = _UnionT[_PointerLike, Array[Any], _CArgObject, int]
138 # Same as above, but including types known to be read-only (i. e. bytes).
139 # This distinction is not strictly necessary (ctypes doesn't differentiate between const
140 # and non-const pointers), but it catches errors like memmove(b'foo', buf, 4)
141 # when memmove(buf, b'foo', 4) was intended.
142 _CVoidConstPLike = _UnionT[_CVoidPLike, bytes]
143
144 def addressof(obj: _CData) -> int: ...
145 def alignment(obj_or_type: _CData | Type[_CData]) -> int: ...
146 def byref(obj: _CData, offset: int = ...) -> _CArgObject: ...
147
148 _CastT = TypeVar("_CastT", bound=_CanCastTo)
149
150 def cast(obj: _CData | _CArgObject | int, typ: Type[_CastT]) -> _CastT: ...
151 def create_string_buffer(init: int | bytes, size: int | None = ...) -> Array[c_char]: ...
152
153 c_buffer = create_string_buffer
154
155 def create_unicode_buffer(init: int | str, size: int | None = ...) -> Array[c_wchar]: ...
156
157 if sys.platform == "win32":
158     def DllCanUnloadNow() -> int: ...
159     def DllGetClassObject(rclsid: Any, riid: Any, ppv: Any) -> int: ...  # TODO not documented
160     def FormatError(code: int) -> str: ...
161     def GetLastError() -> int: ...
162
163 def get_errno() -> int: ...
164
165 if sys.platform == "win32":
166     def get_last_error() -> int: ...
167
168 def memmove(dst: _CVoidPLike, src: _CVoidConstPLike, count: int) -> None: ...
169 def memset(dst: _CVoidPLike, c: int, count: int) -> None: ...
170 def POINTER(type: Type[_CT]) -> Type[pointer[_CT]]: ...
171
172 # The real ctypes.pointer is a function, not a class. The stub version of pointer behaves like
173 # ctypes._Pointer in that it is the base class for all pointer types. Unlike the real _Pointer,
174 # it can be instantiated directly (to mimic the behavior of the real pointer function).
175 class pointer(Generic[_CT], _PointerLike, _CData):
176     _type_: Type[_CT]
177     contents: _CT
178     def __init__(self, arg: _CT = ...) -> None: ...
179     @overload
180     def __getitem__(self, i: int) -> _CT: ...
181     @overload
182     def __getitem__(self, s: slice) -> list[_CT]: ...
183     @overload
184     def __setitem__(self, i: int, o: _CT) -> None: ...
185     @overload
186     def __setitem__(self, s: slice, o: Iterable[_CT]) -> None: ...
187
188 def resize(obj: _CData, size: int) -> None: ...
189 def set_errno(value: int) -> int: ...
190
191 if sys.platform == "win32":
192     def set_last_error(value: int) -> int: ...
193
194 def sizeof(obj_or_type: _CData | Type[_CData]) -> int: ...
195 def string_at(address: _CVoidConstPLike, size: int = ...) -> bytes: ...
196
197 if sys.platform == "win32":
198     def WinError(code: int | None = ..., descr: str | None = ...) -> OSError: ...
199
200 def wstring_at(address: _CVoidConstPLike, size: int = ...) -> str: ...
201
202 class _SimpleCData(Generic[_T], _CData):
203     value: _T
204     def __init__(self, value: _T = ...) -> None: ...
205
206 class c_byte(_SimpleCData[int]): ...
207
208 class c_char(_SimpleCData[bytes]):
209     def __init__(self, value: int | bytes = ...) -> None: ...
210
211 class c_char_p(_PointerLike, _SimpleCData[Optional[bytes]]):
212     def __init__(self, value: int | bytes | None = ...) -> None: ...
213
214 class c_double(_SimpleCData[float]): ...
215 class c_longdouble(_SimpleCData[float]): ...
216 class c_float(_SimpleCData[float]): ...
217 class c_int(_SimpleCData[int]): ...
218 class c_int8(_SimpleCData[int]): ...
219 class c_int16(_SimpleCData[int]): ...
220 class c_int32(_SimpleCData[int]): ...
221 class c_int64(_SimpleCData[int]): ...
222 class c_long(_SimpleCData[int]): ...
223 class c_longlong(_SimpleCData[int]): ...
224 class c_short(_SimpleCData[int]): ...
225 class c_size_t(_SimpleCData[int]): ...
226 class c_ssize_t(_SimpleCData[int]): ...
227 class c_ubyte(_SimpleCData[int]): ...
228 class c_uint(_SimpleCData[int]): ...
229 class c_uint8(_SimpleCData[int]): ...
230 class c_uint16(_SimpleCData[int]): ...
231 class c_uint32(_SimpleCData[int]): ...
232 class c_uint64(_SimpleCData[int]): ...
233 class c_ulong(_SimpleCData[int]): ...
234 class c_ulonglong(_SimpleCData[int]): ...
235 class c_ushort(_SimpleCData[int]): ...
236 class c_void_p(_PointerLike, _SimpleCData[Optional[int]]): ...
237 class c_wchar(_SimpleCData[str]): ...
238
239 class c_wchar_p(_PointerLike, _SimpleCData[Optional[str]]):
240     def __init__(self, value: int | str | None = ...) -> None: ...
241
242 class c_bool(_SimpleCData[bool]):
243     def __init__(self, value: bool = ...) -> None: ...
244
245 if sys.platform == "win32":
246     class HRESULT(_SimpleCData[int]): ...  # TODO undocumented
247
248 class py_object(_CanCastTo, _SimpleCData[_T]): ...
249
250 class _CField:
251     offset: int
252     size: int
253
254 class _StructUnionMeta(_CDataMeta):
255     _fields_: Sequence[tuple[str, Type[_CData]] | tuple[str, Type[_CData], int]]
256     _pack_: int
257     _anonymous_: Sequence[str]
258     def __getattr__(self, name: str) -> _CField: ...
259
260 class _StructUnionBase(_CData, metaclass=_StructUnionMeta):
261     def __init__(self, *args: Any, **kw: Any) -> None: ...
262     def __getattr__(self, name: str) -> Any: ...
263     def __setattr__(self, name: str, value: Any) -> None: ...
264
265 class Union(_StructUnionBase): ...
266 class Structure(_StructUnionBase): ...
267 class BigEndianStructure(Structure): ...
268 class LittleEndianStructure(Structure): ...
269
270 class Array(Generic[_CT], _CData):
271     _length_: int
272     _type_: Type[_CT]
273     raw: bytes  # Note: only available if _CT == c_char
274     value: Any  # Note: bytes if _CT == c_char, str if _CT == c_wchar, unavailable otherwise
275     # TODO These methods cannot be annotated correctly at the moment.
276     # All of these "Any"s stand for the array's element type, but it's not possible to use _CT
277     # here, because of a special feature of ctypes.
278     # By default, when accessing an element of an Array[_CT], the returned object has type _CT.
279     # However, when _CT is a "simple type" like c_int, ctypes automatically "unboxes" the object
280     # and converts it to the corresponding Python primitive. For example, when accessing an element
281     # of an Array[c_int], a Python int object is returned, not a c_int.
282     # This behavior does *not* apply to subclasses of "simple types".
283     # If MyInt is a subclass of c_int, then accessing an element of an Array[MyInt] returns
284     # a MyInt, not an int.
285     # This special behavior is not easy to model in a stub, so for now all places where
286     # the array element type would belong are annotated with Any instead.
287     def __init__(self, *args: Any) -> None: ...
288     @overload
289     def __getitem__(self, i: int) -> Any: ...
290     @overload
291     def __getitem__(self, s: slice) -> list[Any]: ...
292     @overload
293     def __setitem__(self, i: int, o: Any) -> None: ...
294     @overload
295     def __setitem__(self, s: slice, o: Iterable[Any]) -> None: ...
296     def __iter__(self) -> Iterator[Any]: ...
297     # Can't inherit from Sized because the metaclass conflict between
298     # Sized and _CData prevents using _CDataMeta.
299     def __len__(self) -> int: ...
300     if sys.version_info >= (3, 9):
301         def __class_getitem__(cls, item: Any) -> GenericAlias: ...