[Python-checkins] cpython: #23949: Improve tuple unpacking error messages.

r.david.murray python-checkins at python.org
Wed Apr 15 23:09:10 CEST 2015


https://hg.python.org/cpython/rev/e55cc0834e9c
changeset:   95680:e55cc0834e9c
user:        R David Murray <rdmurray at bitdance.com>
date:        Wed Apr 15 17:08:45 2015 -0400
summary:
  #23949: Improve tuple unpacking error messages.

Patch by Arnon Yaari.

files:
  Lib/test/test_unpack.py    |   2 +-
  Lib/test/test_unpack_ex.py |   9 ++++++++-
  Python/ceval.c             |  24 +++++++++++++++++-------
  3 files changed, 26 insertions(+), 9 deletions(-)


diff --git a/Lib/test/test_unpack.py b/Lib/test/test_unpack.py
--- a/Lib/test/test_unpack.py
+++ b/Lib/test/test_unpack.py
@@ -76,7 +76,7 @@
     >>> 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
 
diff --git a/Lib/test/test_unpack_ex.py b/Lib/test/test_unpack_ex.py
--- a/Lib/test/test_unpack_ex.py
+++ b/Lib/test/test_unpack_ex.py
@@ -85,7 +85,14 @@
     >>> 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
diff --git a/Python/ceval.c b/Python/ceval.c
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -3825,9 +3825,17 @@
         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 @@
             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 @@
 
     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;
     }
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list