Skip to content

Commit 6a650aa

Browse files
sir-sigurdrhettinger
authored andcommitted
bpo-37976: Prevent shadowing of TypeError in zip() (GH-15592)
1 parent 496058f commit 6a650aa

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
@@ -4442,10 +4442,6 @@ zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
44424442
PyObject *item = PyTuple_GET_ITEM(args, i);
44434443
PyObject *it = PyObject_GetIter(item);
44444444
if (it == NULL) {
4445-
if (PyErr_ExceptionMatches(PyExc_TypeError))
4446-
PyErr_Format(PyExc_TypeError,
4447-
"zip_longest argument #%zd must support iteration",
4448-
i+1);
44494445
Py_DECREF(ittuple);
44504446
return NULL;
44514447
}

Python/bltinmodule.c

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

0 commit comments

Comments
 (0)