diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2019-08-24 10:11:52 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-24 10:11:52 (GMT) |
commit | ef61c524ddeeb56da3858b86e349e7288d68178e (patch) | |
tree | e8a9defe85faff05464db6335140eb0990499f15 /Lib/test/test_grammar.py | |
parent | e9c90aa43144b0be1e4e393e8cb549573437a5da (diff) | |
download | cpython-ef61c524ddeeb56da3858b86e349e7288d68178e.zip cpython-ef61c524ddeeb56da3858b86e349e7288d68178e.tar.gz cpython-ef61c524ddeeb56da3858b86e349e7288d68178e.tar.bz2 |
bpo-37830: Fix compilation of break and continue in finally. (GH-15320)
Fix compilation of "break" and "continue" in the
"finally" block when the corresponding "try" block
contains "return" with a non-constant value.
Diffstat (limited to 'Lib/test/test_grammar.py')
-rw-r--r-- | Lib/test/test_grammar.py | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py index 78d9459..2ed73e3 100644 --- a/Lib/test/test_grammar.py +++ b/Lib/test/test_grammar.py @@ -991,6 +991,60 @@ class GrammarTests(unittest.TestCase): return 4 self.assertEqual(g3(), 4) + def test_break_in_finally_after_return(self): + # See issue #37830 + def g1(x): + for count in [0, 1]: + count2 = 0 + while count2 < 20: + count2 += 10 + try: + return count + count2 + finally: + if x: + break + return 'end', count, count2 + self.assertEqual(g1(False), 10) + self.assertEqual(g1(True), ('end', 1, 10)) + + def g2(x): + for count in [0, 1]: + for count2 in [10, 20]: + try: + return count + count2 + finally: + if x: + break + return 'end', count, count2 + self.assertEqual(g2(False), 10) + self.assertEqual(g2(True), ('end', 1, 10)) + + def test_continue_in_finally_after_return(self): + # See issue #37830 + def g1(x): + count = 0 + while count < 100: + count += 1 + try: + return count + finally: + if x: + continue + return 'end', count + self.assertEqual(g1(False), 1) + self.assertEqual(g1(True), ('end', 100)) + + def g2(x): + for count in [0, 1]: + try: + return count + finally: + if x: + continue + return 'end', count + self.assertEqual(g2(False), 0) + self.assertEqual(g2(True), ('end', 1)) + def test_yield(self): # Allowed as standalone statement def g(): yield 1 |