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

pickle: accept ReadableBuffer #7678

Merged
merged 3 commits into from Apr 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 12 additions & 8 deletions stdlib/pickle.pyi
@@ -1,7 +1,8 @@
import sys
from _typeshed import ReadableBuffer
from collections.abc import Callable, Iterable, Iterator, Mapping
from typing import Any, ClassVar, Protocol, Union
from typing_extensions import TypeAlias, final
from typing import Any, ClassVar, Protocol, SupportsBytes, Union
from typing_extensions import SupportsIndex, TypeAlias, final

if sys.version_info >= (3, 8):
__all__ = [
Expand Down Expand Up @@ -183,11 +184,9 @@ class _WritableFileobj(Protocol):
def write(self, __b: bytes) -> Any: ...

if sys.version_info >= (3, 8):
# TODO: holistic design for buffer interface (typing.Buffer?)
@final
class PickleBuffer:
# buffer must be a buffer-providing object
def __init__(self, buffer: Any) -> None: ...
def __init__(self, buffer: ReadableBuffer) -> None: ...
def raw(self) -> memoryview: ...
def release(self) -> None: ...
_BufferCallback: TypeAlias = Callable[[PickleBuffer], Any] | None
Expand All @@ -211,14 +210,19 @@ if sys.version_info >= (3, 8):
buffers: Iterable[Any] | None = ...,
) -> Any: ...
def loads(
__data: bytes, *, fix_imports: bool = ..., encoding: str = ..., errors: str = ..., buffers: Iterable[Any] | None = ...
__data: ReadableBuffer,
*,
fix_imports: bool = ...,
encoding: str = ...,
errors: str = ...,
buffers: Iterable[Any] | None = ...,
) -> Any: ...

else:
def dump(obj: Any, file: _WritableFileobj, protocol: int | None = ..., *, fix_imports: bool = ...) -> None: ...
def dumps(obj: Any, protocol: int | None = ..., *, fix_imports: bool = ...) -> bytes: ...
def load(file: _ReadableFileobj, *, fix_imports: bool = ..., encoding: str = ..., errors: str = ...) -> Any: ...
def loads(data: bytes, *, fix_imports: bool = ..., encoding: str = ..., errors: str = ...) -> Any: ...
def loads(data: ReadableBuffer, *, fix_imports: bool = ..., encoding: str = ..., errors: str = ...) -> Any: ...

class PickleError(Exception): ...
class PicklingError(PickleError): ...
Expand Down Expand Up @@ -359,7 +363,7 @@ if sys.version_info >= (3, 8):
READONLY_BUFFER: bytes

def encode_long(x: int) -> bytes: ... # undocumented
def decode_long(data: bytes) -> int: ... # undocumented
def decode_long(data: Iterable[SupportsIndex] | SupportsBytes | ReadableBuffer) -> int: ... # undocumented

# pure-Python implementations
_Pickler = Pickler # undocumented
Expand Down