Skip to content

Commit c1f352b

Browse files
authored
pythongh-128657: Run test_hashlib with --parallel-threads (pythonGH-129833)
* pythongh-128657: Run test_hashlib with `--parallel-threads` This catches the race in `py_digest_by_name` that is fixed separately in pythongh-128886. * Adjust assertion order
1 parent 5ce70ad commit c1f352b

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

Lib/test/libregrtest/tsan.py

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
# the regression test runner with the `--parallel-threads` option enabled.
3434
TSAN_PARALLEL_TESTS = [
3535
'test_abc',
36+
'test_hashlib',
3637
]
3738

3839

Lib/test/test_hashlib.py

+19-22
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
import os
1414
import sys
1515
import sysconfig
16+
import tempfile
1617
import threading
1718
import unittest
1819
import warnings
1920
from test import support
2021
from test.support import _4G, bigmemtest
2122
from test.support.import_helper import import_fresh_module
22-
from test.support import os_helper
2323
from test.support import requires_resource
2424
from test.support import threading_helper
2525
from http.client import HTTPException
@@ -414,21 +414,18 @@ def check_file_digest(self, name, data, hexdigest):
414414
digests = [name]
415415
digests.extend(self.constructors_to_test[name])
416416

417-
with open(os_helper.TESTFN, "wb") as f:
417+
with tempfile.TemporaryFile() as f:
418418
f.write(data)
419419

420-
try:
421420
for digest in digests:
422421
buf = io.BytesIO(data)
423422
buf.seek(0)
424423
self.assertEqual(
425424
hashlib.file_digest(buf, digest).hexdigest(), hexdigest
426425
)
427-
with open(os_helper.TESTFN, "rb") as f:
428-
digestobj = hashlib.file_digest(f, digest)
426+
f.seek(0)
427+
digestobj = hashlib.file_digest(f, digest)
429428
self.assertEqual(digestobj.hexdigest(), hexdigest)
430-
finally:
431-
os.unlink(os_helper.TESTFN)
432429

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

1181-
with open(os_helper.TESTFN, "rb") as f:
1182-
d2 = hashlib.file_digest(f, hashlib.sha256)
1178+
with open(fp.name, "rb") as f:
1179+
d2 = hashlib.file_digest(f, hashlib.sha256)
11831180

1184-
self.assertEqual(d1.hexdigest(), d2.hexdigest())
1185-
self.assertEqual(d1.name, d2.name)
1186-
self.assertIs(type(d1), type(d2))
1181+
self.assertEqual(d1.hexdigest(), d2.hexdigest())
1182+
self.assertEqual(d1.name, d2.name)
1183+
self.assertIs(type(d1), type(d2))
11871184

1188-
with self.assertRaises(ValueError):
1189-
hashlib.file_digest(None, "sha256")
1185+
with self.assertRaises(ValueError):
1186+
with open(fp.name, "r") as f:
1187+
hashlib.file_digest(f, "sha256")
11901188

1191-
with self.assertRaises(ValueError):
1192-
with open(os_helper.TESTFN, "r") as f:
1193-
hashlib.file_digest(f, "sha256")
1189+
with self.assertRaises(ValueError):
1190+
with open(fp.name, "wb") as f:
1191+
hashlib.file_digest(f, "sha256")
11941192

11951193
with self.assertRaises(ValueError):
1196-
with open(os_helper.TESTFN, "wb") as f:
1197-
hashlib.file_digest(f, "sha256")
1194+
hashlib.file_digest(None, "sha256")
11981195

11991196

12001197
if __name__ == "__main__":

0 commit comments

Comments
 (0)