fix(code-runtime-python): stop the program's compile from inheriting the module's future annotations

bootstrap.py imports from __future__ import annotations; compile(wrapped) was
inheriting that PEP 563 flag, stringifying the program's type annotations and
changing the semantics of a legal program that reads f.__annotations__ at
runtime. compile(..., dont_inherit=True) stops the leak; a regression test
defines an annotated function and asserts the annotation is the live int class,
verified fail-before by removing dont_inherit (the test turns red).
This commit is contained in:
Chinesezjc
2026-08-31 14:49:54 +08:00
committed by Tianyi Cui
parent 6a659df999
commit 44205c4949
2 changed files with 23 additions and 1 deletions
@@ -1300,7 +1300,11 @@ async def _run(channel: ProtocolChannel) -> None:
ast.copy_location(wrapper, anchor)
wrapped = ast.Module(body=[wrapper], type_ignores=[])
ast.fix_missing_locations(wrapped)
code = compile(wrapped, "<model>", "exec")
# `dont_inherit=True` stops this module's `from __future__ import
# annotations` (line 14) from leaking into the program's compile: PEP 563
# would otherwise stringify the program's type annotations, changing the
# semantics of a legal program that reads `f.__annotations__` at runtime.
code = compile(wrapped, "<model>", "exec", dont_inherit=True)
exec(code, ns) # noqa: S102 -- defines __dsh_main__; executing model code is the point
value = await ns["__dsh_main__"]()
die_if_cpu_exhausted(cpu_seconds)