Skip to content

gh-128657: Run test_hashlib with --parallel-threads #129833

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

Merged
merged 3 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Lib/test/libregrtest/tsan.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
# the regression test runner with the `--parallel-threads` option enabled.
TSAN_PARALLEL_TESTS = [
'test_abc',
'test_hashlib',
]


Expand Down
41 changes: 19 additions & 22 deletions Lib/test/test_hashlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
import os
import sys
import sysconfig
import tempfile
import threading
import unittest
import warnings
from test import support
from test.support import _4G, bigmemtest
from test.support.import_helper import import_fresh_module
from test.support import os_helper
from test.support import requires_resource
from test.support import threading_helper
from http.client import HTTPException
Expand Down Expand Up @@ -414,21 +414,18 @@ def check_file_digest(self, name, data, hexdigest):
digests = [name]
digests.extend(self.constructors_to_test[name])

with open(os_helper.TESTFN, "wb") as f:
with tempfile.TemporaryFile() as f:
f.write(data)

try:
for digest in digests:
buf = io.BytesIO(data)
buf.seek(0)
self.assertEqual(
hashlib.file_digest(buf, digest).hexdigest(), hexdigest
)
with open(os_helper.TESTFN, "rb") as f:
digestobj = hashlib.file_digest(f, digest)
f.seek(0)
digestobj = hashlib.file_digest(f, digest)
self.assertEqual(digestobj.hexdigest(), hexdigest)
finally:
os.unlink(os_helper.TESTFN)

def check_no_unicode(self, algorithm_name):
# Unicode objects are not allowed as input.
Expand Down Expand Up @@ -1172,29 +1169,29 @@ def test_normalized_name(self):
def test_file_digest(self):
data = b'a' * 65536
d1 = hashlib.sha256()
self.addCleanup(os.unlink, os_helper.TESTFN)
with open(os_helper.TESTFN, "wb") as f:
with tempfile.NamedTemporaryFile(delete_on_close=False) as fp:
for _ in range(10):
d1.update(data)
f.write(data)
fp.write(data)
fp.close()

with open(os_helper.TESTFN, "rb") as f:
d2 = hashlib.file_digest(f, hashlib.sha256)
with open(fp.name, "rb") as f:
d2 = hashlib.file_digest(f, hashlib.sha256)

self.assertEqual(d1.hexdigest(), d2.hexdigest())
self.assertEqual(d1.name, d2.name)
self.assertIs(type(d1), type(d2))
self.assertEqual(d1.hexdigest(), d2.hexdigest())
self.assertEqual(d1.name, d2.name)
self.assertIs(type(d1), type(d2))

with self.assertRaises(ValueError):
hashlib.file_digest(None, "sha256")
with self.assertRaises(ValueError):
with open(fp.name, "r") as f:
hashlib.file_digest(f, "sha256")

with self.assertRaises(ValueError):
with open(os_helper.TESTFN, "r") as f:
hashlib.file_digest(f, "sha256")
with self.assertRaises(ValueError):
with open(fp.name, "wb") as f:
hashlib.file_digest(f, "sha256")

with self.assertRaises(ValueError):
with open(os_helper.TESTFN, "wb") as f:
hashlib.file_digest(f, "sha256")
hashlib.file_digest(None, "sha256")


if __name__ == "__main__":
Expand Down
Loading