python2rust 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- python2rust/__init__.py +8 -0
- python2rust/__main__.py +5 -0
- python2rust/_core.py +44 -0
- python2rust/agent/__init__.py +0 -0
- python2rust/agent/agents.py +333 -0
- python2rust/agent/apply.py +516 -0
- python2rust/agent/finish.py +295 -0
- python2rust/agent/fixers.py +229 -0
- python2rust/agent/loop.py +441 -0
- python2rust/agent/patches.py +1165 -0
- python2rust/agent/port.py +579 -0
- python2rust/agent/prompts/judge_v2.md +96 -0
- python2rust/agent/review.py +34 -0
- python2rust/agent/rustlex.py +232 -0
- python2rust/cli/__init__.py +0 -0
- python2rust/cli/doctor.py +62 -0
- python2rust/cli/inputs.py +105 -0
- python2rust/cli/main.py +1162 -0
- python2rust/cli/ops.py +642 -0
- python2rust/cli/workflow.py +178 -0
- python2rust/core/__init__.py +38 -0
- python2rust/core/config.py +263 -0
- python2rust/core/constructs.py +124 -0
- python2rust/core/diagnostics.py +280 -0
- python2rust/core/emit/__init__.py +5 -0
- python2rust/core/emit/crates.py +51 -0
- python2rust/core/emit/render.py +561 -0
- python2rust/core/emit/schema.py +168 -0
- python2rust/core/emit/translate.py +304 -0
- python2rust/core/frontend/__init__.py +1 -0
- python2rust/core/frontend/module.py +896 -0
- python2rust/core/frontend/names.py +129 -0
- python2rust/core/frontend/project.py +277 -0
- python2rust/core/frontend/project_imports.py +173 -0
- python2rust/core/frontend/symbols.py +132 -0
- python2rust/core/frontend/walrus.py +59 -0
- python2rust/core/ir/__init__.py +1 -0
- python2rust/core/ir/nodes.py +675 -0
- python2rust/core/ir/rtypes.py +122 -0
- python2rust/core/ir/types.py +326 -0
- python2rust/core/lower/__init__.py +1 -0
- python2rust/core/lower/calls.py +1125 -0
- python2rust/core/lower/context.py +223 -0
- python2rust/core/lower/exceptions.py +317 -0
- python2rust/core/lower/exprs.py +845 -0
- python2rust/core/lower/fstrings.py +141 -0
- python2rust/core/lower/funcs.py +276 -0
- python2rust/core/lower/lowerer.py +366 -0
- python2rust/core/lower/modelcalls.py +195 -0
- python2rust/core/lower/models.py +645 -0
- python2rust/core/lower/stmts.py +722 -0
- python2rust/core/lower/testing.py +601 -0
- python2rust/core/lower/validation.py +253 -0
- python2rust/core/passes/__init__.py +1 -0
- python2rust/core/passes/aliasing.py +311 -0
- python2rust/core/passes/analysis.py +207 -0
- python2rust/core/passes/devirt.py +329 -0
- python2rust/core/passes/ownership.py +259 -0
- python2rust/core/passes/raises.py +275 -0
- python2rust/core/rt_lib.rs +2071 -0
- python2rust/data/README.md +81 -0
- python2rust/data/py2many_builtins.toml +4744 -0
- python2rust/errors.py +28 -0
- python2rust/py.typed +0 -0
- python2rust/report/__init__.py +0 -0
- python2rust/report/constructs.py +31 -0
- python2rust/report/contract.py +688 -0
- python2rust/report/corpus.py +323 -0
- python2rust/report/explain.py +168 -0
- python2rust/report/schemas.py +914 -0
- python2rust/server/__init__.py +0 -0
- python2rust/server/mcp_server.py +289 -0
- python2rust/server/mcp_workflow.py +100 -0
- python2rust/skill/SKILL.md +214 -0
- python2rust/verify/__init__.py +0 -0
- python2rust/verify/check.py +1135 -0
- python2rust/verify/equiv/__init__.py +1264 -0
- python2rust/verify/equiv/compare.py +455 -0
- python2rust/verify/equiv/harness.py +505 -0
- python2rust/verify/equiv/judge.py +358 -0
- python2rust/verify/equiv/procs.py +255 -0
- python2rust/verify/equiv/runner.py +248 -0
- python2rust/verify/equiv/types.py +543 -0
- python2rust/verify/keystore.py +76 -0
- python2rust/verify/ladder.py +396 -0
- python2rust/verify/sandbox.py +370 -0
- python2rust/verify/testrun.py +280 -0
- python2rust/workflow/__init__.py +0 -0
- python2rust/workflow/brief.py +312 -0
- python2rust/workflow/interview.py +366 -0
- python2rust/workflow/layout.py +103 -0
- python2rust/workflow/planner.py +409 -0
- python2rust/workflow/project.py +134 -0
- python2rust/workflow/schemas.py +265 -0
- python2rust/workflow/state.py +392 -0
- python2rust/workflow/staticscan.py +209 -0
- python2rust-0.1.0.dist-info/METADATA +395 -0
- python2rust-0.1.0.dist-info/RECORD +102 -0
- python2rust-0.1.0.dist-info/WHEEL +4 -0
- python2rust-0.1.0.dist-info/entry_points.txt +4 -0
- python2rust-0.1.0.dist-info/licenses/LICENSE +21 -0
- python2rust-0.1.0.dist-info/licenses/NOTICE +65 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"""Cross-module symbol table for project mode: what each translated module exports.
|
|
2
|
+
|
|
3
|
+
A module's `ModuleSymbols` holds the definitions other modules may import (function signatures,
|
|
4
|
+
models, enums, constants, exception classes, type aliases), the Rust type names it defines (so an
|
|
5
|
+
importer can `use` the types a signature mentions), and its re-exports (`from .x import y` in an
|
|
6
|
+
`__init__.py`). `ProjectIndex.lookup` follows re-export chains.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from dataclasses import dataclass, field
|
|
12
|
+
from typing import Any
|
|
13
|
+
|
|
14
|
+
from ..ir import nodes as ir
|
|
15
|
+
from ..ir.rtypes import RType
|
|
16
|
+
from ..lower.context import ConstInfo, Ctx, EnumInfo, FuncSig, ModelInfo
|
|
17
|
+
from .names import mangle
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass
|
|
21
|
+
class ModuleSymbols:
|
|
22
|
+
name: str
|
|
23
|
+
is_package: bool = False
|
|
24
|
+
sigs: dict[str, FuncSig] = field(default_factory=dict)
|
|
25
|
+
models: dict[str, ModelInfo] = field(default_factory=dict)
|
|
26
|
+
enums: dict[str, EnumInfo] = field(default_factory=dict)
|
|
27
|
+
consts: dict[str, ConstInfo] = field(default_factory=dict)
|
|
28
|
+
exceptions: dict[str, str | None] = field(default_factory=dict)
|
|
29
|
+
aliases: dict[str, RType] = field(default_factory=dict)
|
|
30
|
+
alias_meta: dict[str, list] = field(default_factory=dict) # Annotated metadata of aliases
|
|
31
|
+
types: set[str] = field(default_factory=set) # Rust type names defined in this module
|
|
32
|
+
lit_enums: dict[str, dict[object, str]] = field(default_factory=dict)
|
|
33
|
+
unions: dict[str, list[tuple[str, RType]]] = field(default_factory=dict)
|
|
34
|
+
reexports: dict[str, tuple[str, str]] = field(default_factory=dict) # local -> (module, name)
|
|
35
|
+
has_init: bool = False # the module has import-time code: `py2rs_module_init()` exists
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@dataclass(frozen=True)
|
|
39
|
+
class Symbol:
|
|
40
|
+
"""A resolved import target: `kind` is func/model/enum/const/exception/alias/module."""
|
|
41
|
+
|
|
42
|
+
kind: str
|
|
43
|
+
module: str # the defining module (for kind "module": the module itself)
|
|
44
|
+
name: str
|
|
45
|
+
obj: Any = None
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@dataclass
|
|
49
|
+
class ProjectIndex:
|
|
50
|
+
"""All modules of a project (known up front) and the symbols of those already translated."""
|
|
51
|
+
|
|
52
|
+
names: set[str] # dotted names of every module and package
|
|
53
|
+
packages: set[str] = field(default_factory=set)
|
|
54
|
+
modules: dict[str, ModuleSymbols] = field(default_factory=dict)
|
|
55
|
+
catchable: set[str] = field(
|
|
56
|
+
default_factory=set
|
|
57
|
+
) # exception classes caught anywhere in the project
|
|
58
|
+
|
|
59
|
+
@property
|
|
60
|
+
def roots(self) -> set[str]:
|
|
61
|
+
return {n.split(".")[0] for n in self.names}
|
|
62
|
+
|
|
63
|
+
def rust_path(self, module: str) -> str:
|
|
64
|
+
return "::".join(["crate", *(mangle(p) for p in module.split("."))])
|
|
65
|
+
|
|
66
|
+
def lookup(
|
|
67
|
+
self, module: str, name: str, _seen: frozenset[tuple[str, str]] = frozenset()
|
|
68
|
+
) -> Symbol | None:
|
|
69
|
+
"""Resolve `name` in `module`, following re-exports; a submodule counts as a symbol."""
|
|
70
|
+
if (module, name) in _seen:
|
|
71
|
+
return None
|
|
72
|
+
syms = self.modules.get(module)
|
|
73
|
+
if syms is not None:
|
|
74
|
+
for kind, table in (
|
|
75
|
+
("func", syms.sigs), ("model", syms.models), ("enum", syms.enums),
|
|
76
|
+
("const", syms.consts), ("exception", syms.exceptions), ("alias", syms.aliases),
|
|
77
|
+
): # fmt: skip
|
|
78
|
+
if name in table:
|
|
79
|
+
return Symbol(kind, module, name, table[name])
|
|
80
|
+
if name in syms.reexports:
|
|
81
|
+
origin, original = syms.reexports[name]
|
|
82
|
+
return self.lookup(origin, original, _seen | {(module, name)})
|
|
83
|
+
if f"{module}.{name}" in self.names:
|
|
84
|
+
return Symbol("module", f"{module}.{name}", name)
|
|
85
|
+
return None
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
@dataclass(frozen=True)
|
|
89
|
+
class ProjectContext:
|
|
90
|
+
"""What a single translation needs to know about the project it belongs to."""
|
|
91
|
+
|
|
92
|
+
index: ProjectIndex
|
|
93
|
+
module: str # dotted name of the module being translated
|
|
94
|
+
is_package: bool = False
|
|
95
|
+
|
|
96
|
+
def absolute(self, module: str | None, level: int) -> str | None:
|
|
97
|
+
"""Resolve a (possibly relative) `from` import to a dotted module name."""
|
|
98
|
+
if level == 0:
|
|
99
|
+
return module or ""
|
|
100
|
+
parts = self.module.split(".")
|
|
101
|
+
base = parts if self.is_package else parts[:-1]
|
|
102
|
+
if level - 1 > len(base):
|
|
103
|
+
return None
|
|
104
|
+
base = base[: len(base) - (level - 1)]
|
|
105
|
+
return ".".join([*base, *([module] if module else [])])
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def export_symbols(ctx: Ctx, module: ir.Module, project: ProjectContext) -> ModuleSymbols:
|
|
109
|
+
"""Snapshot what the translated module defines (imports are excluded; re-exports are tracked)."""
|
|
110
|
+
|
|
111
|
+
def mine(table: dict) -> dict:
|
|
112
|
+
return {k: v for k, v in table.items() if k not in ctx.imported_names}
|
|
113
|
+
|
|
114
|
+
types = {i.name for i in module.items if isinstance(i, ir.Struct | ir.EnumDef | ir.TypeAlias)}
|
|
115
|
+
return ModuleSymbols(
|
|
116
|
+
name=project.module,
|
|
117
|
+
is_package=project.is_package,
|
|
118
|
+
sigs=mine(ctx.sigs),
|
|
119
|
+
models=mine(ctx.models),
|
|
120
|
+
enums=mine(ctx.enums),
|
|
121
|
+
consts=mine(ctx.consts),
|
|
122
|
+
exceptions=mine(ctx.exceptions),
|
|
123
|
+
aliases=mine(ctx.aliases),
|
|
124
|
+
alias_meta=mine(ctx.alias_meta),
|
|
125
|
+
types=types,
|
|
126
|
+
lit_enums=dict(ctx.lit_enums),
|
|
127
|
+
unions=dict(ctx.unions),
|
|
128
|
+
reexports=dict(ctx.reexports),
|
|
129
|
+
has_init=any(
|
|
130
|
+
isinstance(i, ir.Function) and i.name == "py2rs_module_init" for i in module.items
|
|
131
|
+
),
|
|
132
|
+
)
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"""Hoist `:=` out of a condition into preceding `let` bindings (evaluation order preserved)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import ast
|
|
6
|
+
import copy
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class _Hoister(ast.NodeTransformer):
|
|
10
|
+
def __init__(self) -> None:
|
|
11
|
+
self.found: list[tuple[str, ast.expr]] = []
|
|
12
|
+
self.blocked = False
|
|
13
|
+
|
|
14
|
+
def visit_NamedExpr(self, node: ast.NamedExpr) -> ast.AST:
|
|
15
|
+
value = self.visit(node.value)
|
|
16
|
+
self.found.append((node.target.id, value))
|
|
17
|
+
return ast.copy_location(ast.Name(id=node.target.id, ctx=ast.Load()), node)
|
|
18
|
+
|
|
19
|
+
def _guarded(self, node: ast.AST) -> ast.AST:
|
|
20
|
+
"""Operands that may not run (short-circuit, lambda, comprehension) cannot be hoisted."""
|
|
21
|
+
if any(isinstance(n, ast.NamedExpr) for n in ast.walk(node)):
|
|
22
|
+
self.blocked = True
|
|
23
|
+
return node
|
|
24
|
+
|
|
25
|
+
def visit_BoolOp(self, node: ast.BoolOp) -> ast.AST:
|
|
26
|
+
node.values[0] = self.visit(node.values[0])
|
|
27
|
+
for i in range(1, len(node.values)):
|
|
28
|
+
self._guarded(node.values[i])
|
|
29
|
+
return node
|
|
30
|
+
|
|
31
|
+
def visit_IfExp(self, node: ast.IfExp) -> ast.AST:
|
|
32
|
+
node.test = self.visit(node.test)
|
|
33
|
+
self._guarded(node.body)
|
|
34
|
+
self._guarded(node.orelse)
|
|
35
|
+
return node
|
|
36
|
+
|
|
37
|
+
def visit_Lambda(self, node: ast.Lambda) -> ast.AST:
|
|
38
|
+
return self._guarded(node)
|
|
39
|
+
|
|
40
|
+
def visit_ListComp(self, node: ast.ListComp) -> ast.AST:
|
|
41
|
+
return self._guarded(node)
|
|
42
|
+
|
|
43
|
+
def visit_SetComp(self, node: ast.SetComp) -> ast.AST:
|
|
44
|
+
return self._guarded(node)
|
|
45
|
+
|
|
46
|
+
def visit_DictComp(self, node: ast.DictComp) -> ast.AST:
|
|
47
|
+
return self._guarded(node)
|
|
48
|
+
|
|
49
|
+
def visit_GeneratorExp(self, node: ast.GeneratorExp) -> ast.AST:
|
|
50
|
+
return self._guarded(node)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def hoist_walrus(test: ast.expr) -> tuple[list[tuple[str, ast.expr]], ast.expr] | None:
|
|
54
|
+
"""(bindings, rewritten test), or None when a walrus sits where it cannot be hoisted."""
|
|
55
|
+
if not any(isinstance(n, ast.NamedExpr) for n in ast.walk(test)):
|
|
56
|
+
return [], test
|
|
57
|
+
h = _Hoister()
|
|
58
|
+
new = h.visit(copy.deepcopy(test))
|
|
59
|
+
return None if h.blocked else (h.found, new)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Typed Rust IR (`nodes`), Rust types (`rtypes`) and the annotation mapper (`types`)."""
|