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

Method __init__

Lib/test/test_hashlib.py:131–202  ·  view source on GitHub ↗
(self, *args, **kwargs)

Source from the content-addressed store, hash-verified

129 return None
130
131 def __init__(self, *args, **kwargs):
132 algorithms = set()
133 for algorithm in self.supported_hash_names:
134 algorithms.add(algorithm.lower())
135
136 _blake2 = self._conditional_import_module('_blake2')
137 blake2_hashes = {'blake2b', 'blake2s'}
138 if _blake2:
139 algorithms.update(blake2_hashes)
140 else:
141 algorithms.difference_update(blake2_hashes)
142
143 self.constructors_to_test = {}
144 for algorithm in algorithms:
145 self.constructors_to_test[algorithm] = set()
146
147 # For each algorithm, test the direct constructor and the use
148 # of hashlib.new given the algorithm name.
149 for algorithm, constructors in self.constructors_to_test.items():
150 constructors.add(getattr(hashlib, algorithm))
151 def c(*args, __algorithm_name=algorithm, **kwargs):
152 return hashlib.new(__algorithm_name, *args, **kwargs)
153 c.__name__ = f'do_test_algorithm_via_hashlib_new_{algorithm}'
154 constructors.add(c)
155
156 _hashlib = self._conditional_import_module('_hashlib')
157 self._hashlib = _hashlib
158 if _hashlib:
159 # These algorithms should always be present when this module
160 # is compiled. If not, something was compiled wrong.
161 self.assertHasAttr(_hashlib, 'openssl_md5')
162 self.assertHasAttr(_hashlib, 'openssl_sha1')
163 for algorithm, constructors in self.constructors_to_test.items():
164 constructor = getattr(_hashlib, 'openssl_'+algorithm, None)
165 if constructor:
166 try:
167 constructor()
168 except ValueError:
169 # default constructor blocked by crypto policy
170 pass
171 else:
172 constructors.add(constructor)
173
174 def add_builtin_constructor(name):
175 constructor = getattr(hashlib, "__get_builtin_constructor")(name)
176 self.constructors_to_test[name].add(constructor)
177
178 _md5 = self._conditional_import_module('_md5')
179 if _md5:
180 add_builtin_constructor('md5')
181 _sha1 = self._conditional_import_module('_sha1')
182 if _sha1:
183 add_builtin_constructor('sha1')
184 _sha2 = self._conditional_import_module('_sha2')
185 if _sha2:
186 add_builtin_constructor('sha224')
187 add_builtin_constructor('sha256')
188 add_builtin_constructor('sha384')

Callers

nothing calls this directly

Calls 10

setFunction · 0.85
constructorFunction · 0.85
superClass · 0.85
difference_updateMethod · 0.80
assertHasAttrMethod · 0.80
addMethod · 0.45
lowerMethod · 0.45
updateMethod · 0.45
itemsMethod · 0.45

Tested by

no test coverage detected