bpo-33729: Fix issues with arguments parsing in hashlib. (GH-8346) · pythoncapi/cpython@f1d36d8 · GitHub
Skip to content

Commit f1d36d8

Browse files
bpo-33729: Fix issues with arguments parsing in hashlib. (pythonGH-8346)
* help(hashlib) didn't work because of incorrect module name in blake2b and blake2s classes. * Constructors blake2*(), sha3_*(), shake_*() and keccak_*() incorrectly accepted keyword argument "string" for binary data, but documented as accepting the "data" keyword argument. Now this parameter is positional-only. * Keyword-only parameters in blake2b() and blake2s() were not documented as keyword-only. * Default value for some parameters of blake2b() and blake2s() was None, which is not acceptable value. * The length argument for shake_*.digest() was wrapped out to 32 bits. * The argument for shake_128.digest() and shake_128.hexdigest() was not positional-only as intended. * TypeError messages for incorrect arguments in all constructors sha3_*(), shake_*() and keccak_*() incorrectly referred to sha3_224. Also made the following enhancements: * More accurately specified input and result types for strings, bytes and bytes-like objects. * Unified positional parameter names for update() and constructors. * Improved formatting.
1 parent 4b8a7f5 commit f1d36d8

12 files changed

Lines changed: 214 additions & 232 deletions

File tree

Doc/library/hashlib.rst

Lines changed: 14 additions & 13 deletions

Lib/hashlib.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,18 @@
2525
sha384 and sha512 will be slow on 32 bit platforms.
2626
2727
Hash objects have these methods:
28-
- update(arg): Update the hash object with the bytes in arg. Repeated calls
29-
are equivalent to a single call with the concatenation of all
30-
the arguments.
31-
- digest(): Return the digest of the bytes passed to the update() method
32-
so far.
33-
- hexdigest(): Like digest() except the digest is returned as a unicode
34-
object of double length, containing only hexadecimal digits.
35-
- copy(): Return a copy (clone) of the hash object. This can be used to
36-
efficiently compute the digests of strings that share a common
37-
initial substring.
38-
39-
For example, to obtain the digest of the string 'Nobody inspects the
28+
- update(data): Update the hash object with the bytes in data. Repeated calls
29+
are equivalent to a single call with the concatenation of all
30+
the arguments.
31+
- digest(): Return the digest of the bytes passed to the update() method
32+
so far as a bytes object.
33+
- hexdigest(): Like digest() except the digest is returned as a string
34+
of double length, containing only hexadecimal digits.
35+
- copy(): Return a copy (clone) of the hash object. This can be used to
36+
efficiently compute the digests of datas that share a common
37+
initial substring.
38+
39+
For example, to obtain the digest of the byte string 'Nobody inspects the
4040
spammish repetition':
4141
4242
>>> import hashlib
@@ -130,14 +130,15 @@ def __get_openssl_constructor(name):
130130

131131
def __py_new(name, data=b'', **kwargs):
132132
"""new(name, data=b'', **kwargs) - Return a new hashing object using the
133-
named algorithm; optionally initialized with data (which must be bytes).
133+
named algorithm; optionally initialized with data (which must be
134+
a bytes-like object).
134135
"""
135136
return __get_builtin_constructor(name)(data, **kwargs)
136137

137138

138139
def __hash_new(name, data=b'', **kwargs):
139140
"""new(name, data=b'') - Return a new hashing object using the named algorithm;
140-
optionally initialized with data (which must be bytes).
141+
optionally initialized with data (which must be a bytes-like object).
141142
"""
142143
if name in {'blake2b', 'blake2s'}:
143144
# Prefer our blake2 implementation.

Lib/test/test_hashlib.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,12 @@ def check_blake2(self, constructor, salt_size, person_size, key_size,
568568
self.assertRaises(ValueError, constructor, node_offset=-1)
569569
self.assertRaises(OverflowError, constructor, node_offset=max_offset+1)
570570

571+
self.assertRaises(TypeError, constructor, data=b'')
572+
self.assertRaises(TypeError, constructor, string=b'')
573+
self.assertRaises(TypeError, constructor, '')
574+
571575
constructor(
572-
string=b'',
576+
b'',
573577
key=b'',
574578
salt=b'',
575579
person=b'',
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed issues with arguments parsing in :mod:`hashlib`.

Modules/_blake2/blake2b_impl.c

Lines changed: 31 additions & 30 deletions

0 commit comments

Comments
 (0)