diff --git a/pylingual/control_flow_reconstruction/templates/Conditional.py b/pylingual/control_flow_reconstruction/templates/Conditional.py index f163981..f0f06a8 100644 --- a/pylingual/control_flow_reconstruction/templates/Conditional.py +++ b/pylingual/control_flow_reconstruction/templates/Conditional.py @@ -1,13 +1,14 @@ from ..cft import ControlFlowTemplate, EdgeKind, register_template from ..utils import T, N, defer_source_to, run_is, has_no_lines, with_instructions, has_instval, starting_instructions, to_indented_source, make_try_match, without_top_level_instructions +from .Loop import BreakTemplate @register_template(1, 40) class IfElse(ControlFlowTemplate): template = T( if_header=~N("if_body", "else_body").with_cond(without_top_level_instructions("WITH_EXCEPT_START", "CHECK_EXC_MATCH", "FOR_ITER")), - if_body=~N("tail.").with_in_deg(1), - else_body=~N("tail.").with_cond(without_top_level_instructions("RERAISE", "END_FINALLY")).with_in_deg(1), + if_body=N.tail().with_in_deg(1).of_type(BreakTemplate) | ~N("tail.").with_in_deg(1), + else_body=N.tail().with_in_deg(1).of_type(BreakTemplate) | ~N("tail.").with_cond(without_top_level_instructions("RERAISE", "END_FINALLY")).with_in_deg(1), tail=N.tail(), ) diff --git a/test/Loop.py b/test/Loop.py index 26e6a8b..0340277 100644 --- a/test/Loop.py +++ b/test/Loop.py @@ -380,7 +380,6 @@ def x1_continue_with_else_nofallthru(): print("Else clause still executes after continue") print("end") -# 3.6 Naive break detection, an unexpected buffer POP_BLOCK to end # 3.9/3.11 Naive break detection, break statement is further up def y0_break_in_try_except(): for i in range(5): @@ -391,7 +390,6 @@ def y0_break_in_try_except(): except: print("Exception occurred") -# 3.6 Naive break detection, an unexpected buffer POP_BLOCK to end # 3.9/3.11 Naive break detection, break statement is further up def y1_break_in_try_except_nofallthru(): for i in range(5): @@ -403,6 +401,18 @@ def y1_break_in_try_except_nofallthru(): print("Exception occurred") print("end") +# 3.9/3.11 Naive break detection, break statement is further up +def y2_return_in_try_except_nofallthru(): + for i in range(5): + try: + if i == 3: + print(f"Value: {i}") + else: + break + except: + print("Exception occurred") + print("end") + # 3.6/3.9/3.11 No continue detection def z0_continue_in_try_except(): for i in range(5):