]> granicus.if.org Git - python/commitdiff
Issue #27123: When an exception is raised within the context being
authorGregory P. Smith <greg@krypto.org>
Tue, 14 Jun 2016 16:19:20 +0000 (09:19 -0700)
committerGregory P. Smith <greg@krypto.org>
Tue, 14 Jun 2016 16:19:20 +0000 (09:19 -0700)
managed by a contextlib.ExitStack() and one of the exit stack
generators catches and raises it in a chain, do not re-raise the
original exception when exiting, let the new chained one through.
This avoids the PEP 479 bug described in issue25782.

Lib/contextlib.py
Lib/test/test_contextlib.py
Misc/NEWS

index 5377987ddfe13dcbbbf7ded66acf59756aeaf1ff..d44edd6e198ad94b09d173088f473126287ed8dc 100644 (file)
@@ -82,6 +82,9 @@ class _GeneratorContextManager(ContextDecorator):
                 # raised inside the "with" statement from being suppressed.
                 return exc is not value
             except RuntimeError as exc:
+                # Don't re-raise the passed in exception. (issue27112)
+                if exc is value:
+                    return False
                 # Likewise, avoid suppressing if a StopIteration exception
                 # was passed to throw() and later wrapped into a RuntimeError
                 # (see PEP 479).
index 30a6377bed89222facc99035cda5716b704f16dd..a246c43187f7c0390b654938e4536e9890aab737 100644 (file)
@@ -762,6 +762,34 @@ class TestExitStack(unittest.TestCase):
         stack.push(cm)
         self.assertIs(stack._exit_callbacks[-1], cm)
 
+    def test_dont_reraise_RuntimeError(self):
+        """https://bugs.python.org/issue27122"""
+        class UniqueException(Exception): pass
+
+        @contextmanager
+        def second():
+            try:
+                yield 1
+            except Exception as exc:
+                raise UniqueException("new exception") from exc
+
+        @contextmanager
+        def first():
+            try:
+                yield 1
+            except Exception as exc:
+                raise exc
+
+        # The RuntimeError should be caught by second()'s exception
+        # handler which chain raised a new UniqueException.
+        with self.assertRaises(UniqueException) as err_ctx:
+            with ExitStack() as es_ctx:
+                es_ctx.enter_context(second())
+                es_ctx.enter_context(first())
+                raise RuntimeError("please no infinite loop.")
+
+        self.assertEqual(err_ctx.exception.args[0], "new exception")
+
 
 class TestRedirectStream:
 
index 7940cdcb6b82393d2c997b22a8123bcad21e0cb1..c85f6d0da8e5d62d82ad90f1ed5c502ee4a1bdea 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -13,6 +13,12 @@ Core and Builtins
 Library
 -------
 
+- Issue #27123: When an exception is raised within the context being managed
+  by a contextlib.ExitStack() and one of the exit stack generators
+  catches and raises it in a chain, do not re-raise the original exception
+  when exiting, let the new chained one through.  This avoids the PEP 479
+  bug described in issue25782.
+
 - Issue #27278: Fix os.urandom() implementation using getrandom() on Linux.
   Truncate size to INT_MAX and loop until we collected enough random bytes,
   instead of casting a directly Py_ssize_t to int.