From 376ae3d72fe9c3d53954c8fa42ca58519573f005 Mon Sep 17 00:00:00 2001 From: caandt Date: Thu, 12 Jun 2025 17:07:41 -0500 Subject: [PATCH] nit --- dev_scripts/cflow.py | 8 ++-- .../templates/Exception.py | 47 ++++++++++++++----- .../control_flow_reconstruction/utils.py | 2 + 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/dev_scripts/cflow.py b/dev_scripts/cflow.py index fc37f34..9c3ac74 100644 --- a/dev_scripts/cflow.py +++ b/dev_scripts/cflow.py @@ -72,9 +72,11 @@ def run(file: Path, out_dir: Path, version: PythonVersion, print=False): edit_pyc_lines(pyc, src_lines) cfts = {bc.codeobj: bc_to_cft(bc) for bc in pyc.iter_bytecodes()} - out_src = (str(SourceContext(pyc, src_lines, cfts))) - try: out_src = normalize_source(out_src) - except: pass + out_src = str(SourceContext(pyc, src_lines, cfts)) + try: + out_src = normalize_source(out_src) + except: + pass out_path = out_dir / "b.py" out_path.write_text(out_src, encoding="utf-8") diff --git a/pylingual/control_flow_reconstruction/templates/Exception.py b/pylingual/control_flow_reconstruction/templates/Exception.py index b0b7a88..012a66a 100644 --- a/pylingual/control_flow_reconstruction/templates/Exception.py +++ b/pylingual/control_flow_reconstruction/templates/Exception.py @@ -4,7 +4,22 @@ from typing import override from .Block import BlockTemplate from .Conditional import IfElse, IfThen from ..cft import ControlFlowTemplate, EdgeCategory, EdgeKind, InstTemplate, SourceLine, SourceContext, register_template -from ..utils import E, N, T, condense_mapping, defer_source_to, with_instructions, ending_instructions, exact_instructions, no_back_edges, revert_on_fail, starting_instructions, to_indented_source, make_try_match, versions_from, hook_template +from ..utils import ( + E, + N, + T, + condense_mapping, + defer_source_to, + with_instructions, + ending_instructions, + exact_instructions, + no_back_edges, + revert_on_fail, + starting_instructions, + to_indented_source, + make_try_match, + versions_from, +) reraise = +N().with_cond(exact_instructions("COPY", "POP_EXCEPT", "RERAISE")) @@ -20,6 +35,7 @@ class Except3_11(ControlFlowTemplate): if x := BareExcept3_11.try_match(cfg, node): return x + class Except3_10(ControlFlowTemplate): @classmethod @override @@ -33,6 +49,7 @@ class Except3_10(ControlFlowTemplate): if isinstance(node, Except3_10): return node + @register_template(0, 0, *versions_from(3, 12)) class Try3_12(ControlFlowTemplate): template = T( @@ -99,7 +116,9 @@ class TryElse3_12(ControlFlowTemplate): else: {try_else} """ -@register_template(0, 1, *versions_from(3, 10)) + + +@register_template(0, 1, (3, 10)) class Try3_10(ControlFlowTemplate): template = T( try_header=N("try_body"), @@ -129,7 +148,9 @@ class Try3_10(ControlFlowTemplate): {try_body} {except_body} """ -@register_template(0, 0, *versions_from(3, 10)) + + +@register_template(0, 0, (3, 10)) class TryElse3_10(ControlFlowTemplate): template = T( try_header=N("try_body"), @@ -165,6 +186,7 @@ class TryElse3_10(ControlFlowTemplate): {else_body} """ + class ExcBody3_10(ControlFlowTemplate): @classmethod @override @@ -173,6 +195,7 @@ class ExcBody3_10(ControlFlowTemplate): return x return node + class NamedExc3_10(ExcBody3_10): template = T( header=N("body", None).with_cond(with_instructions("POP_TOP", "STORE_FAST")), @@ -187,7 +210,6 @@ class NamedExc3_10(ExcBody3_10): to_indented_source = defer_source_to("body") -# @register_template(0, 0, *versions_from(3, 10)) class BareExcept3_10(Except3_10): template = T( except_body=N("tail.", None).of_type(BlockTemplate).with_cond(with_instructions("POP_EXCEPT")), @@ -195,7 +217,7 @@ class BareExcept3_10(Except3_10): ) try_match = revert_on_fail( - make_try_match( + make_try_match( { EdgeKind.Fall: "tail", }, @@ -210,17 +232,17 @@ class BareExcept3_10(Except3_10): {except_body} """ -#@register_template(0, 0, *versions_from(3, 10)) + class ExceptExc3_10(Except3_10): template = T( - except_header = N("body", "falsejump"), - body = N("tail.").of_subtemplate(ExcBody3_10), - falsejump = N("tail.").of_subtemplate(Except3_10), - tail = N.tail(), - ) + except_header=N("body", "falsejump"), + body=N("tail.").of_subtemplate(ExcBody3_10), + falsejump=N("tail.").of_subtemplate(Except3_10), + tail=N.tail(), + ) try_match = revert_on_fail( - make_try_match( + make_try_match( { EdgeKind.Fall: "tail", }, @@ -238,6 +260,7 @@ class ExceptExc3_10(Except3_10): {falsejump} """ + class BareExcept3_11(Except3_11): template = T( except_body=N("except_footer", None, "reraise"), diff --git a/pylingual/control_flow_reconstruction/utils.py b/pylingual/control_flow_reconstruction/utils.py index f318d75..cc6e254 100644 --- a/pylingual/control_flow_reconstruction/utils.py +++ b/pylingual/control_flow_reconstruction/utils.py @@ -74,6 +74,7 @@ def without_instructions(*opnames: str): return check_instructions + def with_instructions(*opnames: str): def check_instructions(cfg: CFG, node: ControlFlowTemplate | None) -> bool: ops = {x.opname for x in node.get_instructions()} @@ -81,6 +82,7 @@ def with_instructions(*opnames: str): return check_instructions + def without_top_level_instructions(*opnames: str): from .templates.Block import BlockTemplate