diff --git a/pylingual/control_flow_reconstruction/templates/Exception.py b/pylingual/control_flow_reconstruction/templates/Exception.py index 1ec2159..7e611a2 100644 --- a/pylingual/control_flow_reconstruction/templates/Exception.py +++ b/pylingual/control_flow_reconstruction/templates/Exception.py @@ -11,6 +11,7 @@ from ..utils import ( condense_mapping, defer_source_to, with_instructions, + without_instructions, ending_instructions, exact_instructions, no_back_edges, @@ -630,8 +631,8 @@ class TryElse3_6(ControlFlowTemplate): template = T( try_header=~N("try_body").with_cond(exact_instructions("SETUP_EXCEPT"), exact_instructions("SETUP_FINALLY")), try_body=N("try_footer.", None, "except_body"), - try_footer=~N("else_body").with_in_deg(1), - except_body=~N("tail.").with_in_deg(1).of_subtemplate(Except3_6), + try_footer=~N("else_body").with_in_deg(1), + except_body=~N("tail.").with_in_deg(1).of_subtemplate(Except3_6).with_cond(without_instructions("RETURN_VALUE")), else_body=~N("tail.").with_in_deg(1), tail=N.tail(), ) @@ -660,6 +661,36 @@ class TryElse3_6(ControlFlowTemplate): {else_body} """ +@register_template(0, 0, (3, 6), (3, 7), (3, 8)) +class ReturnFinally3_6(ControlFlowTemplate): + template = T( + try_header=~N("try_body").with_cond(exact_instructions("SETUP_FINALLY")), + try_body=N(None, None, "fail_body").with_cond(with_instructions("LOAD_CONST","RETURN_VALUE")), + fail_body=~N("tail."), + tail=N.tail(), + ) + + try_match = revert_on_fail( + make_try_match( + { + EdgeKind.Fall: "tail", + }, + "try_header", + "try_body", + "fail_body", + ) + ) + + @to_indented_source + def to_indented_source(): + """ + {try_header} + try: + {try_body} + finally: + {fail_body} + """ + class BareExcept3_6(Except3_6): template = T( @@ -691,12 +722,13 @@ class TryFinally3_6(ControlFlowTemplate): tail=N.tail(), ) template2 = T( - try_except=N("finally_tail", None, "fail_body").of_type(TryElse3_6, Try3_6), + try_except=N("finally_tail", None, "fail_body").of_type(TryElse3_6, Try3_6, ReturnFinally3_6), finally_tail=N("finally_body", None, "fail_body"), finally_body=~N("fail_body").with_in_deg(1).with_cond(no_back_edges), fail_body=N("tail.").with_cond(with_instructions("POP_TOP", "END_FINALLY"),with_instructions("LOAD_CONST", "RETURN_VALUE")), tail=N.tail(), ) + cutoff: int