Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4171bbe

Browse files
committed
#23949: Improve tuple unpacking error messages.
Patch by Arnon Yaari.
1 parent 13a6ee0 commit 4171bbe

3 files changed

Lines changed: 26 additions & 9 deletions

File tree

Lib/test/test_unpack.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
>>> a, b, c, d = Seq()
7777
Traceback (most recent call last):
7878
...
79-
ValueError: need more than 3 values to unpack
79+
ValueError: not enough values to unpack (expected 4, got 3)
8080
8181
Unpacking sequence too long
8282

Lib/test/test_unpack_ex.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,14 @@
8585
>>> a, *b, c, d, e = Seq()
8686
Traceback (most recent call last):
8787
...
88-
ValueError: need more than 3 values to unpack
88+
ValueError: not enough values to unpack (expected at least 4, got 3)
89+
90+
Unpacking sequence too short and target appears last
91+
92+
>>> a, b, c, d, *e = Seq()
93+
Traceback (most recent call last):
94+
...
95+
ValueError: not enough values to unpack (expected at least 4, got 3)
8996
9097
Unpacking a sequence where the test for too long raises a different kind of
9198
error

Python/ceval.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3825,9 +3825,17 @@ unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
38253825
if (w == NULL) {
38263826
/* Iterator done, via error or exhaustion. */
38273827
if (!PyErr_Occurred()) {
3828-
PyErr_Format(PyExc_ValueError,
3829-
"need more than %d value%s to unpack",
3830-
i, i == 1 ? "" : "s");
3828+
if (argcntafter == -1) {
3829+
PyErr_Format(PyExc_ValueError,
3830+
"not enough values to unpack (expected %d, got %d)",
3831+
argcnt, i);
3832+
}
3833+
else {
3834+
PyErr_Format(PyExc_ValueError,
3835+
"not enough values to unpack "
3836+
"(expected at least %d, got %d)",
3837+
argcnt + argcntafter, i);
3838+
}
38313839
}
38323840
goto Error;
38333841
}
@@ -3844,8 +3852,9 @@ unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
38443852
return 1;
38453853
}
38463854
Py_DECREF(w);
3847-
PyErr_Format(PyExc_ValueError, "too many values to unpack "
3848-
"(expected %d)", argcnt);
3855+
PyErr_Format(PyExc_ValueError,
3856+
"too many values to unpack (expected %d)",
3857+
argcnt);
38493858
goto Error;
38503859
}
38513860

@@ -3857,8 +3866,9 @@ unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
38573866

38583867
ll = PyList_GET_SIZE(l);
38593868
if (ll < argcntafter) {
3860-
PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3861-
argcnt + ll);
3869+
PyErr_Format(PyExc_ValueError,
3870+
"not enough values to unpack (expected at least %d, got %zd)",
3871+
argcnt + argcntafter, argcnt + ll);
38623872
goto Error;
38633873
}
38643874

0 commit comments

Comments
 (0)