mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
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
2 changed files with 41 additions and 20 deletions
|
@ -64,26 +64,29 @@ __all__ = __always_supported + ('new', 'algorithms_guaranteed',
|
|||
|
||||
|
||||
def __get_builtin_constructor(name):
|
||||
if name in ('SHA1', 'sha1'):
|
||||
import _sha1
|
||||
return _sha1.sha1
|
||||
elif name in ('MD5', 'md5'):
|
||||
import _md5
|
||||
return _md5.md5
|
||||
elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'):
|
||||
import _sha256
|
||||
bs = name[3:]
|
||||
if bs == '256':
|
||||
return _sha256.sha256
|
||||
elif bs == '224':
|
||||
return _sha256.sha224
|
||||
elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'):
|
||||
import _sha512
|
||||
bs = name[3:]
|
||||
if bs == '512':
|
||||
return _sha512.sha512
|
||||
elif bs == '384':
|
||||
return _sha512.sha384
|
||||
try:
|
||||
if name in ('SHA1', 'sha1'):
|
||||
import _sha1
|
||||
return _sha1.sha1
|
||||
elif name in ('MD5', 'md5'):
|
||||
import _md5
|
||||
return _md5.md5
|
||||
elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'):
|
||||
import _sha256
|
||||
bs = name[3:]
|
||||
if bs == '256':
|
||||
return _sha256.sha256
|
||||
elif bs == '224':
|
||||
return _sha256.sha224
|
||||
elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'):
|
||||
import _sha512
|
||||
bs = name[3:]
|
||||
if bs == '512':
|
||||
return _sha512.sha512
|
||||
elif bs == '384':
|
||||
return _sha512.sha384
|
||||
except ImportError:
|
||||
pass # no extension module, this hash is unsupported.
|
||||
|
||||
raise ValueError('unsupported hash type %s' % name)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue