]> granicus.if.org Git - python/commitdiff
bpo-32478: Add tests for 'break' and 'return' inside 'finally' clause. (GH-5078)...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 2 Jan 2018 08:20:12 +0000 (00:20 -0800)
committerSerhiy Storchaka <storchaka@gmail.com>
Tue, 2 Jan 2018 08:20:12 +0000 (10:20 +0200)
(cherry picked from commit 7cc42c356b0dc5ad9eaa9392789e84bd4aa1c7de)

Lib/test/test_grammar.py

index 65e26bfd383823e5387b86b960fd74fd94045bf2..62f3251f051d8a8d029887186834bcba8f4a87b4 100644 (file)
@@ -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