Skip to content

[3.8] bpo-37976: Prevent shadowing of TypeError in zip() (GH-15592) #15607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
12 changes: 12 additions & 0 deletions Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,18 @@ def test_zip_pickle(self):
z1 = zip(a, b)
self.check_iter_pickle(z1, t, proto)

def test_zip_bad_iterable(self):
exception = TypeError()

class BadIterable:
def __iter__(self):
raise exception

with self.assertRaises(TypeError) as cm:
zip(BadIterable())

self.assertIs(cm.exception, exception)

def test_format(self):
# Test the basic machinery of the format() builtin. Don't test
# the specifics of the various formatters
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_itertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,18 @@ def test_zip_longest_pickling(self):
self.pickletest(proto, zip_longest("abc", "defgh", fillvalue=1))
self.pickletest(proto, zip_longest("", "defgh"))

def test_zip_longest_bad_iterable(self):
exception = TypeError()

class BadIterable:
def __iter__(self):
raise exception

with self.assertRaises(TypeError) as cm:
zip_longest(BadIterable())

self.assertIs(cm.exception, exception)

def test_bug_7244(self):

class Repeater:
Expand Down
4 changes: 0 additions & 4 deletions Modules/itertoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -4434,10 +4434,6 @@ zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
PyObject *item = PyTuple_GET_ITEM(args, i);
PyObject *it = PyObject_GetIter(item);
if (it == NULL) {
if (PyErr_ExceptionMatches(PyExc_TypeError))
PyErr_Format(PyExc_TypeError,
"zip_longest argument #%zd must support iteration",
i+1);
Py_DECREF(ittuple);
return NULL;
}
Expand Down
4 changes: 0 additions & 4 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2549,10 +2549,6 @@ zip_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
PyObject *item = PyTuple_GET_ITEM(args, i);
PyObject *it = PyObject_GetIter(item);
if (it == NULL) {
if (PyErr_ExceptionMatches(PyExc_TypeError))
PyErr_Format(PyExc_TypeError,
"zip argument #%zd must support iteration",
i+1);
Py_DECREF(ittuple);
return NULL;
}
Expand Down