Commit 4171bbe6 authored by R David Murray's avatar R David Murray

#23949: Improve tuple unpacking error messages.

Patch by Arnon Yaari.
parent 13a6ee0a
...@@ -76,7 +76,7 @@ Unpacking sequence too short ...@@ -76,7 +76,7 @@ Unpacking sequence too short
>>> a, b, c, d = Seq() >>> a, b, c, d = Seq()
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValueError: need more than 3 values to unpack ValueError: not enough values to unpack (expected 4, got 3)
Unpacking sequence too long Unpacking sequence too long
......
...@@ -85,7 +85,14 @@ Unpacking sequence too short ...@@ -85,7 +85,14 @@ Unpacking sequence too short
>>> a, *b, c, d, e = Seq() >>> a, *b, c, d, e = Seq()
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValueError: need more than 3 values to unpack ValueError: not enough values to unpack (expected at least 4, got 3)
Unpacking sequence too short and target appears last
>>> a, b, c, d, *e = Seq()
Traceback (most recent call last):
...
ValueError: not enough values to unpack (expected at least 4, got 3)
Unpacking a sequence where the test for too long raises a different kind of Unpacking a sequence where the test for too long raises a different kind of
error error
......
...@@ -3825,9 +3825,17 @@ unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp) ...@@ -3825,9 +3825,17 @@ unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
if (w == NULL) { if (w == NULL) {
/* Iterator done, via error or exhaustion. */ /* Iterator done, via error or exhaustion. */
if (!PyErr_Occurred()) { if (!PyErr_Occurred()) {
PyErr_Format(PyExc_ValueError, if (argcntafter == -1) {
"need more than %d value%s to unpack", PyErr_Format(PyExc_ValueError,
i, i == 1 ? "" : "s"); "not enough values to unpack (expected %d, got %d)",
argcnt, i);
}
else {
PyErr_Format(PyExc_ValueError,
"not enough values to unpack "
"(expected at least %d, got %d)",
argcnt + argcntafter, i);
}
} }
goto Error; goto Error;
} }
...@@ -3844,8 +3852,9 @@ unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp) ...@@ -3844,8 +3852,9 @@ unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
return 1; return 1;
} }
Py_DECREF(w); Py_DECREF(w);
PyErr_Format(PyExc_ValueError, "too many values to unpack " PyErr_Format(PyExc_ValueError,
"(expected %d)", argcnt); "too many values to unpack (expected %d)",
argcnt);
goto Error; goto Error;
} }
...@@ -3857,8 +3866,9 @@ unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp) ...@@ -3857,8 +3866,9 @@ unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
ll = PyList_GET_SIZE(l); ll = PyList_GET_SIZE(l);
if (ll < argcntafter) { if (ll < argcntafter) {
PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack", PyErr_Format(PyExc_ValueError,
argcnt + ll); "not enough values to unpack (expected at least %d, got %zd)",
argcnt + argcntafter, argcnt + ll);
goto Error; goto Error;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment