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

gh-130250: use support.swap_attr() in test and don't assume we're beginning with a clean system state (GH-130342)
(cherry picked from commit 048ee2d5ec)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2025-02-20 12:45:42 +01:00 committed by GitHub
parent b259b7577f
commit 96a914ad7e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -329,14 +329,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")