]> granicus.if.org Git - python/commitdiff
Fix for bug 1705170 - contextmanager swallowing StopIteration (2.5 backport candidate)
authorNick Coghlan <ncoghlan@gmail.com>
Fri, 2 Nov 2007 10:09:12 +0000 (10:09 +0000)
committerNick Coghlan <ncoghlan@gmail.com>
Fri, 2 Nov 2007 10:09:12 +0000 (10:09 +0000)
Lib/contextlib.py
Lib/test/test_with.py

index 4f83ef6f32fa38480e7ca27fed80b45e0abe8c1a..dbd1c5744d7a3484bcbf833be465cb42873aba54 100644 (file)
@@ -25,6 +25,10 @@ class GeneratorContextManager(object):
             else:
                 raise RuntimeError("generator didn't stop")
         else:
+            if value is None:
+                # Need to force instantiation so we can reliably
+                # tell if we get the same exception back
+                value = type()
             try:
                 self.gen.throw(type, value, traceback)
                 raise RuntimeError("generator didn't stop after throw()")
index 57505085dd3e5a6d4bdab8924b4ea0ffd54e0df7..8242c912c526d268d6c20eee1a68bf0d6af34654 100644 (file)
@@ -440,6 +440,7 @@ class ExceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin):
         self.assertAfterWithGeneratorInvariantsNoError(self.bar)
 
     def testRaisedStopIteration1(self):
+        # From bug 1462485
         @contextmanager
         def cm():
             yield
@@ -451,6 +452,7 @@ class ExceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin):
         self.assertRaises(StopIteration, shouldThrow)
 
     def testRaisedStopIteration2(self):
+        # From bug 1462485
         class cm(object):
             def __enter__(self):
                 pass
@@ -463,7 +465,21 @@ class ExceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin):
 
         self.assertRaises(StopIteration, shouldThrow)
 
+    def testRaisedStopIteration3(self):
+        # Another variant where the exception hasn't been instantiated
+        # From bug 1705170
+        @contextmanager
+        def cm():
+            yield
+
+        def shouldThrow():
+            with cm():
+                raise iter([]).next()
+
+        self.assertRaises(StopIteration, shouldThrow)
+
     def testRaisedGeneratorExit1(self):
+        # From bug 1462485
         @contextmanager
         def cm():
             yield
@@ -475,6 +491,7 @@ class ExceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin):
         self.assertRaises(GeneratorExit, shouldThrow)
 
     def testRaisedGeneratorExit2(self):
+        # From bug 1462485
         class cm (object):
             def __enter__(self):
                 pass