Skip to content

Commit e064af5

Browse files
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 82c5322) Co-authored-by: Yellow Dusk <[email protected]>
1 parent b37f3e9 commit e064af5

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

Lib/test/test_exceptions.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,15 +598,27 @@ def test_str(self):
598598
self.assertTrue(str(Exception('a')))
599599
self.assertTrue(str(Exception('a', 'b')))
600600

601-
def testExceptionCleanupNames(self):
601+
def test_exception_cleanup_names(self):
602602
# Make sure the local variable bound to the exception instance by
603603
# an "except" statement is only visible inside the except block.
604604
try:
605605
raise Exception()
606606
except Exception as e:
607-
self.assertTrue(e)
607+
self.assertIsInstance(e, Exception)
608+
self.assertNotIn('e', locals())
609+
with self.assertRaises(UnboundLocalError):
610+
e
611+
612+
def test_exception_cleanup_names2(self):
613+
# Make sure the cleanup doesn't break if the variable is explicitly deleted.
614+
try:
615+
raise Exception()
616+
except Exception as e:
617+
self.assertIsInstance(e, Exception)
608618
del e
609619
self.assertNotIn('e', locals())
620+
with self.assertRaises(UnboundLocalError):
621+
e
610622

611623
def testExceptionCleanupState(self):
612624
# Make sure exception state is cleaned up as soon as the except

0 commit comments

Comments
 (0)