From c37f14e422c93bd57fb423a1713fdf101ce077b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sat, 26 Apr 2025 13:58:02 +0200 Subject: [PATCH] allow UBSan to check SHA-3 module --- Lib/test/test_hashlib.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py index 53afb2a8c631d7..6ae5c9303121bf 100644 --- a/Lib/test/test_hashlib.py +++ b/Lib/test/test_hashlib.py @@ -62,10 +62,12 @@ def get_fips_mode(): requires_blake2 = unittest.skipUnless(_blake2, 'requires _blake2') -# bpo-46913: Don't test the _sha3 extension on a Python UBSAN build -# TODO(gh-99108): Revisit this after _sha3 uses HACL*. -SKIP_SHA3 = support.check_sanitizer(ub=True) -requires_sha3 = unittest.skipUnless(not SKIP_SHA3, 'requires _sha3') +try: + import _sha3 +except ImportError: + _sha3 = None + +requires_sha3 = unittest.skipUnless(_sha3, 'requires _sha3') def hexstr(s): @@ -132,8 +134,6 @@ def __init__(self, *args, **kwargs): self.constructors_to_test = {} for algorithm in algorithms: - if SKIP_SHA3 and algorithm.startswith('sha3_'): - continue self.constructors_to_test[algorithm] = set() # For each algorithm, test the direct constructor and the use @@ -180,20 +180,18 @@ def add_builtin_constructor(name): add_builtin_constructor('sha256') add_builtin_constructor('sha384') add_builtin_constructor('sha512') + _sha3 = self._conditional_import_module('_sha3') + if _sha3: + add_builtin_constructor('sha3_224') + add_builtin_constructor('sha3_256') + add_builtin_constructor('sha3_384') + add_builtin_constructor('sha3_512') + add_builtin_constructor('shake_128') + add_builtin_constructor('shake_256') if _blake2: add_builtin_constructor('blake2s') add_builtin_constructor('blake2b') - if not SKIP_SHA3: - _sha3 = self._conditional_import_module('_sha3') - if _sha3: - add_builtin_constructor('sha3_224') - add_builtin_constructor('sha3_256') - add_builtin_constructor('sha3_384') - add_builtin_constructor('sha3_512') - add_builtin_constructor('shake_128') - add_builtin_constructor('shake_256') - super(HashLibTestCase, self).__init__(*args, **kwargs) @property