From 6e7716ca11c1771d26cd6dc137ce6b4dca3bbe57 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Mon, 16 Mar 2020 23:42:14 -0600 Subject: [PATCH] bpo-39816: More descriptive error msg than "too many values to unpack" --- Lib/test/test_unpack.py | 12 ++++++------ Lib/test/test_unpack_ex.py | 4 ++-- Python/ceval.c | 21 +++++++++++++-------- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/Lib/test/test_unpack.py b/Lib/test/test_unpack.py index 1c0c523d685836..ceab16fffb41f7 100644 --- a/Lib/test/test_unpack.py +++ b/Lib/test/test_unpack.py @@ -62,28 +62,28 @@ >>> a, b = t Traceback (most recent call last): ... - ValueError: too many values to unpack (expected 2) + ValueError: too many values to unpack (expected 2) from object of type 'tuple' -Unpacking tuple of wrong size +Unpacking list of wrong size >>> a, b = l Traceback (most recent call last): ... - ValueError: too many values to unpack (expected 2) + ValueError: too many values to unpack (expected 2) from object of type 'list' Unpacking sequence too short >>> a, b, c, d = Seq() Traceback (most recent call last): ... - ValueError: not enough values to unpack (expected 4, got 3) + ValueError: not enough values to unpack (expected 4, got 3) from object of type 'Seq' Unpacking sequence too long >>> a, b = Seq() Traceback (most recent call last): ... - ValueError: too many values to unpack (expected 2) + ValueError: too many values to unpack (expected 2) from object of type 'Seq' Unpacking a sequence where the test for too long raises a different kind of error @@ -136,7 +136,7 @@ >>> () = [42] Traceback (most recent call last): ... - ValueError: too many values to unpack (expected 0) + ValueError: too many values to unpack (expected 0) from object of type 'list' """ diff --git a/Lib/test/test_unpack_ex.py b/Lib/test/test_unpack_ex.py index 46f70c2b98c709..a5923d70a83663 100644 --- a/Lib/test/test_unpack_ex.py +++ b/Lib/test/test_unpack_ex.py @@ -270,14 +270,14 @@ >>> a, *b, c, d, e = Seq() Traceback (most recent call last): ... - ValueError: not enough values to unpack (expected at least 4, got 3) + ValueError: not enough values to unpack (expected at least 4, got 3) from object of type 'Seq' 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) + ValueError: not enough values to unpack (expected at least 4, got 3) from object of type 'Seq' 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 index b359fb079841cd..531a094759cec6 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -4387,14 +4387,17 @@ unpack_iterable(PyThreadState *tstate, PyObject *v, if (argcntafter == -1) { _PyErr_Format(tstate, PyExc_ValueError, "not enough values to unpack " - "(expected %d, got %d)", - argcnt, i); + "(expected %d, got %d) from object of type " + "'%.200s'", + argcnt, i, Py_TYPE(v)->tp_name); } else { _PyErr_Format(tstate, PyExc_ValueError, "not enough values to unpack " - "(expected at least %d, got %d)", - argcnt + argcntafter, i); + "(expected at least %d, got %d) from " + "object of type '%.200s'", + argcnt + argcntafter, i, + Py_TYPE(v)->tp_name); } } goto Error; @@ -4413,8 +4416,9 @@ unpack_iterable(PyThreadState *tstate, PyObject *v, } Py_DECREF(w); _PyErr_Format(tstate, PyExc_ValueError, - "too many values to unpack (expected %d)", - argcnt); + "too many values to unpack (expected %d) from object " + "of type '%.200s'", + argcnt, Py_TYPE(v)->tp_name); goto Error; } @@ -4427,8 +4431,9 @@ unpack_iterable(PyThreadState *tstate, PyObject *v, ll = PyList_GET_SIZE(l); if (ll < argcntafter) { _PyErr_Format(tstate, PyExc_ValueError, - "not enough values to unpack (expected at least %d, got %zd)", - argcnt + argcntafter, argcnt + ll); + "not enough values to unpack (expected at least %d, got %zd) " + "from object of type '%.200s'", + argcnt + argcntafter, argcnt + ll, Py_TYPE(v)->tp_name); goto Error; }