Skip to content

Commit a7a04fc

Browse files
authored
Merge branch 'main' into gh-119726-generate-and-patch-AArch64-trampolines
2 parents f2cab0c + 3597642 commit a7a04fc

7 files changed

Lines changed: 91 additions & 9 deletions

File tree

Doc/c-api/buffer.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ The following fields are not influenced by *flags* and must always be filled in
244244
with the correct values: :c:member:`~Py_buffer.obj`, :c:member:`~Py_buffer.buf`,
245245
:c:member:`~Py_buffer.len`, :c:member:`~Py_buffer.itemsize`, :c:member:`~Py_buffer.ndim`.
246246

247-
248247
readonly, format
249248
~~~~~~~~~~~~~~~~
250249

@@ -253,7 +252,8 @@ readonly, format
253252
Controls the :c:member:`~Py_buffer.readonly` field. If set, the exporter
254253
MUST provide a writable buffer or else report failure. Otherwise, the
255254
exporter MAY provide either a read-only or writable buffer, but the choice
256-
MUST be consistent for all consumers.
255+
MUST be consistent for all consumers. For example, :c:expr:`PyBUF_SIMPLE | PyBUF_WRITABLE`
256+
can be used to request a simple writable buffer.
257257

258258
.. c:macro:: PyBUF_FORMAT
259259
@@ -265,8 +265,9 @@ readonly, format
265265
Since :c:macro:`PyBUF_SIMPLE` is defined as 0, :c:macro:`PyBUF_WRITABLE`
266266
can be used as a stand-alone flag to request a simple writable buffer.
267267

268-
:c:macro:`PyBUF_FORMAT` can be \|'d to any of the flags except :c:macro:`PyBUF_SIMPLE`.
269-
The latter already implies format ``B`` (unsigned bytes).
268+
:c:macro:`PyBUF_FORMAT` must be \|'d to any of the flags except :c:macro:`PyBUF_SIMPLE`, because
269+
the latter already implies format ``B`` (unsigned bytes). :c:macro:`!PyBUF_FORMAT` cannot be
270+
used on its own.
270271

271272

272273
shape, strides, suboffsets

Doc/library/dis.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,8 @@ operation is being performed, so the intermediate analysis object isn't useful:
312312
.. versionchanged:: 3.14
313313
Added the *show_positions* parameter.
314314

315-
.. function:: disassemble(code, lasti=-1, *, file=None, show_caches=False, adaptive=False)
315+
.. function:: disassemble(code, lasti=-1, *, file=None, show_caches=False,\
316+
adaptive=False, show_offsets=False, show_positions=False)
316317
disco(code, lasti=-1, *, file=None, show_caches=False, adaptive=False,\
317318
show_offsets=False, show_positions=False)
318319

Doc/library/tomllib.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
--------------
1515

16-
This module provides an interface for parsing TOML (Tom's Obvious Minimal
16+
This module provides an interface for parsing TOML 1.0.0 (Tom's Obvious Minimal
1717
Language, `https://toml.io <https://toml.io/en/>`_). This module does not
1818
support writing TOML.
1919

Doc/whatsnew/3.14.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@ Summary -- Release highlights
7070
New Features
7171
============
7272

73+
Improved Error Messages
74+
-----------------------
75+
76+
* When unpacking assignment fails due to incorrect number of variables, the
77+
error message prints the received number of values in more cases than before.
78+
(Contributed by Tushar Sadhwani in :gh:`122239`.)
79+
80+
.. code-block:: pycon
81+
82+
>>> x, y, z = 1, 2, 3, 4
83+
Traceback (most recent call last):
84+
File "<stdin>", line 1, in <module>
85+
x, y, z = 1, 2, 3, 4
86+
^^^^^^^
87+
ValueError: too many values to unpack (expected 3, got 4)
7388
7489
7590
Other Language Changes

Lib/test/test_unpack.py

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
>>> a == 4 and b == 5 and c == 6
1919
True
2020
21+
Unpack dict
22+
23+
>>> d = {4: 'four', 5: 'five', 6: 'six'}
24+
>>> a, b, c = d
25+
>>> a == 4 and b == 5 and c == 6
26+
True
27+
2128
Unpack implied tuple
2229
2330
>>> a, b, c = 7, 8, 9
@@ -66,14 +73,14 @@
6673
>>> a, b = t
6774
Traceback (most recent call last):
6875
...
69-
ValueError: too many values to unpack (expected 2)
76+
ValueError: too many values to unpack (expected 2, got 3)
7077
7178
Unpacking tuple of wrong size
7279
7380
>>> a, b = l
7481
Traceback (most recent call last):
7582
...
76-
ValueError: too many values to unpack (expected 2)
83+
ValueError: too many values to unpack (expected 2, got 3)
7784
7885
Unpacking sequence too short
7986
@@ -140,8 +147,52 @@
140147
>>> () = [42]
141148
Traceback (most recent call last):
142149
...
143-
ValueError: too many values to unpack (expected 0)
150+
ValueError: too many values to unpack (expected 0, got 1)
151+
152+
Unpacking a larger iterable should raise ValuleError, but it
153+
should not entirely consume the iterable
144154
155+
>>> it = iter(range(100))
156+
>>> x, y, z = it
157+
Traceback (most recent call last):
158+
...
159+
ValueError: too many values to unpack (expected 3)
160+
>>> next(it)
161+
4
162+
163+
Unpacking unbalanced dict
164+
165+
>>> d = {4: 'four', 5: 'five', 6: 'six', 7: 'seven'}
166+
>>> a, b, c = d
167+
Traceback (most recent call last):
168+
...
169+
ValueError: too many values to unpack (expected 3, got 4)
170+
171+
Ensure that custom `__len__()` is NOT called when showing the error message
172+
173+
>>> class LengthTooLong:
174+
... def __len__(self):
175+
... return 5
176+
... def __getitem__(self, i):
177+
... return i*2
178+
...
179+
>>> x, y, z = LengthTooLong()
180+
Traceback (most recent call last):
181+
...
182+
ValueError: too many values to unpack (expected 3)
183+
184+
For evil cases like these as well, no actual count to be shown
185+
186+
>>> class BadLength:
187+
... def __len__(self):
188+
... return 1
189+
... def __getitem__(self, i):
190+
... return i*2
191+
...
192+
>>> x, y, z = BadLength()
193+
Traceback (most recent call last):
194+
...
195+
ValueError: too many values to unpack (expected 3)
145196
"""
146197

147198
__test__ = {'doctests' : doctests}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
When a :class:`list`, :class:`tuple` or :class:`dict`
2+
with too many elements is unpacked, show the actual
3+
length in the error message.

Python/ceval.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,6 +2148,17 @@ _PyEval_UnpackIterableStackRef(PyThreadState *tstate, _PyStackRef v_stackref,
21482148
return 1;
21492149
}
21502150
Py_DECREF(w);
2151+
2152+
if (PyList_CheckExact(v) || PyTuple_CheckExact(v)
2153+
|| PyDict_CheckExact(v)) {
2154+
ll = PyDict_CheckExact(v) ? PyDict_Size(v) : Py_SIZE(v);
2155+
if (ll > argcnt) {
2156+
_PyErr_Format(tstate, PyExc_ValueError,
2157+
"too many values to unpack (expected %d, got %zd)",
2158+
argcnt, ll);
2159+
goto Error;
2160+
}
2161+
}
21512162
_PyErr_Format(tstate, PyExc_ValueError,
21522163
"too many values to unpack (expected %d)",
21532164
argcnt);

0 commit comments

Comments
 (0)