gh-130250: use support.swap_attr() in test and don't assume we're beginning with a clean system state (#130342)

This commit is contained in:
Irit Katriel 2025-02-20 11:29:18 +00:00 committed by GitHub
parent 417372bd43
commit 048ee2d5ec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -511,14 +511,10 @@ class TracebackCases(unittest.TestCase):
self.assertEqual(output.getvalue(), "Exception: projector\n")
def test_print_last(self):
self.assertIsNone(getattr(sys, "last_exc", None))
try:
sys.last_exc = ValueError(42)
with support.swap_attr(sys, 'last_exc', ValueError(42)):
output = StringIO()
traceback.print_last(file=output)
self.assertEqual(output.getvalue(), "ValueError: 42\n")
finally:
sys.last_exc = None
def test_format_exception_exc(self):
e = Exception("projector")