Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: precompile bytecode after setup so the first launch does not (#15646)
Daniel Han1ba04b036527
diff --git a/.github/workflows/cross-platform-parity-ci.yml b/.github/workflows/cross-platform-parity-ci.ymlindex 1e005f1244c..8dfa426854d 100644--- a/.github/workflows/cross-platform-parity-ci.yml+++ b/.github/workflows/cross-platform-parity-ci.yml@@ -65,6 +65,7 @@ on:- 'tests/studio/install/test_keep_install_backcompat_9979.py'- 'studio/prebuilt_core.py'- 'studio/nvidia_probe.py'+ - 'studio/precompile_bytecode.py'- 'studio/backend/utils/prebuilt/llama_backend.py'- 'tests/sh/test_install_rollback_lifecycle.sh'- 'tests/studio/test_install_rollback_lifecycle.ps1'@@ -167,6 +168,7 @@ on:- 'tests/studio/install/test_keep_install_backcompat_9979.py'- 'studio/prebuilt_core.py'- 'studio/nvidia_probe.py'+ - 'studio/precompile_bytecode.py'- 'studio/backend/utils/prebuilt/llama_backend.py'- 'tests/sh/test_install_rollback_lifecycle.sh'- 'tests/studio/test_install_rollback_lifecycle.ps1'diff --git a/studio/precompile_bytecode.py b/studio/precompile_bytecode.pynew file mode 100644index 00000000000..01e6f8117d2--- /dev/null+++ b/studio/precompile_bytecode.py@@ -0,0 +1,325 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Best-effort .pyc compile of the Studio venv after a dependency pass (uv installs without .pyc).++Only adds missing .pyc, never rewrites one; always exits 0; prints nothing; skipped under 4 GiB free.+A timeout or Ctrl+C kills the worker tree and removes exactly what this run created.+Opt out: UV_COMPILE_BYTECODE / UNSLOTH_STUDIO_COMPILE_BYTECODE false. Bound:+UNSLOTH_STUDIO_COMPILE_BYTECODE_TIMEOUT (240 s, under the desktop installer's 5 min silence report).+Usage: python -I precompile_bytecode.py [DIR ...]+"""++import os+import re+import shutil+import subprocess+import sys+import sysconfig++_FALSE = ("0", "false", "no", "off")+_DEFAULT_TIMEOUT = 240.0+_MIN_FREE_BYTES = 4 * 1024**3+_PACKAGES = ("studio", "unsloth", "unsloth_cli")+_CHECKOUT_SKIP_DIRS = ("node_modules", "frontend", "src-tauri", "tests")+# importlib's _write_atomic writes "<path>.<id(path)>" and renames it over <path>.+_PARTIAL_PYC = re.compile(r"\.pyc\.\d+$")+_CHILD_FLAG = "--compile-child"+# Past this, more workers only add memory.+_MAX_WORKERS = 32+++def _disabled():+ for name in ("UV_COMPILE_BYTECODE", "UNSLOTH_STUDIO_COMPILE_BYTECODE"):+ if os.environ.get(name, "").strip().lower() in _FALSE:+ return True+ return False+++def _timeout():+ try:+ value = float(os.environ.get("UNSLOTH_STUDIO_COMPILE_BYTECODE_TIMEOUT", ""))+ except ValueError:+ return _DEFAULT_TIMEOUT+ return value if value > 0 else _DEFAULT_TIMEOUT+++def _same(a, b):+ try:+ return os.path.normcase(os.path.realpath(a)) == os.path.normcase(os.path.realpath(b))+ except Exception:+ return False+++def _inside(path, roots):+ path = os.path.normcase(os.path.realpath(path))+ for root in roots:+ root = os.path.normcase(os.path.realpath(root))+ if path == root or path.startswith(root.rstrip(os.sep) + os.sep):+ return True+ return False+++def _enough_space(dirs):+ for path in dirs:+ try:+ if shutil.disk_usage(path).free < _MIN_FREE_BYTES:+ return False+ except Exception:+ return False+ return True+++def default_dirs():+ """(site dirs, checkout dirs) for this interpreter."""+ paths = sysconfig.get_paths()+ site = []+ for key in ("purelib", "platlib"):+ path = paths.get(key)+ if path and os.path.isdir(path) and not _inside(path, site):+ site.append(path)+ checkout = []+ import importlib.util++ # Only what the venv itself resolves: PYTHONPATH, cwd and this dir can hold unrelated trees.+ blocked = [p for p in os.environ.get("PYTHONPATH", "").split(os.pathsep) if p]+ blocked += [os.getcwd(), os.path.dirname(os.path.abspath(__file__))]+ saved = list(sys.path)+ sys.path[:] = [p for p in sys.path if p and not any(_same(p, b) for b in blocked)]+ try:+ for name in _PACKAGES:+ try:+ spec = importlib.util.find_spec(name)+ except Exception:+ continue+ for path in list(getattr(spec, "submodule_search_locations", None) or []):+ if os.path.isfile(os.path.join(path, "__init__.py")) and not _inside(+ path, site + checkout+ ):+ checkout.append(path)+ finally:+ sys.path[:] = saved+ return site, checkout+++def plan(site, checkout):+ """(sources lacking a .pyc, their .pyc paths, __pycache__ dirs that do not exist yet)."""+ import importlib.util++ sources, targets, new_dirs = [], [], set()++ def add(directory, names):+ for name in names:+ if not name.endswith(".py"):+ continue+ source = os.path.join(directory, name)+ try:+ target = importlib.util.cache_from_source(source)+ except Exception:+ continue+ if os.path.lexists(target):+ continue+ sources.append(source)+ targets.append(target)+ cache = os.path.dirname(target)+ if not os.path.isdir(cache):+ new_dirs.add(cache)++ for root in site:+ for current, subdirs, files in os.walk(root):+ subdirs[:] = [d for d in subdirs if d != "__pycache__"]+ add(current, files)+ for root in checkout:+ for current, subdirs, files in os.walk(root):+ subdirs[:] = [+ d+ for d in subdirs+ if d != "__pycache__" and not d.startswith(".") and d not in _CHECKOUT_SKIP_DIRS+ ]+ add(current, files)+ return sources, targets, new_dirs+++def _child_command(site, checkout):+ return (+ [sys.executable, "-I", os.path.abspath(__file__), _CHILD_FLAG]+ + ["site:" + d for d in site]+ + ["checkout:" + d for d in checkout]+ )+++def _compile_child(args):+ import functools+ import py_compile++ site = [a[len("site:") :] for a in args if a.startswith("site:")]+ checkout = [a[len("checkout:") :] for a in args if a.startswith("checkout:")]+ sources = plan(site, checkout)[0]+ job = functools.partial(py_compile.compile, doraise = False, quiet = 2)+ try:+ from concurrent.futures import ProcessPoolExecutor++ cpus = getattr(os, "process_cpu_count", os.cpu_count)() or 1+ with ProcessPoolExecutor(max_workers = min(cpus, _MAX_WORKERS)) as pool:+ for _ in pool.map(job, sources, chunksize = 64):+ pass+ return+ except Exception:+ pass+ # No working process pool (e.g. no sem_open).+ for source in sources:+ try:+ job(source)+ except Exception:+ pass+++def undo(targets, new_dirs):+ caches = {}+ for target in targets:+ caches.setdefault(os.path.dirname(target), set()).add(os.path.basename(target))+ try:+ os.remove(target)+ except Exception:+ pass+ for cache, names in caches.items():+ try:+ leftovers = os.listdir(cache)+ except Exception:+ continue+ for name in leftovers:+ match = _PARTIAL_PYC.search(name)+ if match and name[: match.start() + 4] in names:+ try:+ os.remove(os.path.join(cache, name))+ except Exception:+ pass+ for cache in sorted(new_dirs, key = len, reverse = True):+ try:+ os.rmdir(cache) # only when empty, i.e. nothing else wrote there meanwhile+ except Exception:+ pass+++def _kill_tree(proc):+ try:+ if os.name == "nt":+ subprocess.run(+ ["taskkill", "/F", "/T", "/PID", str(proc.pid)],+ stdout = subprocess.DEVNULL,+ stderr = subprocess.DEVNULL,+ timeout = 30,+ )+ else:+ import signal+ os.killpg(proc.pid, signal.SIGKILL)+ except Exception:+ pass+ try:+ proc.kill()+ except Exception:+ pass+ try:+ proc.wait(timeout = 30)+ except Exception:+ pass+++def run(site, checkout, timeout):+ """True when the child tree finished within timeout."""+ kwargs = {}+ if os.name != "nt":+ # Own process group, so a timeout kills the pool workers too.+ kwargs["start_new_session"] = True+ try:+ proc = subprocess.Popen(+ _child_command(site, checkout),+ stdin = subprocess.DEVNULL,+ stdout = subprocess.DEVNULL,+ stderr = subprocess.DEVNULL,+ **kwargs,+ )+ except Exception:+ return True # nothing started, nothing written+ try:+ proc.wait(timeout = timeout)+ except subprocess.TimeoutExpired:+ _kill_tree(proc)+ return False+ except BaseException:+ _kill_tree(proc)+ raise+ return True+++class _Stopped(BaseException):+ def __init__(self, signum):+ self.signum = signum+++def _stop(signum, frame):+ raise _Stopped(signum)+++def _catch_stop_signals():+ """The worker group is its own session, so a SIGTERM to setup's group (the desktop's Stop)+ or a hangup must reach it through this process."""+ if os.name == "nt":+ return+ import signal++ for name in ("SIGTERM", "SIGHUP"):+ try:+ signal.signal(getattr(signal, name), _stop)+ except Exception:+ pass+++def _reraise(signum):+ """Die of the same signal: bash stops on a child's Ctrl+C only when the child dies of SIGINT."""+ if os.name != "nt":+ import signal+ try:+ signal.signal(signum, signal.SIG_DFL)+ os.kill(os.getpid(), signum)+ except Exception:+ pass+ os._exit(128 + int(signum))+++def main(argv):+ targets, new_dirs = [], set()+ try:+ if _disabled():+ return 0+ if argv:+ site, checkout = [d for d in argv if os.path.isdir(d)], []+ else:+ site, checkout = default_dirs()+ if not site + checkout or not _enough_space(site + checkout):+ return 0+ sources, targets, new_dirs = plan(site, checkout)+ if not sources:+ return 0+ _catch_stop_signals()+ if not run(site, checkout, _timeout()):+ undo(targets, new_dirs)+ except (KeyboardInterrupt, _Stopped) as stopped:+ try:+ undo(targets, new_dirs)+ except BaseException:+ pass+ _reraise(getattr(stopped, "signum", 2))+ except Exception:+ pass+ return 0+++if __name__ == "__main__":+ if sys.argv[1:2] == [_CHILD_FLAG]:+ try:+ _compile_child(sys.argv[2:])+ except BaseException:+ pass+ sys.exit(0)+ sys.exit(main(sys.argv[1:]))diff --git a/studio/setup.ps1 b/studio/setup.ps1index f219fb8f5dc..4cc608ec877 100644--- a/studio/setup.ps1+++ b/studio/setup.ps1@@ -158,6 +158,7 @@ $script:NvidiaLibraryInventoryProbed = $false$script:NvidiaLibraryInventory = $null# Initialised for Set-StrictMode and for a dot-sourced rerun in the same session.$script:OfflineFastPath = $false+$script:SetupVenvChanged = $false$script:CudaToolkitReady = $false$script:NvccPath = $null$script:CudaToolkitRoot = $null@@ -8394,6 +8395,7 @@ if (-not $NoTorchMode) {substep "running ordered dependency installation..."python "$PSScriptRoot\install_python_stack.py"$stackExit = $LASTEXITCODE+$script:SetupVenvChanged = $true# ── Intel XPU: bitsandbytes must carry XPU kernels ──# Only bitsandbytes' XPU library exports the 4-bit kernels; floor 0.50.0 (<=0.49.2 NaNs on AMD).@@ -10111,6 +10113,22 @@ if (-not $llamaCppIsLink -and (Complete-FrontendBuildBackground+# ── Precompile bytecode ── (uv installs without .pyc; only after a dependency pass, never fatal)+$_pycPython = Join-Path $VenvDir 'Scripts\python.exe'+$_pycHelper = Join-Path $PSScriptRoot 'precompile_bytecode.py'+$_pycOptedOut = @(@($env:UV_COMPILE_BYTECODE, $env:UNSLOTH_STUDIO_COMPILE_BYTECODE) |+ Where-Object { $_ -and (@('0', 'false', 'no', 'off') -contains $_.Trim().ToLowerInvariant()) }).Count -gt 0+if (($script:SetupVenvChanged -or $env:UNSLOTH_SETUP_RERUN -eq '1') -and -not $_pycOptedOut -and+ (Test-Path -LiteralPath $_pycPython -PathType Leaf) -and+ (Test-Path -LiteralPath $_pycHelper -PathType Leaf)) {+ step "python" "precompiling bytecode..."+ $_pycPrevExit = $global:LASTEXITCODE+ $_pycPrevEAP = $ErrorActionPreference+ $ErrorActionPreference = 'Continue'+ try { & $_pycPython -I $_pycHelper } catch { } finally { $ErrorActionPreference = $_pycPrevEAP }+ $global:LASTEXITCODE = $_pycPrevExit+}+# ─────────────────────────────────────────────# Footer# ─────────────────────────────────────────────diff --git a/studio/setup.sh b/studio/setup.shindex 5c3cdd3845a..2ca675e3e86 100755--- a/studio/setup.sh+++ b/studio/setup.sh@@ -3279,6 +3279,7 @@ sys.exit(0 if installed is not None and required is not None and installed >= re}_SKIP_PYTHON_DEPS=false+_SETUP_VENV_CHANGED=false_SKIP_VERSION_CHECK=false# Set here so an exported value from the caller cannot reach it._OFFLINE_FAST_PATH=false@@ -3397,6 +3398,7 @@ fiif [ "$_SKIP_PYTHON_DEPS" = false ]; theninstall_python_stack+ _SETUP_VENV_CHANGED=true_setup_rerun_if_replacedelsestep "python" "dependencies up to date"@@ -5486,6 +5488,23 @@ _print_llama_gpu_notes() {_frontend_bg_join+# ── Precompile bytecode ── (uv installs without .pyc; only after a dependency pass, never fatal)+_setup_bytecode_opted_out() {+ local _sbo+ for _sbo in "${UV_COMPILE_BYTECODE:-}" "${UNSLOTH_STUDIO_COMPILE_BYTECODE:-}"; do+ case "$(printf '%s' "$_sbo" | tr -d '[:space:]' | tr '[:upper:]' '[:lower:]')" in+ 0|false|no|off) return 0 ;;+ esac+ done+ return 1+}+if { [ "${_SETUP_VENV_CHANGED:-false}" = true ] || [ "${UNSLOTH_SETUP_RERUN:-}" = 1 ]; } \+ && [ "${_COLAB_NO_VENV:-false}" != true ] && [ -x "$VENV_DIR/bin/python" ] \+ && [ -f "$SCRIPT_DIR/precompile_bytecode.py" ] && ! _setup_bytecode_opted_out; then+ step "python" "precompiling bytecode..."+ "$VENV_DIR/bin/python" -I "$SCRIPT_DIR/precompile_bytecode.py" </dev/null || true+fi+# ── Footer ──if [ "$_LLAMA_ONLY" = "1" ]; thenecho ""diff --git a/tests/python/test_installer_compile_bytecode.py b/tests/python/test_installer_compile_bytecode.pynew file mode 100644index 00000000000..0a97741d353--- /dev/null+++ b/tests/python/test_installer_compile_bytecode.py@@ -0,0 +1,555 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Setup precompiles the Studio environment's bytecode after a dependency pass, and it can never+fail an install.++uv installs without .pyc, so the first launch paid for compiling every module `import main`+loads. studio/precompile_bytecode.py runs compileall over site-packages (and an editable checkout)+after setup.sh / setup.ps1 changed the venv. uv itself is left alone: no installer sets+UV_COMPILE_BYTECODE, because a uv compile failure or timeout fails the install command (#5983).+"""++from __future__ import annotations++import importlib.util+import os+import re+import shutil+import stat+import subprocess+import sys+import time+from pathlib import Path++import pytest++REPO_ROOT = Path(__file__).resolve().parents[2]+INSTALL_SH = REPO_ROOT / "install.sh"+INSTALL_PS1 = REPO_ROOT / "install.ps1"+SETUP_SH = REPO_ROOT / "studio" / "setup.sh"+SETUP_PS1 = REPO_ROOT / "studio" / "setup.ps1"+HELPER = REPO_ROOT / "studio" / "precompile_bytecode.py"+BASH = shutil.which("bash")+PWSH = shutil.which("pwsh") or shutil.which("powershell")+++def _lines(path: Path) -> list[str]:+ return path.read_text(encoding = "utf-8").splitlines()+++def _index(+ lines: list[str],+ pattern: str,+ start: int = 0,+) -> int:+ rx = re.compile(pattern)+ for i in range(start, len(lines)):+ if rx.search(lines[i]) and not lines[i].lstrip().startswith("#"):+ return i+ raise AssertionError(f"no line matches {pattern!r}")+++def _helper():+ spec = importlib.util.spec_from_file_location("precompile_bytecode_under_test", HELPER)+ module = importlib.util.module_from_spec(spec)+ spec.loader.exec_module(module)+ return module+++def _clean_env(monkeypatch):+ for name in (+ "UV_COMPILE_BYTECODE",+ "UNSLOTH_STUDIO_COMPILE_BYTECODE",+ "UNSLOTH_STUDIO_COMPILE_BYTECODE_TIMEOUT",+ "UNSLOTH_VERBOSE",+ "SOURCE_DATE_EPOCH",+ ):+ monkeypatch.delenv(name, raising = False)+++def _tree(root: Path) -> Path:+ pkg = root / "pkg"+ pkg.mkdir(parents = True)+ (pkg / "__init__.py").write_text("X = 1\n", encoding = "utf-8")+ (pkg / "mod.py").write_text("def f():\n return 2\n", encoding = "utf-8")+ return pkg+++def _pycs(root: Path) -> list[Path]:+ return sorted(root.rglob("*.pyc"))+++def _snapshot(root: Path) -> dict:+ """Every path under root, and each file's size and mtime: equal snapshots = nothing changed.+ A directory is compared by its entries, not its mtime, which any create-then-delete moves."""+ return {+ str(p.relative_to(root)): ("dir",)+ if p.is_dir()+ else (p.stat().st_size, p.stat().st_mtime_ns)+ for p in sorted(root.rglob("*"))+ }+++def test_setup_sh_marks_the_deps_pass_and_compiles_before_the_footer():+ lines = _lines(SETUP_SH)+ stack = _index(lines, r"^ install_python_stack$")+ assert lines[stack + 1] == " _SETUP_VENV_CHANGED=true"+ assert lines[stack + 2] == " _setup_rerun_if_replaced"+ assert _index(lines, r"^_SETUP_VENV_CHANGED=false$") < stack+ block = _index(lines, r"_SETUP_VENV_CHANGED:-false")+ call = _index(lines, r'"\$VENV_DIR/bin/python" -I "\$SCRIPT_DIR/precompile_bytecode.py"', block)+ assert lines[call].rstrip().endswith("|| true")+ assert "UNSLOTH_SETUP_RERUN" in lines[block]+ assert '"${_COLAB_NO_VENV:-false}" != true' in lines[block + 1]+ # After every install phase and the frontend join, before the footer.+ assert _index(lines, r"^_frontend_bg_join$", stack) < block+ assert call < next(i for i, line in enumerate(lines) if line.startswith("# ── Footer ──"))+ assert block > _index(lines, r"install_audio_cpp_prebuilt\.py")+++def test_setup_ps1_marks_the_deps_pass_and_compiles_before_the_footer():+ lines = _lines(SETUP_PS1)+ assert _index(lines, r"^\$script:SetupVenvChanged = \$false$") < 200+ stack = _index(lines, r'^python "\$PSScriptRoot\\install_python_stack\.py"$')+ assert lines[stack + 1] == "$stackExit = $LASTEXITCODE"+ assert lines[stack + 2] == "$script:SetupVenvChanged = $true"+ block = _index(lines, r"\$script:SetupVenvChanged -or \$env:UNSLOTH_SETUP_RERUN -eq '1'")+ assert block > stack+ footer = next(i for i, line in enumerate(lines) if line == "# Footer")+ assert block < footer+ body = "\n".join(lines[block:footer])+ assert "$ErrorActionPreference = 'Continue'" in body+ assert "catch { }" in body+ assert "$_pycPrevExit = $global:LASTEXITCODE" in body+ assert "$global:LASTEXITCODE = $_pycPrevExit" in body+++def test_uv_bytecode_behaviour_is_left_alone():+ for path in (INSTALL_SH, INSTALL_PS1, SETUP_SH, SETUP_PS1):+ text = path.read_text(encoding = "utf-8")+ assert "UV_COMPILE_BYTECODE:=" not in text, path+ assert "$env:UV_COMPILE_BYTECODE =" not in text, path+ assert "export UV_COMPILE_BYTECODE\n" not in text, path+ assert "--compile-bytecode" not in text, path+++def test_no_installer_disables_pip_compile():+ for path in (+ INSTALL_SH,+ INSTALL_PS1,+ SETUP_SH,+ SETUP_PS1,+ REPO_ROOT / "studio" / "install_python_stack.py",+ ):+ text = path.read_text(encoding = "utf-8")+ assert "--no-compile" not in text, path+ assert "PIP_NO_COMPILE" not in text, path+++def _setup_sh_block() -> str:+ lines = _lines(SETUP_SH)+ start = _index(lines, r"^_setup_bytecode_opted_out\(\) \{$")+ gate = _index(lines, r"_SETUP_VENV_CHANGED:-false", start)+ end = next(i for i in range(gate, len(lines)) if lines[i] == "fi")+ return "\n".join(lines[start : end + 1])++[email protected](BASH is None, reason = "needs bash")+ "changed, rerun, opt_out, fake_exit, expect_call",+ [+ ("true", "", None, 1, True),+ ("true", "", None, 0, True),+ ("false", "1", None, 1, True),+ ("false", "", None, 0, False),+ ("true", "", ("UV_COMPILE_BYTECODE", " FALSE "), 0, False),+ ("true", "", ("UNSLOTH_STUDIO_COMPILE_BYTECODE", "0"), 0, False),+ ("true", "", ("UV_COMPILE_BYTECODE", "1"), 0, True),+ ],+)+def test_setup_sh_block_never_fails_and_runs_only_after_a_deps_pass(+ tmp_path, changed, rerun, opt_out, fake_exit, expect_call+):+ venv = tmp_path / "venv"+ (venv / "bin").mkdir(parents = True)+ marker = tmp_path / "called"+ fake = venv / "bin" / "python"+ fake.write_text(+ f'#!/bin/sh\n: > "{marker}"\necho noisy >&2\nexit {fake_exit}\n', encoding = "utf-8"+ )+ fake.chmod(0o755)+ script_dir = tmp_path / "studio"+ script_dir.mkdir()+ (script_dir / "precompile_bytecode.py").write_text("", encoding = "utf-8")+ script = "\n".join(+ [+ "set -euo pipefail",+ 'step() { printf "%s %s\\n" "$1" "$2"; }',+ f'VENV_DIR="{venv}"',+ f'SCRIPT_DIR="{script_dir}"',+ "_COLAB_NO_VENV=false",+ f"_SETUP_VENV_CHANGED={changed}",+ _setup_sh_block(),+ "echo reached-the-footer",+ ]+ )+ env = {"PATH": "/usr/bin:/bin"}+ if rerun:+ env["UNSLOTH_SETUP_RERUN"] = rerun+ if opt_out:+ env[opt_out[0]] = opt_out[1]+ result = subprocess.run([BASH, "-c", script], env = env, capture_output = True, text = True)+ assert result.returncode == 0, result.stderr+ expected = (["python precompiling bytecode..."] if expect_call else []) + ["reached-the-footer"]+ assert result.stdout.splitlines() == expected+ assert marker.exists() is expect_call++[email protected](PWSH is None or BASH is None, reason = "needs PowerShell")+ "opt_out, expect_call",+ [+ (None, True),+ (("UV_COMPILE_BYTECODE", "False"), False),+ (("UNSLOTH_STUDIO_COMPILE_BYTECODE", "off"), False),+ ],+)+def test_setup_ps1_block_never_fails(tmp_path, opt_out, expect_call):+ lines = _lines(SETUP_PS1)+ start = _index(lines, r"^\$_pycPython = ")+ footer = next(i for i, line in enumerate(lines) if line == "# Footer")+ block = "\n".join(lines[start : footer - 1])+ venv = tmp_path / "venv"+ marker = tmp_path / "called"+ studio = tmp_path / "studio"+ studio.mkdir()+ if os.name == "nt":+ # Windows runs only real executables: a real venv python, and a helper that marks, is noisy and exits 7.+ import venv as venv_module+ venv_module.EnvBuilder(with_pip = False).create(venv)+ (studio / "precompile_bytecode.py").write_text(+ f"import pathlib, sys\npathlib.Path(r'{marker}').touch()\n"+ "sys.stderr.write('noisy\\n')\nsys.exit(7)\n",+ encoding = "utf-8",+ )+ else:+ (venv / "Scripts").mkdir(parents = True)+ fake = venv / "Scripts" / "python.exe"+ fake.write_text(f'#!{BASH}\n: > "{marker}"\necho noisy >&2\nexit 7\n', encoding = "utf-8")+ fake.chmod(0o755)+ (studio / "precompile_bytecode.py").write_text("", encoding = "utf-8")+ script = "\n".join(+ [+ "$ErrorActionPreference = 'Stop'",+ 'function step { param($a, $b) Write-Output "$a $b" }',+ f"$VenvDir = '{venv}'",+ f"$PSScriptRoot = '{studio}'",+ "$script:SetupVenvChanged = $true",+ "$global:LASTEXITCODE = 5",+ block,+ '"reached-the-footer exit=$LASTEXITCODE"',+ ]+ )+ if os.name == "nt":+ # PowerShell on Windows needs SystemRoot and friends; only the switches under test are cleared.+ env = {+ k: v+ for k, v in os.environ.items()+ if k+ not in ("UV_COMPILE_BYTECODE", "UNSLOTH_STUDIO_COMPILE_BYTECODE", "UNSLOTH_SETUP_RERUN")+ }+ else:+ env = {"PATH": "/usr/bin:/bin", "HOME": str(tmp_path)}+ if opt_out:+ env[opt_out[0]] = opt_out[1]+ result = subprocess.run(+ [PWSH, "-NoProfile", "-NonInteractive", "-Command", script],+ env = env,+ capture_output = True,+ text = True,+ )+ assert result.returncode == 0, result.stderr+ # $LASTEXITCODE is left as the step found it, whatever the helper returned (the fake exits 7).+ expected = (["python precompiling bytecode..."] if expect_call else []) + [+ "reached-the-footer exit=5"+ ]+ assert [line for line in result.stdout.splitlines() if line.strip()] == expected+ assert marker.exists() is expect_call+++def test_helper_compiles_a_tree_and_is_idempotent(tmp_path, monkeypatch):+ _clean_env(monkeypatch)+ pkg = _tree(tmp_path)+ assert _helper().main([str(tmp_path)]) == 0+ assert len(_pycs(pkg)) == 2+ before = _snapshot(tmp_path)+ time.sleep(0.05)+ assert _helper().main([str(tmp_path)]) == 0+ assert _snapshot(tmp_path) == before, "a re-run over a compiled tree must write nothing"+ # The installed interpreter's view: the .pyc are current, so an import writes nothing either.+ subprocess.run([sys.executable, "-c", "import pkg.mod"], cwd = tmp_path, check = True)+ assert _snapshot(tmp_path) == before+++ "name, value",+ [+ ("UV_COMPILE_BYTECODE", "0"),+ ("UV_COMPILE_BYTECODE", "false"),+ ("UNSLOTH_STUDIO_COMPILE_BYTECODE", "0"),+ ("UNSLOTH_STUDIO_COMPILE_BYTECODE", "off"),+ ],+)+def test_helper_is_skipped_when_disabled(tmp_path, monkeypatch, name, value):+ _clean_env(monkeypatch)+ monkeypatch.setenv(name, value)+ _tree(tmp_path)+ assert _helper().main([str(tmp_path)]) == 0+ assert _pycs(tmp_path) == []+++ os.name == "nt" or (hasattr(os, "geteuid") and os.geteuid() == 0),+ reason = "root ignores the read-only bit",+)+def test_helper_succeeds_on_a_read_only_tree(tmp_path, monkeypatch):+ _clean_env(monkeypatch)+ pkg = _tree(tmp_path)+ before = _snapshot(tmp_path)+ pkg.chmod(stat.S_IRUSR | stat.S_IXUSR)+ try:+ result = subprocess.run(+ [sys.executable, str(HELPER), str(tmp_path)], capture_output = True, text = True+ )+ finally:+ pkg.chmod(0o755)+ assert result.returncode == 0+ assert result.stdout == "" and result.stderr == ""+ assert _snapshot(tmp_path) == before+++def test_helper_succeeds_when_compileall_exits_1(tmp_path, monkeypatch):+ _clean_env(monkeypatch)+ pkg = _tree(tmp_path)+ (pkg / "broken.py").write_text("def (:\n", encoding = "utf-8")+ result = subprocess.run(+ [sys.executable, "-m", "compileall", "-q", str(tmp_path)], capture_output = True+ )+ assert result.returncode == 1, "the control must really fail"+ for name in _pycs(tmp_path):+ name.unlink()+ (pkg / "warns.py").write_text('X = "\\_"\n', encoding = "utf-8") # SyntaxWarning on 3.12++ result = subprocess.run(+ [sys.executable, str(HELPER), str(tmp_path)],+ capture_output = True,+ text = True,+ env = {**os.environ, "UNSLOTH_VERBOSE": "1"},+ )+ assert result.returncode == 0+ # Silent in verbose mode too: setup's progress line is the only output.+ assert result.stdout == "" and result.stderr == ""+ assert len(_pycs(pkg)) == 3+++def test_helper_survives_a_failing_or_missing_command(tmp_path, monkeypatch):+ _clean_env(monkeypatch)+ _tree(tmp_path)+ before = _snapshot(tmp_path)+ helper = _helper()+ for child in ([sys.executable, "-c", "raise SystemExit(1)"], ["/nonexistent/python-for-test"]):+ monkeypatch.setattr(helper, "_child_command", lambda site, checkout, child = child: child)+ assert helper.main([str(tmp_path)]) == 0+ assert _snapshot(tmp_path) == before+ monkeypatch.setattr(helper, "plan", lambda site, checkout: 1 / 0)+ assert helper.main([str(tmp_path)]) == 0+ assert _snapshot(tmp_path) == before+++def _slow_compile(tree: Path, pidfile: Path) -> str:+ """Writes what a compile cut short leaves behind (a finished .pyc, a temp file, a new+ __pycache__ dir), then hangs."""+ target = importlib.util.cache_from_source(str(tree / "pkg" / "mod.py"))+ fresh = importlib.util.cache_from_source(str(tree / "other" / "x.py"))+ return (+ "import os, time\n"+ f"open({str(pidfile)!r}, 'w').write(str(os.getpid()))\n"+ f"os.makedirs(os.path.dirname({target!r}), exist_ok=True)\n"+ f"open({target!r}, 'wb').write(b'x')\n"+ f"open({target!r} + '.140234', 'wb').write(b'x')\n"+ f"os.makedirs(os.path.dirname({fresh!r}), exist_ok=True)\n"+ f"open({fresh!r} + '.99', 'wb').write(b'x')\n"+ "time.sleep(120)\n"+ )+++def test_helper_timeout_bounds_the_compile_and_leaves_the_tree_as_it_was(tmp_path, monkeypatch):+ _clean_env(monkeypatch)+ monkeypatch.setenv("UNSLOTH_STUDIO_COMPILE_BYTECODE_TIMEOUT", "1")+ tree = tmp_path / "site"+ pkg = _tree(tree)+ (pkg / "__pycache__").mkdir()+ (tree / "other").mkdir()+ (tree / "other" / "x.py").write_text("Y = 1\n", encoding = "utf-8")+ old = Path(importlib.util.cache_from_source(str(pkg / "__init__.py")))+ old.write_bytes(b"from an earlier run")+ os.utime(old, (time.time() - 3600, time.time() - 3600))+ before = _snapshot(tree)+ pidfile = tmp_path / "pid"+ helper = _helper()+ monkeypatch.setattr(+ helper,+ "_child_command",+ lambda site, checkout: [sys.executable, "-c", _slow_compile(tree, pidfile)],+ )+ started = time.monotonic()+ assert helper.main([str(tree)]) == 0+ assert time.monotonic() - started < 30+ pid = int(pidfile.read_text())+ if os.name != "nt":+ with pytest.raises(ProcessLookupError):+ os.kill(pid, 0)+ assert _snapshot(tree) == before++[email protected](os.name == "nt", reason = "POSIX signal semantics")[email protected]("signame", ["SIGINT", "SIGTERM", "SIGHUP"])+def test_a_stop_signal_kills_the_compile_and_undoes_it(tmp_path, signame):+ """bash only stops on a child's Ctrl+C when the child dies of SIGINT, so the helper must; the+ desktop's Stop sends SIGTERM to setup's group, which the compile's own session does not get."""+ signum = getattr(__import__("signal"), signame)+ tree = tmp_path / "site"+ _tree(tree)+ (tree / "other").mkdir()+ (tree / "other" / "x.py").write_text("Y = 1\n", encoding = "utf-8")+ before = _snapshot(tree)+ pidfile = tmp_path / "pid"+ driver = tmp_path / "driver.py"+ driver.write_text(+ "import importlib.util, sys\n"+ f"spec = importlib.util.spec_from_file_location('h', {str(HELPER)!r})\n"+ "h = importlib.util.module_from_spec(spec); spec.loader.exec_module(h)\n"+ f"h._child_command = lambda s, c: [sys.executable, '-c', {_slow_compile(tree, pidfile)!r}]\n"+ f"sys.exit(h.main([{str(tree)!r}]))\n",+ encoding = "utf-8",+ )+ proc = subprocess.Popen(+ [sys.executable, str(driver)], stdout = subprocess.PIPE, stderr = subprocess.PIPE+ )+ deadline = time.monotonic() + 30+ while not pidfile.exists() and time.monotonic() < deadline:+ time.sleep(0.05)+ time.sleep(0.3)+ proc.send_signal(signum)+ out, err = proc.communicate(timeout = 30)+ assert proc.returncode == -signum+ assert out == b"" and err == b""+ with pytest.raises(ProcessLookupError):+ os.kill(int(pidfile.read_text()), 0)+ assert _snapshot(tree) == before+++def test_helper_skips_when_disk_is_low(tmp_path, monkeypatch):+ _clean_env(monkeypatch)+ helper = _helper()+ _tree(tmp_path)+ before = _snapshot(tmp_path)++ class Usage:+ free = helper._MIN_FREE_BYTES - 1++ monkeypatch.setattr(helper.shutil, "disk_usage", lambda path: Usage())+ assert helper.main([str(tmp_path)]) == 0+ assert _snapshot(tmp_path) == before+++def test_default_timeout_stays_under_the_desktop_silence_report():+ """install_watchdog.rs reports "still waiting" after REPORT_INTERVAL of silence: the one+ progress line must stay the only output, so the default bound has to end the step first."""+ rust = (REPO_ROOT / "studio" / "src-tauri" / "src" / "install_watchdog.rs").read_text(+ encoding = "utf-8"+ )+ match = re.search(r"REPORT_INTERVAL: Duration = Duration::from_secs\(([0-9 *]+)\)", rust)+ assert match, "REPORT_INTERVAL moved"+ report_interval = eval(match.group(1), {"__builtins__": {}})+ assert _helper()._DEFAULT_TIMEOUT + 30 <= report_interval+++def test_helper_default_dirs_add_an_editable_checkout_once(tmp_path, monkeypatch):+ helper = _helper()+ site = tmp_path / "site-packages"+ (site / "unsloth").mkdir(parents = True)+ checkout = tmp_path / "checkout" / "studio"+ checkout.mkdir(parents = True)+ (checkout / "__init__.py").write_text("", encoding = "utf-8")+ monkeypatch.setattr(+ helper.sysconfig, "get_paths", lambda: {"purelib": str(site), "platlib": str(site)}+ )++ class Spec:+ def __init__(self, path):+ self.submodule_search_locations = [path]++ locations = {"studio": str(checkout), "unsloth": str(site / "unsloth"), "unsloth_cli": None}+ monkeypatch.setattr(+ importlib.util,+ "find_spec",+ lambda name: Spec(locations[name]) if locations.get(name) else None,+ )+ got_site, got_checkout = helper.default_dirs()+ assert got_site == [str(site)]+ assert got_checkout == [str(checkout)]++ def src(path):+ path.parent.mkdir(parents = True, exist_ok = True)+ path.write_text("Z = 1\n", encoding = "utf-8")+ return str(path)++ want = [+ str(checkout / "__init__.py"),+ src(site / "unsloth" / "a.py"),+ src(site / "pkg" / "tests" / "test_kept_in_site.py"),+ src(checkout / "top.py"),+ src(checkout / "backend" / "main.py"),+ ]+ compiled = src(site / "unsloth" / "done.py")+ Path(importlib.util.cache_from_source(compiled)).parent.mkdir(exist_ok = True)+ Path(importlib.util.cache_from_source(compiled)).write_bytes(b"stale or not, never replaced")+ for skipped in (+ "frontend/node_modules/x.py",+ "src-tauri/y.py",+ "tests/test_z.py",+ "backend/tests/test_w.py",+ ".git/hooks/h.py",+ ):+ src(checkout / skipped)+ sources, targets, new_dirs = helper.plan(got_site, got_checkout)+ assert sorted(sources) == sorted(want)+ assert targets == [importlib.util.cache_from_source(s) for s in sources]+ assert new_dirs == {os.path.dirname(t) for t in targets} - {+ os.path.dirname(importlib.util.cache_from_source(compiled))+ }+++def test_helper_ignores_pythonpath_and_namespace_dirs(tmp_path, monkeypatch):+ """A PYTHONPATH tree or a bare namespace dir is not the app's code; a .pth-added checkout is."""+ helper = _helper()+ site = tmp_path / "site-packages"+ site.mkdir()+ monkeypatch.setattr(helper.sysconfig, "get_paths", lambda: {"purelib": str(site)})+ on_pythonpath = tmp_path / "pp"+ (on_pythonpath / "ib_fake_a").mkdir(parents = True)+ (on_pythonpath / "ib_fake_a" / "__init__.py").write_text("", encoding = "utf-8")+ editable = tmp_path / "checkout"+ (editable / "ib_fake_b").mkdir(parents = True)+ (editable / "ib_fake_b" / "__init__.py").write_text("", encoding = "utf-8")+ (editable / "ib_fake_c").mkdir() # namespace only: no __init__.py+ monkeypatch.setenv("PYTHONPATH", str(on_pythonpath))+ monkeypatch.syspath_prepend(str(on_pythonpath))+ monkeypatch.syspath_prepend(str(editable))+ monkeypatch.setattr(helper, "_PACKAGES", ("ib_fake_a", "ib_fake_b", "ib_fake_c"))+ before = list(sys.path)+ got_site, got_checkout = helper.default_dirs()+ assert sys.path == before+ assert got_site == [str(site)]+ assert got_checkout == [str(editable / "ib_fake_b")]diff --git a/tests/test_installer_interactive_prompts.py b/tests/test_installer_interactive_prompts.pyindex f576f8db42a..7c46bd570e1 100644--- a/tests/test_installer_interactive_prompts.py+++ b/tests/test_installer_interactive_prompts.py@@ -39,6 +39,7 @@ SCANNED_SCRIPTS = ENTRY_POINTS + ("studio/install_sd_cpp_prebuilt.py","studio/install_whisper_prebuilt.py","studio/nvidia_probe.py",+ "studio/precompile_bytecode.py","studio/backend/requirements/single-env/patch_metadata.py","studio/systemd/install_user_service.sh",)