2 from _typeshed import ReadableBuffer, WriteableBuffer
20 if sys.version_info >= (3, 9):
21 from types import GenericAlias
24 _DLLT = TypeVar("_DLLT", bound=CDLL)
25 _CT = TypeVar("_CT", bound=_CData)
32 _func_flags_: ClassVar[int]
33 _func_restype_: ClassVar[_CData]
36 _FuncPtr: Type[_FuncPointer]
37 if sys.version_info >= (3, 8):
42 handle: int | None = ...,
43 use_errno: bool = ...,
44 use_last_error: bool = ...,
45 winmode: int | None = ...,
49 self, name: str | None, mode: int = ..., handle: int | None = ..., use_errno: bool = ..., use_last_error: bool = ...
51 def __getattr__(self, name: str) -> _NamedFuncPointer: ...
52 def __getitem__(self, name: str) -> _NamedFuncPointer: ...
54 if sys.platform == "win32":
55 class OleDLL(CDLL): ...
56 class WinDLL(CDLL): ...
58 class PyDLL(CDLL): ...
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: ...
68 cdll: LibraryLoader[CDLL]
69 if sys.platform == "win32":
70 windll: LibraryLoader[WinDLL]
71 oledll: LibraryLoader[OleDLL]
72 pydll: LibraryLoader[PyDLL]
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
82 class _CData(metaclass=_CDataMeta):
85 _objects: Mapping[Any, int] | None
87 def from_buffer(cls: Type[_CT], source: WriteableBuffer, offset: int = ...) -> _CT: ...
89 def from_buffer_copy(cls: Type[_CT], source: ReadableBuffer, offset: int = ...) -> _CT: ...
91 def from_address(cls: Type[_CT], address: int) -> _CT: ...
93 def from_param(cls: Type[_CT], obj: Any) -> _CT | _CArgObject: ...
95 def in_dll(cls: Type[_CT], library: CDLL, name: str) -> _CT: ...
97 class _CanCastTo(_CData): ...
98 class _PointerLike(_CanCastTo): ...
100 _ECT = Callable[[Optional[Type[_CData]], _FuncPointer, Tuple[_CData, ...]], _CData]
101 _PF = _UnionT[Tuple[int], Tuple[int, str], Tuple[int, str, Any]]
103 class _FuncPointer(_PointerLike, _CData):
104 restype: Type[_CData] | Callable[[int], Any] | None
105 argtypes: Sequence[Type[_CData]]
108 def __init__(self, address: int) -> None: ...
110 def __init__(self, callable: Callable[..., Any]) -> None: ...
112 def __init__(self, func_spec: tuple[str | int, CDLL], paramflags: Tuple[_PF, ...] = ...) -> None: ...
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: ...
117 class _NamedFuncPointer(_FuncPointer):
120 class ArgumentError(Exception): ...
123 restype: Type[_CData] | None, *argtypes: Type[_CData], use_errno: bool = ..., use_last_error: bool = ...
124 ) -> Type[_FuncPointer]: ...
126 if sys.platform == "win32":
128 restype: Type[_CData] | None, *argtypes: Type[_CData], use_errno: bool = ..., use_last_error: bool = ...
129 ) -> Type[_FuncPointer]: ...
131 def PYFUNCTYPE(restype: Type[_CData] | None, *argtypes: Type[_CData]) -> Type[_FuncPointer]: ...
133 class _CArgObject: ...
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]
144 def addressof(obj: _CData) -> int: ...
145 def alignment(obj_or_type: _CData | Type[_CData]) -> int: ...
146 def byref(obj: _CData, offset: int = ...) -> _CArgObject: ...
148 _CastT = TypeVar("_CastT", bound=_CanCastTo)
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]: ...
153 c_buffer = create_string_buffer
155 def create_unicode_buffer(init: int | str, size: int | None = ...) -> Array[c_wchar]: ...
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: ...
163 def get_errno() -> int: ...
165 if sys.platform == "win32":
166 def get_last_error() -> int: ...
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]]: ...
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):
178 def __init__(self, arg: _CT = ...) -> None: ...
180 def __getitem__(self, i: int) -> _CT: ...
182 def __getitem__(self, s: slice) -> list[_CT]: ...
184 def __setitem__(self, i: int, o: _CT) -> None: ...
186 def __setitem__(self, s: slice, o: Iterable[_CT]) -> None: ...
188 def resize(obj: _CData, size: int) -> None: ...
189 def set_errno(value: int) -> int: ...
191 if sys.platform == "win32":
192 def set_last_error(value: int) -> int: ...
194 def sizeof(obj_or_type: _CData | Type[_CData]) -> int: ...
195 def string_at(address: _CVoidConstPLike, size: int = ...) -> bytes: ...
197 if sys.platform == "win32":
198 def WinError(code: int | None = ..., descr: str | None = ...) -> OSError: ...
200 def wstring_at(address: _CVoidConstPLike, size: int = ...) -> str: ...
202 class _SimpleCData(Generic[_T], _CData):
204 def __init__(self, value: _T = ...) -> None: ...
206 class c_byte(_SimpleCData[int]): ...
208 class c_char(_SimpleCData[bytes]):
209 def __init__(self, value: int | bytes = ...) -> None: ...
211 class c_char_p(_PointerLike, _SimpleCData[Optional[bytes]]):
212 def __init__(self, value: int | bytes | None = ...) -> None: ...
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]): ...
239 class c_wchar_p(_PointerLike, _SimpleCData[Optional[str]]):
240 def __init__(self, value: int | str | None = ...) -> None: ...
242 class c_bool(_SimpleCData[bool]):
243 def __init__(self, value: bool = ...) -> None: ...
245 if sys.platform == "win32":
246 class HRESULT(_SimpleCData[int]): ... # TODO undocumented
248 class py_object(_CanCastTo, _SimpleCData[_T]): ...
254 class _StructUnionMeta(_CDataMeta):
255 _fields_: Sequence[tuple[str, Type[_CData]] | tuple[str, Type[_CData], int]]
257 _anonymous_: Sequence[str]
258 def __getattr__(self, name: str) -> _CField: ...
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: ...
265 class Union(_StructUnionBase): ...
266 class Structure(_StructUnionBase): ...
267 class BigEndianStructure(Structure): ...
268 class LittleEndianStructure(Structure): ...
270 class Array(Generic[_CT], _CData):
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: ...
289 def __getitem__(self, i: int) -> Any: ...
291 def __getitem__(self, s: slice) -> list[Any]: ...
293 def __setitem__(self, i: int, o: Any) -> None: ...
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: ...