Consistently raise a TypeError when a non str is passed to hashlib.new
authorGregory P. Smith <greg@krypto.org>
Sun, 22 Jul 2012 04:19:53 +0000 (21:19 -0700)
committerGregory P. Smith <greg@krypto.org>
Sun, 22 Jul 2012 04:19:53 +0000 (21:19 -0700)
regardless of which of the two implementations of new is used.

Lib/hashlib.py
Lib/test/test_hashlib.py

index 91080955ddd24c2a2e6d33534ad952da87fb9b25..21454c7d3066f5fecb00c7ff678f323ca00a4a49 100644 (file)
@@ -88,7 +88,7 @@ def __get_builtin_constructor(name):
     except ImportError:
         pass  # no extension module, this hash is unsupported.
 
-    raise ValueError('unsupported hash type %s' % name)
+    raise ValueError('unsupported hash type ' + name)
 
 
 def __get_openssl_constructor(name):
index 97981dd20d55f7e3fa48049505f18f16cb88ad35..29d3a1cc44fa58e57b9e726a736beffc6627c9b8 100644 (file)
@@ -111,12 +111,8 @@ class HashLibTestCase(unittest.TestCase):
                             issubset(hashlib.algorithms_available))
 
     def test_unknown_hash(self):
-        try:
-            hashlib.new('spam spam spam spam spam')
-        except ValueError:
-            pass
-        else:
-            self.assertTrue(0 == "hashlib didn't reject bogus hash name")
+        self.assertRaises(ValueError, hashlib.new, 'spam spam spam spam spam')
+        self.assertRaises(TypeError, hashlib.new, 1)
 
     def test_get_builtin_constructor(self):
         get_builtin_constructor = hashlib.__dict__[
@@ -135,6 +131,7 @@ class HashLibTestCase(unittest.TestCase):
                 sys.modules['_md5'] = _md5
             else:
                 del sys.modules['_md5']
+        self.assertRaises(TypeError, get_builtin_constructor, 3)
 
     def test_hexdigest(self):
         for name in self.supported_hash_names: