summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_itertools.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2009-11-01 21:02:38 (GMT)
committerRaymond Hettinger <python@rcn.com>2009-11-01 21:02:38 (GMT)
commita9311a3c50ff7f8469c61f6e3bfac87358e4ac5a (patch)
tree82c88d4cb70cee113fd4f5340bb9295ad56cffee /Lib/test/test_itertools.py
parent6c4a725a37611fa60efe36f7e85ba8fbeab6dd15 (diff)
downloadcpython-a9311a3c50ff7f8469c61f6e3bfac87358e4ac5a.zip
cpython-a9311a3c50ff7f8469c61f6e3bfac87358e4ac5a.tar.gz
cpython-a9311a3c50ff7f8469c61f6e3bfac87358e4ac5a.tar.bz2
Fix exception handling in itertools.izip_longest().
Diffstat (limited to 'Lib/test/test_itertools.py')
-rw-r--r--Lib/test/test_itertools.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py
index d917262..2f449da 100644
--- a/Lib/test/test_itertools.py
+++ b/Lib/test/test_itertools.py
@@ -581,6 +581,46 @@ class TestBasicOps(unittest.TestCase):
ids = list(map(id, list(zip_longest('abc', 'def'))))
self.assertEqual(len(dict.fromkeys(ids)), len(ids))
+ def test_bug_7244(self):
+
+ class Repeater:
+ # 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 zip_longest(r1, r2, fillvalue=0):
+ with support.captured_output('stdout'):
+ 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)
+ it = zip_longest(r1, r2, fillvalue=0)
+ self.assertEqual(next(it), (1, 2))
+ self.assertEqual(next(it), (1, 2))
+ self.assertEqual(next(it), (1, 2))
+ self.assertRaises(RuntimeError, next, it)
+
def test_product(self):
for args, result in [
([], [()]), # zero iterables