Skip to content

Commit 0bd4000

Browse files
committed
Merge remote-tracking branch 'upstream/main' into gh-114258-getitem
2 parents 2467aca + 796b26b commit 0bd4000

21 files changed

Lines changed: 344 additions & 160 deletions

‎Doc/library/ctypes.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1563,7 +1563,7 @@ way is to instantiate :py:class:`CDLL` or one of its subclasses:
15631563
On non-Windows systems, *name* can be ``None``. In this case,
15641564
:c:func:`!dlopen` is called with ``NULL``, which opens the main program
15651565
as a "library".
1566-
(Some systems do the same is *name* is empty; ``None``/``NULL`` is more
1566+
(Some systems do the same if *name* is empty; ``None``/``NULL`` is more
15671567
portable.)
15681568

15691569
.. admonition:: CPython implementation detail

‎Lib/hmac.py‎

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,27 @@ def digest_wrapper(d=b''):
4949
return digest_wrapper
5050

5151

52+
def _select_blocksize(obj, default, stacklevel=3):
53+
if hasattr(obj, 'block_size'):
54+
blocksize = obj.block_size
55+
if blocksize < 16:
56+
import warnings
57+
58+
warnings.warn(f"block_size of {blocksize} seems too small; "
59+
f"using our default of {default}.",
60+
RuntimeWarning, stacklevel=stacklevel)
61+
blocksize = default
62+
else:
63+
import warnings
64+
65+
warnings.warn("No block_size attribute on given digest object; "
66+
f"Assuming {default}.",
67+
RuntimeWarning, stacklevel=stacklevel)
68+
blocksize = default
69+
return blocksize
70+
71+
72+
5273
class HMAC:
5374
"""RFC 2104 HMAC class. Also complies with RFC 4231.
5475
@@ -116,8 +137,6 @@ def _init_builtin_hmac(self, key, msg, digestmod):
116137
self.block_size = self._hmac.block_size
117138

118139
def _init_old(self, key, msg, digestmod):
119-
import warnings
120-
121140
digest_cons = _get_digest_constructor(digestmod)
122141
if _is_shake_constructor(digest_cons):
123142
raise ValueError(f"unsupported hash algorithm {digestmod}")
@@ -127,19 +146,7 @@ def _init_old(self, key, msg, digestmod):
127146
self._inner = digest_cons()
128147
self.digest_size = self._inner.digest_size
129148

130-
if hasattr(self._inner, 'block_size'):
131-
blocksize = self._inner.block_size
132-
if blocksize < 16:
133-
warnings.warn(f"block_size of {blocksize} seems too small; "
134-
f"using our default of {self.blocksize}.",
135-
RuntimeWarning, 2)
136-
blocksize = self.blocksize # pragma: no cover
137-
else:
138-
warnings.warn("No block_size attribute on given digest object; "
139-
f"Assuming {self.blocksize}.",
140-
RuntimeWarning, 2)
141-
blocksize = self.blocksize # pragma: no cover
142-
149+
blocksize = _select_blocksize(self._inner, self.blocksize)
143150
if len(key) > blocksize:
144151
key = digest_cons(key).digest()
145152

@@ -275,7 +282,7 @@ def _compute_digest_fallback(key, msg, digest):
275282
raise ValueError(f"unsupported hash algorithm {digest}")
276283
inner = digest_cons()
277284
outer = digest_cons()
278-
blocksize = getattr(inner, 'block_size', 64)
285+
blocksize = _select_blocksize(inner, HMAC.blocksize)
279286
if len(key) > blocksize:
280287
key = digest_cons(key).digest()
281288
key = key.ljust(blocksize, b'\0')

‎Lib/test/test_clinic.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3372,7 +3372,7 @@ class Foo "FooObject *" ""
33723372
("Foo.__getitem__\n key: object = None\n /",
33733373
"The parameters of '__getitem__' cannot have a default value"),
33743374
]
3375-
for converter in "Py_ssize_t", "object":
3375+
for converter in ["Py_ssize_t", "object"]:
33763376
with self.subTest(converter=converter):
33773377
function = self.parse_function(
33783378
block.format(converter=converter),

‎Lib/test/test_hmac.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,17 +1501,36 @@ def update(self, v):
15011501
self._x.update(v)
15021502
def digest(self):
15031503
return self._x.digest()
1504+
def copy(self):
1505+
h = MockCrazyHash()
1506+
h._x = self._x.copy()
1507+
return h
15041508

15051509
with warnings.catch_warnings():
15061510
warnings.simplefilter('error', RuntimeWarning)
15071511
with self.assertRaises(RuntimeWarning):
15081512
hmac.HMAC(b'a', b'b', digestmod=MockCrazyHash)
15091513
self.fail('Expected warning about missing block_size')
1514+
with self.assertRaises(RuntimeWarning):
1515+
hmac.digest(b'a', b'b', MockCrazyHash)
1516+
self.fail('Expected warning about missing block_size')
15101517

15111518
MockCrazyHash.block_size = 1
15121519
with self.assertRaises(RuntimeWarning):
15131520
hmac.HMAC(b'a', b'b', digestmod=MockCrazyHash)
15141521
self.fail('Expected warning about small block_size')
1522+
with self.assertRaises(RuntimeWarning):
1523+
hmac.digest(b'a', b'b', MockCrazyHash)
1524+
self.fail('Expected warning about small block_size')
1525+
1526+
# gh-155428: hmac.HMAC() and hmac.digest() must both
1527+
# fall back to the default block size (64 for SHA-256).
1528+
expect = hmac.digest(b'a', b'b', 'sha256')
1529+
with warnings.catch_warnings():
1530+
warnings.simplefilter('ignore', RuntimeWarning)
1531+
h = hmac.HMAC(b'a', b'b', digestmod=MockCrazyHash)
1532+
self.assertEqual(h.digest(), expect)
1533+
self.assertEqual(hmac.digest(b'a', b'b', MockCrazyHash), expect)
15151534

15161535
@hashlib_helper.requires_hashdigest('sha256')
15171536
def test_with_fallback(self):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Ensure that :func:`hmac.digest` and :meth:`hmac.HMAC.digest` output
2+
identical values when using a digest's block size smaller than 16. Patch by
3+
Bénédikt Tran.

‎Modules/_collectionsmodule.c‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,8 +1497,19 @@ deque_remove_impl(dequeobject *deque, PyObject *value)
14971497
Py_RETURN_NONE;
14981498
}
14991499

1500+
/*[clinic input]
1501+
@critical_section
1502+
_collections.deque.__setitem__ as deque_ass_item
1503+
1504+
deque: dequeobject
1505+
i: Py_ssize_t
1506+
v: object
1507+
/
1508+
[clinic start generated code]*/
1509+
15001510
static int
1501-
deque_ass_item_lock_held(dequeobject *deque, Py_ssize_t i, PyObject *v)
1511+
deque_ass_item_impl(dequeobject *deque, Py_ssize_t i, PyObject *v)
1512+
/*[clinic end generated code: output=28739a830dc69ab1 input=b767e1a4972fa537]*/
15021513
{
15031514
block *b;
15041515
Py_ssize_t n, len=Py_SIZE(deque), halflen=(len+1)>>1, index=i;
@@ -1529,17 +1540,6 @@ deque_ass_item_lock_held(dequeobject *deque, Py_ssize_t i, PyObject *v)
15291540
return 0;
15301541
}
15311542

1532-
static int
1533-
deque_ass_item(PyObject *self, Py_ssize_t i, PyObject *v)
1534-
{
1535-
dequeobject *deque = dequeobject_CAST(self);
1536-
int result;
1537-
Py_BEGIN_CRITICAL_SECTION(deque);
1538-
result = deque_ass_item_lock_held(deque, i, v);
1539-
Py_END_CRITICAL_SECTION();
1540-
return result;
1541-
}
1542-
15431543
static void
15441544
deque_dealloc(PyObject *self)
15451545
{

‎Modules/_ctypes/_ctypes.c‎

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,8 @@ bytes(cdata)
133133
/*[clinic input]
134134
module _ctypes
135135
class _ctypes.CFuncPtr "PyCFuncPtrObject *" "&PyCFuncPtr_Type"
136-
class _ctypes.Array "CDataObject *" "clinic_state()->PyCArray_Type"
137-
class _ctypes.Pointer "CDataObject *" "clinic_state()->PyCPointer_Type"
138136
[clinic start generated code]*/
139-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=56de8bab30675d93]*/
137+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=58e8c99474bc631e]*/
140138

141139
#define clinic_state() (get_module_state_by_class(cls))
142140
#define clinic_state_sub() (get_module_state_by_class(cls->tp_base))
@@ -4985,8 +4983,11 @@ static PyType_Spec pycunion_spec = {
49854983
/*
49864984
PyCArray_Type
49874985
*/
4988-
static int Array_ass_item_lock_held(PyObject *, Py_ssize_t, PyObject *);
4989-
static int Array_ass_subscript(PyObject *, PyObject *, PyObject *);
4986+
4987+
/*[clinic input]
4988+
class _ctypes.Array "PyObject *" "clinic_state()->PyCArray_Type"
4989+
[clinic start generated code]*/
4990+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b2d780e2e2dc6e8a]*/
49904991

49914992
static int
49924993
Array_init(PyObject *self, PyObject *args, PyObject *kw)
@@ -5010,7 +5011,7 @@ Array_init(PyObject *self, PyObject *args, PyObject *kw)
50105011
for (i = 0; result >= 0 && i < n; ++i) {
50115012
PyObject *v = PyTuple_GET_ITEM(args, i);
50125013
assert(v != NULL);
5013-
result = Array_ass_item_lock_held(self, i, v);
5014+
result = Array_ass_item_impl(self, i, v);
50145015
}
50155016
Py_END_CRITICAL_SECTION();
50165017
return result;
@@ -5028,14 +5029,14 @@ Array_init(PyObject *self, PyObject *args, PyObject *kw)
50285029
/*[clinic input]
50295030
@critical_section
50305031
_ctypes.Array.__getitem__ as Array_item
5031-
self as myself: self(type="PyObject *")
5032+
myself: self
50325033
index: Py_ssize_t
50335034
/
50345035
[clinic start generated code]*/
50355036

50365037
static PyObject *
50375038
Array_item_impl(PyObject *myself, Py_ssize_t index)
5038-
/*[clinic end generated code: output=d038162d727d7d9b input=5979a1200a1397ca]*/
5039+
/*[clinic end generated code: output=d038162d727d7d9b input=68fa9889c77bb8c9]*/
50395040
{
50405041
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(myself);
50415042

@@ -5067,14 +5068,14 @@ Array_item_impl(PyObject *myself, Py_ssize_t index)
50675068
/*[clinic input]
50685069
@critical_section
50695070
_ctypes.Array.__getitem__ as Array_subscript
5070-
self as myself: self(type="PyObject *")
5071+
myself: self
50715072
item: object
50725073
/
50735074
[clinic start generated code]*/
50745075

50755076
static PyObject *
50765077
Array_subscript_impl(PyObject *myself, PyObject *item)
5077-
/*[clinic end generated code: output=5c88d612ad5c8a1a input=1a68b5a5ba349556]*/
5078+
/*[clinic end generated code: output=5c88d612ad5c8a1a input=510935495a857cac]*/
50785079
{
50795080
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(myself);
50805081

@@ -5192,8 +5193,19 @@ Array_subscript_impl(PyObject *myself, PyObject *item)
51925193
}
51935194

51945195

5196+
/*[clinic input]
5197+
@critical_section
5198+
_ctypes.Array.__setitem__ as Array_ass_item
5199+
5200+
myself: self
5201+
index: Py_ssize_t
5202+
value: object
5203+
/
5204+
[clinic start generated code]*/
5205+
51955206
static int
5196-
Array_ass_item_lock_held(PyObject *myself, Py_ssize_t index, PyObject *value)
5207+
Array_ass_item_impl(PyObject *myself, Py_ssize_t index, PyObject *value)
5208+
/*[clinic end generated code: output=8fb1fd51b5b45da8 input=8b6ddb53d6377b96]*/
51975209
{
51985210
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(myself);
51995211

@@ -5227,19 +5239,19 @@ Array_ass_item_lock_held(PyObject *myself, Py_ssize_t index, PyObject *value)
52275239
index, size, ptr);
52285240
}
52295241

5230-
static int
5231-
Array_ass_item(PyObject *myself, Py_ssize_t index, PyObject *value)
5232-
{
5233-
int result;
5234-
Py_BEGIN_CRITICAL_SECTION(myself);
5235-
result = Array_ass_item_lock_held(myself, index, value);
5236-
Py_END_CRITICAL_SECTION();
5237-
return result;
5238-
}
5242+
/*[clinic input]
5243+
@critical_section
5244+
_ctypes.Array.__setitem__ as Array_ass_subscript
52395245
5246+
myself: self
5247+
item: object
5248+
value: object
5249+
/
5250+
[clinic start generated code]*/
52405251

52415252
static int
5242-
Array_ass_subscript_lock_held(PyObject *myself, PyObject *item, PyObject *value)
5253+
Array_ass_subscript_impl(PyObject *myself, PyObject *item, PyObject *value)
5254+
/*[clinic end generated code: output=cb9a138e4e40c5ac input=4b7aedebbb357853]*/
52435255
{
52445256
CDataObject *self = _CDataObject_CAST(myself);
52455257

@@ -5256,7 +5268,7 @@ Array_ass_subscript_lock_held(PyObject *myself, PyObject *item, PyObject *value)
52565268
return -1;
52575269
if (i < 0)
52585270
i += self->b_length;
5259-
return Array_ass_item_lock_held(myself, i, value);
5271+
return Array_ass_item_impl(myself, i, value);
52605272
}
52615273
else if (PySlice_Check(item)) {
52625274
Py_ssize_t start, stop, step, slicelen, otherlen, i;
@@ -5281,7 +5293,7 @@ Array_ass_subscript_lock_held(PyObject *myself, PyObject *item, PyObject *value)
52815293
int result;
52825294
if (item == NULL)
52835295
return -1;
5284-
result = Array_ass_item_lock_held(myself, cur, item);
5296+
result = Array_ass_item_impl(myself, cur, item);
52855297
Py_DECREF(item);
52865298
if (result == -1)
52875299
return -1;
@@ -5295,17 +5307,6 @@ Array_ass_subscript_lock_held(PyObject *myself, PyObject *item, PyObject *value)
52955307
}
52965308
}
52975309

5298-
static int
5299-
Array_ass_subscript(PyObject *myself, PyObject *item, PyObject *value)
5300-
{
5301-
int result;
5302-
Py_BEGIN_CRITICAL_SECTION(myself);
5303-
result = Array_ass_subscript_lock_held(myself, item, value);
5304-
Py_END_CRITICAL_SECTION();
5305-
return result;
5306-
}
5307-
5308-
53095310
static Py_ssize_t
53105311
Array_length(PyObject *myself)
53115312
{
@@ -5574,17 +5575,22 @@ static PyType_Spec pycsimple_spec = {
55745575
/*
55755576
PyCPointer_Type
55765577
*/
5578+
/*[clinic input]
5579+
class _ctypes._Pointer "PyObject *" "clinic_state()->PyCPointer_Type"
5580+
[clinic start generated code]*/
5581+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=452695db70aa7c3e]*/
5582+
55775583
/*[clinic input]
55785584
@critical_section
5579-
_ctypes.Pointer.__getitem__ as Pointer_item
5580-
self as myself: self(type="PyObject *")
5585+
_ctypes._Pointer.__getitem__ as Pointer_item
5586+
myself: self
55815587
index: Py_ssize_t
55825588
/
55835589
[clinic start generated code]*/
55845590

55855591
static PyObject *
55865592
Pointer_item_impl(PyObject *myself, Py_ssize_t index)
5587-
/*[clinic end generated code: output=4ae9d566e8038853 input=5e1c027271cdbb9f]*/
5593+
/*[clinic end generated code: output=4ae9d566e8038853 input=17943427cd04ed08]*/
55885594
{
55895595
CDataObject *self = _CDataObject_CAST(myself);
55905596
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
@@ -5623,8 +5629,19 @@ Pointer_item_impl(PyObject *myself, Py_ssize_t index)
56235629
index, size, (char *)((char *)deref + offset));
56245630
}
56255631

5632+
/*[clinic input]
5633+
@critical_section
5634+
_ctypes._Pointer.__setitem__ as Pointer_ass_item
5635+
5636+
myself: self
5637+
index: Py_ssize_t
5638+
value: object
5639+
/
5640+
[clinic start generated code]*/
5641+
56265642
static int
5627-
Pointer_ass_item_lock_held(PyObject *myself, Py_ssize_t index, PyObject *value)
5643+
Pointer_ass_item_impl(PyObject *myself, Py_ssize_t index, PyObject *value)
5644+
/*[clinic end generated code: output=4503430bd55b6c69 input=344ad19b3094558c]*/
56285645
{
56295646
CDataObject *self = _CDataObject_CAST(myself);
56305647
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
@@ -5669,16 +5686,6 @@ Pointer_ass_item_lock_held(PyObject *myself, Py_ssize_t index, PyObject *value)
56695686
index, size, ((char *)deref + offset));
56705687
}
56715688

5672-
static int
5673-
Pointer_ass_item(PyObject *self, Py_ssize_t index, PyObject *value)
5674-
{
5675-
int res;
5676-
Py_BEGIN_CRITICAL_SECTION(self);
5677-
res = Pointer_ass_item_lock_held(self, index, value);
5678-
Py_END_CRITICAL_SECTION();
5679-
return res;
5680-
}
5681-
56825689
static PyObject *
56835690
Pointer_get_contents_lock_held(PyObject *self, void *closure)
56845691
{

0 commit comments

Comments
 (0)