From d4acc3cfcee2037655cabec075d282b765fe3888 Mon Sep 17 00:00:00 2001 From: Xinlong Hu Date: Sat, 7 Jun 2025 12:48:06 -0500 Subject: [PATCH 1/5] Partial With 3.10 Implementation --- .../templates/With.py | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/pylingual/control_flow_reconstruction/templates/With.py b/pylingual/control_flow_reconstruction/templates/With.py index 26f2276..673030a 100644 --- a/pylingual/control_flow_reconstruction/templates/With.py +++ b/pylingual/control_flow_reconstruction/templates/With.py @@ -39,3 +39,37 @@ class With3_12(ControlFlowTemplate): {setup_with} {with_body} """ + +class WithCleanup3_10(ControlFlowTemplate): + template = T( + start=~N("reraise", "poptop").with_cond( + starting_instructions("WITH_EXCEPT_START"), # 3.10 + ), + reraise=+N().with_cond(exact_instructions("RERAISE")).with_in_deg(1), + poptop=~N("tail.", None).with_cond(starting_instructions("POP_TOP")).with_in_deg(1), + tail=N.tail(), + ) + + try_match = make_try_match({EdgeKind.Fall: "tail"}, "start", "reraise", "poptop") + + @override + def to_indented_source(self, source): + return [] + +@register_template(0, 10, *versions_from(3, 10)) +class With3_10(ControlFlowTemplate): + template = T( + setup_with=~N("with_body", None), + with_body=N("normal_cleanup", None, "exc_cleanup").with_in_deg(1), + exc_cleanup=N.tail().of_subtemplate(WithCleanup3_10).with_in_deg(1), + normal_cleanup=~N.tail(), + ) + + try_match = make_try_match({EdgeKind.Fall: "normal_cleanup"}, "setup_with", "with_body", "exc_cleanup") + + @to_indented_source + def to_indented_source(): + """ + {setup_with} + {with_body} + """ \ No newline at end of file From 0d759dab4dd07f116c17fcbc1dbb1b0511e14a4c Mon Sep 17 00:00:00 2001 From: Xinlong Hu <118075581+XinlongCS@users.noreply.github.com> Date: Mon, 9 Jun 2025 11:17:59 -0500 Subject: [PATCH 2/5] Update With.py Fixes test case j and k --- pylingual/control_flow_reconstruction/templates/With.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pylingual/control_flow_reconstruction/templates/With.py b/pylingual/control_flow_reconstruction/templates/With.py index 673030a..05eff75 100644 --- a/pylingual/control_flow_reconstruction/templates/With.py +++ b/pylingual/control_flow_reconstruction/templates/With.py @@ -54,13 +54,15 @@ class WithCleanup3_10(ControlFlowTemplate): @override def to_indented_source(self, source): - return [] + """ + {poptop} + """ @register_template(0, 10, *versions_from(3, 10)) class With3_10(ControlFlowTemplate): template = T( setup_with=~N("with_body", None), - with_body=N("normal_cleanup", None, "exc_cleanup").with_in_deg(1), + with_body=N("normal_cleanup.", None, "exc_cleanup").with_in_deg(1), exc_cleanup=N.tail().of_subtemplate(WithCleanup3_10).with_in_deg(1), normal_cleanup=~N.tail(), ) @@ -72,4 +74,5 @@ class With3_10(ControlFlowTemplate): """ {setup_with} {with_body} + {exc_cleanup} """ \ No newline at end of file From 195fe1379fb99776d77ce6e88a709bb7b60404ef Mon Sep 17 00:00:00 2001 From: Xinlong Hu <118075581+XinlongCS@users.noreply.github.com> Date: Fri, 13 Jun 2025 13:46:48 -0500 Subject: [PATCH 3/5] With statement 3.12/3.13 All With statement 3.12 TC completed and most of With statement 3.13 TC completed --- .../templates/With.py | 64 ++++++++++++------- 1 file changed, 42 insertions(+), 22 deletions(-) diff --git a/pylingual/control_flow_reconstruction/templates/With.py b/pylingual/control_flow_reconstruction/templates/With.py index 05eff75..091327a 100644 --- a/pylingual/control_flow_reconstruction/templates/With.py +++ b/pylingual/control_flow_reconstruction/templates/With.py @@ -2,48 +2,50 @@ from typing import override from ..cft import ControlFlowTemplate, EdgeKind, register_template from ..utils import T, N, exact_instructions, starting_instructions, to_indented_source, make_try_match, versions_from - -class WithCleanup3_12(ControlFlowTemplate): +class WithCleanup3_11(ControlFlowTemplate): template = T( start=N("reraise", "poptop", "exc").with_cond( - exact_instructions("PUSH_EXC_INFO", "WITH_EXCEPT_START", "POP_JUMP_IF_TRUE"), # 3.12 - exact_instructions("PUSH_EXC_INFO", "WITH_EXCEPT_START", "TO_BOOL", "POP_JUMP_IF_TRUE"), # 3.13 + starting_instructions("PUSH_EXC_INFO", "WITH_EXCEPT_START"), # 3.11 and later ), reraise=N(None, None, "exc").with_cond(exact_instructions("RERAISE")).with_in_deg(1), - poptop=N("tail", None, "exc").with_cond(exact_instructions("POP_TOP")).with_in_deg(1), + poptop=N("pop_exc", None, "exc").with_cond(exact_instructions("POP_TOP")).with_in_deg(1), exc=+N().with_cond(exact_instructions("COPY", "POP_EXCEPT", "RERAISE")).with_in_deg(3), - tail=~N.tail().with_cond(starting_instructions("POP_EXCEPT", "POP_TOP", "POP_TOP")).with_in_deg(1), + pop_exc=~N("tail.", None).with_cond(starting_instructions("POP_EXCEPT", "POP_TOP")).with_in_deg(1), + tail=N.tail(), ) - try_match = make_try_match({}, "start", "reraise", "poptop", "exc", "tail") + try_match = make_try_match({EdgeKind.Fall: "tail"}, "start", "reraise", "poptop", "exc", "pop_exc") - @override + @to_indented_source def to_indented_source(self, source): - return [] + """ + {pop_exc} + """ - -@register_template(0, 10, *versions_from(3, 12)) -class With3_12(ControlFlowTemplate): +@register_template(0, 10, (3, 11), (3, 12), (3, 13)) +class With3_11(ControlFlowTemplate): template = T( setup_with=~N("with_body", None), - with_body=N("normal_cleanup", None, "exc_cleanup").with_in_deg(1), - exc_cleanup=N.tail().of_subtemplate(WithCleanup3_12).with_in_deg(1), - normal_cleanup=~N.tail(), + with_body=N("normal_cleanup.", None, "exc_cleanup").with_in_deg(1), + exc_cleanup=~N("tail.", None).of_subtemplate(WithCleanup3_11).with_in_deg(1), + normal_cleanup=~N("tail.", None).with_in_deg(1), + tail=N.tail(), ) - try_match = make_try_match({EdgeKind.Fall: "normal_cleanup"}, "setup_with", "with_body", "exc_cleanup") + try_match = make_try_match({EdgeKind.Fall: "tail"}, "setup_with", "with_body", "exc_cleanup", "normal_cleanup") @to_indented_source def to_indented_source(): """ {setup_with} {with_body} + {exc_cleanup} """ -class WithCleanup3_10(ControlFlowTemplate): +class WithCleanup3_9(ControlFlowTemplate): template = T( start=~N("reraise", "poptop").with_cond( - starting_instructions("WITH_EXCEPT_START"), # 3.10 + starting_instructions("WITH_EXCEPT_START"), # 3.9 & 3.10 ), reraise=+N().with_cond(exact_instructions("RERAISE")).with_in_deg(1), poptop=~N("tail.", None).with_cond(starting_instructions("POP_TOP")).with_in_deg(1), @@ -52,18 +54,18 @@ class WithCleanup3_10(ControlFlowTemplate): try_match = make_try_match({EdgeKind.Fall: "tail"}, "start", "reraise", "poptop") - @override + @to_indented_source def to_indented_source(self, source): """ {poptop} """ -@register_template(0, 10, *versions_from(3, 10)) -class With3_10(ControlFlowTemplate): +@register_template(0, 10, (3, 9), (3, 10)) +class With3_9(ControlFlowTemplate): template = T( setup_with=~N("with_body", None), with_body=N("normal_cleanup.", None, "exc_cleanup").with_in_deg(1), - exc_cleanup=N.tail().of_subtemplate(WithCleanup3_10).with_in_deg(1), + exc_cleanup=N.tail().of_subtemplate(WithCleanup3_9).with_in_deg(1), normal_cleanup=~N.tail(), ) @@ -75,4 +77,22 @@ class With3_10(ControlFlowTemplate): {setup_with} {with_body} {exc_cleanup} + """ + +@register_template(0, 10, (3, 6), (3, 7), (3, 8)) +class With3_6(ControlFlowTemplate): + template = T( + setup_with=~N("with_body", None), + with_body=N("buffer_block.", None, "normal_cleanup").with_in_deg(1), + buffer_block=~N("normal_cleanup.", None).with_in_deg(1), + normal_cleanup=~N.tail(), + ) + + try_match = make_try_match({EdgeKind.Fall: "normal_cleanup"}, "setup_with", "with_body", "buffer_block") + + @to_indented_source + def to_indented_source(): + """ + {setup_with} + {with_body} """ \ No newline at end of file From 8993af3fd7f9a9e6ddfb2c3bf4b34fdc33321360 Mon Sep 17 00:00:00 2001 From: Xinlong Hu <118075581+XinlongCS@users.noreply.github.com> Date: Fri, 13 Jun 2025 13:48:39 -0500 Subject: [PATCH 4/5] Await With Edgecase Not sure if it can be optimized into one Await template --- .../templates/Generator.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pylingual/control_flow_reconstruction/templates/Generator.py b/pylingual/control_flow_reconstruction/templates/Generator.py index c6d17e8..c7d21fe 100644 --- a/pylingual/control_flow_reconstruction/templates/Generator.py +++ b/pylingual/control_flow_reconstruction/templates/Generator.py @@ -19,6 +19,19 @@ class Await3_12(ControlFlowTemplate): to_indented_source = defer_source_to("awaited") +@register_template(0, 0) +class AwaitWith3_12(ControlFlowTemplate): + template = T( + awaited=~N("SEND", None).with_cond(no_back_edges), + SEND=~N("YIELD_VALUE", "CLEANUP_THROW").with_in_deg(2).with_cond(exact_instructions("SEND")), + YIELD_VALUE=N("JUMP_BACK_NO_INT", None, "CLEANUP_THROW").with_in_deg(1).with_cond(exact_instructions("YIELD_VALUE")), + JUMP_BACK_NO_INT=~N("SEND", None).with_cond(exact_instructions("JUMP_BACKWARD_NO_INTERRUPT")), + CLEANUP_THROW=N.tail(), + ) + + try_match = make_try_match({EdgeKind.Fall: "CLEANUP_THROW"}, "awaited", "SEND", "YIELD_VALUE", "JUMP_BACK_NO_INT") + + to_indented_source = defer_source_to("awaited") @register_template(0, 0) class Generator3_12(ControlFlowTemplate): From 53e888d397ede68d5121270e8c4576a5242bba26 Mon Sep 17 00:00:00 2001 From: Joel Flores Date: Mon, 23 Jun 2025 10:56:15 -0500 Subject: [PATCH 5/5] format --- .../control_flow_reconstruction/templates/Generator.py | 2 ++ pylingual/control_flow_reconstruction/templates/With.py | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/pylingual/control_flow_reconstruction/templates/Generator.py b/pylingual/control_flow_reconstruction/templates/Generator.py index c7d21fe..87be641 100644 --- a/pylingual/control_flow_reconstruction/templates/Generator.py +++ b/pylingual/control_flow_reconstruction/templates/Generator.py @@ -19,6 +19,7 @@ class Await3_12(ControlFlowTemplate): to_indented_source = defer_source_to("awaited") + @register_template(0, 0) class AwaitWith3_12(ControlFlowTemplate): template = T( @@ -33,6 +34,7 @@ class AwaitWith3_12(ControlFlowTemplate): to_indented_source = defer_source_to("awaited") + @register_template(0, 0) class Generator3_12(ControlFlowTemplate): template = T( diff --git a/pylingual/control_flow_reconstruction/templates/With.py b/pylingual/control_flow_reconstruction/templates/With.py index 091327a..55904b5 100644 --- a/pylingual/control_flow_reconstruction/templates/With.py +++ b/pylingual/control_flow_reconstruction/templates/With.py @@ -2,6 +2,7 @@ from typing import override from ..cft import ControlFlowTemplate, EdgeKind, register_template from ..utils import T, N, exact_instructions, starting_instructions, to_indented_source, make_try_match, versions_from + class WithCleanup3_11(ControlFlowTemplate): template = T( start=N("reraise", "poptop", "exc").with_cond( @@ -22,6 +23,7 @@ class WithCleanup3_11(ControlFlowTemplate): {pop_exc} """ + @register_template(0, 10, (3, 11), (3, 12), (3, 13)) class With3_11(ControlFlowTemplate): template = T( @@ -42,6 +44,7 @@ class With3_11(ControlFlowTemplate): {exc_cleanup} """ + class WithCleanup3_9(ControlFlowTemplate): template = T( start=~N("reraise", "poptop").with_cond( @@ -60,6 +63,7 @@ class WithCleanup3_9(ControlFlowTemplate): {poptop} """ + @register_template(0, 10, (3, 9), (3, 10)) class With3_9(ControlFlowTemplate): template = T( @@ -79,6 +83,7 @@ class With3_9(ControlFlowTemplate): {exc_cleanup} """ + @register_template(0, 10, (3, 6), (3, 7), (3, 8)) class With3_6(ControlFlowTemplate): template = T( @@ -95,4 +100,4 @@ class With3_6(ControlFlowTemplate): """ {setup_with} {with_body} - """ \ No newline at end of file + """