[Python-checkins] r76004 - in python/branches/release26-maint: Lib/test/test_itertools.py Misc/NEWS Modules/itertoolsmodule.c

raymond.hettinger python-checkins at python.org
Sun Nov 1 09:53:21 CET 2009


Author: raymond.hettinger
Date: Sun Nov  1 09:53:21 2009
New Revision: 76004

Log:
Issue 7244: fix exception handling in itertools.izip_longest().

Modified:
   python/branches/release26-maint/Lib/test/test_itertools.py
   python/branches/release26-maint/Misc/NEWS
   python/branches/release26-maint/Modules/itertoolsmodule.c

Modified: python/branches/release26-maint/Lib/test/test_itertools.py
==============================================================================
--- python/branches/release26-maint/Lib/test/test_itertools.py	(original)
+++ python/branches/release26-maint/Lib/test/test_itertools.py	Sun Nov  1 09:53:21 2009
@@ -398,6 +398,42 @@
         ids = map(id, list(izip_longest('abc', 'def')))
         self.assertEqual(len(dict.fromkeys(ids)), len(ids))
 
+    def test_bug_7244(self):
+
+        class Repeater(object):
+            # this class is similar to itertools.repeat
+            def __init__(self, o, t, e):
+                self.o = o
+                self.t = int(t)
+                self.e = e
+            def __iter__(self): # its iterator is itself
+                return self
+            def next(self):
+                if self.t > 0:
+                    self.t -= 1
+                    return self.o
+                else:
+                    raise self.e
+
+        # Formerly this code in would fail in debug mode
+        # with Undetected Error and Stop Iteration
+        r1 = Repeater(1, 3, StopIteration)
+        r2 = Repeater(2, 4, StopIteration)
+        def run(r1, r2):
+            result = []
+            for i, j in izip_longest(r1, r2, fillvalue=0):
+                print(i, j)
+                result.append((i, j))
+            return result
+        self.assertEqual(run(r1, r2), [(1,2), (1,2), (1,2), (0,2)])
+
+        # Formerly, the RuntimeError would be lost
+        # and StopIteration would stop as expected
+        r1 = Repeater(1, 3, RuntimeError)
+        r2 = Repeater(2, 4, StopIteration)
+        mylist = lambda it: [v for v in it]
+        self.assertRaises(RuntimeError, mylist, izip_longest(r1, r2, fillvalue=0))
+
     def test_product(self):
         for args, result in [
             ([], [()]),                     # zero iterables
@@ -687,6 +723,7 @@
             self.assertRaises(StopIteration, f(lambda x:x, []).next)
             self.assertRaises(StopIteration, f(lambda x:x, StopNow()).next)
 
+
 class TestExamples(unittest.TestCase):
 
     def test_chain(self):

Modified: python/branches/release26-maint/Misc/NEWS
==============================================================================
--- python/branches/release26-maint/Misc/NEWS	(original)
+++ python/branches/release26-maint/Misc/NEWS	Sun Nov  1 09:53:21 2009
@@ -24,6 +24,9 @@
 Library
 -------
 
+- Issue #7244: itertools.izip_longest() no longer ignores exceptions
+  raised during the formation of an output tuple.
+
 - Issue #7233: Fix a number of two-argument Decimal methods to make
   sure that they accept an int or long as the second argument.  Also
   fix buggy handling of large arguments (those with coefficient longer

Modified: python/branches/release26-maint/Modules/itertoolsmodule.c
==============================================================================
--- python/branches/release26-maint/Modules/itertoolsmodule.c	(original)
+++ python/branches/release26-maint/Modules/itertoolsmodule.c	Sun Nov  1 09:53:21 2009
@@ -3412,10 +3412,11 @@
                                 item = lz->fillvalue;
                         } else {
                                 assert(PyIter_Check(it));
-                                item = (*Py_TYPE(it)->tp_iternext)(it);
+                                item = PyIter_Next(it);
                                 if (item == NULL) {
-                                        lz->numactive -= 1;      
-                                        if (lz->numactive == 0) {
+                                        lz->numactive -= 1;
+                                        if (lz->numactive == 0 || PyErr_Occurred()) {
+												lz->numactive = 0;
                                                 Py_DECREF(result);
                                                 return NULL;
                                         } else {
@@ -3441,10 +3442,11 @@
                                 item = lz->fillvalue;
                         } else {
                                 assert(PyIter_Check(it));
-                                item = (*Py_TYPE(it)->tp_iternext)(it);
+                                item = PyIter_Next(it);
                                 if (item == NULL) {
                                         lz->numactive -= 1;      
-                                        if (lz->numactive == 0) {
+                                        if (lz->numactive == 0 || PyErr_Occurred()) {
+												lz->numactive = 0;
                                                 Py_DECREF(result);
                                                 return NULL;
                                         } else {


More information about the Python-checkins mailing list