mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
gh-102799: use sys.exception() instead of sys.exc_info() in contextlib (#103311)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
This commit is contained in:
parent
832c37d42a
commit
eeff8e7234
3 changed files with 34 additions and 24 deletions
|
@ -557,11 +557,12 @@ class ExitStack(_BaseExitStack, AbstractContextManager):
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __exit__(self, *exc_details):
|
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 manipulate the exception state so it behaves as though
|
||||||
# we were actually nesting multiple with statements
|
# 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):
|
def _fix_exception_context(new_exc, old_exc):
|
||||||
# Context may not be correct, so find the end of the chain
|
# Context may not be correct, so find the end of the chain
|
||||||
while 1:
|
while 1:
|
||||||
|
@ -584,24 +585,28 @@ class ExitStack(_BaseExitStack, AbstractContextManager):
|
||||||
is_sync, cb = self._exit_callbacks.pop()
|
is_sync, cb = self._exit_callbacks.pop()
|
||||||
assert is_sync
|
assert is_sync
|
||||||
try:
|
try:
|
||||||
|
if exc is None:
|
||||||
|
exc_details = None, None, None
|
||||||
|
else:
|
||||||
|
exc_details = type(exc), exc, exc.__traceback__
|
||||||
if cb(*exc_details):
|
if cb(*exc_details):
|
||||||
suppressed_exc = True
|
suppressed_exc = True
|
||||||
pending_raise = False
|
pending_raise = False
|
||||||
exc_details = (None, None, None)
|
exc = None
|
||||||
except:
|
except BaseException as new_exc:
|
||||||
new_exc_details = sys.exc_info()
|
|
||||||
# simulate the stack of exceptions by setting the context
|
# 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
|
pending_raise = True
|
||||||
exc_details = new_exc_details
|
exc = new_exc
|
||||||
|
|
||||||
if pending_raise:
|
if pending_raise:
|
||||||
try:
|
try:
|
||||||
# bare "raise exc_details[1]" replaces our carefully
|
# bare "raise exc" replaces our carefully
|
||||||
# set-up context
|
# set-up context
|
||||||
fixed_ctx = exc_details[1].__context__
|
fixed_ctx = exc.__context__
|
||||||
raise exc_details[1]
|
raise exc
|
||||||
except BaseException:
|
except BaseException:
|
||||||
exc_details[1].__context__ = fixed_ctx
|
exc.__context__ = fixed_ctx
|
||||||
raise
|
raise
|
||||||
return received_exc and suppressed_exc
|
return received_exc and suppressed_exc
|
||||||
|
|
||||||
|
@ -697,11 +702,12 @@ class AsyncExitStack(_BaseExitStack, AbstractAsyncContextManager):
|
||||||
return self
|
return self
|
||||||
|
|
||||||
async def __aexit__(self, *exc_details):
|
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 manipulate the exception state so it behaves as though
|
||||||
# we were actually nesting multiple with statements
|
# 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):
|
def _fix_exception_context(new_exc, old_exc):
|
||||||
# Context may not be correct, so find the end of the chain
|
# Context may not be correct, so find the end of the chain
|
||||||
while 1:
|
while 1:
|
||||||
|
@ -723,6 +729,10 @@ class AsyncExitStack(_BaseExitStack, AbstractAsyncContextManager):
|
||||||
while self._exit_callbacks:
|
while self._exit_callbacks:
|
||||||
is_sync, cb = self._exit_callbacks.pop()
|
is_sync, cb = self._exit_callbacks.pop()
|
||||||
try:
|
try:
|
||||||
|
if exc is None:
|
||||||
|
exc_details = None, None, None
|
||||||
|
else:
|
||||||
|
exc_details = type(exc), exc, exc.__traceback__
|
||||||
if is_sync:
|
if is_sync:
|
||||||
cb_suppress = cb(*exc_details)
|
cb_suppress = cb(*exc_details)
|
||||||
else:
|
else:
|
||||||
|
@ -731,21 +741,21 @@ class AsyncExitStack(_BaseExitStack, AbstractAsyncContextManager):
|
||||||
if cb_suppress:
|
if cb_suppress:
|
||||||
suppressed_exc = True
|
suppressed_exc = True
|
||||||
pending_raise = False
|
pending_raise = False
|
||||||
exc_details = (None, None, None)
|
exc = None
|
||||||
except:
|
except BaseException as new_exc:
|
||||||
new_exc_details = sys.exc_info()
|
|
||||||
# simulate the stack of exceptions by setting the context
|
# 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
|
pending_raise = True
|
||||||
exc_details = new_exc_details
|
exc = new_exc
|
||||||
|
|
||||||
if pending_raise:
|
if pending_raise:
|
||||||
try:
|
try:
|
||||||
# bare "raise exc_details[1]" replaces our carefully
|
# bare "raise exc" replaces our carefully
|
||||||
# set-up context
|
# set-up context
|
||||||
fixed_ctx = exc_details[1].__context__
|
fixed_ctx = exc.__context__
|
||||||
raise exc_details[1]
|
raise exc
|
||||||
except BaseException:
|
except BaseException:
|
||||||
exc_details[1].__context__ = fixed_ctx
|
exc.__context__ = fixed_ctx
|
||||||
raise
|
raise
|
||||||
return received_exc and suppressed_exc
|
return received_exc and suppressed_exc
|
||||||
|
|
||||||
|
|
|
@ -1085,7 +1085,7 @@ class TestBaseExitStack:
|
||||||
class TestExitStack(TestBaseExitStack, unittest.TestCase):
|
class TestExitStack(TestBaseExitStack, unittest.TestCase):
|
||||||
exit_stack = ExitStack
|
exit_stack = ExitStack
|
||||||
callback_error_internal_frames = [
|
callback_error_internal_frames = [
|
||||||
('__exit__', 'raise exc_details[1]'),
|
('__exit__', 'raise exc'),
|
||||||
('__exit__', 'if cb(*exc_details):'),
|
('__exit__', 'if cb(*exc_details):'),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -557,7 +557,7 @@ class TestAsyncExitStack(TestBaseExitStack, unittest.TestCase):
|
||||||
('__exit__', 'return self.run_coroutine(self.__aexit__(*exc_details))'),
|
('__exit__', 'return self.run_coroutine(self.__aexit__(*exc_details))'),
|
||||||
('run_coroutine', 'raise exc'),
|
('run_coroutine', 'raise exc'),
|
||||||
('run_coroutine', 'raise exc'),
|
('run_coroutine', 'raise exc'),
|
||||||
('__aexit__', 'raise exc_details[1]'),
|
('__aexit__', 'raise exc'),
|
||||||
('__aexit__', 'cb_suppress = cb(*exc_details)'),
|
('__aexit__', 'cb_suppress = cb(*exc_details)'),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue