Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.6] bpo-32478: Add tests for 'break' and 'return' inside 'finally' clause. (GH-5078) #5083

Merged
merged 1 commit into from
Jan 2, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions Lib/test/test_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,80 @@ def g2(): return 1
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
Expand Down