Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/test/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_unpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_unpack_ex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The error message of a TypeError raised when unpack non-iterable is now more
specific.
12 changes: 10 additions & 2 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down