Skip to content

Commit f2bbd72

Browse files
committed
simplify _block_builtin_hmac_constructor
1 parent 2a3f91a commit f2bbd72

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

Lib/test/support/hashlib_helper.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,23 +1065,16 @@ def dummy(data=b'', *, usedforsecurity=True, string=b''):
10651065

10661066
def _block_builtin_hmac_constructor(name):
10671067
"""Block explicit HACL* HMAC constructors."""
1068-
fullname = _EXPLICIT_HMAC_CONSTRUCTORS[name]
1069-
if fullname is None:
1068+
info = _HashInfoItem(_EXPLICIT_HMAC_CONSTRUCTORS[name])
1069+
assert info.module_name is None or info.module_name == "_hmac", info
1070+
if (wrapped := info.import_member()) is None:
10701071
# function shouldn't exist for this implementation
10711072
return contextlib.nullcontext()
1072-
assert fullname.count('.') == 1, fullname
1073-
module_name, method = fullname.split('.', maxsplit=1)
1074-
assert module_name == '_hmac', module_name
1075-
try:
1076-
module = importlib.import_module(module_name)
1077-
except ImportError:
1078-
# module is already disabled
1079-
return contextlib.nullcontext()
1080-
@functools.wraps(wrapped := getattr(module, method))
1073+
@functools.wraps(wrapped)
10811074
def wrapper(key, obj):
10821075
raise ValueError(f"blocked hash name: {name}")
10831076
_ensure_wrapper_signature(wrapper, wrapped)
1084-
return unittest.mock.patch(fullname, wrapper)
1077+
return unittest.mock.patch(info.fullname, wrapper)
10851078

10861079

10871080
@contextlib.contextmanager

0 commit comments

Comments
 (0)