From: Yellow Dusk Date: Sat, 22 Jan 2022 07:09:34 +0000 (-0800) Subject: bpo-46442: improve and rename testExceptionCleanupNames (GH-30758) X-Git-Tag: v3.11.0a5~165 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=82c53229e18f5853c82cb8ab6b9af1925a0e9e58;p=thirdparty%2FPython%2Fcpython.git bpo-46442: improve and rename testExceptionCleanupNames (GH-30758) The test tested that explicitly deleting the local variable bound to the exception did not cause problems, but it did not test what it actually claimed to test, i.e. that the variable is deleted automatically. --- diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py index 531b9c92deae..5932b9d4f667 100644 --- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -671,15 +671,27 @@ class ExceptionTests(unittest.TestCase): self.assertTrue(str(Exception('a'))) self.assertTrue(str(Exception('a', 'b'))) - def testExceptionCleanupNames(self): + def test_exception_cleanup_names(self): # Make sure the local variable bound to the exception instance by # an "except" statement is only visible inside the except block. try: raise Exception() except Exception as e: - self.assertTrue(e) + self.assertIsInstance(e, Exception) + self.assertNotIn('e', locals()) + with self.assertRaises(UnboundLocalError): + e + + def test_exception_cleanup_names2(self): + # Make sure the cleanup doesn't break if the variable is explicitly deleted. + try: + raise Exception() + except Exception as e: + self.assertIsInstance(e, Exception) del e self.assertNotIn('e', locals()) + with self.assertRaises(UnboundLocalError): + e def testExceptionCleanupState(self): # Make sure exception state is cleaned up as soon as the except