summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_grammar.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2018-01-02 00:38:35 (GMT)
committerGitHub <noreply@github.com>2018-01-02 00:38:35 (GMT)
commit7cc42c356b0dc5ad9eaa9392789e84bd4aa1c7de (patch)
treeeccadb8e33d2296edea596fd87c9992cf542c493 /Lib/test/test_grammar.py
parente8ed96550c6aa9a1e39c36e67e892994e25e2c41 (diff)
downloadcpython-7cc42c356b0dc5ad9eaa9392789e84bd4aa1c7de.zip
cpython-7cc42c356b0dc5ad9eaa9392789e84bd4aa1c7de.tar.gz
cpython-7cc42c356b0dc5ad9eaa9392789e84bd4aa1c7de.tar.bz2
bpo-32478: Add tests for 'break' and 'return' inside 'finally' clause. (#5078)
Diffstat (limited to 'Lib/test/test_grammar.py')
-rw-r--r--Lib/test/test_grammar.py74
1 files changed, 74 insertions, 0 deletions
diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py
index 823315f..9f26e9c 100644
--- a/Lib/test/test_grammar.py
+++ b/Lib/test/test_grammar.py
@@ -805,6 +805,80 @@ class GrammarTests(unittest.TestCase):
x = g2()
check_syntax_error(self, "class foo:return 1")
+ def test_break_in_finally(self):
+ count = 0
+ while count < 2:
+ count += 1
+ try:
+ pass
+ finally:
+ break
+ self.assertEqual(count, 1)
+
+ count = 0
+ while count < 2:
+ count += 1
+ try:
+ continue
+ finally:
+ break
+ self.assertEqual(count, 1)
+
+ count = 0
+ while count < 2:
+ count += 1
+ try:
+ 1/0
+ finally:
+ break
+ self.assertEqual(count, 1)
+
+ for count in [0, 1]:
+ self.assertEqual(count, 0)
+ try:
+ pass
+ finally:
+ break
+ self.assertEqual(count, 0)
+
+ for count in [0, 1]:
+ self.assertEqual(count, 0)
+ try:
+ continue
+ finally:
+ break
+ self.assertEqual(count, 0)
+
+ for count in [0, 1]:
+ self.assertEqual(count, 0)
+ try:
+ 1/0
+ finally:
+ break
+ self.assertEqual(count, 0)
+
+ def test_return_in_finally(self):
+ def g1():
+ try:
+ pass
+ finally:
+ return 1
+ self.assertEqual(g1(), 1)
+
+ def g2():
+ try:
+ return 2
+ finally:
+ return 3
+ self.assertEqual(g2(), 3)
+
+ def g3():
+ try:
+ 1/0
+ finally:
+ return 4
+ self.assertEqual(g3(), 4)
+
def test_yield(self):
# Allowed as standalone statement
def g(): yield 1