summaryrefslogtreecommitdiff
path: root/Lib/contextlib.py
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2012-06-01 00:00:38 +1000
committerNick Coghlan <ncoghlan@gmail.com>2012-06-01 00:00:38 +1000
commit656ad9bc0b7338572c667014e2a3c4902c6f2cb8 (patch)
tree9a9af0c492d61dfdfb4458356e2c14aabba86ae9 /Lib/contextlib.py
parentc671c4c47c97dac98a3cbe3799f0689de5598d4e (diff)
downloadcpython-656ad9bc0b7338572c667014e2a3c4902c6f2cb8.tar.gz
Close #14963: Use an iterative algorithm in contextlib.ExitStack.__exit__ (Patch by Alon Horev)
Diffstat (limited to 'Lib/contextlib.py')
-rw-r--r--Lib/contextlib.py41
1 files changed, 15 insertions, 26 deletions
diff --git a/Lib/contextlib.py b/Lib/contextlib.py
index ead11554cb..f5232b6a84 100644
--- a/Lib/contextlib.py
+++ b/Lib/contextlib.py
@@ -225,32 +225,21 @@ class ExitStack(object):
return self
def __exit__(self, *exc_details):
- if not self._exit_callbacks:
- return
- # This looks complicated, but it is really just
- # setting up a chain of try-expect statements to ensure
- # that outer callbacks still get invoked even if an
- # inner one throws an exception
- def _invoke_next_callback(exc_details):
- # Callbacks are removed from the list in FIFO order
- # but the recursion means they're invoked in LIFO order
- cb = self._exit_callbacks.popleft()
- if not self._exit_callbacks:
- # Innermost callback is invoked directly
- return cb(*exc_details)
- # More callbacks left, so descend another level in the stack
+ # Callbacks are invoked in LIFO order to match the behaviour of
+ # nested context managers
+ suppressed_exc = False
+ while self._exit_callbacks:
+ cb = self._exit_callbacks.pop()
try:
- suppress_exc = _invoke_next_callback(exc_details)
+ if cb(*exc_details):
+ suppressed_exc = True
+ exc_details = (None, None, None)
except:
- suppress_exc = cb(*sys.exc_info())
- # Check if this cb suppressed the inner exception
- if not suppress_exc:
+ new_exc_details = sys.exc_info()
+ if exc_details != (None, None, None):
+ # simulate the stack of exceptions by setting the context
+ new_exc_details[1].__context__ = exc_details[1]
+ if not self._exit_callbacks:
raise
- else:
- # Check if inner cb suppressed the original exception
- if suppress_exc:
- exc_details = (None, None, None)
- suppress_exc = cb(*exc_details) or suppress_exc
- return suppress_exc
- # Kick off the recursive chain
- return _invoke_next_callback(exc_details)
+ exc_details = new_exc_details
+ return suppressed_exc