diff -r 35da5d848ffd Lib/contextlib.py --- a/Lib/contextlib.py Mon Sep 23 23:24:38 2013 +0300 +++ b/Lib/contextlib.py Wed Sep 25 21:09:02 2013 +0200 @@ -237,6 +237,8 @@ return self def __exit__(self, *exc_details): + orig_exc_details = exc_details + # We manipulate the exception state so it behaves as though # we were actually nesting multiple with statements frame_exc = sys.exc_info()[1] @@ -251,17 +253,20 @@ # 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: + raise exc_details[1] + return suppressed_exc and orig_exc_details[0] is not None