Skip to content

Commit cca2ef5

Browse files
committed
TypedPython tests: child processes install the compiled builtin themselves
Plain CPython without typedpython_builtins.pth has no builtins.compiled; the child scripts now prepend conftest's COMPILED_SHIM. Also fixes test_regions' split that left a dangling @compiled.
1 parent a59096f commit cca2ef5

9 files changed

Lines changed: 23 additions & 11 deletions

File tree

‎python-multiplatform-ksp/src/test/python/conftest.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
# The `typedpython_builtins.pth` of an installed wheel; the test interpreter has no such install.
1010
builtins.compiled = getattr(builtins, "compiled", lambda f, /: f)
1111

12+
# Prepended to every child-process script that loads a module whose source uses `@compiled`: a plain
13+
# CPython has the builtin only when `typedpython_builtins.pth` is installed, so the child installs it.
14+
COMPILED_SHIM = "import builtins\nif not hasattr(builtins, 'compiled'):\n builtins.compiled = lambda f: f\n"
15+
1216
FIXTURES = Path(__file__).parent / "fixtures"
1317

1418

‎python-multiplatform-ksp/src/test/python/test_class_methods.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
import pytest
4242

43-
from conftest import decorate_compiled
43+
from conftest import COMPILED_SHIM, decorate_compiled
4444
from typedpython import cbuild, cgen, cost, frontend, pipeline, verify
4545

4646
HERE = Path(__file__).resolve().parent
@@ -553,7 +553,7 @@ def test_class_info_reports_the_methods(built):
553553

554554
# --- class state changed after import: child processes --------------------------------------------
555555

556-
CHILD = '''
556+
CHILD = COMPILED_SHIM + '''
557557
import json, sys
558558
from importlib.util import module_from_spec, spec_from_file_location
559559
from pathlib import Path
@@ -736,7 +736,7 @@ def test_reference_counts_are_flat_over_many_calls(built):
736736

737737
# --- free-threaded concurrency --------------------------------------------------------------------
738738

739-
THREADS = '''
739+
THREADS = COMPILED_SHIM + '''
740740
import sys, threading
741741
from pathlib import Path
742742
sys.path.insert(0, {main!r})
@@ -800,7 +800,7 @@ def test_concurrent_calls_on_shared_and_private_instances(built):
800800

801801
# --- mutation: the exact-type guard of `self` -------------------------------------------------------
802802

803-
GUARD_PROBE = '''
803+
GUARD_PROBE = COMPILED_SHIM + '''
804804
import sys
805805
from pathlib import Path
806806
sys.path.insert(0, {main!r})

‎python-multiplatform-ksp/src/test/python/test_eval_breaker.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import pytest
2020

21+
from conftest import COMPILED_SHIM
2122
from typedpython import pipeline
2223

2324
posix_signals = pytest.mark.skipif(sys.platform == "win32", reason="needs POSIX signals (SIGALRM/SIGUSR1/setitimer) or fork; Windows has none")
@@ -49,7 +50,7 @@ def total(xs: list[float], rounds: int) -> float:
4950
return s
5051
"""
5152

52-
LOADER = """\
53+
LOADER = COMPILED_SHIM + """\
5354
import importlib.machinery, importlib.util, json, signal, sys, threading, time
5455
path = sys.argv[1]
5556
loader = importlib.machinery.ExtensionFileLoader("loops", path)

‎python-multiplatform-ksp/src/test/python/test_fast_entry.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import pytest
2222

23+
from conftest import COMPILED_SHIM
2324
from typedpython import cbuild, cgen, frontend, verify
2425

2526
posix_signals = pytest.mark.skipif(sys.platform == "win32", reason="needs POSIX signals (SIGALRM/SIGUSR1/setitimer) or fork; Windows has none")
@@ -170,7 +171,7 @@ def test_the_fast_impl_is_never_addressed(traced):
170171
assert "&tp_fimpl_" not in c and "(tp_fimpl_" not in c
171172

172173

173-
LOAD = """\
174+
LOAD = COMPILED_SHIM + """\
174175
import importlib.machinery, importlib.util, json, sys
175176
def load(name, path, ext):
176177
if ext:

‎python-multiplatform-ksp/src/test/python/test_free_threaded.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import pytest
2929

30+
from conftest import COMPILED_SHIM
3031
from typedpython import cbuild, cgen, frontend, pipeline, verify
3132

3233
STRESS = max(1, int(os.environ.get("TP_FT_STRESS", "1")))
@@ -155,7 +156,7 @@ def usek(x: int) -> int:
155156
return x + K
156157
'''
157158

158-
LOADER = """\
159+
LOADER = COMPILED_SHIM + """\
159160
import faulthandler, importlib.machinery, importlib.util, json, sys, threading, time
160161
faulthandler.enable()
161162
path = sys.argv[1]

‎python-multiplatform-ksp/src/test/python/test_parallel_loops.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import pytest
2727

28+
from conftest import COMPILED_SHIM
2829
from typedpython import cbuild, cgen, frontend, ir, verify
2930

3031
posix_signals = pytest.mark.skipif(sys.platform == "win32", reason="needs POSIX signals (SIGALRM/SIGUSR1/setitimer) or fork; Windows has none")
@@ -335,7 +336,7 @@ def test_list_untouched_when_the_call_raises_before_the_loop(mod):
335336

336337
# --- child processes: thread counts, creation failure, concurrency -----------------------------------
337338

338-
CHILD = """\
339+
CHILD = COMPILED_SHIM + """\
339340
import importlib.machinery, importlib.util, json, struct, sys, threading
340341
path = sys.argv[1]
341342
name = path.replace(chr(92), "/").rsplit("/", 1)[-1].split(".")[0]

‎python-multiplatform-ksp/src/test/python/test_recursion.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import pytest
1717

18+
from conftest import COMPILED_SHIM
1819
from typedpython import pipeline
1920

2021
PACKAGE = Path(__file__).resolve().parents[2] / "main" / "python"
@@ -239,7 +240,7 @@ def inner(j):
239240

240241
# --- sanitizers: the RecursionError path ---------------------------------------------------------
241242

242-
ASAN_SCRIPT = textwrap.dedent('''\
243+
ASAN_SCRIPT = COMPILED_SHIM + textwrap.dedent('''\
243244
import sys
244245
sys.path.insert(0, {pkg!r})
245246
from typedpython import cbuild

‎python-multiplatform-ksp/src/test/python/test_regions.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import pytest
2626

2727
from test_cgen import runtime_dir, need_runtime
28+
from conftest import COMPILED_SHIM
2829
from typedpython import cbuild, cgen, cost, frontend, verify
2930

3031
posix_signals = pytest.mark.skipif(sys.platform == "win32", reason="needs POSIX signals (SIGALRM/SIGUSR1/setitimer) or fork; Windows has none")
@@ -392,10 +393,11 @@ def test_a_forged_module_level_variant_set_is_reported(built):
392393

393394

394395
def test_a_finalizer_or_a_field_store_keeps_the_class_out_of_a_region(tmp_path):
395-
base = SOURCE.split("def escapes")[0]
396+
base = SOURCE.split("@compiled\ndef escapes")[0]
396397
with_del = base.replace(" self.right = right\n",
397398
" self.right = right\n\n def __del__(self) -> None:\n pass\n", 1)
398399
with_store = base + textwrap.dedent('''\
400+
@compiled
399401
def poke(node: Node) -> int:
400402
node.left = None
401403
return 0

‎python-multiplatform-ksp/src/test/python/test_tracing.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import pytest
1818

19+
from conftest import COMPILED_SHIM
1920
from typedpython import pipeline
2021

2122
SOURCE = """\
@@ -47,7 +48,7 @@ def outer(cb: Callable[[], None], n: float) -> float:
4748
return leaf(cb, n) + leaf(cb, n)
4849
"""
4950

50-
LOADER = """\
51+
LOADER = COMPILED_SHIM + """\
5152
import importlib.machinery, importlib.util, json, sys, threading
5253
path = sys.argv[1]
5354
loader = importlib.machinery.ExtensionFileLoader("tmod", path)

0 commit comments

Comments
 (0)