]> granicus.if.org Git - python/commitdiff
Issue #15225: improve error message when hmac is passed a wrong key type.
authorAntoine Pitrou <solipsis@pitrou.net>
Sat, 30 Jun 2012 15:27:56 +0000 (17:27 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Sat, 30 Jun 2012 15:27:56 +0000 (17:27 +0200)
Patch by Marc Abramowitz.

Lib/hmac.py
Lib/test/test_hmac.py
Misc/ACKS

index 9c080233da8aebb8624b0129796f321223e9ada5..4297a7171abe6ece187d95cbb2deaee26fab9ee5 100644 (file)
@@ -35,7 +35,7 @@ class HMAC:
         """
 
         if not isinstance(key, bytes):
-            raise TypeError("expected bytes, but got %r" % type(key).__name__)
+            raise TypeError("key: expected bytes, but got %r" % type(key).__name__)
 
         if digestmod is None:
             import hashlib
index 11cacb4d03efbcdcb52a284f637707bb28082d4b..4ca7cec44ce840214bb474dc083407619f20e0e0 100644 (file)
@@ -234,6 +234,18 @@ class ConstructorTestCase(unittest.TestCase):
         except:
             self.fail("Standard constructor call raised exception.")
 
+    def test_with_str_key(self):
+        # Pass a key of type str, which is an error, because it expects a key
+        # of type bytes
+        with self.assertRaises(TypeError):
+            h = hmac.HMAC("key")
+
+    def test_dot_new_with_str_key(self):
+        # Pass a key of type str, which is an error, because it expects a key
+        # of type bytes
+        with self.assertRaises(TypeError):
+            h = hmac.new("key")
+
     def test_withtext(self):
         # Constructor call with text.
         try:
index e2a4f9bed9b4774d3dfa27b735806dd4b173f4d6..9af9dcd8acc4cefe023c1ae2e2195aa495f842cf 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -13,6 +13,7 @@ and the list is in rough alphabetical order by last names.
 
 Rajiv Abraham
 David Abrahams
+Marc Abramowitz
 Ron Adam
 Ali Afshar
 Jim Ahlstrom