From 12c9d028ed0a1803019f3796014e75c74d9de2f6 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Sat, 14 May 2011 15:15:49 -0700 Subject: [PATCH 1/3] 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. --- Lib/hashlib.py | 43 +++++++++++++++++++++------------------- Lib/test/test_hashlib.py | 18 +++++++++++++++++ 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/Lib/hashlib.py b/Lib/hashlib.py index 0d7e3250d5b..0ebeed5a46e 100644 --- a/Lib/hashlib.py +++ b/Lib/hashlib.py @@ -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) diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py index fe316fdd39a..17d752b893e 100644 --- a/Lib/test/test_hashlib.py +++ b/Lib/test/test_hashlib.py @@ -118,6 +118,24 @@ class HashLibTestCase(unittest.TestCase): else: 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): for name in self.supported_hash_names: h = hashlib.new(name) From a3221f82099d1b000387bf2d75e906ccd6801262 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Sat, 14 May 2011 15:35:19 -0700 Subject: [PATCH 2/3] fix whitespace --- Lib/hashlib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/hashlib.py b/Lib/hashlib.py index 0ebeed5a46e..91080955ddd 100644 --- a/Lib/hashlib.py +++ b/Lib/hashlib.py @@ -86,7 +86,7 @@ def __get_builtin_constructor(name): elif bs == '384': return _sha512.sha384 except ImportError: - pass # no extension module, this hash is unsupported. + pass # no extension module, this hash is unsupported. raise ValueError('unsupported hash type %s' % name) From 1161a9ca4059ab34f637574e7498ba79ecd6efea Mon Sep 17 00:00:00 2001 From: Nadeem Vawda Date: Sun, 15 May 2011 00:48:24 +0200 Subject: [PATCH 3/3] Fix copy-paste mistake in new zlib test. --- Lib/test/test_zlib.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_zlib.py b/Lib/test/test_zlib.py index 0909aa17b21..599ce24f41d 100644 --- a/Lib/test/test_zlib.py +++ b/Lib/test/test_zlib.py @@ -528,9 +528,11 @@ class CompressObjectTestCase(BaseCompressTestCase, unittest.TestCase): if size < _4G + 100: self.skipTest("not enough free memory, need at least 4 GB") data = b'x' * size + c = zlib.compressobj(1) + d = zlib.decompressobj() try: - self.assertRaises(OverflowError, zlib.compress, data, 1) - self.assertRaises(OverflowError, zlib.decompress, data) + self.assertRaises(OverflowError, c.compress, data) + self.assertRaises(OverflowError, d.decompress, data) finally: data = None