Skip to content

Commit 4936441

Browse files
committed
update
1 parent e176b93 commit 4936441

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

Lib/test/support/hashlib_helper.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,22 @@ 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)

Lib/test/test_hashlib.py

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,6 @@ def read_vectors(hash_name):
9696
yield parts
9797

9898

99-
def find_gil_minsize(*modules_names, default=2048):
100-
sizes = []
101-
for module_name in modules_names:
102-
if SKIP_SHA3 and module_name == '_sha3':
103-
continue
104-
try:
105-
module = importlib.import_module(module_name)
106-
except ImportError:
107-
continue
108-
sizes.append(module._GIL_MINSIZE)
109-
return max(sizes, default=default)
110-
111-
11299
class HashLibTestCase(unittest.TestCase):
113100
supported_hash_names = ( 'md5', 'MD5', 'sha1', 'SHA1',
114101
'sha224', 'SHA224', 'sha256', 'SHA256',
@@ -930,10 +917,10 @@ def test_gil(self):
930917
# for multithreaded operation. Currently, all cryptographic modules
931918
# have the same constant value (2048) but in the future it might not
932919
# be the case.
933-
gil_minsize = find_gil_minsize(
934-
'_md5', '_sha1', '_sha2', '_sha3', '_blake2', '_hashlib',
935-
)
920+
mods = ['_md5', '_sha1', '_sha2', '_sha3', '_blake2', '_hashlib']
921+
gil_minsize = hashlib_helper.find_gil_minsize(mods)
936922
for cons in self.hash_constructors:
923+
# constructors belong to one of the above modules
937924
m = cons(usedforsecurity=False)
938925
m.update(b'1')
939926
m.update(b'#' * gil_minsize)
@@ -943,7 +930,7 @@ def test_gil(self):
943930
m.update(b'1')
944931

945932
def test_sha256_gil(self):
946-
gil_minsize = find_gil_minsize('_sha2', '_hashlib')
933+
gil_minsize = hashlib_helper.find_gil_minsize(['_sha2', '_hashlib'])
947934
m = hashlib.sha256()
948935
m.update(b'1')
949936
m.update(b'#' * gil_minsize)

0 commit comments

Comments
 (0)