Skip to content

Commit 996992d

Browse files
committed
deprecate string keyword parameter for hash function constructors
1 parent 5507eff commit 996992d

File tree

6 files changed

+58
-5
lines changed

6 files changed

+58
-5
lines changed

Doc/deprecations/pending-removal-in-3.17.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
Pending removal in Python 3.17
22
------------------------------
33

4+
* :mod:`hashlib`:
5+
6+
- Supporting the undocumented ``string`` keyword parameter in :func:`~hashlib.new`
7+
and hash function constructors such as :func:`~hashlib.md5` is deprecated.
8+
Use the ``data`` keyword parameter instead, or simply pass the data to hash
9+
as a positional argument.
10+
11+
Before Python 3.13, the ``string`` keyword parameter was not correctly
12+
supported depending on the backend implementation of hash functions, and
13+
users should prefer passing the data to hash as a positional argument.
14+
415
* :mod:`typing`:
516

617
- Before Python 3.14, old-style unions were implemented using the private class

Doc/library/hashlib.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ accessible by name via :func:`new`. See :data:`algorithms_available`.
9494
OpenSSL does not provide we fall back to a verified implementation from
9595
the `HACL\* project`_.
9696

97+
.. deprecated-removed:: 3.15 3.17
98+
The *undocumented* ``string`` keyword parameter in :func:`new` and hash
99+
function constructors such as :func:`md5` is deprecated.
100+
Use the ``data`` keyword parameter instead, or simply pass the data to hash
101+
as a positional argument.
102+
103+
97104
Usage
98105
-----
99106

Doc/whatsnew/3.15.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,15 @@ module_name
129129
Deprecated
130130
==========
131131

132-
* module_name:
133-
TODO
132+
hashlib
133+
-------
134+
135+
* Supporting the undocumented ``string`` keyword parameter in
136+
:func:`~hashlib.new` and hash function constructors such as
137+
:func:`~hashlib.md5` is deprecated and slated for removal in Python 3.17.
138+
Use the ``data`` keyword parameter instead, or simply pass the data to hash
139+
as a positional argument.
140+
(Contributed by Bénédikt Tran in :gh:`134978`.)
134141

135142

136143
.. Add deprecations above alphabetically, not here at the end.

Lib/test/test_hashlib.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ def read_vectors(hash_name):
9898
yield parts
9999

100100

101+
DEPRECATED_STRING_PARAMETER = re.escape(
102+
"the 'string' keyword parameter is deprecated since "
103+
"Python 3.15 and slated for removal in Python 3.17; "
104+
"use the 'data' keyword parameter or pass the data "
105+
"to hash as a positional argument instead"
106+
)
107+
108+
101109
class HashLibTestCase(unittest.TestCase):
102110
supported_hash_names = ( 'md5', 'MD5', 'sha1', 'SHA1',
103111
'sha224', 'SHA224', 'sha256', 'SHA256',
@@ -255,17 +263,23 @@ def test_clinic_signature(self):
255263
with self.subTest(constructor.__name__):
256264
constructor(b'')
257265
constructor(data=b'')
258-
constructor(string=b'') # should be deprecated in the future
266+
with self.assertWarnsRegex(DeprecationWarning,
267+
DEPRECATED_STRING_PARAMETER):
268+
constructor(string=b'')
259269

260270
digest_name = constructor(b'').name
261271
with self.subTest(digest_name):
262272
hashlib.new(digest_name, b'')
263273
hashlib.new(digest_name, data=b'')
264-
hashlib.new(digest_name, string=b'')
274+
with self.assertWarnsRegex(DeprecationWarning,
275+
DEPRECATED_STRING_PARAMETER):
276+
hashlib.new(digest_name, string=b'')
265277
if self._hashlib:
266278
self._hashlib.new(digest_name, b'')
267279
self._hashlib.new(digest_name, data=b'')
268-
self._hashlib.new(digest_name, string=b'')
280+
with self.assertWarnsRegex(DeprecationWarning,
281+
DEPRECATED_STRING_PARAMETER):
282+
self._hashlib.new(digest_name, string=b'')
269283

270284
@unittest.skipIf(get_fips_mode(), "skip in FIPS mode")
271285
def test_clinic_signature_errors(self):
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Supporting the undocumented ``string`` keyword parameter in :func:`hashlib.new`
2+
and hash function constructors such as :func:`hashlib.md5` is deprecated and
3+
slated for removal in Python 3.17. Use the ``data`` keyword parameter instead,
4+
or simply pass the data to hash as a positional argument.
5+
Patch by Bénédikt Tran.

Modules/hashlib.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ _Py_hashlib_data_argument(PyObject **res, PyObject *data, PyObject *string)
8686
}
8787
else if (data == NULL && string != NULL) {
8888
// called as H(string=...)
89+
if (PyErr_WarnEx(PyExc_DeprecationWarning,
90+
"the 'string' keyword parameter is deprecated since "
91+
"Python 3.15 and slated for removal in Python 3.17; "
92+
"use the 'data' keyword parameter or pass the data "
93+
"to hash as a positional argument instead", 1) < 0)
94+
{
95+
*res = NULL;
96+
return -1;
97+
}
8998
*res = string;
9099
return 1;
91100
}

0 commit comments

Comments
 (0)