Virtual-LPU / lpu /_bootstrap /sitecustomize.py
iparaco's picture
Upload 118 files
e59bd9d verified
Raw History Blame Contribute Delete
1.83 kB
"""LPU bootstrap sitecustomize - runs inside the TARGET project's interpreter.
Behavior (strictly ordered):
1. CHAIN to any pre-existing sitecustomize BEFORE doing anything of our own,
so user tooling (coverage, debuggers, profilers) keeps working untouched.
2. Only when LPU_ENABLED=1: import LPU's guarded run_bootstrap().
3. Any failure inside LPU bootstrap is swallowed after logging - the user's
program must never crash because of LPU.
This file is placed on PYTHONPATH by lpu-run (level-1 integration only).
"""
import os
import sys
def _chain_existing_sitecustomize():
"""Run the next real sitecustomize found on sys.path (not this one)."""
import importlib.util
import os
here = os.path.normcase(os.path.dirname(os.path.abspath(__file__)))
for entry in sys.path:
if not entry:
continue
if os.path.normcase(os.path.abspath(entry)) == here:
continue
candidate = os.path.join(entry, "sitecustomize.py")
if os.path.isfile(candidate):
try:
spec = importlib.util.spec_from_file_location(
"_lpu_chained_sitecustomize", candidate
)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return
except Exception: # noqa: BLE001 - user tooling must not break
pass
break
_chain_existing_sitecustomize()
if os.environ.get("LPU_ENABLED") == "1":
try:
import lpu.integration.hooks as _lpu_hooks
_lpu_hooks.run_bootstrap()
except Exception as _exc: # noqa: BLE001 - fail open, always
try:
sys.stderr.write(f"[lpu] bootstrap failed (continuing without LPU): {_exc}\n")
except Exception: # noqa: BLE001
pass