From ede88da1f0ff406501099b2daa66810b2f0869e5 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Thu, 6 Apr 2023 12:56:39 +0100 Subject: [PATCH 1/3] gh-102799: use sys.exception() instead of sys.exc_info() in contextlib --- Lib/contextlib.py | 54 ++++++++++++++++++++++--------------- Lib/test/test_contextlib.py | 2 +- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/Lib/contextlib.py b/Lib/contextlib.py index 30d9ac25b2bbec..ff55031d84a294 100644 --- a/Lib/contextlib.py +++ b/Lib/contextlib.py @@ -544,11 +544,12 @@ def __enter__(self): return self def __exit__(self, *exc_details): - received_exc = exc_details[0] is not None + exc = exc_details[1] + received_exc = exc 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] + frame_exc = sys.exception() def _fix_exception_context(new_exc, old_exc): # Context may not be correct, so find the end of the chain while 1: @@ -571,24 +572,28 @@ def _fix_exception_context(new_exc, old_exc): is_sync, cb = self._exit_callbacks.pop() assert is_sync try: + if exc is None: + exc_details = None, None, None + else: + exc_details = type(exc), exc, exc.__traceback__ if cb(*exc_details): suppressed_exc = True pending_raise = False - exc_details = (None, None, None) - except: - new_exc_details = sys.exc_info() + exc = None + except BaseException as new_exc: # simulate the stack of exceptions by setting the context - _fix_exception_context(new_exc_details[1], exc_details[1]) + _fix_exception_context(new_exc, exc) pending_raise = True - exc_details = new_exc_details + exc = new_exc + if pending_raise: try: - # bare "raise exc_details[1]" replaces our carefully + # bare "raise exc" replaces our carefully # set-up context - fixed_ctx = exc_details[1].__context__ - raise exc_details[1] + fixed_ctx = exc.__context__ + raise exc except BaseException: - exc_details[1].__context__ = fixed_ctx + exc.__context__ = fixed_ctx raise return received_exc and suppressed_exc @@ -684,11 +689,12 @@ async def __aenter__(self): return self async def __aexit__(self, *exc_details): - received_exc = exc_details[0] is not None + exc = exc_details[1] + received_exc = exc 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] + frame_exc = sys.exception() def _fix_exception_context(new_exc, old_exc): # Context may not be correct, so find the end of the chain while 1: @@ -710,6 +716,10 @@ def _fix_exception_context(new_exc, old_exc): while self._exit_callbacks: is_sync, cb = self._exit_callbacks.pop() try: + if exc is None: + exc_details = None, None, None + else: + exc_details = type(exc), exc, exc.__traceback__ if is_sync: cb_suppress = cb(*exc_details) else: @@ -718,21 +728,21 @@ def _fix_exception_context(new_exc, old_exc): if cb_suppress: suppressed_exc = True pending_raise = False - exc_details = (None, None, None) - except: - new_exc_details = sys.exc_info() + exc = None + except BaseException as new_exc: # simulate the stack of exceptions by setting the context - _fix_exception_context(new_exc_details[1], exc_details[1]) + _fix_exception_context(new_exc, exc_details[1]) pending_raise = True - exc_details = new_exc_details + exc = new_exc + if pending_raise: try: - # bare "raise exc_details[1]" replaces our carefully + # bare "raise exc" replaces our carefully # set-up context - fixed_ctx = exc_details[1].__context__ - raise exc_details[1] + fixed_ctx = exc.__context__ + raise exc except BaseException: - exc_details[1].__context__ = fixed_ctx + exc.__context__ = fixed_ctx raise return received_exc and suppressed_exc diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py index ec06785b5667a6..76ffb7d0664f0e 100644 --- a/Lib/test/test_contextlib.py +++ b/Lib/test/test_contextlib.py @@ -1074,7 +1074,7 @@ def first(): class TestExitStack(TestBaseExitStack, unittest.TestCase): exit_stack = ExitStack callback_error_internal_frames = [ - ('__exit__', 'raise exc_details[1]'), + ('__exit__', 'raise exc'), ('__exit__', 'if cb(*exc_details):'), ] From 7adb1c48c9f54bfde6b673565fbb043aa4f49176 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Thu, 6 Apr 2023 14:03:51 +0100 Subject: [PATCH 2/3] fix Lib/test/test_contextlib_async.py --- Lib/test/test_contextlib_async.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_contextlib_async.py b/Lib/test/test_contextlib_async.py index 3d43ed0fcab168..b7cc5f62b15cf8 100644 --- a/Lib/test/test_contextlib_async.py +++ b/Lib/test/test_contextlib_async.py @@ -545,7 +545,7 @@ def __exit__(self, *exc_details): ('__exit__', 'return self.run_coroutine(self.__aexit__(*exc_details))'), ('run_coroutine', 'raise exc'), ('run_coroutine', 'raise exc'), - ('__aexit__', 'raise exc_details[1]'), + ('__aexit__', 'raise exc'), ('__aexit__', 'cb_suppress = cb(*exc_details)'), ] From cc749034536332b3a5a7423446bdbfbc86d1ed08 Mon Sep 17 00:00:00 2001 From: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Date: Mon, 17 Jul 2023 10:27:38 +0100 Subject: [PATCH 3/3] exc_details[1] --> exc Co-authored-by: Kumar Aditya --- Lib/contextlib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/contextlib.py b/Lib/contextlib.py index fdd79fef80b121..b669459dba17d9 100644 --- a/Lib/contextlib.py +++ b/Lib/contextlib.py @@ -740,7 +740,7 @@ def _fix_exception_context(new_exc, old_exc): exc = None except BaseException as new_exc: # simulate the stack of exceptions by setting the context - _fix_exception_context(new_exc, exc_details[1]) + _fix_exception_context(new_exc, exc) pending_raise = True exc = new_exc