MCPcopy Index your code
hub / github.com/python/cpython / _block_builtin_hash_new

Function _block_builtin_hash_new

Lib/test/support/hashlib_helper.py:903–933  ·  view source on GitHub ↗

Block a buitin-in hash name from the hashlib.new() interface.

(name)

Source from the content-addressed store, hash-verified

901
902
903def _block_builtin_hash_new(name):
904 """Block a buitin-in hash name from the hashlib.new() interface."""
905 assert isinstance(name, str), name
906 assert name.lower() == name, f"invalid name: {name}"
907 assert name in _HashId, f"invalid hash: {name}"
908
909 # Re-import 'hashlib' in case it was mocked
910 hashlib = importlib.import_module('hashlib')
911 builtin_constructor_cache = getattr(hashlib, '__builtin_constructor_cache')
912 builtin_constructor_cache_mock = builtin_constructor_cache.copy()
913 builtin_constructor_cache_mock.pop(name, None)
914 builtin_constructor_cache_mock.pop(name.upper(), None)
915
916 # __get_builtin_constructor() imports the HACL* modules on demand,
917 # so we need to block the possibility of importing it, but only
918 # during the call to __get_builtin_constructor().
919 get_builtin_constructor = getattr(hashlib, '__get_builtin_constructor')
920 builtin_module_name = get_hash_func_info(name).builtin.module_name
921
922 @functools.wraps(get_builtin_constructor)
923 def get_builtin_constructor_mock(name):
924 with import_helper.isolated_modules():
925 sys = importlib.import_module("sys")
926 sys.modules[builtin_module_name] = None # block module's import
927 return get_builtin_constructor(name)
928
929 return unittest.mock.patch.multiple(
930 hashlib,
931 __get_builtin_constructor=get_builtin_constructor_mock,
932 __builtin_constructor_cache=builtin_constructor_cache_mock,
933 )
934
935
936def _block_builtin_hmac_new(blocked_name):

Callers 1

block_algorithmFunction · 0.85

Calls 6

get_hash_func_infoFunction · 0.85
lowerMethod · 0.45
import_moduleMethod · 0.45
copyMethod · 0.45
popMethod · 0.45
upperMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…