Skip to content

Commit 76c28f7

Browse files
committed
Consistently raise a TypeError when a non str is passed to hashlib.new
regardless of which of the two implementations of new is used.
1 parent 00528e8 commit 76c28f7

File tree

2 files changed

+4
-7
lines changed

2 files changed

+4
-7
lines changed

Lib/hashlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def __get_builtin_constructor(name):
8888
except ImportError:
8989
pass # no extension module, this hash is unsupported.
9090

91-
raise ValueError('unsupported hash type %s' % name)
91+
raise ValueError('unsupported hash type ' + name)
9292

9393

9494
def __get_openssl_constructor(name):

Lib/test/test_hashlib.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,8 @@ def test_algorithms_available(self):
111111
issubset(hashlib.algorithms_available))
112112

113113
def test_unknown_hash(self):
114-
try:
115-
hashlib.new('spam spam spam spam spam')
116-
except ValueError:
117-
pass
118-
else:
119-
self.assertTrue(0 == "hashlib didn't reject bogus hash name")
114+
self.assertRaises(ValueError, hashlib.new, 'spam spam spam spam spam')
115+
self.assertRaises(TypeError, hashlib.new, 1)
120116

121117
def test_get_builtin_constructor(self):
122118
get_builtin_constructor = hashlib.__dict__[
@@ -135,6 +131,7 @@ def test_get_builtin_constructor(self):
135131
sys.modules['_md5'] = _md5
136132
else:
137133
del sys.modules['_md5']
134+
self.assertRaises(TypeError, get_builtin_constructor, 3)
138135

139136
def test_hexdigest(self):
140137
for name in self.supported_hash_names:

0 commit comments

Comments
 (0)