Use the ReadableBuffer type in more places (#4245)

This is a follow-up on #4232. memoryview, hashlib, and hmac are updated
to use ReadableBuffer type instead of their own home-spun unions of
bytes, bytearray and whatever else each use case used. mmap is being
handled in #4244, and I'll leave BinaryIO for another day (or possibly
another person) because it's going to require some messy code
duplication because the relevant methods are defined in IO[AnyStr].

There's one corner case I'm not quite sure how best to handle: the
documentation for hmac.digest claim that the parmaeters have the same
meanings as in hmac.new, but in CPython the latter has an explicit check
that `key` is bytes or bytearray while the former works with a
memory-view. For now I've matched the documentation.

Also, the documentation for HMAC.update says that `msg` can be any type
supported by hashlib from Python 3.4; but I can't see anything in the
Python 2.7 implementation that would prevent it also taking bytes-like
objects, so I've not tried to treat Python 2 any different to Python 3.
This commit is contained in:
Bruce Merry
2020-06-22 15:17:24 +02:00
committed by GitHub
parent e1d89e5742
commit fb398b1d59
5 changed files with 58 additions and 46 deletions

View File

@@ -15,7 +15,11 @@ from io import (
TextIOWrapper, FileIO, BufferedRandom, BufferedReader, BufferedWriter
)
from types import TracebackType, CodeType
from _typeshed import AnyPath, OpenBinaryMode, OpenTextMode, OpenBinaryModeUpdating, OpenBinaryModeWriting, OpenBinaryModeReading, SupportsWrite
from _typeshed import (
AnyPath, OpenBinaryMode, OpenTextMode,
OpenBinaryModeUpdating, OpenBinaryModeWriting, OpenBinaryModeReading,
SupportsWrite, ReadableBuffer,
)
from typing_extensions import Literal
import sys
@@ -821,12 +825,12 @@ class memoryview(Sized, Container[_mv_container_type]):
f_contiguous: bool
contiguous: bool
nbytes: int
def __init__(self, obj: Union[bytes, bytearray, memoryview]) -> None: ...
def __init__(self, obj: ReadableBuffer) -> None: ...
def __enter__(self) -> memoryview: ...
def __exit__(self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]) -> None: ...
def cast(self, format: str, shape: Union[List[int], Tuple[int]] = ...) -> memoryview: ...
else:
def __init__(self, obj: Union[bytes, bytearray, buffer, memoryview]) -> None: ...
def __init__(self, obj: ReadableBuffer) -> None: ...
@overload
def __getitem__(self, i: int) -> _mv_container_type: ...