Skip to content

Commit 7309e0f

Browse files
committed
gh-155419: test hashlib.file_digest on a non-io.BytesIO buffer
`BytesIOProxy` exposes the `io.BytesIO` interface without being an instance of it, so `hashlib.file_digest` reads it with `readinto()` from its current position.
1 parent 644ac60 commit 7309e0f

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

‎Lib/test/test_hashlib.py‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ def make_hash_objects(*digestmods, buf=b"", **kwargs):
117117
return objects
118118

119119

120+
class BytesIOProxy:
121+
"""A file-like object with the io.BytesIO interface, not an io.BytesIO."""
122+
123+
def __init__(self, data):
124+
self._buffer = io.BytesIO(data)
125+
126+
def __getattr__(self, name):
127+
return getattr(self._buffer, name)
128+
129+
120130
class HashLibTestCase(unittest.TestCase):
121131
supported_hash_names = ( 'md5', 'MD5', 'sha1', 'SHA1',
122132
'sha224', 'SHA224', 'sha256', 'SHA256',
@@ -584,9 +594,10 @@ def check_file_digest(self, name, data, hexdigest, pos=0):
584594
with tempfile.TemporaryFile() as f:
585595
f.write(data)
586596
buf = io.BytesIO(data)
597+
buf_like = BytesIOProxy(data)
587598

588599
for digest, _ in digests:
589-
for fobj in [buf, f]:
600+
for fobj in [buf, buf_like, f]:
590601
fobj.seek(pos)
591602
digestobj = hashlib.file_digest(fobj, digest)
592603
self.assertEqual(digestobj.hexdigest(), hexdigest)

0 commit comments

Comments
 (0)