Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/inferrsi/box/kernel/swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -1689,7 +1689,9 @@ def pairs_from(parent):
constructed = _code(class_units[top], path)
construction_nested = tuple(
(a, b)
for a, b in _nested_pairs(constructed, replacement)
for a, b, _ in _nested_pairs(
constructed, replacement, new_signatures, new_signatures
)
if a is not None and b is not None
)
plan.reversals.extend(
Expand Down
70 changes: 70 additions & 0 deletions tests/box/conformance/test_structural_transactions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""CPU contracts for structural planning, one live namespace, and reversible settlement."""

import gc
import importlib
import linecache
import sys
Expand Down Expand Up @@ -2059,3 +2060,72 @@ def test_failed_construction_leaking_new_closure_isolates(setup, action):
create=creating,
)
assert box.install(leak_fail)["code"] == "in_doubt" and box.isolated and len(leaked) == 1


@pytest.mark.parametrize("action", ["edit", "create"])
def test_construction_time_same_line_lambdas_and_local_capture_round_trip(setup, action):
module, _, _, box = setup
creating = action == "create"
target_name = f"{module.__package__}.capture_closures" if creating else module.__name__
prefix = "" if creating else BASE + "\n"
p1_src = (
prefix
+ "def factory(base, extra=1):\n"
+ " def inner(scale=2):\n"
+ " return base * scale\n"
+ " return inner\n\n"
+ "def make_pair(base):\n"
+ " return (lambda x: base + x, lambda x, y: base + x + y)\n\n"
+ "def make_nested(base):\n"
+ " return (lambda x: (lambda y: base + x + y), lambda x, y: base + x * y)\n\n"
+ "class Builder:\n"
+ " def make(self, base):\n"
+ " return (lambda x: base + x, lambda x, y: base + x + y)\n\n"
+ "CALLBACK = factory(3)\n"
+ "PAIR = make_pair(3)\n"
+ "NESTED = make_nested(4)\n"
+ "CLASS_PAIR = Builder().make(10)\n"
)
assert box.install(patch(box, target_name, p1_src, name="p1", create=creating))["code"] == "ok"
target_mod = sys.modules[target_name]
assert target_mod.CALLBACK() == 6
assert target_mod.PAIR[0](2) == 5 and target_mod.PAIR[1](2, 4) == 9
assert target_mod.NESTED[0](2)(3) == 9 and target_mod.NESTED[1](2, 3) == 10
assert target_mod.CLASS_PAIR[0](1) == 11 and target_mod.CLASS_PAIR[1](1, 2) == 13

same_line_edit = p1_src.replace(
"lambda x: base + x, lambda x, y: base + x + y",
"lambda x: base + x + 1, lambda x, y: base + x + y + 1",
1,
)
assert box.install(patch(box, target_name, same_line_edit, name="same-line"))["code"] == (
"restart_required"
)

if sys.implementation.name == "cpython" and not gc.get_freeze_count():
cb_id = id(target_mod.CALLBACK)
capture_src = p1_src.replace(
"return base * scale\n", "return base * scale + 5\n"
).replace(
" return inner", " (lambda: extra)()\n return inner"
)
assert box.install(patch(box, target_name, capture_src, name="p2"))["code"] == "ok"
assert id(target_mod.CALLBACK) == cb_id
assert target_mod.CALLBACK() == 11 and target_mod.factory(3)() == 11
partial = box.uninstall("p2")
assert partial["code"] == "ok" and partial["details"]["patch_stack"] == ["p1"]
assert id(target_mod.CALLBACK) == cb_id and target_mod.CALLBACK() == 6

del target_mod
assert box.uninstall("p1")["code"] == "ok" and not box.isolated
if creating:
assert target_name not in sys.modules
else:
assert {"CALLBACK", "PAIR", "NESTED", "CLASS_PAIR"}.isdisjoint(vars(module))

assert (
box.install(patch(box, target_name, p1_src, name="p1-esc", create=creating))["code"] == "ok"
)
escaped_lambda = sys.modules[target_name].CLASS_PAIR[1]
assert box.uninstall("p1-esc")["code"] == "in_doubt" and box.isolated
assert escaped_lambda(1, 2) == 13
Loading