]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Close #19092: ExitStack now reraises exceptions from __exit__
authorNick Coghlan <ncoghlan@gmail.com>
Tue, 1 Oct 2013 13:24:56 +0000 (23:24 +1000)
committerNick Coghlan <ncoghlan@gmail.com>
Tue, 1 Oct 2013 13:24:56 +0000 (23:24 +1000)
Report and patch by Hrvoje Nikšić

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

index 0b6bf71b08ca08daa5c7763ce2cd0b34a94d5518..f8e026b7e2feec0dc46ef21a5d56db304dc74edb 100644 (file)
@@ -225,6 +225,8 @@ class ExitStack(object):
         return self
 
     def __exit__(self, *exc_details):
+        received_exc = exc_details[0] is not None
+
         # We manipulate the exception state so it behaves as though
         # we were actually nesting multiple with statements
         frame_exc = sys.exc_info()[1]
@@ -239,17 +241,27 @@ class ExitStack(object):
         # Callbacks are invoked in LIFO order to match the behaviour of
         # nested context managers
         suppressed_exc = False
+        pending_raise = False
         while self._exit_callbacks:
             cb = self._exit_callbacks.pop()
             try:
                 if cb(*exc_details):
                     suppressed_exc = True
+                    pending_raise = False
                     exc_details = (None, None, None)
             except:
                 new_exc_details = sys.exc_info()
                 # simulate the stack of exceptions by setting the context
                 _fix_exception_context(new_exc_details[1], exc_details[1])
-                if not self._exit_callbacks:
-                    raise
+                pending_raise = True
                 exc_details = new_exc_details
-        return suppressed_exc
+        if pending_raise:
+            try:
+                # bare "raise exc_details[1]" replaces our carefully
+                # set-up context
+                fixed_ctx = exc_details[1].__context__
+                raise exc_details[1]
+            except BaseException:
+                exc_details[1].__context__ = fixed_ctx
+                raise
+        return received_exc and suppressed_exc
index e52ed91a585c8a68bab1ce08ca265dce3f3a249c..9e45f70f85712caf80606b997fd6f8b327947476 100644 (file)
@@ -573,6 +573,43 @@ class TestExitStack(unittest.TestCase):
         self.assertIsInstance(inner_exc, ValueError)
         self.assertIsInstance(inner_exc.__context__, ZeroDivisionError)
 
+    def test_exit_exception_non_suppressing(self):
+        # http://bugs.python.org/issue19092
+        def raise_exc(exc):
+            raise exc
+
+        def suppress_exc(*exc_details):
+            return True
+
+        try:
+            with ExitStack() as stack:
+                stack.callback(lambda: None)
+                stack.callback(raise_exc, IndexError)
+        except Exception as exc:
+            self.assertIsInstance(exc, IndexError)
+        else:
+            self.fail("Expected IndexError, but no exception was raised")
+
+        try:
+            with ExitStack() as stack:
+                stack.callback(raise_exc, KeyError)
+                stack.push(suppress_exc)
+                stack.callback(raise_exc, IndexError)
+        except Exception as exc:
+            self.assertIsInstance(exc, KeyError)
+        else:
+            self.fail("Expected KeyError, but no exception was raised")
+
+    def test_body_exception_suppress(self):
+        def suppress_exc(*exc_details):
+            return True
+        try:
+            with ExitStack() as stack:
+                stack.push(suppress_exc)
+                1/0
+        except IndexError as exc:
+            self.fail("Expected no exception, got IndexError")
+
     def test_exit_exception_chaining_suppress(self):
         with ExitStack() as stack:
             stack.push(lambda *exc: True)
index 63c126c7cc0564ed9a98de5aa656c27a7c4f4de3..400f5288d16269bb90a63a052efe39c8872e12ec 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -882,7 +882,7 @@ Samuel Nicolary
 Jonathan Niehof
 Gustavo Niemeyer
 Oscar Nierstrasz
-Hrvoje Niksic
+Hrvoje Nikšić
 Gregory Nofi
 Jesse Noller
 Bill Noon
index 7898b9b592c59be6c8eb3bb1b8123438e7c6e050..30e61117fe3969d791814783400c23676fe384cf 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -71,6 +71,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #19092: contextlib.ExitStack now correctly reraises exceptions
+  from the __exit__ callbacks of inner context managers (Patch by Hrvoje
+  Nikšić)
+
 - Issue #12641: Avoid passing "-mno-cygwin" to the mingw32 compiler, except
   when necessary.  Patch by Oscar Benjamin.