This commit is contained in:
caandt
2025-06-12 17:07:41 -05:00
parent 34c5fe521d
commit 376ae3d72f
3 changed files with 42 additions and 15 deletions
+5 -3
View File
@@ -72,9 +72,11 @@ def run(file: Path, out_dir: Path, version: PythonVersion, print=False):
edit_pyc_lines(pyc, src_lines) edit_pyc_lines(pyc, src_lines)
cfts = {bc.codeobj: bc_to_cft(bc) for bc in pyc.iter_bytecodes()} cfts = {bc.codeobj: bc_to_cft(bc) for bc in pyc.iter_bytecodes()}
out_src = (str(SourceContext(pyc, src_lines, cfts))) out_src = str(SourceContext(pyc, src_lines, cfts))
try: out_src = normalize_source(out_src) try:
except: pass out_src = normalize_source(out_src)
except:
pass
out_path = out_dir / "b.py" out_path = out_dir / "b.py"
out_path.write_text(out_src, encoding="utf-8") out_path.write_text(out_src, encoding="utf-8")
@@ -4,7 +4,22 @@ from typing import override
from .Block import BlockTemplate from .Block import BlockTemplate
from .Conditional import IfElse, IfThen from .Conditional import IfElse, IfThen
from ..cft import ControlFlowTemplate, EdgeCategory, EdgeKind, InstTemplate, SourceLine, SourceContext, register_template 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")) 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): if x := BareExcept3_11.try_match(cfg, node):
return x return x
class Except3_10(ControlFlowTemplate): class Except3_10(ControlFlowTemplate):
@classmethod @classmethod
@override @override
@@ -33,6 +49,7 @@ class Except3_10(ControlFlowTemplate):
if isinstance(node, Except3_10): if isinstance(node, Except3_10):
return node return node
@register_template(0, 0, *versions_from(3, 12)) @register_template(0, 0, *versions_from(3, 12))
class Try3_12(ControlFlowTemplate): class Try3_12(ControlFlowTemplate):
template = T( template = T(
@@ -99,7 +116,9 @@ class TryElse3_12(ControlFlowTemplate):
else: else:
{try_else} {try_else}
""" """
@register_template(0, 1, *versions_from(3, 10))
@register_template(0, 1, (3, 10))
class Try3_10(ControlFlowTemplate): class Try3_10(ControlFlowTemplate):
template = T( template = T(
try_header=N("try_body"), try_header=N("try_body"),
@@ -129,7 +148,9 @@ class Try3_10(ControlFlowTemplate):
{try_body} {try_body}
{except_body} {except_body}
""" """
@register_template(0, 0, *versions_from(3, 10))
@register_template(0, 0, (3, 10))
class TryElse3_10(ControlFlowTemplate): class TryElse3_10(ControlFlowTemplate):
template = T( template = T(
try_header=N("try_body"), try_header=N("try_body"),
@@ -165,6 +186,7 @@ class TryElse3_10(ControlFlowTemplate):
{else_body} {else_body}
""" """
class ExcBody3_10(ControlFlowTemplate): class ExcBody3_10(ControlFlowTemplate):
@classmethod @classmethod
@override @override
@@ -173,6 +195,7 @@ class ExcBody3_10(ControlFlowTemplate):
return x return x
return node return node
class NamedExc3_10(ExcBody3_10): class NamedExc3_10(ExcBody3_10):
template = T( template = T(
header=N("body", None).with_cond(with_instructions("POP_TOP", "STORE_FAST")), 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") to_indented_source = defer_source_to("body")
# @register_template(0, 0, *versions_from(3, 10))
class BareExcept3_10(Except3_10): class BareExcept3_10(Except3_10):
template = T( template = T(
except_body=N("tail.", None).of_type(BlockTemplate).with_cond(with_instructions("POP_EXCEPT")), 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( try_match = revert_on_fail(
make_try_match( make_try_match(
{ {
EdgeKind.Fall: "tail", EdgeKind.Fall: "tail",
}, },
@@ -210,17 +232,17 @@ class BareExcept3_10(Except3_10):
{except_body} {except_body}
""" """
#@register_template(0, 0, *versions_from(3, 10))
class ExceptExc3_10(Except3_10): class ExceptExc3_10(Except3_10):
template = T( template = T(
except_header = N("body", "falsejump"), except_header=N("body", "falsejump"),
body = N("tail.").of_subtemplate(ExcBody3_10), body=N("tail.").of_subtemplate(ExcBody3_10),
falsejump = N("tail.").of_subtemplate(Except3_10), falsejump=N("tail.").of_subtemplate(Except3_10),
tail = N.tail(), tail=N.tail(),
) )
try_match = revert_on_fail( try_match = revert_on_fail(
make_try_match( make_try_match(
{ {
EdgeKind.Fall: "tail", EdgeKind.Fall: "tail",
}, },
@@ -238,6 +260,7 @@ class ExceptExc3_10(Except3_10):
{falsejump} {falsejump}
""" """
class BareExcept3_11(Except3_11): class BareExcept3_11(Except3_11):
template = T( template = T(
except_body=N("except_footer", None, "reraise"), except_body=N("except_footer", None, "reraise"),
@@ -74,6 +74,7 @@ def without_instructions(*opnames: str):
return check_instructions return check_instructions
def with_instructions(*opnames: str): def with_instructions(*opnames: str):
def check_instructions(cfg: CFG, node: ControlFlowTemplate | None) -> bool: def check_instructions(cfg: CFG, node: ControlFlowTemplate | None) -> bool:
ops = {x.opname for x in node.get_instructions()} ops = {x.opname for x in node.get_instructions()}
@@ -81,6 +82,7 @@ def with_instructions(*opnames: str):
return check_instructions return check_instructions
def without_top_level_instructions(*opnames: str): def without_top_level_instructions(*opnames: str):
from .templates.Block import BlockTemplate from .templates.Block import BlockTemplate