|
1 | 1 | import sys |
2 | | -from _ctypes import RTLD_GLOBAL as RTLD_GLOBAL, RTLD_LOCAL as RTLD_LOCAL |
3 | | -from _typeshed import ReadableBuffer, WriteableBuffer |
4 | | -from abc import abstractmethod |
5 | | -from collections.abc import Callable, Iterable, Iterator, Mapping, Sequence |
| 2 | +from _ctypes import ( |
| 3 | + RTLD_GLOBAL as RTLD_GLOBAL, |
| 4 | + RTLD_LOCAL as RTLD_LOCAL, |
| 5 | + Array as Array, |
| 6 | + _CData as _CData, |
| 7 | + _CDataMeta as _CDataMeta, |
| 8 | + _SimpleCData as _SimpleCData, |
| 9 | +) |
| 10 | +from collections.abc import Callable, Sequence |
6 | 11 | from typing import Any, ClassVar, Generic, TypeVar, overload |
7 | | -from typing_extensions import Self, TypeAlias |
| 12 | +from typing_extensions import TypeAlias |
8 | 13 |
|
9 | 14 | if sys.version_info >= (3, 9): |
10 | 15 | from types import GenericAlias |
@@ -65,28 +70,6 @@ if sys.platform == "win32": |
65 | 70 | pydll: LibraryLoader[PyDLL] |
66 | 71 | pythonapi: PyDLL |
67 | 72 |
|
68 | | -class _CDataMeta(type): |
69 | | - # By default mypy complains about the following two methods, because strictly speaking cls |
70 | | - # might not be a Type[_CT]. However this can never actually happen, because the only class that |
71 | | - # uses _CDataMeta as its metaclass is _CData. So it's safe to ignore the errors here. |
72 | | - def __mul__(cls: type[_CT], other: int) -> type[Array[_CT]]: ... # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues] |
73 | | - def __rmul__(cls: type[_CT], other: int) -> type[Array[_CT]]: ... # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues] |
74 | | - |
75 | | -class _CData(metaclass=_CDataMeta): |
76 | | - _b_base_: int |
77 | | - _b_needsfree_: bool |
78 | | - _objects: Mapping[Any, int] | None |
79 | | - @classmethod |
80 | | - def from_buffer(cls, source: WriteableBuffer, offset: int = ...) -> Self: ... |
81 | | - @classmethod |
82 | | - def from_buffer_copy(cls, source: ReadableBuffer, offset: int = ...) -> Self: ... |
83 | | - @classmethod |
84 | | - def from_address(cls, address: int) -> Self: ... |
85 | | - @classmethod |
86 | | - def from_param(cls, obj: Any) -> Self | _CArgObject: ... |
87 | | - @classmethod |
88 | | - def in_dll(cls, library: CDLL, name: str) -> Self: ... |
89 | | - |
90 | 73 | class _CanCastTo(_CData): ... |
91 | 74 | class _PointerLike(_CanCastTo): ... |
92 | 75 |
|
@@ -190,12 +173,6 @@ if sys.platform == "win32": |
190 | 173 |
|
191 | 174 | def wstring_at(address: _CVoidConstPLike, size: int = -1) -> str: ... |
192 | 175 |
|
193 | | -class _SimpleCData(Generic[_T], _CData): |
194 | | - value: _T |
195 | | - # The TypeVar can be unsolved here, |
196 | | - # but we can't use overloads without creating many, many mypy false-positive errors |
197 | | - def __init__(self, value: _T = ...) -> None: ... # pyright: ignore[reportInvalidTypeVarUse] |
198 | | - |
199 | 176 | class c_byte(_SimpleCData[int]): ... |
200 | 177 |
|
201 | 178 | class c_char(_SimpleCData[bytes]): |
@@ -259,48 +236,3 @@ class Union(_StructUnionBase): ... |
259 | 236 | class Structure(_StructUnionBase): ... |
260 | 237 | class BigEndianStructure(Structure): ... |
261 | 238 | class LittleEndianStructure(Structure): ... |
262 | | - |
263 | | -class Array(Generic[_CT], _CData): |
264 | | - @property |
265 | | - @abstractmethod |
266 | | - def _length_(self) -> int: ... |
267 | | - @_length_.setter |
268 | | - def _length_(self, value: int) -> None: ... |
269 | | - @property |
270 | | - @abstractmethod |
271 | | - def _type_(self) -> type[_CT]: ... |
272 | | - @_type_.setter |
273 | | - def _type_(self, value: type[_CT]) -> None: ... |
274 | | - # Note: only available if _CT == c_char |
275 | | - @property |
276 | | - def raw(self) -> bytes: ... |
277 | | - @raw.setter |
278 | | - def raw(self, value: ReadableBuffer) -> None: ... |
279 | | - value: Any # Note: bytes if _CT == c_char, str if _CT == c_wchar, unavailable otherwise |
280 | | - # TODO These methods cannot be annotated correctly at the moment. |
281 | | - # All of these "Any"s stand for the array's element type, but it's not possible to use _CT |
282 | | - # here, because of a special feature of ctypes. |
283 | | - # By default, when accessing an element of an Array[_CT], the returned object has type _CT. |
284 | | - # However, when _CT is a "simple type" like c_int, ctypes automatically "unboxes" the object |
285 | | - # and converts it to the corresponding Python primitive. For example, when accessing an element |
286 | | - # of an Array[c_int], a Python int object is returned, not a c_int. |
287 | | - # This behavior does *not* apply to subclasses of "simple types". |
288 | | - # If MyInt is a subclass of c_int, then accessing an element of an Array[MyInt] returns |
289 | | - # a MyInt, not an int. |
290 | | - # This special behavior is not easy to model in a stub, so for now all places where |
291 | | - # the array element type would belong are annotated with Any instead. |
292 | | - def __init__(self, *args: Any) -> None: ... |
293 | | - @overload |
294 | | - def __getitem__(self, __key: int) -> Any: ... |
295 | | - @overload |
296 | | - def __getitem__(self, __key: slice) -> list[Any]: ... |
297 | | - @overload |
298 | | - def __setitem__(self, __key: int, __value: Any) -> None: ... |
299 | | - @overload |
300 | | - def __setitem__(self, __key: slice, __value: Iterable[Any]) -> None: ... |
301 | | - def __iter__(self) -> Iterator[Any]: ... |
302 | | - # Can't inherit from Sized because the metaclass conflict between |
303 | | - # Sized and _CData prevents using _CDataMeta. |
304 | | - def __len__(self) -> int: ... |
305 | | - if sys.version_info >= (3, 9): |
306 | | - def __class_getitem__(cls, item: Any) -> GenericAlias: ... |
0 commit comments