gh-132993: expose HASHLIB_GIL_MINSIZE to private extension modules (#132999)

This commit is contained in:
Bénédikt Tran 2025-04-28 00:20:15 +02:00 committed by GitHub
parent 019ee49d50
commit 3695ba93d5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 119 additions and 28 deletions

View file

@ -1100,6 +1100,11 @@ class UpdateTestCaseMixin:
"""Create a HMAC object."""
raise NotImplementedError
@property
def gil_minsize(self):
"""Get the maximal input length for the GIL to be held."""
raise NotImplementedError
def check_update(self, key, chunks):
chunks = list(chunks)
msg = b''.join(chunks)
@ -1118,11 +1123,10 @@ class UpdateTestCaseMixin:
self.check_update(key, [msg])
def test_update_large(self):
HASHLIB_GIL_MINSIZE = 2048
gil_minsize = self.gil_minsize
key = random.randbytes(16)
top = random.randbytes(HASHLIB_GIL_MINSIZE + 1)
bot = random.randbytes(HASHLIB_GIL_MINSIZE + 1)
top = random.randbytes(gil_minsize + 1)
bot = random.randbytes(gil_minsize + 1)
self.check_update(key, [top, bot])
def test_update_exceptions(self):
@ -1132,12 +1136,16 @@ class UpdateTestCaseMixin:
self.assertRaises(TypeError, h.update, msg)
@hashlib_helper.requires_hashdigest('sha256')
@requires_builtin_sha2()
class PyUpdateTestCase(PyModuleMixin, UpdateTestCaseMixin, unittest.TestCase):
def HMAC(self, key, msg=None):
return self.hmac.HMAC(key, msg, digestmod='sha256')
@property
def gil_minsize(self):
return sha2._GIL_MINSIZE
@hashlib_helper.requires_openssl_hashdigest('sha256')
class OpenSSLUpdateTestCase(UpdateTestCaseMixin, unittest.TestCase):
@ -1145,6 +1153,10 @@ class OpenSSLUpdateTestCase(UpdateTestCaseMixin, unittest.TestCase):
def HMAC(self, key, msg=None):
return _hashlib.hmac_new(key, msg, digestmod='sha256')
@property
def gil_minsize(self):
return _hashlib._GIL_MINSIZE
class BuiltinUpdateTestCase(BuiltinModuleMixin,
UpdateTestCaseMixin, unittest.TestCase):
@ -1154,6 +1166,10 @@ class BuiltinUpdateTestCase(BuiltinModuleMixin,
# are still built, making it possible to use SHA-2 hashes.
return self.hmac.new(key, msg, digestmod='sha256')
@property
def gil_minsize(self):
return self.hmac._GIL_MINSIZE
class CopyBaseTestCase: