]> granicus.if.org Git - python/commitdiff
#23949: Improve tuple unpacking error messages.
authorR David Murray <rdmurray@bitdance.com>
Wed, 15 Apr 2015 21:08:45 +0000 (17:08 -0400)
committerR David Murray <rdmurray@bitdance.com>
Wed, 15 Apr 2015 21:08:45 +0000 (17:08 -0400)
Patch by Arnon Yaari.

Lib/test/test_unpack.py
Lib/test/test_unpack_ex.py
Python/ceval.c

index b1c483d4d550ad47c54eeb93931a7a83ca044d62..d1ccb38937b7422178e362894bc61875d3d6ba8a 100644 (file)
@@ -76,7 +76,7 @@ Unpacking sequence too short
     >>> a, b, c, d = Seq()
     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
 
index ae2dcbd985ed5a93f672ac58023ae6e694da7365..54666b0cf031365692d23f774ae4a7a2f6d148aa 100644 (file)
@@ -85,7 +85,14 @@ Unpacking sequence too short
     >>> a, *b, c, d, e = Seq()
     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
 error
index d68cdc6292e95e8f57d80888f4cec3380bca6957..2f3d3ad8b8f6bf7121de9a736099a341a5e34383 100644 (file)
@@ -3825,9 +3825,17 @@ unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
         if (w == NULL) {
             /* Iterator done, via error or exhaustion. */
             if (!PyErr_Occurred()) {
-                PyErr_Format(PyExc_ValueError,
-                    "need more than %d value%s to unpack",
-                    i, i == 1 ? "" : "s");
+                if (argcntafter == -1) {
+                    PyErr_Format(PyExc_ValueError,
+                        "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;
         }
@@ -3844,8 +3852,9 @@ unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
             return 1;
         }
         Py_DECREF(w);
-        PyErr_Format(PyExc_ValueError, "too many values to unpack "
-                     "(expected %d)", argcnt);
+        PyErr_Format(PyExc_ValueError,
+            "too many values to unpack (expected %d)",
+            argcnt);
         goto Error;
     }
 
@@ -3857,8 +3866,9 @@ unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
 
     ll = PyList_GET_SIZE(l);
     if (ll < argcntafter) {
-        PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
-                     argcnt + ll);
+        PyErr_Format(PyExc_ValueError,
+            "not enough values to unpack (expected at least %d, got %zd)",
+            argcnt + argcntafter, argcnt + ll);
         goto Error;
     }