Skip to content
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

ssl, socket, array: Improve bytes handling #8997

Merged
merged 3 commits into from
Oct 28, 2022

Conversation

JelleZijlstra
Copy link
Member

No description provided.

_Address: TypeAlias = tuple[Any, ...] | str
# AF_NETLINK, AF_TIPC) or strings/buffers (AF_UNIX).
# See getsockaddrarg() in socketmodule.c.
_Address: TypeAlias = tuple[Any, ...] | str | ReadableBuffer
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All uses of this alias in the socket stubs allow a ReadableBuffer, so I put ReadableBuffer in the alias and removed explicit usage of bytes below.

The alias is used in some other stubs, but we can review those in a different PR.

@@ -21,15 +21,19 @@ class array(MutableSequence[_T], Generic[_T]):
@property
def itemsize(self) -> int: ...
@overload
def __init__(self: array[int], __typecode: _IntTypeCode, __initializer: bytes | Iterable[int] = ...) -> None: ...
def __init__(self: array[int], __typecode: _IntTypeCode, __initializer: bytes | bytearray | Iterable[int] = ...) -> None: ...
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bytes and bytearray specifically are treated differently. Arbitrary non-iterable buffers (e.g., pickle.PickleBuffer) don't work.

@@ -788,5 +788,10 @@ if sys.version_info >= (3, 8):

# the 5th tuple item is an address
def getaddrinfo(
host: bytes | str | None, port: str | int | None, family: int = ..., type: int = ..., proto: int = ..., flags: int = ...
host: bytes | str | None,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It explicitly accepts only bytes, not bytearray.

@@ -11,7 +11,7 @@ _PCTRTTT: TypeAlias = tuple[_PCTRTT, ...]
_PeerCertRetDictType: TypeAlias = dict[str, str | _PCTRTTT | _PCTRTT]
_PeerCertRetType: TypeAlias = _PeerCertRetDictType | bytes | None
_EnumRetType: TypeAlias = list[tuple[bytes, str, set[str] | bool]]
_PasswordType: TypeAlias = Union[Callable[[], str | bytes], str, bytes]
_PasswordType: TypeAlias = Union[Callable[[], str | bytes | bytearray], str, bytes, bytearray]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The handling code handles bytes and bytearray, but not arbitrary buffers.

def match_hostname(cert: _PeerCertRetType, hostname: str) -> None: ...
def RAND_add(__string: str | ReadableBuffer, __entropy: float) -> None: ...

if sys.version_info < (3, 12):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noticed this one is gone in 3.12.

timeout: int
ticket_lifetime_hint: int
has_ticket: bool
@property
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these are read-only.

@github-actions

This comment has been minimized.

# AF_NETLINK, AF_TIPC) or strings (AF_UNIX).
_Address: TypeAlias = tuple[Any, ...] | str
# AF_NETLINK, AF_TIPC) or strings/buffers (AF_UNIX).
# See getsockaddrarg() in socketmodule.c.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more case is bytes only for BTPROTO_HCI on NetBSD, but I don't think this exotic case is worth mentioning.

stdlib/socket.pyi Outdated Show resolved Hide resolved
stdlib/socket.pyi Outdated Show resolved Hide resolved
Co-authored-by: Sebastian Rittau <[email protected]>
@JelleZijlstra
Copy link
Member Author

Thanks @srittau!

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@srittau srittau merged commit 287fce4 into python:master Oct 28, 2022
@JelleZijlstra JelleZijlstra deleted the sslbytes branch October 28, 2022 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants