Skip to content

move some pointer-related-stuffs from ctypes/__init__.pyi to _ctypes.pyi, as at runtime. #10133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion stdlib/_ctypes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -70,6 +70,25 @@ 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 _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
Expand Down
25 changes: 5 additions & 20 deletions stdlib/ctypes/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import sys
from _ctypes import (
POINTER as POINTER,
RTLD_GLOBAL as RTLD_GLOBAL,
RTLD_LOCAL as RTLD_LOCAL,
ArgumentError as ArgumentError,
Array as Array,
Structure as Structure,
Union as Union,
_CanCastTo as _CanCastTo,
_CData as _CData,
_CDataMeta as _CDataMeta,
_CField as _CField,
_Pointer as _Pointer,
_PointerLike as _PointerLike,
_SimpleCData as _SimpleCData,
_StructUnionBase as _StructUnionBase,
_StructUnionMeta as _StructUnionMeta,
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,
Expand All @@ -31,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

Expand Down Expand Up @@ -85,9 +89,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]

Expand Down Expand Up @@ -148,22 +149,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":
Expand Down
2 changes: 0 additions & 2 deletions tests/stubtest_allowlists/py3_common.txt
Original file line number Diff line number Diff line change
Expand Up @@ -335,15 +335,13 @@ wave.Wave_read.initfp
wave.Wave_write.initfp

_ctypes.CFuncPtr
_ctypes.POINTER
_ctypes.PyObj_FromPtr
_ctypes.Py_DECREF
_ctypes.Py_INCREF
_ctypes.buffer_info
_ctypes.byref
_ctypes.call_cdeclfunction
_ctypes.call_function
_ctypes.pointer

# ==========
# Allowlist entries that cannot or should not be fixed
Expand Down