Skip to content

Commit 17eb629

Browse files
committed
add skip_if_blake2_not_builtin helper and use it also from test_threaded_hashing_fast
1 parent 1fdc8c7 commit 17eb629

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

Lib/test/test_hashlib.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -234,20 +234,14 @@ def test_algorithms_available(self):
234234
issubset(hashlib.algorithms_available))
235235
# all available algorithms must be loadable, bpo-47101
236236
self.assertNotIn("undefined", hashlib.algorithms_available)
237-
algorithms_builtin = sysconfig.get_config_var("PY_BUILTIN_HASHLIB_HASHES").split(",")
238237
for name in hashlib.algorithms_available:
239238
with self.subTest(name):
240239
try:
241240
digest = hashlib.new(name, usedforsecurity=False)
242241
assert digest is not None
243242
except ValueError as verr:
244-
# builtins may be absent if python built with
245-
# a subset of --with-builtin-hashlib-hashes or none.
246-
if ("blake2" in name and
247-
"blake2" not in algorithms_builtin):
248-
self.skipTest(verr)
249-
else:
250-
raise
243+
self.skip_if_blake2_not_builtin(name, verr)
244+
raise
251245

252246
def test_usedforsecurity_true(self):
253247
hashlib.new("sha256", usedforsecurity=True)
@@ -814,6 +808,12 @@ def test_case_sha512_3(self):
814808
"e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"+
815809
"de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b")
816810

811+
def skip_if_blake2_not_builtin(self, name, skip_reason):
812+
# blake2 builtins may be absent if python built with
813+
# a subset of --with-builtin-hashlib-hashes or none.
814+
if "blake2" in name and "blake2" not in builtin_hashes:
815+
self.skipTest(skip_reason)
816+
817817
def check_blake2(self, constructor, salt_size, person_size, key_size,
818818
digest_size, max_offset):
819819
self.assertEqual(constructor.SALT_SIZE, salt_size)
@@ -1096,13 +1096,16 @@ def test_sha256_gil(self):
10961096
def test_threaded_hashing_fast(self):
10971097
# Same as test_threaded_hashing_slow() but only tests some functions
10981098
# since otherwise test_hashlib.py becomes too slow during development.
1099-
algos = ['md5', 'sha1', 'sha256', 'sha3_256']
1100-
if _blake2:
1101-
algos.append('blake2s')
1099+
algos = ['md5', 'sha1', 'sha256', 'sha3_256', 'blake2s']
11021100
for name in algos:
11031101
if constructor := getattr(hashlib, name, None):
11041102
with self.subTest(name):
1105-
self.do_test_threaded_hashing(constructor, is_shake=False)
1103+
try:
1104+
self.do_test_threaded_hashing(constructor, is_shake=False)
1105+
except ValueError as verr:
1106+
self.skip_if_blake2_not_builtin(name, verr)
1107+
raise
1108+
11061109
if shake_128 := getattr(hashlib, 'shake_128', None):
11071110
self.do_test_threaded_hashing(shake_128, is_shake=True)
11081111

0 commit comments

Comments
 (0)