bpo-37976: Prevent shadowing of TypeError in zip() (GH-15592)

This commit is contained in:
Sergey Fedoseev 2019-08-30 09:25:48 +05:00 committed by Raymond Hettinger
parent 496058f599
commit 6a650aaf77
4 changed files with 24 additions and 8 deletions

View File

@ -1477,6 +1477,18 @@ class BuiltinTest(unittest.TestCase):
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

View File

@ -971,6 +971,18 @@ class TestBasicOps(unittest.TestCase):
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:

View File

@ -4442,10 +4442,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;
}

View File

@ -2548,10 +2548,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;
}