]> granicus.if.org Git - python/commitdiff
bpo-32259: Make a TypeError message when unpack non-iterable more specific. (#4903)
authorSerhiy Storchaka <storchaka@gmail.com>
Tue, 26 Dec 2017 10:30:41 +0000 (12:30 +0200)
committerGitHub <noreply@github.com>
Tue, 26 Dec 2017 10:30:41 +0000 (12:30 +0200)
Lib/test/test_dataclasses.py
Lib/test/test_unpack.py
Lib/test/test_unpack_ex.py
Misc/NEWS.d/next/Core and Builtins/2017-12-16-14-30-21.bpo-32259.GoOJiX.rst [new file with mode: 0644]
Python/ceval.c

index 18ca202ca74afea2950f42a88e987a73cbd2b7e9..7fbea76ccd8ea6de627201a632a9c2cb0931a0fb 100755 (executable)
@@ -866,7 +866,7 @@ class TestCase(unittest.TestCase):
         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
index 3fcb18fb43af301bec022c4043fd732ef7fe7704..1c0c523d6858366bdefdbe843fecd5f20c7e3184 100644 (file)
@@ -55,7 +55,7 @@ Unpacking non-sequence
     >>> 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 @@ Unpacking non-iterables should raise TypeError
     >>> () = 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
 
index 6be8f551fcc7ff3230f01f0c5e7376931ce3e1cb..45cf051f1ec1a9da8c673f398e1b841170657cb7 100644 (file)
@@ -263,7 +263,7 @@ Unpacking non-sequence
     >>> 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 (file)
index 0000000..1129c75
--- /dev/null
@@ -0,0 +1,2 @@
+The error message of a TypeError raised when unpack non-iterable is now more
+specific.
index 287f1df26b5043a4e4462cfd30c181200681c16c..9276755f0d16cb6bcf437ea6a05c682a63a30256 100644 (file)
@@ -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);