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)
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")
@@ -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")),
@@ -210,7 +232,7 @@ 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"),
@@ -238,6 +260,7 @@ class ExceptExc3_10(Except3_10):
{falsejump}
"""
class BareExcept3_11(Except3_11):
template = T(
except_body=N("except_footer", None, "reraise"),
@@ -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