]> granicus.if.org Git - python/commitdiff
bpo-37976: Prevent shadowing of TypeError in zip() (GH-15592) (GH-15608)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 30 Aug 2019 06:23:17 +0000 (23:23 -0700)
committerRaymond Hettinger <rhettinger@users.noreply.github.com>
Fri, 30 Aug 2019 06:23:17 +0000 (23:23 -0700)
(cherry picked from commit 6a650aaf7735e30636db2721247f317064c2cfd4)

Co-authored-by: Sergey Fedoseev <fedoseev.sergey@gmail.com>
Lib/test/test_builtin.py
Lib/test/test_itertools.py
Modules/itertoolsmodule.c
Python/bltinmodule.c

index 61155799c44a92ac6360403c45aa3e9b538d85f5..1100c49e9b8831d2b5cd253d31534942749b87b9 100644 (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
index 573739fde14c93fba0e5977efaa09531df52cc09..98b8c83731899a97e554e09773303f5c21357707 100644 (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:
index 00e3cbb31b53b89fe3b847419902a6cd7cac7d7f..22c04f29353e26d0fe43b22c33bbcf7cb0aebe34 100644 (file)
@@ -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;
         }
index 62502218ef4cdbc458102ce2cecdb5ef1d2ca248..b85bfb2b91e39bac260664ec7c8ad73e66c4de65 100644 (file)
@@ -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;
         }