Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 24ef3e9

Browse files
committed
Issue #15225: improve error message when hmac is passed a wrong key type.
Patch by Marc Abramowitz.
1 parent 9c7817e commit 24ef3e9

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

Lib/hmac.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __init__(self, key, msg = None, digestmod = None):
3535
"""
3636

3737
if not isinstance(key, bytes):
38-
raise TypeError("expected bytes, but got %r" % type(key).__name__)
38+
raise TypeError("key: expected bytes, but got %r" % type(key).__name__)
3939

4040
if digestmod is None:
4141
import hashlib

Lib/test/test_hmac.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,18 @@ def test_normal(self):
234234
except:
235235
self.fail("Standard constructor call raised exception.")
236236

237+
def test_with_str_key(self):
238+
# Pass a key of type str, which is an error, because it expects a key
239+
# of type bytes
240+
with self.assertRaises(TypeError):
241+
h = hmac.HMAC("key")
242+
243+
def test_dot_new_with_str_key(self):
244+
# Pass a key of type str, which is an error, because it expects a key
245+
# of type bytes
246+
with self.assertRaises(TypeError):
247+
h = hmac.new("key")
248+
237249
def test_withtext(self):
238250
# Constructor call with text.
239251
try:

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and the list is in rough alphabetical order by last names.
1313

1414
Rajiv Abraham
1515
David Abrahams
16+
Marc Abramowitz
1617
Ron Adam
1718
Ali Afshar
1819
Jim Ahlstrom

0 commit comments

Comments
 (0)