[Python-checkins] bpo-46442: improve and rename testExceptionCleanupNames (GH-30758)

miss-islington webhook-mailer at python.org
Sat Jan 22 02:37:41 EST 2022


https://github.com/python/cpython/commit/e064af564c8580285a76199229864ddfb1e50c0f
commit: e064af564c8580285a76199229864ddfb1e50c0f
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-01-21T23:37:32-08:00
summary:

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.
(cherry picked from commit 82c53229e18f5853c82cb8ab6b9af1925a0e9e58)

Co-authored-by: Yellow Dusk <yellow.dusk1590 at fastmail.com>

files:
M Lib/test/test_exceptions.py

diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index 5168b0b8a0831..90d7f37dd8670 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -598,15 +598,27 @@ def test_str(self):
         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



More information about the Python-checkins mailing list