summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_exception_variations.py
diff options
context:
space:
mode:
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>2022-01-27 13:40:44 (GMT)
committerGitHub <noreply@github.com>2022-01-27 13:40:44 (GMT)
commit3d2ce3471646704ebd5252f4b20f065f139a53b1 (patch)
tree8942b677163981fb1b61cf35dcd461831557c28e /Lib/test/test_exception_variations.py
parentc7f810b34d91a5c2fbe0a8385562015d2dd961f2 (diff)
downloadcpython-3d2ce3471646704ebd5252f4b20f065f139a53b1.zip
cpython-3d2ce3471646704ebd5252f4b20f065f139a53b1.tar.gz
cpython-3d2ce3471646704ebd5252f4b20f065f139a53b1.tar.bz2
bpo-46458: emit code for else of a try block immediately after the try body (GH-30751)
Diffstat (limited to 'Lib/test/test_exception_variations.py')
-rw-r--r--Lib/test/test_exception_variations.py123
1 files changed, 122 insertions, 1 deletions
diff --git a/Lib/test/test_exception_variations.py b/Lib/test/test_exception_variations.py
index aabac6d..a83a41d 100644
--- a/Lib/test/test_exception_variations.py
+++ b/Lib/test/test_exception_variations.py
@@ -1,7 +1,7 @@
import unittest
-class ExceptionTestCase(unittest.TestCase):
+class ExceptTestCases(unittest.TestCase):
def test_try_except_else_finally(self):
hit_except = False
hit_else = False
@@ -172,6 +172,127 @@ class ExceptionTestCase(unittest.TestCase):
self.assertTrue(hit_finally)
self.assertTrue(hit_except)
+ def test_nested_exception_in_except(self):
+ hit_else = False
+ hit_finally = False
+ hit_except = False
+ hit_inner_except = False
+ hit_inner_else = False
+
+ try:
+ try:
+ raise Exception('inner exception')
+ except:
+ hit_inner_except = True
+ raise Exception('outer exception')
+ else:
+ hit_inner_else = True
+ except:
+ hit_except = True
+ else:
+ hit_else = True
+ finally:
+ hit_finally = True
+
+ self.assertTrue(hit_inner_except)
+ self.assertFalse(hit_inner_else)
+ self.assertFalse(hit_else)
+ self.assertTrue(hit_finally)
+ self.assertTrue(hit_except)
+
+ def test_nested_exception_in_else(self):
+ hit_else = False
+ hit_finally = False
+ hit_except = False
+ hit_inner_except = False
+ hit_inner_else = False
+
+ try:
+ try:
+ pass
+ except:
+ hit_inner_except = True
+ else:
+ hit_inner_else = True
+ raise Exception('outer exception')
+ except:
+ hit_except = True
+ else:
+ hit_else = True
+ finally:
+ hit_finally = True
+
+ self.assertFalse(hit_inner_except)
+ self.assertTrue(hit_inner_else)
+ self.assertFalse(hit_else)
+ self.assertTrue(hit_finally)
+ self.assertTrue(hit_except)
+
+ def test_nested_exception_in_finally_no_exception(self):
+ hit_else = False
+ hit_finally = False
+ hit_except = False
+ hit_inner_except = False
+ hit_inner_else = False
+ hit_inner_finally = False
+
+ try:
+ try:
+ pass
+ except:
+ hit_inner_except = True
+ else:
+ hit_inner_else = True
+ finally:
+ hit_inner_finally = True
+ raise Exception('outer exception')
+ except:
+ hit_except = True
+ else:
+ hit_else = True
+ finally:
+ hit_finally = True
+
+ self.assertFalse(hit_inner_except)
+ self.assertTrue(hit_inner_else)
+ self.assertTrue(hit_inner_finally)
+ self.assertFalse(hit_else)
+ self.assertTrue(hit_finally)
+ self.assertTrue(hit_except)
+
+ def test_nested_exception_in_finally_with_exception(self):
+ hit_else = False
+ hit_finally = False
+ hit_except = False
+ hit_inner_except = False
+ hit_inner_else = False
+ hit_inner_finally = False
+
+ try:
+ try:
+ raise Exception('inner exception')
+ except:
+ hit_inner_except = True
+ else:
+ hit_inner_else = True
+ finally:
+ hit_inner_finally = True
+ raise Exception('outer exception')
+ except:
+ hit_except = True
+ else:
+ hit_else = True
+ finally:
+ hit_finally = True
+
+
+ self.assertTrue(hit_inner_except)
+ self.assertFalse(hit_inner_else)
+ self.assertTrue(hit_inner_finally)
+ self.assertFalse(hit_else)
+ self.assertTrue(hit_finally)
+ self.assertTrue(hit_except)
+
class ExceptStarTestCases(unittest.TestCase):
def test_try_except_else_finally(self):