|
48 | 48 |
|
49 | 49 | import hashlib
|
50 | 50 | import pathlib
|
| 51 | +import sys |
51 | 52 | from typing import BinaryIO
|
52 | 53 |
|
53 | 54 | from typing_extensions import override
|
@@ -192,11 +193,24 @@ def digest_name(self) -> str:
|
192 | 193 |
|
193 | 194 | @override
|
194 | 195 | def compute(self) -> hashing.Digest:
|
195 |
| - # https://github.com/python/typeshed/issues/2166 |
196 |
| - # pytype: disable=wrong-arg-types |
197 |
| - digest = hashlib.file_digest(self._fd, self._algorithm) |
198 |
| - # pytype: enable=wrong-arg-types |
199 |
| - return hashing.Digest(self.digest_name, digest.digest()) |
| 196 | + if sys.version_info >= (3, 11): |
| 197 | + # https://github.com/python/typeshed/issues/2166 |
| 198 | + # pytype: disable=wrong-arg-types |
| 199 | + digest = hashlib.file_digest(self._fd, self._algorithm) |
| 200 | + # pytype: enable=wrong-arg-types |
| 201 | + return hashing.Digest(self.digest_name, digest.digest()) |
| 202 | + |
| 203 | + # Polyfill for Python 3.10 |
| 204 | + # https://github.com/python/cpython/blob/4deb32a992/Lib/hashlib.py#L195 |
| 205 | + hasher = hashlib.new(self._algorithm) |
| 206 | + buffer = bytearray(2**18) |
| 207 | + view = memoryview(buffer) |
| 208 | + while True: |
| 209 | + size = self._fd.readinto(buffer) |
| 210 | + if size == 0: |
| 211 | + break |
| 212 | + hasher.update(view[:size]) |
| 213 | + return hashing.Digest(self.digest_name, hasher.digest()) |
200 | 214 |
|
201 | 215 | @property
|
202 | 216 | @override
|
|
0 commit comments