Skip to content

Commit 27f4186

Browse files
miss-islingtonsir-sigurd
authored andcommitted
bpo-37976: Prevent shadowing of TypeError in zip() (GH-15592) (GH-15608)
(cherry picked from commit 6a650aa) Co-authored-by: Sergey Fedoseev <[email protected]>
1 parent c19d6bc commit 27f4186

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

Lib/test/test_builtin.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,18 @@ def test_zip_pickle(self):
14771477
z1 = zip(a, b)
14781478
self.check_iter_pickle(z1, t, proto)
14791479

1480+
def test_zip_bad_iterable(self):
1481+
exception = TypeError()
1482+
1483+
class BadIterable:
1484+
def __iter__(self):
1485+
raise exception
1486+
1487+
with self.assertRaises(TypeError) as cm:
1488+
zip(BadIterable())
1489+
1490+
self.assertIs(cm.exception, exception)
1491+
14801492
def test_format(self):
14811493
# Test the basic machinery of the format() builtin. Don't test
14821494
# the specifics of the various formatters

Lib/test/test_itertools.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,18 @@ def test_zip_longest_pickling(self):
971971
self.pickletest(proto, zip_longest("abc", "defgh", fillvalue=1))
972972
self.pickletest(proto, zip_longest("", "defgh"))
973973

974+
def test_zip_longest_bad_iterable(self):
975+
exception = TypeError()
976+
977+
class BadIterable:
978+
def __iter__(self):
979+
raise exception
980+
981+
with self.assertRaises(TypeError) as cm:
982+
zip_longest(BadIterable())
983+
984+
self.assertIs(cm.exception, exception)
985+
974986
def test_bug_7244(self):
975987

976988
class Repeater:

Modules/itertoolsmodule.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4434,10 +4434,6 @@ zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
44344434
PyObject *item = PyTuple_GET_ITEM(args, i);
44354435
PyObject *it = PyObject_GetIter(item);
44364436
if (it == NULL) {
4437-
if (PyErr_ExceptionMatches(PyExc_TypeError))
4438-
PyErr_Format(PyExc_TypeError,
4439-
"zip_longest argument #%zd must support iteration",
4440-
i+1);
44414437
Py_DECREF(ittuple);
44424438
return NULL;
44434439
}

Python/bltinmodule.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2549,10 +2549,6 @@ zip_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
25492549
PyObject *item = PyTuple_GET_ITEM(args, i);
25502550
PyObject *it = PyObject_GetIter(item);
25512551
if (it == NULL) {
2552-
if (PyErr_ExceptionMatches(PyExc_TypeError))
2553-
PyErr_Format(PyExc_TypeError,
2554-
"zip argument #%zd must support iteration",
2555-
i+1);
25562552
Py_DECREF(ittuple);
25572553
return NULL;
25582554
}

0 commit comments

Comments
 (0)