4 Any, Callable, ClassVar, Iterator, Iterable, List, Mapping, Optional, Sequence, Sized, Text,
\r
5 Tuple, Type, Generic, TypeVar, overload,
\r
7 from typing import Union as _UnionT
\r
11 _DLLT = TypeVar('_DLLT', bound=CDLL)
\r
12 _CT = TypeVar('_CT', bound=_CData)
\r
15 RTLD_GLOBAL: int = ...
\r
16 RTLD_LOCAL: int = ...
\r
17 DEFAULT_MODE: int = ...
\r
21 _func_flags_: ClassVar[int] = ...
\r
22 _func_restype_: ClassVar[_CData] = ...
\r
25 _FuncPtr: Type[_FuncPointer] = ...
\r
26 def __init__(self, name: str, mode: int = ..., handle: Optional[int] = ...,
\r
27 use_errno: bool = ..., use_last_error: bool = ...) -> None: ...
\r
28 def __getattr__(self, name: str) -> _FuncPointer: ...
\r
29 def __getitem__(self, name: str) -> _FuncPointer: ...
\r
30 if sys.platform == 'win32':
\r
31 class OleDLL(CDLL): ...
\r
32 class WinDLL(CDLL): ...
\r
33 class PyDLL(CDLL): ...
\r
35 class LibraryLoader(Generic[_DLLT]):
\r
36 def __init__(self, dlltype: Type[_DLLT]) -> None: ...
\r
37 def __getattr__(self, name: str) -> _DLLT: ...
\r
38 def __getitem__(self, name: str) -> _DLLT: ...
\r
39 def LoadLibrary(self, name: str) -> _DLLT: ...
\r
41 cdll: LibraryLoader[CDLL] = ...
\r
42 if sys.platform == 'win32':
\r
43 windll: LibraryLoader[WinDLL] = ...
\r
44 oledll: LibraryLoader[OleDLL] = ...
\r
45 pydll: LibraryLoader[PyDLL] = ...
\r
46 pythonapi: PyDLL = ...
\r
48 class _CDataMeta(type):
\r
49 # By default mypy complains about the following two methods, because strictly speaking cls
\r
50 # might not be a Type[_CT]. However this can never actually happen, because the only class that
\r
51 # uses _CDataMeta as its metaclass is _CData. So it's safe to ignore the errors here.
\r
52 def __mul__(cls: Type[_CT], other: int) -> Type[Array[_CT]]: ... # type: ignore
\r
53 def __rmul__(cls: Type[_CT], other: int) -> Type[Array[_CT]]: ... # type: ignore
\r
54 class _CData(metaclass=_CDataMeta):
\r
56 _b_needsfree_: bool = ...
\r
57 _objects: Optional[Mapping[Any, int]] = ...
\r
59 def from_buffer(cls: Type[_CT], source: bytearray, offset: int = ...) -> _CT: ...
\r
61 def from_buffer_copy(cls: Type[_CT], source: bytearray, offset: int = ...) -> _CT: ...
\r
63 def from_address(cls: Type[_CT], address: int) -> _CT: ...
\r
65 def from_param(cls: Type[_CT], obj: Any) -> _UnionT[_CT, _CArgObject]: ...
\r
67 def in_dll(cls: Type[_CT], library: CDLL, name: str) -> _CT: ...
\r
69 class _PointerLike(_CData): ...
\r
71 _ECT = Callable[[Optional[Type[_CData]],
\r
73 Tuple[_CData, ...]],
\r
78 Tuple[int, str, Any]
\r
80 class _FuncPointer(_PointerLike, _CData):
\r
81 restype: _UnionT[Type[_CData], Callable[[int], None], None] = ...
\r
82 argtypes: Sequence[Type[_CData]] = ...
\r
83 errcheck: _ECT = ...
\r
85 def __init__(self, address: int) -> None: ...
\r
87 def __init__(self, callable: Callable[..., Any]) -> None: ...
\r
89 def __init__(self, func_spec: Tuple[_UnionT[str, int], CDLL],
\r
90 paramflags: Tuple[_PF, ...] = ...) -> None: ...
\r
92 def __init__(self, vtlb_index: int, name: str,
\r
93 paramflags: Tuple[_PF, ...] = ...,
\r
94 iid: pointer[c_int] = ...) -> None: ...
\r
95 def __call__(self, *args: Any, **kwargs: Any) -> Any: ...
\r
97 class ArgumentError(Exception): ...
\r
100 def CFUNCTYPE(restype: Optional[Type[_CData]],
\r
101 *argtypes: Type[_CData],
\r
102 use_errno: bool = ...,
\r
103 use_last_error: bool = ...) -> Type[_FuncPointer]: ...
\r
104 if sys.platform == 'win32':
\r
105 def WINFUNCTYPE(restype: Optional[Type[_CData]],
\r
106 *argtypes: Type[_CData],
\r
107 use_errno: bool = ...,
\r
108 use_last_error: bool = ...) -> Type[_FuncPointer]: ...
\r
109 def PYFUNCTYPE(restype: Optional[Type[_CData]],
\r
110 *argtypes: Type[_CData]) -> Type[_FuncPointer]: ...
\r
112 class _CArgObject: ...
\r
114 # Any type that can be implicitly converted to c_void_p when passed as a C function argument.
\r
115 # (bytes is not included here, see below.)
\r
116 _CVoidPLike = _UnionT[_PointerLike, Array[Any], _CArgObject, int]
\r
117 # Same as above, but including types known to be read-only (i. e. bytes).
\r
118 # This distinction is not strictly necessary (ctypes doesn't differentiate between const
\r
119 # and non-const pointers), but it catches errors like memmove(b'foo', buf, 4)
\r
120 # when memmove(buf, b'foo', 4) was intended.
\r
121 _CVoidConstPLike = _UnionT[_CVoidPLike, bytes]
\r
123 def addressof(obj: _CData) -> int: ...
\r
124 def alignment(obj_or_type: _UnionT[_CData, Type[_CData]]) -> int: ...
\r
125 def byref(obj: _CData, offset: int = ...) -> _CArgObject: ...
\r
126 _PT = TypeVar('_PT', bound=_PointerLike)
\r
127 def cast(obj: _UnionT[_CData, _CArgObject], type: Type[_PT]) -> _PT: ...
\r
128 def create_string_buffer(init_or_size: _UnionT[int, bytes],
\r
129 size: Optional[int] = ...) -> Array[c_char]: ...
\r
130 c_buffer = create_string_buffer
\r
131 def create_unicode_buffer(init_or_size: _UnionT[int, Text],
\r
132 size: Optional[int] = ...) -> Array[c_wchar]: ...
\r
133 if sys.platform == 'win32':
\r
134 def DllCanUnloadNow() -> int: ...
\r
135 def DllGetClassObject(rclsid: Any, riid: Any, ppv: Any) -> int: ... # TODO not documented
\r
136 def FormatError(code: int) -> str: ...
\r
137 def GetLastError() -> int: ...
\r
138 def get_errno() -> int: ...
\r
139 if sys.platform == 'win32':
\r
140 def get_last_error() -> int: ...
\r
141 def memmove(dst: _CVoidPLike, src: _CVoidConstPLike, count: int) -> None: ...
\r
142 def memset(dst: _CVoidPLike, c: int, count: int) -> None: ...
\r
143 def POINTER(type: Type[_CT]) -> Type[pointer[_CT]]: ...
\r
145 # The real ctypes.pointer is a function, not a class. The stub version of pointer behaves like
\r
146 # ctypes._Pointer in that it is the base class for all pointer types. Unlike the real _Pointer,
\r
147 # it can be instantiated directly (to mimic the behavior of the real pointer function).
\r
148 class pointer(Generic[_CT], _PointerLike, _CData):
\r
149 _type_: ClassVar[Type[_CT]] = ...
\r
150 contents: _CT = ...
\r
151 def __init__(self, arg: _CT = ...) -> None: ...
\r
153 def __getitem__(self, i: int) -> _CT: ...
\r
155 def __getitem__(self, s: slice) -> List[_CT]: ...
\r
157 def __setitem__(self, i: int, o: _CT) -> None: ...
\r
159 def __setitem__(self, s: slice, o: Iterable[_CT]) -> None: ...
\r
161 def resize(obj: _CData, size: int) -> None: ...
\r
162 if sys.version_info < (3,):
\r
163 def set_conversion_mode(encoding: str, errors: str) -> Tuple[str, str]: ...
\r
164 def set_errno(value: int) -> int: ...
\r
165 if sys.platform == 'win32':
\r
166 def set_last_error(value: int) -> int: ...
\r
167 def sizeof(obj_or_type: _UnionT[_CData, Type[_CData]]) -> int: ...
\r
168 def string_at(address: _CVoidConstPLike, size: int = ...) -> bytes: ...
\r
169 if sys.platform == 'win32':
\r
170 def WinError(code: Optional[int] = ...,
\r
171 desc: Optional[str] = ...) -> WindowsError: ...
\r
172 def wstring_at(address: _CVoidConstPLike, size: int = ...) -> str: ...
\r
174 class _SimpleCData(Generic[_T], _CData):
\r
176 def __init__(self, value: _T = ...) -> None: ...
\r
178 class c_byte(_SimpleCData[int]): ...
\r
180 class c_char(_SimpleCData[bytes]):
\r
181 def __init__(self, value: _UnionT[int, bytes] = ...) -> None: ...
\r
182 class c_char_p(_PointerLike, _SimpleCData[Optional[bytes]]):
\r
183 def __init__(self, value: Optional[_UnionT[int, bytes]] = ...) -> None: ...
\r
185 class c_double(_SimpleCData[float]): ...
\r
186 class c_longdouble(_SimpleCData[float]): ...
\r
187 class c_float(_SimpleCData[float]): ...
\r
189 class c_int(_SimpleCData[int]): ...
\r
190 class c_int8(_SimpleCData[int]): ...
\r
191 class c_int16(_SimpleCData[int]): ...
\r
192 class c_int32(_SimpleCData[int]): ...
\r
193 class c_int64(_SimpleCData[int]): ...
\r
195 class c_long(_SimpleCData[int]): ...
\r
196 class c_longlong(_SimpleCData[int]): ...
\r
198 class c_short(_SimpleCData[int]): ...
\r
200 class c_size_t(_SimpleCData[int]): ...
\r
201 class c_ssize_t(_SimpleCData[int]): ...
\r
203 class c_ubyte(_SimpleCData[int]): ...
\r
205 class c_uint(_SimpleCData[int]): ...
\r
206 class c_uint8(_SimpleCData[int]): ...
\r
207 class c_uint16(_SimpleCData[int]): ...
\r
208 class c_uint32(_SimpleCData[int]): ...
\r
209 class c_uint64(_SimpleCData[int]): ...
\r
211 class c_ulong(_SimpleCData[int]): ...
\r
212 class c_ulonglong(_SimpleCData[int]): ...
\r
214 class c_ushort(_SimpleCData[int]): ...
\r
216 class c_void_p(_PointerLike, _SimpleCData[Optional[int]]): ...
\r
218 class c_wchar(_SimpleCData[Text]): ...
\r
219 class c_wchar_p(_PointerLike, _SimpleCData[Optional[Text]]):
\r
220 def __init__(self, value: Optional[_UnionT[int, Text]] = ...) -> None: ...
\r
222 class c_bool(_SimpleCData[bool]):
\r
223 def __init__(self, value: bool) -> None: ...
\r
225 if sys.platform == 'win32':
\r
226 class HRESULT(_SimpleCData[int]): ... # TODO undocumented
\r
228 class py_object(_SimpleCData[_T]): ...
\r
233 class _StructUnionMeta(_CDataMeta):
\r
234 _fields_: Sequence[_UnionT[Tuple[str, Type[_CData]], Tuple[str, Type[_CData], int]]] = ...
\r
236 _anonymous_: Sequence[str] = ...
\r
237 def __getattr__(self, name: str) -> _CField: ...
\r
238 class _StructUnionBase(_CData, metaclass=_StructUnionMeta):
\r
239 def __init__(self, *args: Any, **kw: Any) -> None: ...
\r
240 def __getattr__(self, name: str) -> Any: ...
\r
241 def __setattr__(self, name: str, value: Any) -> None: ...
\r
243 class Union(_StructUnionBase): ...
\r
244 class Structure(_StructUnionBase): ...
\r
245 class BigEndianStructure(Structure): ...
\r
246 class LittleEndianStructure(Structure): ...
\r
248 class Array(Generic[_T], _CData):
\r
249 _length_: ClassVar[int] = ...
\r
250 _type_: ClassVar[Type[_T]] = ...
\r
251 raw: bytes = ... # TODO only available with _T == c_char
\r
252 value: bytes = ... # TODO only available with _T == c_char
\r
253 # TODO These methods cannot be annotated correctly at the moment.
\r
254 # All of these "Any"s stand for the array's element type, but it's not possible to use _T here,
\r
255 # because of a special feature of ctypes.
\r
256 # By default, when accessing an element of an Array[_T], the returned object has type _T.
\r
257 # However, when _T is a "simple type" like c_int, ctypes automatically "unboxes" the object
\r
258 # and converts it to the corresponding Python primitive. For example, when accessing an element
\r
259 # of an Array[c_int], a Python int object is returned, not a c_int.
\r
260 # This behavior does *not* apply to subclasses of "simple types".
\r
261 # If MyInt is a subclass of c_int, then accessing an element of an Array[MyInt] returns
\r
262 # a MyInt, not an int.
\r
263 # This special behavior is not easy to model in a stub, so for now all places where
\r
264 # the array element type would belong are annotated with Any instead.
\r
265 def __init__(self, *args: Any) -> None: ...
\r
267 def __getitem__(self, i: int) -> Any: ...
\r
269 def __getitem__(self, s: slice) -> List[Any]: ...
\r
271 def __setitem__(self, i: int, o: Any) -> None: ...
\r
273 def __setitem__(self, s: slice, o: Iterable[Any]) -> None: ...
\r
274 def __iter__(self) -> Iterator[Any]: ...
\r
275 # Can't inherit from Sized because the metaclass conflict between
\r
276 # Sized and _CData prevents using _CDataMeta.
\r
277 def __len__(self) -> int: ...
\r