Skip to content

Commit d13077c

Browse files
committed
update
1 parent 6cb265e commit d13077c

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

Lib/test/support/hashlib_helper.py

+20
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,23 @@ def wrapper(*args, **kwargs):
100100
def decorator(func_or_class):
101101
return _decorate_func_or_class(func_or_class, decorator_func)
102102
return decorator
103+
104+
105+
def find_gil_minsize(modules_names, default=2048):
106+
"""Get the largest GIL_MINSIZE value for the given cryptographic modules.
107+
108+
The valid module names are the following:
109+
110+
- _hashlib
111+
- _md5, _sha1, _sha2, _sha3, _blake2
112+
- _hmac
113+
"""
114+
sizes = []
115+
for module_name in modules_names:
116+
try:
117+
module = importlib.import_module(module_name)
118+
except ImportError:
119+
continue
120+
sizes.append(getattr(module, '_GIL_MINSIZE', default))
121+
return max(sizes, default=default)
122+

Lib/test/test_hashlib.py

+4-17
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,6 @@ def read_vectors(hash_name):
9898
yield parts
9999

100100

101-
def find_gil_minsize(*modules_names, default=2048):
102-
sizes = []
103-
for module_name in modules_names:
104-
if SKIP_SHA3 and module_name == '_sha3':
105-
continue
106-
try:
107-
module = importlib.import_module(module_name)
108-
except ImportError:
109-
continue
110-
sizes.append(module._GIL_MINSIZE)
111-
return max(sizes, default=default)
112-
113-
114101
class HashLibTestCase(unittest.TestCase):
115102
supported_hash_names = ( 'md5', 'MD5', 'sha1', 'SHA1',
116103
'sha224', 'SHA224', 'sha256', 'SHA256',
@@ -928,10 +915,10 @@ def test_gil(self):
928915
# for multithreaded operation. Currently, all cryptographic modules
929916
# have the same constant value (2048) but in the future it might not
930917
# be the case.
931-
gil_minsize = find_gil_minsize(
932-
'_md5', '_sha1', '_sha2', '_sha3', '_blake2', '_hashlib',
933-
)
918+
mods = ['_md5', '_sha1', '_sha2', '_sha3', '_blake2', '_hashlib']
919+
gil_minsize = hashlib_helper.find_gil_minsize(mods)
934920
for cons in self.hash_constructors:
921+
# constructors belong to one of the above modules
935922
m = cons(usedforsecurity=False)
936923
m.update(b'1')
937924
m.update(b'#' * gil_minsize)
@@ -941,7 +928,7 @@ def test_gil(self):
941928
m.update(b'1')
942929

943930
def test_sha256_gil(self):
944-
gil_minsize = find_gil_minsize('_sha2', '_hashlib')
931+
gil_minsize = hashlib_helper.find_gil_minsize(['_sha2', '_hashlib'])
945932
m = hashlib.sha256()
946933
m.update(b'1')
947934
m.update(b'#' * gil_minsize)

0 commit comments

Comments
 (0)