[Python-checkins] bpo-32259: Make a TypeError message when unpack non-iterable more specific. (#4903)

Serhiy Storchaka webhook-mailer at python.org
Tue Dec 26 05:30:43 EST 2017


https://github.com/python/cpython/commit/13a6c098c215921e35004f9d3a9b70f601e56500
commit: 13a6c098c215921e35004f9d3a9b70f601e56500
branch: master
author: Serhiy Storchaka <storchaka at gmail.com>
committer: GitHub <noreply at github.com>
date: 2017-12-26T12:30:41+02:00
summary:

bpo-32259: Make a TypeError message when unpack non-iterable more specific. (#4903)

files:
A Misc/NEWS.d/next/Core and Builtins/2017-12-16-14-30-21.bpo-32259.GoOJiX.rst
M Lib/test/test_dataclasses.py
M Lib/test/test_unpack.py
M Lib/test/test_unpack_ex.py
M Python/ceval.c

diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py
index 18ca202ca74..7fbea76ccd8 100755
--- a/Lib/test/test_dataclasses.py
+++ b/Lib/test/test_dataclasses.py
@@ -866,7 +866,7 @@ class Date:
         self.assertNotEqual(Point3D(1, 2, 3), (1, 2, 3))
 
         # Make sure we can't unpack
-        with self.assertRaisesRegex(TypeError, 'is not iterable'):
+        with self.assertRaisesRegex(TypeError, 'unpack'):
             x, y, z = Point3D(4, 5, 6)
 
         # Maka sure another class with the same field names isn't
diff --git a/Lib/test/test_unpack.py b/Lib/test/test_unpack.py
index 3fcb18fb43a..1c0c523d685 100644
--- a/Lib/test/test_unpack.py
+++ b/Lib/test/test_unpack.py
@@ -55,7 +55,7 @@
     >>> a, b, c = 7
     Traceback (most recent call last):
       ...
-    TypeError: 'int' object is not iterable
+    TypeError: cannot unpack non-iterable int object
 
 Unpacking tuple of wrong size
 
@@ -129,7 +129,7 @@
     >>> () = 42
     Traceback (most recent call last):
       ...
-    TypeError: 'int' object is not iterable
+    TypeError: cannot unpack non-iterable int object
 
 Unpacking to an empty iterable should raise ValueError
 
diff --git a/Lib/test/test_unpack_ex.py b/Lib/test/test_unpack_ex.py
index 6be8f551fcc..45cf051f1ec 100644
--- a/Lib/test/test_unpack_ex.py
+++ b/Lib/test/test_unpack_ex.py
@@ -263,7 +263,7 @@
     >>> a, *b = 7
     Traceback (most recent call last):
       ...
-    TypeError: 'int' object is not iterable
+    TypeError: cannot unpack non-iterable int object
 
 Unpacking sequence too short
 
diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-12-16-14-30-21.bpo-32259.GoOJiX.rst b/Misc/NEWS.d/next/Core and Builtins/2017-12-16-14-30-21.bpo-32259.GoOJiX.rst
new file mode 100644
index 00000000000..1129c759f9f
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2017-12-16-14-30-21.bpo-32259.GoOJiX.rst	
@@ -0,0 +1,2 @@
+The error message of a TypeError raised when unpack non-iterable is now more
+specific.
diff --git a/Python/ceval.c b/Python/ceval.c
index 287f1df26b5..9276755f0d1 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -4137,8 +4137,16 @@ unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
     assert(v != NULL);
 
     it = PyObject_GetIter(v);
-    if (it == NULL)
-        goto Error;
+    if (it == NULL) {
+        if (PyErr_ExceptionMatches(PyExc_TypeError) &&
+            v->ob_type->tp_iter == NULL && !PySequence_Check(v))
+        {
+            PyErr_Format(PyExc_TypeError,
+                         "cannot unpack non-iterable %.200s object",
+                         v->ob_type->tp_name);
+        }
+        return 0;
+    }
 
     for (; i < argcnt; i++) {
         w = PyIter_Next(it);



More information about the Python-checkins mailing list