From ed3208ddda1d1c4f3845fef7fa0aa58da0e8381b Mon Sep 17 00:00:00 2001 From: junkmd Date: Thu, 4 May 2023 01:06:46 +0900 Subject: [PATCH 1/9] move from `ctypes/__init__.pyi` to `_ctypes.pyi` - `_FuncPointer` - TypeVars - update importing stuffs --- stdlib/_ctypes.pyi | 21 +++++++++++++++++++-- stdlib/ctypes/__init__.pyi | 18 +----------------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/stdlib/_ctypes.pyi b/stdlib/_ctypes.pyi index 51e091d9240f..ae353e56d5e7 100644 --- a/stdlib/_ctypes.pyi +++ b/stdlib/_ctypes.pyi @@ -1,8 +1,8 @@ import sys from _typeshed import ReadableBuffer, WriteableBuffer from abc import abstractmethod -from collections.abc import Iterable, Iterator, Mapping, Sequence -from ctypes import CDLL +from collections.abc import Callable, Iterable, Iterator, Mapping, Sequence +from ctypes import CDLL, c_int from typing import Any, Generic, TypeVar, overload from typing_extensions import Self, TypeAlias @@ -93,6 +93,23 @@ class _CArgObject: ... def byref(obj: _CData, offset: int = ...) -> _CArgObject: ... +_ECT: TypeAlias = Callable[[type[_CData] | None, _FuncPointer, tuple[_CData, ...]], _CData] +_PF: TypeAlias = tuple[int] | tuple[int, str] | tuple[int, str, Any] + +class _FuncPointer(_PointerLike, _CData): + restype: type[_CData] | Callable[[int], Any] | None + argtypes: Sequence[type[_CData]] + errcheck: _ECT + @overload + def __init__(self, address: int) -> None: ... + @overload + def __init__(self, callable: Callable[..., Any]) -> None: ... + @overload + def __init__(self, func_spec: tuple[str | int, CDLL], paramflags: tuple[_PF, ...] = ...) -> None: ... + @overload + def __init__(self, vtlb_index: int, name: str, paramflags: tuple[_PF, ...] = ..., iid: _Pointer[c_int] = ...) -> None: ... + def __call__(self, *args: Any, **kwargs: Any) -> Any: ... + class _CField: offset: int size: int diff --git a/stdlib/ctypes/__init__.pyi b/stdlib/ctypes/__init__.pyi index 66f60e0f16e1..02fb11653980 100644 --- a/stdlib/ctypes/__init__.pyi +++ b/stdlib/ctypes/__init__.pyi @@ -12,6 +12,7 @@ from _ctypes import ( _CData as _CData, _CDataMeta as _CDataMeta, _CField as _CField, + _FuncPointer as _FuncPointer, _Pointer as _Pointer, _PointerLike as _PointerLike, _SimpleCData as _SimpleCData, @@ -91,23 +92,6 @@ if sys.platform == "win32": pydll: LibraryLoader[PyDLL] pythonapi: PyDLL -_ECT: TypeAlias = Callable[[type[_CData] | None, _FuncPointer, tuple[_CData, ...]], _CData] -_PF: TypeAlias = tuple[int] | tuple[int, str] | tuple[int, str, Any] - -class _FuncPointer(_PointerLike, _CData): - restype: type[_CData] | Callable[[int], Any] | None - argtypes: Sequence[type[_CData]] - errcheck: _ECT - @overload - def __init__(self, address: int) -> None: ... - @overload - def __init__(self, callable: Callable[..., Any]) -> None: ... - @overload - def __init__(self, func_spec: tuple[str | int, CDLL], paramflags: tuple[_PF, ...] = ...) -> None: ... - @overload - def __init__(self, vtlb_index: int, name: str, paramflags: tuple[_PF, ...] = ..., iid: _Pointer[c_int] = ...) -> None: ... - def __call__(self, *args: Any, **kwargs: Any) -> Any: ... - class _NamedFuncPointer(_FuncPointer): __name__: str From c5324ed5eb1069ab61d2dd9ce1f11eeab0a3f5eb Mon Sep 17 00:00:00 2001 From: junkmd Date: Thu, 4 May 2023 01:06:46 +0900 Subject: [PATCH 2/9] remove unused symbols in `ctypes/__init__.pyi` - `collections.abc` - `Callable` - `Sequence` - `typing` - `overload` --- stdlib/ctypes/__init__.pyi | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stdlib/ctypes/__init__.pyi b/stdlib/ctypes/__init__.pyi index 02fb11653980..1e5e2e055534 100644 --- a/stdlib/ctypes/__init__.pyi +++ b/stdlib/ctypes/__init__.pyi @@ -27,8 +27,7 @@ from _ctypes import ( set_errno as set_errno, sizeof as sizeof, ) -from collections.abc import Callable, Sequence -from typing import Any, ClassVar, Generic, TypeVar, overload +from typing import Any, ClassVar, Generic, TypeVar from typing_extensions import TypeAlias if sys.platform == "win32": From 10fc7d727d6bf57476f3d125d23ded7cb7a2ea02 Mon Sep 17 00:00:00 2001 From: junkmd Date: Thu, 4 May 2023 01:06:46 +0900 Subject: [PATCH 3/9] update classname from `_FuncPointer` to `CFuncPtr` --- stdlib/_ctypes.pyi | 4 ++-- stdlib/ctypes/__init__.pyi | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/stdlib/_ctypes.pyi b/stdlib/_ctypes.pyi index ae353e56d5e7..91898593be4a 100644 --- a/stdlib/_ctypes.pyi +++ b/stdlib/_ctypes.pyi @@ -93,10 +93,10 @@ class _CArgObject: ... def byref(obj: _CData, offset: int = ...) -> _CArgObject: ... -_ECT: TypeAlias = Callable[[type[_CData] | None, _FuncPointer, tuple[_CData, ...]], _CData] +_ECT: TypeAlias = Callable[[type[_CData] | None, CFuncPtr, tuple[_CData, ...]], _CData] _PF: TypeAlias = tuple[int] | tuple[int, str] | tuple[int, str, Any] -class _FuncPointer(_PointerLike, _CData): +class CFuncPtr(_PointerLike, _CData): restype: type[_CData] | Callable[[int], Any] | None argtypes: Sequence[type[_CData]] errcheck: _ECT diff --git a/stdlib/ctypes/__init__.pyi b/stdlib/ctypes/__init__.pyi index 1e5e2e055534..addda1ecffcb 100644 --- a/stdlib/ctypes/__init__.pyi +++ b/stdlib/ctypes/__init__.pyi @@ -5,6 +5,7 @@ from _ctypes import ( RTLD_LOCAL as RTLD_LOCAL, ArgumentError as ArgumentError, Array as Array, + CFuncPtr as _FuncPointer, Structure as Structure, Union as Union, _CanCastTo as _CanCastTo, @@ -12,7 +13,6 @@ from _ctypes import ( _CData as _CData, _CDataMeta as _CDataMeta, _CField as _CField, - _FuncPointer as _FuncPointer, _Pointer as _Pointer, _PointerLike as _PointerLike, _SimpleCData as _SimpleCData, From 95646f210b0d38a527422a6968a59b7b15a98d87 Mon Sep 17 00:00:00 2001 From: junkmd Date: Thu, 4 May 2023 01:06:46 +0900 Subject: [PATCH 4/9] redefine `_FuncPointer` as subclass of `_CFuncPtr` --- stdlib/ctypes/__init__.pyi | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/stdlib/ctypes/__init__.pyi b/stdlib/ctypes/__init__.pyi index addda1ecffcb..7a185a5b523e 100644 --- a/stdlib/ctypes/__init__.pyi +++ b/stdlib/ctypes/__init__.pyi @@ -5,7 +5,7 @@ from _ctypes import ( RTLD_LOCAL as RTLD_LOCAL, ArgumentError as ArgumentError, Array as Array, - CFuncPtr as _FuncPointer, + CFuncPtr as _CFuncPtr, Structure as Structure, Union as Union, _CanCastTo as _CanCastTo, @@ -91,6 +91,8 @@ if sys.platform == "win32": pydll: LibraryLoader[PyDLL] pythonapi: PyDLL +class _FuncPointer(_CFuncPtr): ... + class _NamedFuncPointer(_FuncPointer): __name__: str From 33358c583af2a0e23c7d11f260b16c40cbd81fa0 Mon Sep 17 00:00:00 2001 From: junkmd Date: Thu, 4 May 2023 01:06:46 +0900 Subject: [PATCH 5/9] update `stubtest_allowlists/py3_common.txt` --- tests/stubtest_allowlists/py3_common.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/stubtest_allowlists/py3_common.txt b/tests/stubtest_allowlists/py3_common.txt index 8d0df1655c04..4e3aca4ca7c0 100644 --- a/tests/stubtest_allowlists/py3_common.txt +++ b/tests/stubtest_allowlists/py3_common.txt @@ -334,7 +334,6 @@ turtle.ScrolledCanvas.onResize wave.Wave_read.initfp wave.Wave_write.initfp -_ctypes.CFuncPtr _ctypes.PyObj_FromPtr _ctypes.Py_DECREF _ctypes.Py_INCREF From 6399ecd425efade47a6c73ba830984f58ccad094 Mon Sep 17 00:00:00 2001 From: junkmd Date: Fri, 5 May 2023 15:15:29 +0900 Subject: [PATCH 6/9] add `_ctypes.CFuncPtr._flags_` to allowlists --- tests/stubtest_allowlists/py3_common.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/stubtest_allowlists/py3_common.txt b/tests/stubtest_allowlists/py3_common.txt index 4e3aca4ca7c0..1db066b6c71e 100644 --- a/tests/stubtest_allowlists/py3_common.txt +++ b/tests/stubtest_allowlists/py3_common.txt @@ -334,6 +334,7 @@ turtle.ScrolledCanvas.onResize wave.Wave_read.initfp wave.Wave_write.initfp +_ctypes.CFuncPtr._flags_ _ctypes.PyObj_FromPtr _ctypes.Py_DECREF _ctypes.Py_INCREF From caedbdacd082071fda86cf1fa0e2d0d6ac7ae31c Mon Sep 17 00:00:00 2001 From: junkmd Date: Fri, 5 May 2023 15:31:22 +0900 Subject: [PATCH 7/9] add the `_flags_` attribute to `_ctypes.CFuncPtr` and remove `_ctypes.CFuncPtr._flags_` from allowlists --- stdlib/_ctypes.pyi | 1 + tests/stubtest_allowlists/py3_common.txt | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/_ctypes.pyi b/stdlib/_ctypes.pyi index 91898593be4a..e4a516eee859 100644 --- a/stdlib/_ctypes.pyi +++ b/stdlib/_ctypes.pyi @@ -100,6 +100,7 @@ class CFuncPtr(_PointerLike, _CData): restype: type[_CData] | Callable[[int], Any] | None argtypes: Sequence[type[_CData]] errcheck: _ECT + _flags_: int @overload def __init__(self, address: int) -> None: ... @overload diff --git a/tests/stubtest_allowlists/py3_common.txt b/tests/stubtest_allowlists/py3_common.txt index 1db066b6c71e..4e3aca4ca7c0 100644 --- a/tests/stubtest_allowlists/py3_common.txt +++ b/tests/stubtest_allowlists/py3_common.txt @@ -334,7 +334,6 @@ turtle.ScrolledCanvas.onResize wave.Wave_read.initfp wave.Wave_write.initfp -_ctypes.CFuncPtr._flags_ _ctypes.PyObj_FromPtr _ctypes.Py_DECREF _ctypes.Py_INCREF From e7fc61948ecc2a102bf5879a3aac2511a530fc91 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Fri, 5 May 2023 07:48:55 +0100 Subject: [PATCH 8/9] Hush stubtest --- tests/stubtest_allowlists/py3_common.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/stubtest_allowlists/py3_common.txt b/tests/stubtest_allowlists/py3_common.txt index 4e3aca4ca7c0..c2e0b96e64c9 100644 --- a/tests/stubtest_allowlists/py3_common.txt +++ b/tests/stubtest_allowlists/py3_common.txt @@ -24,6 +24,7 @@ _collections_abc.Set.__rsub__ _collections_abc.Set.__rxor__ _csv.Dialect.__init__ # C __init__ signature is inaccurate +_ctypes.CFuncPtr # stubtest erroneously thinks it can't be subclassed _threading_local.local.__new__ _weakref.ref.* # Alias for _weakref.ReferenceType, problems should be fixed there _weakref.CallableProxyType.__getattr__ # Should have all attributes of proxy From b22975703981379ee7c49463759329ce633f03c3 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Fri, 5 May 2023 08:00:24 +0100 Subject: [PATCH 9/9] Tweak slightly --- stdlib/_ctypes.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/_ctypes.pyi b/stdlib/_ctypes.pyi index e4a516eee859..3e3c8d29ac7d 100644 --- a/stdlib/_ctypes.pyi +++ b/stdlib/_ctypes.pyi @@ -3,7 +3,7 @@ from _typeshed import ReadableBuffer, WriteableBuffer from abc import abstractmethod from collections.abc import Callable, Iterable, Iterator, Mapping, Sequence from ctypes import CDLL, c_int -from typing import Any, Generic, TypeVar, overload +from typing import Any, ClassVar, Generic, TypeVar, overload from typing_extensions import Self, TypeAlias if sys.version_info >= (3, 9): @@ -100,7 +100,7 @@ class CFuncPtr(_PointerLike, _CData): restype: type[_CData] | Callable[[int], Any] | None argtypes: Sequence[type[_CData]] errcheck: _ECT - _flags_: int + _flags_: ClassVar[int] # Abstract attribute that must be defined on subclasses @overload def __init__(self, address: int) -> None: ... @overload