Spaces:
Running
Running
File size: 3,051 Bytes
db5917b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | """Every shipped module must compile on the Python the Space actually runs.
This exists because it already happened: a backslash inside an f-string
expression is legal from 3.12 and a SyntaxError on 3.11. It passed every local
check on a 3.14 interpreter, deployed green, and took the live Space down with
RUNTIME_ERROR at import.
The obvious guard does NOT work. `ast.parse(src, feature_version=(3, 11))`
happily parses that exact file — feature_version gates a handful of AST
features, not the f-string grammar. Anything built on it would have passed while
the site was down, which is worse than no test.
So this compiles the source with a real 3.11 interpreter. If one is not
installed it skips loudly rather than pretending to have checked.
"""
import shutil
import subprocess
from pathlib import Path
import pytest
# The interpreter in the Space's Docker image. Change both when that changes.
DEPLOY_PYTHON = "3.11"
DEPLOY_BIN = shutil.which(f"python{DEPLOY_PYTHON}")
ROOT = Path(__file__).resolve().parent.parent
# compile(), not py_compile — a pure syntax check that writes no bytecode.
_COMPILE = ("import sys; src=open(sys.argv[1], encoding='utf-8').read(); "
"compile(src, sys.argv[1], 'exec')")
SHIPPED = sorted(
p for p in [*ROOT.glob("*.py"), *(ROOT / "core").glob("*.py")]
if "isolate" not in str(p) and p.name != "__init__.py"
)
pytestmark = pytest.mark.skipif(
DEPLOY_BIN is None,
reason=(f"python{DEPLOY_PYTHON} is not installed, so deployment-grammar "
f"checking is impossible here. Install it (brew install "
f"python@{DEPLOY_PYTHON}) — without it a syntax error that only "
f"appears on the Space can be pushed with every test green."),
)
def test_there_is_something_to_check():
assert SHIPPED, "no shipped modules found — the glob is wrong, not the code"
@pytest.mark.parametrize("path", SHIPPED, ids=lambda p: p.name)
def test_compiles_on_the_deployment_interpreter(path):
"""A real compile, not ast.parse — only the interpreter sees its own grammar."""
r = subprocess.run(
[DEPLOY_BIN, "-c", _COMPILE, str(path)],
capture_output=True, text=True,
)
if r.returncode != 0:
pytest.fail(
f"{path.relative_to(ROOT)} compiles on this interpreter but not on "
f"Python {DEPLOY_PYTHON}, which is what the Space runs:\n\n"
f"{(r.stderr or r.stdout).strip()}\n\n"
f"This would deploy green and crash at import."
)
def test_the_check_actually_catches_the_bug_that_caused_the_outage(tmp_path):
"""A guard nobody has seen fail is a guard nobody should trust."""
bad = tmp_path / "regression.py"
bad.write_text('link = "x"\ns = f"{\'<a href=\\"\' + link if link else \'\'}"\n')
r = subprocess.run(
[DEPLOY_BIN, "-c", _COMPILE, str(bad)],
capture_output=True, text=True,
)
assert r.returncode != 0, "the 3.11 compile must reject a backslash in an f-string"
assert "backslash" in (r.stderr + r.stdout).lower()
|