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

This commit is contained in:
Serhiy Storchaka 2017-12-26 12:30:41 +02:00 committed by GitHub
parent a8f4e15f3d
commit 13a6c098c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 16 additions and 6 deletions

View File

@ -866,7 +866,7 @@ class TestCase(unittest.TestCase):
self.assertNotEqual(Point3D(1, 2, 3), (1, 2, 3)) self.assertNotEqual(Point3D(1, 2, 3), (1, 2, 3))
# Make sure we can't unpack # 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) x, y, z = Point3D(4, 5, 6)
# Maka sure another class with the same field names isn't # Maka sure another class with the same field names isn't

View File

@ -55,7 +55,7 @@ Unpacking non-sequence
>>> a, b, c = 7 >>> a, b, c = 7
Traceback (most recent call last): Traceback (most recent call last):
... ...
TypeError: 'int' object is not iterable TypeError: cannot unpack non-iterable int object
Unpacking tuple of wrong size Unpacking tuple of wrong size
@ -129,7 +129,7 @@ Unpacking non-iterables should raise TypeError
>>> () = 42 >>> () = 42
Traceback (most recent call last): 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 Unpacking to an empty iterable should raise ValueError

View File

@ -263,7 +263,7 @@ Unpacking non-sequence
>>> a, *b = 7 >>> a, *b = 7
Traceback (most recent call last): Traceback (most recent call last):
... ...
TypeError: 'int' object is not iterable TypeError: cannot unpack non-iterable int object
Unpacking sequence too short Unpacking sequence too short

View File

@ -0,0 +1,2 @@
The error message of a TypeError raised when unpack non-iterable is now more
specific.

View File

@ -4137,8 +4137,16 @@ unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
assert(v != NULL); assert(v != NULL);
it = PyObject_GetIter(v); it = PyObject_GetIter(v);
if (it == NULL) if (it == NULL) {
goto Error; 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++) { for (; i < argcnt; i++) {
w = PyIter_Next(it); w = PyIter_Next(it);