From 026555cbcc6490c4d75182c301150bdd9706860d Mon Sep 17 00:00:00 2001 From: junkmd Date: Wed, 3 May 2023 10:49:02 +0900 Subject: [PATCH 1/4] move from `ctypes/__init__.pyi` to `_ctypes.pyi` - `_CanCastTo` - `_PointerLike` - update importing stuffs --- stdlib/_ctypes.pyi | 5 ++++- stdlib/ctypes/__init__.pyi | 5 ++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/stdlib/_ctypes.pyi b/stdlib/_ctypes.pyi index b297191ab4cd..326f77abb6b8 100644 --- a/stdlib/_ctypes.pyi +++ b/stdlib/_ctypes.pyi @@ -2,7 +2,7 @@ import sys from _typeshed import ReadableBuffer, WriteableBuffer from abc import abstractmethod from collections.abc import Iterable, Iterator, Mapping, Sequence -from ctypes import CDLL, _CArgObject, _PointerLike +from ctypes import CDLL, _CArgObject from typing import Any, Generic, TypeVar, overload from typing_extensions import Self, TypeAlias @@ -70,6 +70,9 @@ class _SimpleCData(Generic[_T], _CData): # but we can't use overloads without creating many, many mypy false-positive errors def __init__(self, value: _T = ...) -> None: ... # pyright: ignore[reportInvalidTypeVarUse] +class _CanCastTo(_CData): ... +class _PointerLike(_CanCastTo): ... + class _CField: offset: int size: int diff --git a/stdlib/ctypes/__init__.pyi b/stdlib/ctypes/__init__.pyi index 9bf07fa5bcbd..d99867d8b191 100644 --- a/stdlib/ctypes/__init__.pyi +++ b/stdlib/ctypes/__init__.pyi @@ -6,9 +6,11 @@ from _ctypes import ( Array as Array, Structure as Structure, Union as Union, + _CanCastTo as _CanCastTo, _CData as _CData, _CDataMeta as _CDataMeta, _CField as _CField, + _PointerLike as _PointerLike, _SimpleCData as _SimpleCData, _StructUnionBase as _StructUnionBase, _StructUnionMeta as _StructUnionMeta, @@ -85,9 +87,6 @@ if sys.platform == "win32": pydll: LibraryLoader[PyDLL] pythonapi: PyDLL -class _CanCastTo(_CData): ... -class _PointerLike(_CanCastTo): ... - _ECT: TypeAlias = Callable[[type[_CData] | None, _FuncPointer, tuple[_CData, ...]], _CData] _PF: TypeAlias = tuple[int] | tuple[int, str] | tuple[int, str, Any] From 1d7d00968cb72ecf38aa222f016f57bba28dd977 Mon Sep 17 00:00:00 2001 From: junkmd Date: Wed, 3 May 2023 10:49:02 +0900 Subject: [PATCH 2/4] move from `ctypes/__init__.pyi` to `_ctypes.pyi` - `_Pointer` - `POINTER` - `pointer` --- stdlib/_ctypes.pyi | 16 ++++++++++++++++ stdlib/ctypes/__init__.pyi | 19 +++---------------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/stdlib/_ctypes.pyi b/stdlib/_ctypes.pyi index 326f77abb6b8..26f52ef57c00 100644 --- a/stdlib/_ctypes.pyi +++ b/stdlib/_ctypes.pyi @@ -73,6 +73,22 @@ class _SimpleCData(Generic[_T], _CData): class _CanCastTo(_CData): ... class _PointerLike(_CanCastTo): ... +class _Pointer(Generic[_CT], _PointerLike, _CData): + _type_: type[_CT] + contents: _CT + @overload + def __init__(self) -> None: ... + @overload + def __init__(self, arg: _CT) -> None: ... + @overload + def __getitem__(self, __key: int) -> Any: ... + @overload + def __getitem__(self, __key: slice) -> list[Any]: ... + def __setitem__(self, __key: int, __value: Any) -> None: ... + +def POINTER(type: type[_CT]) -> type[_Pointer[_CT]]: ... +def pointer(__arg: _CT) -> _Pointer[_CT]: ... + class _CField: offset: int size: int diff --git a/stdlib/ctypes/__init__.pyi b/stdlib/ctypes/__init__.pyi index d99867d8b191..f145437997d5 100644 --- a/stdlib/ctypes/__init__.pyi +++ b/stdlib/ctypes/__init__.pyi @@ -1,5 +1,6 @@ import sys from _ctypes import ( + POINTER as POINTER, RTLD_GLOBAL as RTLD_GLOBAL, RTLD_LOCAL as RTLD_LOCAL, ArgumentError as ArgumentError, @@ -10,6 +11,7 @@ from _ctypes import ( _CData as _CData, _CDataMeta as _CDataMeta, _CField as _CField, + _Pointer as _Pointer, _PointerLike as _PointerLike, _SimpleCData as _SimpleCData, _StructUnionBase as _StructUnionBase, @@ -17,6 +19,7 @@ from _ctypes import ( addressof as addressof, alignment as alignment, get_errno as get_errno, + pointer as pointer, resize as resize, set_errno as set_errno, sizeof as sizeof, @@ -147,22 +150,6 @@ if sys.platform == "win32": def memmove(dst: _CVoidPLike, src: _CVoidConstPLike, count: int) -> int: ... def memset(dst: _CVoidPLike, c: int, count: int) -> int: ... -def POINTER(type: type[_CT]) -> type[_Pointer[_CT]]: ... - -class _Pointer(Generic[_CT], _PointerLike, _CData): - _type_: type[_CT] - contents: _CT - @overload - def __init__(self) -> None: ... - @overload - def __init__(self, arg: _CT) -> None: ... - @overload - def __getitem__(self, __key: int) -> Any: ... - @overload - def __getitem__(self, __key: slice) -> list[Any]: ... - def __setitem__(self, __key: int, __value: Any) -> None: ... - -def pointer(__arg: _CT) -> _Pointer[_CT]: ... def string_at(address: _CVoidConstPLike, size: int = -1) -> bytes: ... if sys.platform == "win32": From c20847126e997f4ea011da5c417e691711e9e86e Mon Sep 17 00:00:00 2001 From: junkmd Date: Wed, 3 May 2023 10:49:02 +0900 Subject: [PATCH 3/4] remove unused `TypeVar` "CT" --- stdlib/ctypes/__init__.pyi | 1 - 1 file changed, 1 deletion(-) diff --git a/stdlib/ctypes/__init__.pyi b/stdlib/ctypes/__init__.pyi index f145437997d5..bcb450172dfe 100644 --- a/stdlib/ctypes/__init__.pyi +++ b/stdlib/ctypes/__init__.pyi @@ -36,7 +36,6 @@ if sys.version_info >= (3, 9): _T = TypeVar("_T") _DLLT = TypeVar("_DLLT", bound=CDLL) -_CT = TypeVar("_CT", bound=_CData) DEFAULT_MODE: int From b8896f3cfd3f40d6374d7a6913c0268abe6bbddb Mon Sep 17 00:00:00 2001 From: junkmd Date: Wed, 3 May 2023 10:49:02 +0900 Subject: [PATCH 4/4] update `stubtest_allowlists/py3_common.txt` --- tests/stubtest_allowlists/py3_common.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/stubtest_allowlists/py3_common.txt b/tests/stubtest_allowlists/py3_common.txt index 655fb86074a2..8bf7762d5bc6 100644 --- a/tests/stubtest_allowlists/py3_common.txt +++ b/tests/stubtest_allowlists/py3_common.txt @@ -335,7 +335,6 @@ wave.Wave_read.initfp wave.Wave_write.initfp _ctypes.CFuncPtr -_ctypes.POINTER _ctypes.PyObj_FromPtr _ctypes.Py_DECREF _ctypes.Py_INCREF @@ -343,7 +342,6 @@ _ctypes.buffer_info _ctypes.byref _ctypes.call_cdeclfunction _ctypes.call_function -_ctypes.pointer # ========== # Allowlist entries that cannot or should not be fixed