[Python-checkins] bpo-37976: Prevent shadowing of TypeError in zip() (GH-15592)

Raymond Hettinger webhook-mailer at python.org
Fri Aug 30 00:25:52 EDT 2019


https://github.com/python/cpython/commit/6a650aaf7735e30636db2721247f317064c2cfd4
commit: 6a650aaf7735e30636db2721247f317064c2cfd4
branch: master
author: Sergey Fedoseev <fedoseev.sergey at gmail.com>
committer: Raymond Hettinger <rhettinger at users.noreply.github.com>
date: 2019-08-29T21:25:48-07:00
summary:

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

files:
M Lib/test/test_builtin.py
M Lib/test/test_itertools.py
M Modules/itertoolsmodule.c
M Python/bltinmodule.c

diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index 61155799c44a..1100c49e9b88 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -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
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py
index 573739fde14c..98b8c8373189 100644
--- a/Lib/test/test_itertools.py
+++ b/Lib/test/test_itertools.py
@@ -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:
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
index 101addcfd3a9..ab473e29fbd5 100644
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -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;
         }
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 7f187eacd169..63e58128651a 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -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;
         }



More information about the Python-checkins mailing list