Skip to content

Commit 3c1de38

Browse files
committed
reuse blake2 conditionals in missing places and skip subtest for test_algorithms_available if implementation is not provided
1 parent 0bbdb4e commit 3c1de38

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

Lib/test/test_hashlib.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,11 @@ def __init__(self, *args, **kwargs):
134134
algorithms.add(algorithm.lower())
135135

136136
_blake2 = self._conditional_import_module('_blake2')
137+
blake2_hashes = {'blake2b', 'blake2s'}
137138
if _blake2:
138-
algorithms.update({'blake2b', 'blake2s'})
139+
algorithms.update(blake2_hashes)
140+
else:
141+
algorithms.difference_update(blake2_hashes)
139142

140143
self.constructors_to_test = {}
141144
for algorithm in algorithms:
@@ -232,7 +235,14 @@ def test_algorithms_available(self):
232235
# all available algorithms must be loadable, bpo-47101
233236
self.assertNotIn("undefined", hashlib.algorithms_available)
234237
for name in hashlib.algorithms_available:
235-
digest = hashlib.new(name, usedforsecurity=False)
238+
with self.subTest(name):
239+
try:
240+
digest = hashlib.new(name, usedforsecurity=False)
241+
assert digest is not None
242+
except ValueError as verr:
243+
# builtins may be absent if python built with
244+
# a subset of --with-builtin-hashlib-hashes or none.
245+
self.skipTest(verr)
236246

237247
def test_usedforsecurity_true(self):
238248
hashlib.new("sha256", usedforsecurity=True)
@@ -504,6 +514,7 @@ def test_sha3_256_update_over_4gb(self):
504514
self.assertEqual(h.hexdigest(), "e2d4535e3b613135c14f2fe4e026d7ad8d569db44901740beffa30d430acb038")
505515

506516
@requires_resource('cpu')
517+
@requires_blake2
507518
def test_blake2_update_over_4gb(self):
508519
# blake2s or blake2b doesn't matter based on how our C code is structured, this tests the
509520
# common loop macro logic.
@@ -1052,7 +1063,9 @@ def test_gil(self):
10521063
# for multithreaded operation. Currently, all cryptographic modules
10531064
# have the same constant value (2048) but in the future it might not
10541065
# be the case.
1055-
mods = ['_md5', '_sha1', '_sha2', '_sha3', '_blake2', '_hashlib']
1066+
mods = ['_md5', '_sha1', '_sha2', '_sha3', '_hashlib']
1067+
if _blake2:
1068+
mods.append('_blake2')
10561069
gil_minsize = hashlib_helper.find_gil_minsize(mods)
10571070
for cons in self.hash_constructors:
10581071
# constructors belong to one of the above modules
@@ -1080,7 +1093,10 @@ def test_sha256_gil(self):
10801093
def test_threaded_hashing_fast(self):
10811094
# Same as test_threaded_hashing_slow() but only tests some functions
10821095
# since otherwise test_hashlib.py becomes too slow during development.
1083-
for name in ['md5', 'sha1', 'sha256', 'sha3_256', 'blake2s']:
1096+
algos = ['md5', 'sha1', 'sha256', 'sha3_256']
1097+
if _blake2:
1098+
algos.append('blake2s')
1099+
for name in algos:
10841100
if constructor := getattr(hashlib, name, None):
10851101
with self.subTest(name):
10861102
self.do_test_threaded_hashing(constructor, is_shake=False)

0 commit comments

Comments
 (0)