Skip to content

Commit 7c2a040

Browse files
authored
[3.9] bpo-44594: fix (Async)ExitStack handling of __context__ (gh-27089) (GH-28731)
Make enter_context(foo()) / enter_async_context(foo()) equivalent to `[async] with foo()` regarding __context__ when an exception is raised. Previously exceptions would be caught and re-raised with the wrong context when explicitly overriding __context__ with None.. (cherry picked from commit e6d1aa1) Co-authored-by: John Belmonte <[email protected]> Automerge-Triggered-By: GH:njsmith
1 parent e9ce081 commit 7c2a040

File tree

4 files changed

+76
-4
lines changed

4 files changed

+76
-4
lines changed

Lib/contextlib.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -496,10 +496,10 @@ def _fix_exception_context(new_exc, old_exc):
496496
# Context may not be correct, so find the end of the chain
497497
while 1:
498498
exc_context = new_exc.__context__
499-
if exc_context is old_exc:
499+
if exc_context is None or exc_context is old_exc:
500500
# Context is already set correctly (see issue 20317)
501501
return
502-
if exc_context is None or exc_context is frame_exc:
502+
if exc_context is frame_exc:
503503
break
504504
new_exc = exc_context
505505
# Change the end of the chain to point to the exception
@@ -630,10 +630,10 @@ def _fix_exception_context(new_exc, old_exc):
630630
# Context may not be correct, so find the end of the chain
631631
while 1:
632632
exc_context = new_exc.__context__
633-
if exc_context is old_exc:
633+
if exc_context is None or exc_context is old_exc:
634634
# Context is already set correctly (see issue 20317)
635635
return
636-
if exc_context is None or exc_context is frame_exc:
636+
if exc_context is frame_exc:
637637
break
638638
new_exc = exc_context
639639
# Change the end of the chain to point to the exception

Lib/test/test_contextlib.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,40 @@ def suppress_exc(*exc_details):
777777
self.assertIsInstance(inner_exc, ValueError)
778778
self.assertIsInstance(inner_exc.__context__, ZeroDivisionError)
779779

780+
def test_exit_exception_explicit_none_context(self):
781+
# Ensure ExitStack chaining matches actual nested `with` statements
782+
# regarding explicit __context__ = None.
783+
784+
class MyException(Exception):
785+
pass
786+
787+
@contextmanager
788+
def my_cm():
789+
try:
790+
yield
791+
except BaseException:
792+
exc = MyException()
793+
try:
794+
raise exc
795+
finally:
796+
exc.__context__ = None
797+
798+
@contextmanager
799+
def my_cm_with_exit_stack():
800+
with self.exit_stack() as stack:
801+
stack.enter_context(my_cm())
802+
yield stack
803+
804+
for cm in (my_cm, my_cm_with_exit_stack):
805+
with self.subTest():
806+
try:
807+
with cm():
808+
raise IndexError()
809+
except MyException as exc:
810+
self.assertIsNone(exc.__context__)
811+
else:
812+
self.fail("Expected IndexError, but no exception was raised")
813+
780814
def test_exit_exception_non_suppressing(self):
781815
# http://bugs.python.org/issue19092
782816
def raise_exc(exc):

Lib/test/test_contextlib_async.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,41 @@ async def suppress_exc(*exc_details):
463463
self.assertIsInstance(inner_exc, ValueError)
464464
self.assertIsInstance(inner_exc.__context__, ZeroDivisionError)
465465

466+
@_async_test
467+
async def test_async_exit_exception_explicit_none_context(self):
468+
# Ensure AsyncExitStack chaining matches actual nested `with` statements
469+
# regarding explicit __context__ = None.
470+
471+
class MyException(Exception):
472+
pass
473+
474+
@asynccontextmanager
475+
async def my_cm():
476+
try:
477+
yield
478+
except BaseException:
479+
exc = MyException()
480+
try:
481+
raise exc
482+
finally:
483+
exc.__context__ = None
484+
485+
@asynccontextmanager
486+
async def my_cm_with_exit_stack():
487+
async with self.exit_stack() as stack:
488+
await stack.enter_async_context(my_cm())
489+
yield stack
490+
491+
for cm in (my_cm, my_cm_with_exit_stack):
492+
with self.subTest():
493+
try:
494+
async with cm():
495+
raise IndexError()
496+
except MyException as exc:
497+
self.assertIsNone(exc.__context__)
498+
else:
499+
self.fail("Expected IndexError, but no exception was raised")
500+
466501

467502
if __name__ == '__main__':
468503
unittest.main()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix an edge case of :class:`ExitStack` and :class:`AsyncExitStack` exception
2+
chaining. They will now match ``with`` block behavior when ``__context__`` is
3+
explicitly set to ``None`` when the exception is in flight.

0 commit comments

Comments
 (0)