Skip to content

Commit f791c73

Browse files
committed
use exposed GIL_MINSIZE constant in tests
1 parent 8f73b52 commit f791c73

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

Lib/test/test_hashlib.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import warnings
2121
from test import support
2222
from test.support import _4G, bigmemtest
23-
from test.support.import_helper import import_fresh_module
23+
from test.support.import_helper import import_fresh_module, import_module
2424
from test.support import requires_resource
2525
from test.support import threading_helper
2626
from http.client import HTTPException
@@ -95,6 +95,19 @@ def read_vectors(hash_name):
9595
yield parts
9696

9797

98+
def find_gil_minsize(*modules_names, default=2048):
99+
gil_minsize = default
100+
for module_name in modules_names:
101+
if SKIP_SHA3 and module_name == '_sha3':
102+
continue
103+
try:
104+
module = importlib.import_module(module_name)
105+
except ImportError:
106+
continue
107+
gil_minsize = max(gil_minsize, module.GIL_MINSIZE)
108+
return gil_minsize
109+
110+
98111
class HashLibTestCase(unittest.TestCase):
99112
supported_hash_names = ( 'md5', 'MD5', 'sha1', 'SHA1',
100113
'sha224', 'SHA224', 'sha256', 'SHA256',
@@ -913,9 +926,12 @@ def test_case_shake256_vector(self):
913926

914927
def test_gil(self):
915928
# Check things work fine with an input larger than the size required
916-
# for multithreaded operation (which is hardwired to 2048).
917-
gil_minsize = 2048
918-
929+
# for multithreaded operation. Currently, all cryptographic modules
930+
# have the same constant value (2048) but in the future it might not
931+
# be the case.
932+
gil_minsize = find_gil_minsize(
933+
'_md5', '_sha1', '_sha2', '_sha3', '_blake2', '_hashlib',
934+
)
919935
for cons in self.hash_constructors:
920936
m = cons(usedforsecurity=False)
921937
m.update(b'1')
@@ -925,6 +941,8 @@ def test_gil(self):
925941
m = cons(b'x' * gil_minsize, usedforsecurity=False)
926942
m.update(b'1')
927943

944+
def test_sha256_gil(self):
945+
gil_minsize = find_gil_minsize('_sha2', '_hashlib')
928946
m = hashlib.sha256()
929947
m.update(b'1')
930948
m.update(b'#' * gil_minsize)

Lib/test/test_hmac.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,11 @@ def HMAC(self, key, msg=None):
11021102
"""Create a HMAC object."""
11031103
raise NotImplementedError
11041104

1105+
@property
1106+
def gil_minsize(self):
1107+
"""Get the maximal input length for the GIL to be held."""
1108+
raise NotImplementedError
1109+
11051110
def check_update(self, key, chunks):
11061111
chunks = list(chunks)
11071112
msg = b''.join(chunks)
@@ -1120,11 +1125,10 @@ def test_update(self):
11201125
self.check_update(key, [msg])
11211126

11221127
def test_update_large(self):
1123-
HASHLIB_GIL_MINSIZE = 2048
1124-
1128+
gil_minsize = self.gil_minsize
11251129
key = random.randbytes(16)
1126-
top = random.randbytes(HASHLIB_GIL_MINSIZE + 1)
1127-
bot = random.randbytes(HASHLIB_GIL_MINSIZE + 1)
1130+
top = random.randbytes(gil_minsize + 1)
1131+
bot = random.randbytes(gil_minsize + 1)
11281132
self.check_update(key, [top, bot])
11291133

11301134
def test_update_exceptions(self):
@@ -1140,13 +1144,21 @@ class PyUpdateTestCase(PyModuleMixin, UpdateTestCaseMixin, unittest.TestCase):
11401144
def HMAC(self, key, msg=None):
11411145
return self.hmac.HMAC(key, msg, digestmod='sha256')
11421146

1147+
@property
1148+
def gil_minsize(self):
1149+
self.skipTest("GIL is always held")
1150+
11431151

11441152
@hashlib_helper.requires_openssl_hashdigest('sha256')
11451153
class OpenSSLUpdateTestCase(UpdateTestCaseMixin, unittest.TestCase):
11461154

11471155
def HMAC(self, key, msg=None):
11481156
return _hashlib.hmac_new(key, msg, digestmod='sha256')
11491157

1158+
@property
1159+
def gil_minsize(self):
1160+
return _hashlib.GIL_MINSIZE
1161+
11501162

11511163
class BuiltinUpdateTestCase(BuiltinModuleMixin,
11521164
UpdateTestCaseMixin, unittest.TestCase):
@@ -1156,6 +1168,10 @@ def HMAC(self, key, msg=None):
11561168
# are still built, making it possible to use SHA-2 hashes.
11571169
return self.hmac.new(key, msg, digestmod='sha256')
11581170

1171+
@property
1172+
def gil_minsize(self):
1173+
return self.hmac.GIL_MINSIZE
1174+
11591175

11601176
class CopyBaseTestCase:
11611177

0 commit comments

Comments
 (0)