MCPcopy Index your code
hub / github.com/python/cpython / test_exit_exception_chaining

Method test_exit_exception_chaining

Lib/test/test_contextlib.py:994–1023  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

992 self.assertIsInstance(inner_exc.__context__, ZeroDivisionError)
993
994 def test_exit_exception_chaining(self):
995 # Ensure exception chaining matches the reference behaviour
996 def raise_exc(exc):
997 raise exc
998
999 saved_details = None
1000 def suppress_exc(*exc_details):
1001 nonlocal saved_details
1002 saved_details = exc_details
1003 return True
1004
1005 try:
1006 with self.exit_stack() as stack:
1007 stack.callback(raise_exc, IndexError)
1008 stack.callback(raise_exc, KeyError)
1009 stack.callback(raise_exc, AttributeError)
1010 stack.push(suppress_exc)
1011 stack.callback(raise_exc, ValueError)
1012 1 / 0
1013 except IndexError as exc:
1014 self.assertIsInstance(exc.__context__, KeyError)
1015 self.assertIsInstance(exc.__context__.__context__, AttributeError)
1016 # Inner exceptions were suppressed
1017 self.assertIsNone(exc.__context__.__context__.__context__)
1018 else:
1019 self.fail("Expected IndexError, but no exception was raised")
1020 # Check the inner exceptions
1021 inner_exc = saved_details[1]
1022 self.assertIsInstance(inner_exc, ValueError)
1023 self.assertIsInstance(inner_exc.__context__, ZeroDivisionError)
1024
1025 def test_exit_exception_explicit_none_context(self):
1026 # Ensure ExitStack chaining matches actual nested `with` statements

Callers

nothing calls this directly

Calls 5

assertIsInstanceMethod · 0.80
assertIsNoneMethod · 0.80
callbackMethod · 0.45
pushMethod · 0.45
failMethod · 0.45

Tested by

no test coverage detected