[Python-checkins] cpython: Issue #15225: improve error message when hmac is passed a wrong key type.

antoine.pitrou python-checkins at python.org
Sat Jun 30 17:29:19 CEST 2012


http://hg.python.org/cpython/rev/b0605b34b2de
changeset:   77871:b0605b34b2de
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Sat Jun 30 17:27:56 2012 +0200
summary:
  Issue #15225: improve error message when hmac is passed a wrong key type.
Patch by Marc Abramowitz.

files:
  Lib/hmac.py           |   2 +-
  Lib/test/test_hmac.py |  12 ++++++++++++
  Misc/ACKS             |   1 +
  3 files changed, 14 insertions(+), 1 deletions(-)


diff --git a/Lib/hmac.py b/Lib/hmac.py
--- a/Lib/hmac.py
+++ b/Lib/hmac.py
@@ -35,7 +35,7 @@
         """
 
         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
diff --git a/Lib/test/test_hmac.py b/Lib/test/test_hmac.py
--- a/Lib/test/test_hmac.py
+++ b/Lib/test/test_hmac.py
@@ -234,6 +234,18 @@
         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:
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -13,6 +13,7 @@
 
 Rajiv Abraham
 David Abrahams
+Marc Abramowitz
 Ron Adam
 Ali Afshar
 Jim Ahlstrom

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


More information about the Python-checkins mailing list