Skip to content

Commit ca0ab3b

Browse files
committed
update
1 parent e176b93 commit ca0ab3b

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

Lib/test/support/hashlib_helper.py

+20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import functools
22
import hashlib
3+
import importlib
34
import unittest
45
from test.support.import_helper import import_module
56

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

Lib/test/test_hashlib.py

+5-18
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from test import support
2222
from test.support import _4G, bigmemtest
2323
from test.support import hashlib_helper
24-
from test.support.import_helper import import_fresh_module, import_module
24+
from test.support.import_helper import import_fresh_module
2525
from test.support import requires_resource
2626
from test.support import threading_helper
2727
from http.client import HTTPException
@@ -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)