Commit 25301fc9 authored by Vitja Makarov's avatar Vitja Makarov

Fix finally inside/outside the loop

parent 22cb7155
......@@ -200,6 +200,7 @@ class LoopDescr(object):
def __init__(self, next_block, loop_block):
self.next_block = next_block
self.loop_block = loop_block
self.exceptions = []
class ExceptionDescr(object):
"""Exception handling helper.
......@@ -827,12 +828,17 @@ class CreateControlFlowGraph(CythonTransform):
self.visit(node.finally_clause)
finally_exit = self.flow.block
self.flow.exceptions.append(ExceptionDescr(entry_point, finally_enter, finally_exit))
descr = ExceptionDescr(entry_point, finally_enter, finally_exit)
self.flow.exceptions.append(descr)
if self.flow.loops:
self.flow.loops[-1].exceptions.append(descr)
self.flow.block = body_block
## XXX: Is it still required
body_block.add_child(entry_point)
self.visit(node.body)
self.flow.exceptions.pop()
if self.flow.loops:
self.flow.loops[-1].exceptions.pop()
if self.flow.block:
self.flow.block.add_child(finally_enter)
......@@ -878,7 +884,7 @@ class CreateControlFlowGraph(CythonTransform):
return node
loop = self.flow.loops[-1]
self.mark_position(node)
for exception in self.flow.exceptions[::-1]:
for exception in loop.exceptions[::-1]:
if exception.finally_enter:
self.flow.block.add_child(exception.finally_enter)
if exception.finally_exit:
......@@ -895,7 +901,7 @@ class CreateControlFlowGraph(CythonTransform):
return node
loop = self.flow.loops[-1]
self.mark_position(node)
for exception in self.flow.exceptions[::-1]:
for exception in loop.exceptions[::-1]:
if exception.finally_enter:
self.flow.block.add_child(exception.finally_enter)
if exception.finally_exit:
......
......@@ -50,6 +50,35 @@ def for_break(l):
x = i
print x
def for_finally_continue(f):
for i in f:
try:
x = i()
finally:
print x
continue
def for_finally_break(f):
for i in f:
try:
x = i()
finally:
print x
break
def for_finally_outer(p, f):
x = 1
try:
for i in f:
print x
x = i()
if x > 0:
continue
if x < 0:
break
finally:
del x
_ERRORS = """
8:12: local variable 'a' might be referenced before assignment
......@@ -59,4 +88,6 @@ _ERRORS = """
37:16: local variable 'x' might be referenced before assignment
44:11: local variable 'x' might be referenced before assignment
51:11: local variable 'x' might be referenced before assignment
58:19: local variable 'x' might be referenced before assignment
66:19: local variable 'x' might be referenced before assignment
"""
......@@ -24,7 +24,39 @@ def simple_while_pos(n):
a = 1
return a
def while_finally_continue(p, f):
while p():
try:
x = f()
finally:
print x
continue
def while_finally_break(p, f):
while p():
try:
x = f()
finally:
print x
break
def while_finally_outer(p, f):
x = 1
try:
while p():
print x
x = f()
if x > 0:
continue
if x < 0:
break
finally:
del x
_ERRORS = """
9:12: local variable 'a' might be referenced before assignment
17:12: local variable 'a' might be referenced before assignment
32:19: local variable 'x' might be referenced before assignment
40:19: local variable 'x' might be referenced before assignment
"""
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment