mirror of https://github.com/python/cpython
Fixes Issue #12059: Properly handle missing hash functions even when
the expected builtin modules are not present. This includes a unittest for __get_builtin_constructor() in the face of such an error.
This commit is contained in:
parent
3196efe1a5
commit
79aa5bf5ed
|
@ -64,6 +64,7 @@ __all__ = __always_supported + ('new', 'algorithms_guaranteed',
|
||||||
|
|
||||||
|
|
||||||
def __get_builtin_constructor(name):
|
def __get_builtin_constructor(name):
|
||||||
|
try:
|
||||||
if name in ('SHA1', 'sha1'):
|
if name in ('SHA1', 'sha1'):
|
||||||
import _sha1
|
import _sha1
|
||||||
return _sha1.sha1
|
return _sha1.sha1
|
||||||
|
@ -84,6 +85,8 @@ def __get_builtin_constructor(name):
|
||||||
return _sha512.sha512
|
return _sha512.sha512
|
||||||
elif bs == '384':
|
elif bs == '384':
|
||||||
return _sha512.sha384
|
return _sha512.sha384
|
||||||
|
except ImportError:
|
||||||
|
pass # no extension module, this hash is unsupported.
|
||||||
|
|
||||||
raise ValueError('unsupported hash type %s' % name)
|
raise ValueError('unsupported hash type %s' % name)
|
||||||
|
|
||||||
|
|
|
@ -118,6 +118,24 @@ class HashLibTestCase(unittest.TestCase):
|
||||||
else:
|
else:
|
||||||
self.assertTrue(0 == "hashlib didn't reject bogus hash name")
|
self.assertTrue(0 == "hashlib didn't reject bogus hash name")
|
||||||
|
|
||||||
|
def test_get_builtin_constructor(self):
|
||||||
|
get_builtin_constructor = hashlib.__dict__[
|
||||||
|
'__get_builtin_constructor']
|
||||||
|
self.assertRaises(ValueError, get_builtin_constructor, 'test')
|
||||||
|
try:
|
||||||
|
import _md5
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
|
# This forces an ImportError for "import _md5" statements
|
||||||
|
sys.modules['_md5'] = None
|
||||||
|
try:
|
||||||
|
self.assertRaises(ValueError, get_builtin_constructor, 'md5')
|
||||||
|
finally:
|
||||||
|
if '_md5' in locals():
|
||||||
|
sys.modules['_md5'] = _md5
|
||||||
|
else:
|
||||||
|
del sys.modules['_md5']
|
||||||
|
|
||||||
def test_hexdigest(self):
|
def test_hexdigest(self):
|
||||||
for name in self.supported_hash_names:
|
for name in self.supported_hash_names:
|
||||||
h = hashlib.new(name)
|
h = hashlib.new(name)
|
||||||
|
|
Loading…
Reference in New Issue