Skip to content

Commit c73238f

Browse files
committed
gh-162714: Hide the deprecated hashlib string parameter from signatures
The signatures of hash object constructors showed `string=None`, while the constructors treat any explicitly given `string` as conflicting with `data`. Binding a call, applying the signature defaults and forwarding it raised `TypeError`. The signatures are now given by `@text_signature` without `string`. Passing `string` keeps its current behavior.
1 parent 35a703b commit c73238f

14 files changed

Lines changed: 90 additions & 66 deletions

‎Lib/test/test_hashlib.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from binascii import unhexlify
99
import hashlib
1010
import importlib
11+
import inspect
1112
import io
1213
import itertools
1314
import logging
@@ -316,6 +317,18 @@ def test_clinic_signature(self):
316317
DEPRECATED_STRING_PARAMETER):
317318
self._hashlib.new(digest_name, string=b'')
318319

320+
def test_clinic_signature_apply_defaults(self):
321+
# gh-162714: the defaults of the signature form a valid call
322+
for constructor in self.hash_constructors:
323+
try:
324+
signature = inspect.signature(constructor)
325+
except ValueError: # built-in SHA-3 types have no signature
326+
continue
327+
with self.subTest(constructor.__name__):
328+
bound = signature.bind(b'')
329+
bound.apply_defaults()
330+
constructor(*bound.args, **bound.kwargs)
331+
319332
@unittest.skipIf(get_fips_mode(), "skip in FIPS mode")
320333
def test_clinic_signature_errors(self):
321334
nomsg = b''
@@ -354,6 +367,7 @@ def test_clinic_signature_errors(self):
354367
[(nomsg,), dict(string=nomsg), conflicting_call],
355368
[(mymsg,), dict(string=nomsg), conflicting_call],
356369
[(), dict(data=nomsg, string=nomsg), conflicting_call],
370+
[(nomsg,), dict(string=None), conflicting_call],
357371
]:
358372
for constructor in self.hash_constructors:
359373
digest_name = constructor(b'').name
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:mod:`hashlib`: the deprecated *string* keyword parameter of hash object
2+
constructors is no longer part of their signature, so that calls built with
3+
:meth:`inspect.BoundArguments.apply_defaults` are valid.
4+
Patch by Bénédikt Tran.

‎Modules/_hashopenssl.c‎

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,7 @@ _hashlib_HASH(_hashlibstate *state, const char *digestname, PyObject *data_obj,
13421342
/* The module-level function: new() */
13431343

13441344
/*[clinic input]
1345+
@text_signature "($module, /, name, data=b'', *, usedforsecurity=True)"
13451346
_hashlib.new as _hashlib_HASH_new
13461347
13471348
name: str
@@ -1361,7 +1362,7 @@ The MD5 and SHA1 algorithms are always supported.
13611362
static PyObject *
13621363
_hashlib_HASH_new_impl(PyObject *module, const char *name, PyObject *data,
13631364
int usedforsecurity, PyObject *string)
1364-
/*[clinic end generated code: output=b905aaf9840c1bbd input=c34af6c6e696d44e]*/
1365+
/*[clinic end generated code: output=b905aaf9840c1bbd input=8f9d08a66387e4d8]*/
13651366
{
13661367
PyObject *data_obj;
13671368
if (_Py_hashlib_data_argument(&data_obj, data, string) < 0) {
@@ -1373,6 +1374,7 @@ _hashlib_HASH_new_impl(PyObject *module, const char *name, PyObject *data,
13731374

13741375

13751376
/*[clinic input]
1377+
@text_signature "($module, /, data=b'', *, usedforsecurity=True)"
13761378
_hashlib.openssl_md5
13771379
13781380
data: object(c_default="NULL") = b''
@@ -1387,13 +1389,14 @@ Returns a md5 hash object; optionally initialized with a string
13871389
static PyObject *
13881390
_hashlib_openssl_md5_impl(PyObject *module, PyObject *data,
13891391
int usedforsecurity, PyObject *string)
1390-
/*[clinic end generated code: output=ca8cf184d90f7432 input=e7c0adbd6a867db1]*/
1392+
/*[clinic end generated code: output=ca8cf184d90f7432 input=1242105b6a5176d6]*/
13911393
{
13921394
CALL_HASHLIB_NEW(module, Py_hash_md5, data, string, usedforsecurity);
13931395
}
13941396

13951397

13961398
/*[clinic input]
1399+
@text_signature "($module, /, data=b'', *, usedforsecurity=True)"
13971400
_hashlib.openssl_sha1
13981401
13991402
data: object(c_default="NULL") = b''
@@ -1408,13 +1411,14 @@ Returns a sha1 hash object; optionally initialized with a string
14081411
static PyObject *
14091412
_hashlib_openssl_sha1_impl(PyObject *module, PyObject *data,
14101413
int usedforsecurity, PyObject *string)
1411-
/*[clinic end generated code: output=1736fb7b310d64be input=f7e5bb1711e952d8]*/
1414+
/*[clinic end generated code: output=1736fb7b310d64be input=eca1e1e061f67ba8]*/
14121415
{
14131416
CALL_HASHLIB_NEW(module, Py_hash_sha1, data, string, usedforsecurity);
14141417
}
14151418

14161419

14171420
/*[clinic input]
1421+
@text_signature "($module, /, data=b'', *, usedforsecurity=True)"
14181422
_hashlib.openssl_sha224
14191423
14201424
data: object(c_default="NULL") = b''
@@ -1429,13 +1433,14 @@ Returns a sha224 hash object; optionally initialized with a string
14291433
static PyObject *
14301434
_hashlib_openssl_sha224_impl(PyObject *module, PyObject *data,
14311435
int usedforsecurity, PyObject *string)
1432-
/*[clinic end generated code: output=0d6ff57be5e5c140 input=3820fff7ed3a53b8]*/
1436+
/*[clinic end generated code: output=0d6ff57be5e5c140 input=c6bb80a6e9b8e982]*/
14331437
{
14341438
CALL_HASHLIB_NEW(module, Py_hash_sha224, data, string, usedforsecurity);
14351439
}
14361440

14371441

14381442
/*[clinic input]
1443+
@text_signature "($module, /, data=b'', *, usedforsecurity=True)"
14391444
_hashlib.openssl_sha256
14401445
14411446
data: object(c_default="NULL") = b''
@@ -1450,13 +1455,14 @@ Returns a sha256 hash object; optionally initialized with a string
14501455
static PyObject *
14511456
_hashlib_openssl_sha256_impl(PyObject *module, PyObject *data,
14521457
int usedforsecurity, PyObject *string)
1453-
/*[clinic end generated code: output=412ea7111555b6e7 input=9a2f115cf1f7e0eb]*/
1458+
/*[clinic end generated code: output=412ea7111555b6e7 input=2f0e3f6ed9e05e89]*/
14541459
{
14551460
CALL_HASHLIB_NEW(module, Py_hash_sha256, data, string, usedforsecurity);
14561461
}
14571462

14581463

14591464
/*[clinic input]
1465+
@text_signature "($module, /, data=b'', *, usedforsecurity=True)"
14601466
_hashlib.openssl_sha384
14611467
14621468
data: object(c_default="NULL") = b''
@@ -1471,13 +1477,14 @@ Returns a sha384 hash object; optionally initialized with a string
14711477
static PyObject *
14721478
_hashlib_openssl_sha384_impl(PyObject *module, PyObject *data,
14731479
int usedforsecurity, PyObject *string)
1474-
/*[clinic end generated code: output=2e0dc395b59ed726 input=1ea48f6f01e77cfb]*/
1480+
/*[clinic end generated code: output=2e0dc395b59ed726 input=5691ef74159f0bbc]*/
14751481
{
14761482
CALL_HASHLIB_NEW(module, Py_hash_sha384, data, string, usedforsecurity);
14771483
}
14781484

14791485

14801486
/*[clinic input]
1487+
@text_signature "($module, /, data=b'', *, usedforsecurity=True)"
14811488
_hashlib.openssl_sha512
14821489
14831490
data: object(c_default="NULL") = b''
@@ -1492,7 +1499,7 @@ Returns a sha512 hash object; optionally initialized with a string
14921499
static PyObject *
14931500
_hashlib_openssl_sha512_impl(PyObject *module, PyObject *data,
14941501
int usedforsecurity, PyObject *string)
1495-
/*[clinic end generated code: output=4bdd760388dbfc0f input=3cf56903e07d1f5c]*/
1502+
/*[clinic end generated code: output=4bdd760388dbfc0f input=0b97ed23dc54ab56]*/
14961503
{
14971504
CALL_HASHLIB_NEW(module, Py_hash_sha512, data, string, usedforsecurity);
14981505
}
@@ -1501,6 +1508,7 @@ _hashlib_openssl_sha512_impl(PyObject *module, PyObject *data,
15011508
#ifdef PY_OPENSSL_HAS_SHA3
15021509

15031510
/*[clinic input]
1511+
@text_signature "($module, /, data=b'', *, usedforsecurity=True)"
15041512
_hashlib.openssl_sha3_224
15051513
15061514
data: object(c_default="NULL") = b''
@@ -1515,12 +1523,13 @@ Returns a sha3-224 hash object; optionally initialized with a string
15151523
static PyObject *
15161524
_hashlib_openssl_sha3_224_impl(PyObject *module, PyObject *data,
15171525
int usedforsecurity, PyObject *string)
1518-
/*[clinic end generated code: output=6d8dc2a924f3ba35 input=7f14f16a9f6a3158]*/
1526+
/*[clinic end generated code: output=6d8dc2a924f3ba35 input=7107e36646dc3a8e]*/
15191527
{
15201528
CALL_HASHLIB_NEW(module, Py_hash_sha3_224, data, string, usedforsecurity);
15211529
}
15221530

15231531
/*[clinic input]
1532+
@text_signature "($module, /, data=b'', *, usedforsecurity=True)"
15241533
_hashlib.openssl_sha3_256
15251534
15261535
data: object(c_default="NULL") = b''
@@ -1535,12 +1544,13 @@ Returns a sha3-256 hash object; optionally initialized with a string
15351544
static PyObject *
15361545
_hashlib_openssl_sha3_256_impl(PyObject *module, PyObject *data,
15371546
int usedforsecurity, PyObject *string)
1538-
/*[clinic end generated code: output=9e520f537b3a4622 input=7987150939d5e352]*/
1547+
/*[clinic end generated code: output=9e520f537b3a4622 input=a0de473419ff4593]*/
15391548
{
15401549
CALL_HASHLIB_NEW(module, Py_hash_sha3_256, data, string, usedforsecurity);
15411550
}
15421551

15431552
/*[clinic input]
1553+
@text_signature "($module, /, data=b'', *, usedforsecurity=True)"
15441554
_hashlib.openssl_sha3_384
15451555
15461556
data: object(c_default="NULL") = b''
@@ -1555,12 +1565,13 @@ Returns a sha3-384 hash object; optionally initialized with a string
15551565
static PyObject *
15561566
_hashlib_openssl_sha3_384_impl(PyObject *module, PyObject *data,
15571567
int usedforsecurity, PyObject *string)
1558-
/*[clinic end generated code: output=d239ba0463fd6138 input=fc943401f67e3b81]*/
1568+
/*[clinic end generated code: output=d239ba0463fd6138 input=1c9c36f4b0fb859e]*/
15591569
{
15601570
CALL_HASHLIB_NEW(module, Py_hash_sha3_384, data, string, usedforsecurity);
15611571
}
15621572

15631573
/*[clinic input]
1574+
@text_signature "($module, /, data=b'', *, usedforsecurity=True)"
15641575
_hashlib.openssl_sha3_512
15651576
15661577
data: object(c_default="NULL") = b''
@@ -1575,7 +1586,7 @@ Returns a sha3-512 hash object; optionally initialized with a string
15751586
static PyObject *
15761587
_hashlib_openssl_sha3_512_impl(PyObject *module, PyObject *data,
15771588
int usedforsecurity, PyObject *string)
1578-
/*[clinic end generated code: output=17662f21038c2278 input=6601ddd2c6c1516d]*/
1589+
/*[clinic end generated code: output=17662f21038c2278 input=eb09a63a3d478630]*/
15791590
{
15801591
CALL_HASHLIB_NEW(module, Py_hash_sha3_512, data, string, usedforsecurity);
15811592
}
@@ -1584,6 +1595,7 @@ _hashlib_openssl_sha3_512_impl(PyObject *module, PyObject *data,
15841595
#ifdef PY_OPENSSL_HAS_SHAKE
15851596
/*[clinic input]
15861597
@permit_long_summary
1598+
@text_signature "($module, /, data=b'', *, usedforsecurity=True)"
15871599
_hashlib.openssl_shake_128
15881600
15891601
data: object(c_default="NULL") = b''
@@ -1598,13 +1610,14 @@ Returns a shake-128 variable hash object; optionally initialized with a string
15981610
static PyObject *
15991611
_hashlib_openssl_shake_128_impl(PyObject *module, PyObject *data,
16001612
int usedforsecurity, PyObject *string)
1601-
/*[clinic end generated code: output=4e6afed8d18980ad input=0d2803af1158b23c]*/
1613+
/*[clinic end generated code: output=4e6afed8d18980ad input=ef04b0f787e1f5f0]*/
16021614
{
16031615
CALL_HASHLIB_NEW(module, Py_hash_shake_128, data, string, usedforsecurity);
16041616
}
16051617

16061618
/*[clinic input]
16071619
@permit_long_summary
1620+
@text_signature "($module, /, data=b'', *, usedforsecurity=True)"
16081621
_hashlib.openssl_shake_256
16091622
16101623
data: object(c_default="NULL") = b''
@@ -1619,7 +1632,7 @@ Returns a shake-256 variable hash object; optionally initialized with a string
16191632
static PyObject *
16201633
_hashlib_openssl_shake_256_impl(PyObject *module, PyObject *data,
16211634
int usedforsecurity, PyObject *string)
1622-
/*[clinic end generated code: output=62481bce4a77d16c input=f27b98d9c749f55d]*/
1635+
/*[clinic end generated code: output=62481bce4a77d16c input=38a2429ec6143f98]*/
16231636
{
16241637
CALL_HASHLIB_NEW(module, Py_hash_shake_256, data, string, usedforsecurity);
16251638
}

‎Modules/blake2module.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@ py_blake2_new(PyTypeObject *type, PyObject *data, int digest_size,
666666

667667
/*[clinic input]
668668
@classmethod
669+
@text_signature "(data=b'', *, digest_size=_blake2.blake2b.MAX_DIGEST_SIZE, key=b'', salt=b'', person=b'', fanout=1, depth=1, leaf_size=0, node_offset=0, node_depth=0, inner_size=0, last_node=False, usedforsecurity=True)"
669670
_blake2.blake2b.__new__ as py_blake2b_new
670671
data as data_obj: object(c_default="NULL") = b''
671672
*
@@ -693,7 +694,7 @@ py_blake2b_new_impl(PyTypeObject *type, PyObject *data_obj, int digest_size,
693694
unsigned long long node_offset, int node_depth,
694695
int inner_size, int last_node, int usedforsecurity,
695696
PyObject *string)
696-
/*[clinic end generated code: output=de64bd850606b6a0 input=32832fb37d13c03d]*/
697+
/*[clinic end generated code: output=de64bd850606b6a0 input=3910410fa9a13dea]*/
697698
{
698699
PyObject *data;
699700
if (_Py_hashlib_data_argument(&data, data_obj, string) < 0) {
@@ -706,6 +707,7 @@ py_blake2b_new_impl(PyTypeObject *type, PyObject *data_obj, int digest_size,
706707

707708
/*[clinic input]
708709
@classmethod
710+
@text_signature "(data=b'', *, digest_size=_blake2.blake2s.MAX_DIGEST_SIZE, key=b'', salt=b'', person=b'', fanout=1, depth=1, leaf_size=0, node_offset=0, node_depth=0, inner_size=0, last_node=False, usedforsecurity=True)"
709711
_blake2.blake2s.__new__ as py_blake2s_new
710712
data as data_obj: object(c_default="NULL") = b''
711713
*
@@ -733,7 +735,7 @@ py_blake2s_new_impl(PyTypeObject *type, PyObject *data_obj, int digest_size,
733735
unsigned long long node_offset, int node_depth,
734736
int inner_size, int last_node, int usedforsecurity,
735737
PyObject *string)
736-
/*[clinic end generated code: output=582a0c4295cc3a3c input=da467fc9dae646bb]*/
738+
/*[clinic end generated code: output=582a0c4295cc3a3c input=a95acd771b2aa71f]*/
737739
{
738740
PyObject *data;
739741
if (_Py_hashlib_data_argument(&data, data_obj, string) < 0) {

0 commit comments

Comments
 (0)