[Python-checkins] cpython: Fixes Issue #12059: Properly handle missing hash functions even when

gregory.p.smith python-checkins at python.org
Sun May 15 00:36:23 CEST 2011


http://hg.python.org/cpython/rev/298888fd46b0
changeset:   70124:298888fd46b0
parent:      70117:7fb34d30bd59
user:        Gregory P. Smith <greg at krypto.org>
date:        Sat May 14 15:15:49 2011 -0700
summary:
  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.

files:
  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
--- a/Lib/hashlib.py
+++ b/Lib/hashlib.py
@@ -64,26 +64,29 @@
 
 
 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
--- a/Lib/test/test_hashlib.py
+++ b/Lib/test/test_hashlib.py
@@ -118,6 +118,24 @@
         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)

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list