Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a6ecf56

Browse files
junkmdAlexWaygood
andauthored
move and rename from ctypes._FuncPointer to _ctypes.CFuncPtr (#10140)
Co-authored-by: Alex Waygood <[email protected]>
1 parent 853d01d commit a6ecf56

3 files changed

Lines changed: 25 additions & 22 deletions

File tree

stdlib/_ctypes.pyi

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import sys
22
from _typeshed import ReadableBuffer, WriteableBuffer
33
from abc import abstractmethod
4-
from collections.abc import Iterable, Iterator, Mapping, Sequence
5-
from ctypes import CDLL
6-
from typing import Any, Generic, TypeVar, overload
4+
from collections.abc import Callable, Iterable, Iterator, Mapping, Sequence
5+
from ctypes import CDLL, c_int
6+
from typing import Any, ClassVar, Generic, TypeVar, overload
77
from typing_extensions import Self, TypeAlias
88

99
if sys.version_info >= (3, 9):
@@ -93,6 +93,24 @@ class _CArgObject: ...
9393

9494
def byref(obj: _CData, offset: int = ...) -> _CArgObject: ...
9595

96+
_ECT: TypeAlias = Callable[[type[_CData] | None, CFuncPtr, tuple[_CData, ...]], _CData]
97+
_PF: TypeAlias = tuple[int] | tuple[int, str] | tuple[int, str, Any]
98+
99+
class CFuncPtr(_PointerLike, _CData):
100+
restype: type[_CData] | Callable[[int], Any] | None
101+
argtypes: Sequence[type[_CData]]
102+
errcheck: _ECT
103+
_flags_: ClassVar[int] # Abstract attribute that must be defined on subclasses
104+
@overload
105+
def __init__(self, address: int) -> None: ...
106+
@overload
107+
def __init__(self, callable: Callable[..., Any]) -> None: ...
108+
@overload
109+
def __init__(self, func_spec: tuple[str | int, CDLL], paramflags: tuple[_PF, ...] = ...) -> None: ...
110+
@overload
111+
def __init__(self, vtlb_index: int, name: str, paramflags: tuple[_PF, ...] = ..., iid: _Pointer[c_int] = ...) -> None: ...
112+
def __call__(self, *args: Any, **kwargs: Any) -> Any: ...
113+
96114
class _CField:
97115
offset: int
98116
size: int

stdlib/ctypes/__init__.pyi

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ from _ctypes import (
55
RTLD_LOCAL as RTLD_LOCAL,
66
ArgumentError as ArgumentError,
77
Array as Array,
8+
CFuncPtr as _CFuncPtr,
89
Structure as Structure,
910
Union as Union,
1011
_CanCastTo as _CanCastTo,
@@ -26,8 +27,7 @@ from _ctypes import (
2627
set_errno as set_errno,
2728
sizeof as sizeof,
2829
)
29-
from collections.abc import Callable, Sequence
30-
from typing import Any, ClassVar, Generic, TypeVar, overload
30+
from typing import Any, ClassVar, Generic, TypeVar
3131
from typing_extensions import TypeAlias
3232

3333
if sys.platform == "win32":
@@ -91,22 +91,7 @@ if sys.platform == "win32":
9191
pydll: LibraryLoader[PyDLL]
9292
pythonapi: PyDLL
9393

94-
_ECT: TypeAlias = Callable[[type[_CData] | None, _FuncPointer, tuple[_CData, ...]], _CData]
95-
_PF: TypeAlias = tuple[int] | tuple[int, str] | tuple[int, str, Any]
96-
97-
class _FuncPointer(_PointerLike, _CData):
98-
restype: type[_CData] | Callable[[int], Any] | None
99-
argtypes: Sequence[type[_CData]]
100-
errcheck: _ECT
101-
@overload
102-
def __init__(self, address: int) -> None: ...
103-
@overload
104-
def __init__(self, callable: Callable[..., Any]) -> None: ...
105-
@overload
106-
def __init__(self, func_spec: tuple[str | int, CDLL], paramflags: tuple[_PF, ...] = ...) -> None: ...
107-
@overload
108-
def __init__(self, vtlb_index: int, name: str, paramflags: tuple[_PF, ...] = ..., iid: _Pointer[c_int] = ...) -> None: ...
109-
def __call__(self, *args: Any, **kwargs: Any) -> Any: ...
94+
class _FuncPointer(_CFuncPtr): ...
11095

11196
class _NamedFuncPointer(_FuncPointer):
11297
__name__: str

tests/stubtest_allowlists/py3_common.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ _collections_abc.Set.__rsub__
2424
_collections_abc.Set.__rxor__
2525

2626
_csv.Dialect.__init__ # C __init__ signature is inaccurate
27+
_ctypes.CFuncPtr # stubtest erroneously thinks it can't be subclassed
2728
_threading_local.local.__new__
2829
_weakref.ref.* # Alias for _weakref.ReferenceType, problems should be fixed there
2930
_weakref.CallableProxyType.__getattr__ # Should have all attributes of proxy
@@ -334,7 +335,6 @@ turtle.ScrolledCanvas.onResize
334335
wave.Wave_read.initfp
335336
wave.Wave_write.initfp
336337

337-
_ctypes.CFuncPtr
338338
_ctypes.PyObj_FromPtr
339339
_ctypes.Py_DECREF
340340
_ctypes.Py_INCREF

0 commit comments

Comments
 (0)