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: keep the operator's package-manager policy in the installer (#10902)
Daniel Han6a4a041380e7
diff --git a/pyproject.toml b/pyproject.tomlindex 25c9e30baa4..b2ab1949a74 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -1669,6 +1669,7 @@ pythonpath = ["."]markers = ["gpu: needs a real accelerator, and usually downloads a checkpoint to use it","slow: minutes rather than seconds, and covered again more cheaply elsewhere",+ "reads_real_pip_config: opts out of the hermetic pip config stub, for the few tests that drive that read themselves",]# Deselected by default, not deleted: CI runs them in their own leg withdiff --git a/studio/install_python_stack.py b/studio/install_python_stack.pyindex 247f8224b8f..a39b715b431 100644--- a/studio/install_python_stack.py+++ b/studio/install_python_stack.py@@ -17,6 +17,7 @@ import globimport importlibimport importlib.utilimport json+import localeimport osimport platformimport re@@ -547,8 +548,8 @@ def _pytorch_whl_leaf_url(leaf: str) -> "str | None":No URL shape pins a query-auth mirror -- pip joins the project name as text, so the tokenswallows either the leaf or the name (see _warn_query_index_unusable). Constructing one- anyway is worse than declining: --index-url makes _install_env_for_cmd strip pip.conf and- ~/.netrc, the only channel that can carry that credential.+ anyway is worse than declining: --index-url makes _install_env_for_cmd strip pip.conf's+ index keys and ~/.netrc, the only channel that can carry that credential."""if "?" in _PYTORCH_WHL_BASE or "#" in _PYTORCH_WHL_BASE:_warn_query_index_unusable(_PYTORCH_WHL_BASE)@@ -6231,21 +6232,29 @@ def _progress(label: str) -> None:pass+_ENV_FOR_CMD = object()++def run(label: str,cmd: list[str],*,quiet: bool = True,check: bool = True,+ env: "dict[str, str] | None | object" = _ENV_FOR_CMD,) -> subprocess.CompletedProcess[bytes]:- """Run a command; on failure print output and exit, unless ``check`` is False."""+ """Run a command; on failure print output and exit, unless ``check`` is False.++ ``env`` is for a caller that already derived argv from the environment: see+ _pinned_cmd_and_env.+ """if VERBOSE:_step(_LABEL, f"{label}...", _dim)result = subprocess.run(cmd,stdout = subprocess.PIPE if quiet else None,stderr = subprocess.STDOUT if quiet else None,- env = _install_env_for_cmd(cmd),+ env = _install_env_for_cmd(cmd) if env is _ENV_FOR_CMD else env,**_windows_hidden_subprocess_kwargs(),)if result.returncode != 0:@@ -7394,6 +7403,64 @@ def _build_uv_cmd(args: tuple[str, ...]) -> list[str]:return cmd+def _pinned_cmd_and_env(cmd: "list[str]") -> "tuple[list[str], dict[str, str] | None]":+ """A command with its pinned binary-policy arguments, and the env it runs with, from ONE read.++ A failed read is not memoised, so a second one can succeed: the policy would then reach+ the environment but not the argv that carries it (uv) or exempts from it (both).+ """+ env = _install_env_for_cmd(cmd)+ return cmd + _pinned_binary_policy_args(cmd, env), env+++# Wheel-less dependencies of AMD's per-arch (gfx*) indexes: every torch there requires+# rocm[libraries], which AMD publishes only as an sdist (7.9 through 7.13). Exempted on those+# pins alone, so no other index can use the name to get a build past the operator's policy.+_AMD_ARCH_INDEX_SDIST_ONLY_PACKAGES = ("rocm",)+++def _pins_amd_arch_index(cmd: "list[str]") -> bool:+ """True when the index ``cmd`` pins has a gfx leaf, the shape of every AMD per-arch index."""+ for flag, value in zip(cmd, cmd[1:]):+ if flag in ("--index-url", "--default-index"):+ return bool(re.match(r"gfx\d", _torch_index_leaf(value)))+ return False+++def _pinned_binary_policy_args(cmd: "list[str]", env: "dict[str, str] | None") -> "list[str]":+ """The re-asserted only-binary as argv for a pinned command, plus its package exemptions.++ uv reads neither pip.conf nor PIP_ONLY_BINARY, and a pinned command runs with+ UV_NO_CONFIG=1 since a discovered uv.toml outranks the CLI pin (#6898), so the+ environment alone leaves the policy unenforced on the leg that runs. Measured on uv+ 0.10.7: a pinned install builds the sdist with PIP_ONLY_BINARY=:all: set and refuses it+ given --only-binary. Pinned only: others keep their config file and uv applies it.++ Only when a policy is in force, so an unconfigured host's argv is unchanged, and never for a+ package the operator named: a CLI --no-binary overrides their rule for it (pip 26.2).+ """+ if not _is_pinned_index_cmd(cmd):+ return []+ parts = [part.strip() for part in (env or {}).get("PIP_ONLY_BINARY", "").split(",")]+ parts = [part for part in parts if part]+ if not parts:+ return []+ args: list[str] = []+ if cmd[:1] == ["uv"]:+ # Repeatable rather than comma joined, which is the spelling uv takes (pip takes both).+ for part in parts:+ args.extend(["--only-binary", part])+ if not _pins_amd_arch_index(cmd):+ return args+ named = {_canonical_package_name(part) for part in parts}+ exempt = [+ name+ for name in _AMD_ARCH_INDEX_SDIST_ONLY_PACKAGES+ if _canonical_package_name(name) not in named+ ]+ return args + _sdist_only_build_args(*exempt)++# uv ranks --index-url LOWEST, so inherited index vars defeat a pinned repair; neutralise them._UV_INDEX_ENV_VARS = ("UV_CONFIG_FILE",@@ -7417,44 +7484,100 @@ def _is_pinned_index_cmd(cmd: "list[str] | tuple[str, ...]") -> bool:return any(arg in ("--index-url", "--default-index") for arg in cmd)-# Restrictive policy a pinned install must not inherit from the ENVIRONMENT. The pinned-# branch neutralises the config FILES (UV_NO_CONFIG=1 + PIP_CONFIG_FILE=devnull), but an-# env var outranks a config file, so a hardened shell could still fail a torch repair the-# pin was supposed to make deterministic (#8530).-_PM_POLICY_ENV_VARS = (- "UV_NO_BUILD",- "UV_NO_BUILD_PACKAGE",+# Unsatisfiable, not relaxed: every requirements file we ship is unhashed, so+# require-hashes can only abort (#8530). Env beats config, so the variables go too.+_PM_HASH_ENV_VARS = (+ "UV_REQUIRE_HASHES",+ "PIP_REQUIRE_HASHES",+)++# no-binary would FORCE a source build of a pinned wheel: clearing it is less build-time+# execution, not more. UV_EXCLUDE_NEWER because only uv reads it, so honouring it on the uv+# leg alone would let the pip fallback install past the cutoff.+_PM_FORCE_SOURCE_ENV_VARS = ("UV_NO_BINARY","UV_NO_BINARY_PACKAGE",- "UV_REQUIRE_HASHES",- "UV_EXCLUDE_NEWER",- "PIP_ONLY_BINARY","PIP_NO_BINARY",- "PIP_REQUIRE_HASHES",+ "UV_EXCLUDE_NEWER",)+# PIP_ONLY_BINARY stays in force: the pinned indexes serve wheels, rocm aside (see+# _AMD_ARCH_INDEX_SDIST_ONLY_PACKAGES), so it costs the pin nothing. Measured on uv 0.10.7, UV_NO_BUILD / UV_NO_BINARY / UV_ONLY_BINARY are not uv+# environment variables at all, so no uv.toml no-build can reach a pinned command.+def _relaxed_pip_policy_env(cmd: "list[str]") -> "dict[str, str]":- """Overrides that stop a hardened user pip config failing the installer's own pip.-- Empty for anything that is not a `pip install` / `pip download` / `pip wheel` this- module drives, every `uv` command included, so the "non-pinned installs inherit the- caller env unchanged" contract holds on a machine with no hostile pip config.- `wheel` is in that set because the duplicate-metadata repair stages its replacement- with `pip wheel`, where require-hashes applies exactly as it does to install: an- unpinned name is rejected before anything is built, so the repair would abort on a- hardened machine and leave the conflict it exists to remove.-- `require-hashes = true` makes pip reject any requirement without a --hash, which is- every requirements file we ship; that is what took the pip FALLBACK down in #8530- once uv had failed. pip applies env vars AFTER config files, so PIP_REQUIRE_HASHES=0- overrides it while pip.conf's index-url, trusted-host, cert and proxy stay in force.- """- if cmd[:1] == ["uv"] or not any(arg in ("install", "download", "wheel") for arg in cmd):+ """Hash mode off for one pip install / download / wheel, and nothing else relaxed.++ pip applies env vars AFTER config files, so this wins while pip.conf's index-url, cert,+ proxy and only-binary stay in force; the wheel-less requirements go through the+ package-scoped --no-binary in _sdist_only_build_args(). `wheel` is in the set because+ the duplicate-metadata repair stages with it, and require-hashes rejects that too+ (#8530).+ """+ if not _is_pip_subcommand(cmd, ("install", "download", "wheel")):return {}return {"PIP_REQUIRE_HASHES": "0"}+def _executable_stem(path: str) -> str:+ """argv[0] as a bare program name, on either platform's spelling.++ Both separators, because os.path.basename does not split a backslash off-Windows and+ the same argv reaches here from a Windows host, a test, or WSL interop.+ """+ leaf = re.split(r"[\\/]", path)[-1]+ return leaf.split(".")[0].lower()+++def _is_pip_subcommand(cmd: "list[str]", subcommands: "tuple[str, ...]") -> bool:+ """True when ``cmd`` is a pip invocation whose SUBCOMMAND is one of ``subcommands``.++ Structural, so the relaxation cannot ride along on a requirements path, a package+ named ``wheel``, or a uv command that merely contains the word.+ """+ args = list(cmd)+ if args[:1] == ["uv"]:+ return False+ if len(args) >= 3 and args[1] == "-m" and args[2] in ("pip", "pip3"):+ args = args[3:]+ elif args and _executable_stem(args[0]) in ("pip", "pip3"):+ args = args[1:]+ else:+ return False+ # The first token that NAMES a subcommand, not the first without a dash:+ # `pip --cache-dir /tmp/c install x` puts a bare path before it.+ for arg in args:+ if arg in _PIP_SUBCOMMANDS:+ return arg in subcommands+ return False+++# pip 26.2 `pip --help`; only marks where the options stop, so a missing future one is free.+_PIP_SUBCOMMANDS = frozenset(+ (+ "install",+ "download",+ "uninstall",+ "freeze",+ "inspect",+ "list",+ "show",+ "check",+ "config",+ "search",+ "cache",+ "index",+ "wheel",+ "hash",+ "completion",+ "debug",+ "help",+ "lock",+ )+)++def _uv_is_offline() -> bool:"""True when uv has been told not to touch the network.@@ -7612,13 +7735,16 @@ def _pip_config_without_sources(directory: str) -> str:[sys.executable, "-m", "pip", "config", "list"],stdout = subprocess.PIPE,stderr = subprocess.DEVNULL,+ # Dictate the child's encoding; see _decode_pip_output for why sniffing it+ # afterwards does not work. A non-ASCII cert path must survive this read.+ env = {**os.environ, "PYTHONIOENCODING": "utf-8"},**_windows_hidden_subprocess_kwargs(),)except OSError:result = Nonesections: dict[str, list[tuple[str, str]]] = {}if result is not None and result.returncode == 0:- for line in (result.stdout or b"").decode("utf-8", "replace").splitlines():+ for line in _decode_pip_output(result.stdout or b"").splitlines():name, separator, raw = line.partition("=")if not separator or name.startswith(":env:"):continue@@ -7747,13 +7873,13 @@ def _install_env_for_cmd(cmd: "list[str]") -> "dict[str, str] | None":"""Return an env with the uv index vars stripped for a pinned-index install.None (inherit env) when the command does NOT pin an index, so ordinary installs honour- the user's mirror. For pinned commands, the uv index/backend vars are removed,- UV_NO_CONFIG=1 set (a discovered uv.toml outranks the CLI pin), and PIP_CONFIG_FILE- pointed at os.devnull for the pip fallback. Mirrors install.sh's gate (#6898).+ the user's mirror. Pinned ones also get UV_NO_CONFIG=1, since a discovered uv.toml+ outranks the CLI pin (#6898), and no blanket amnesty from the operator's policy: only+ the hash and no-binary variables go, for the reasons at their definitions.- A non-pinned `pip` command also gets hash-required mode switched off, the one- relaxation with no command-line equivalent; the wheel-less requirements go through- the package-scoped --no-binary in _sdist_only_build_args() instead.+ PIP_CONFIG_FILE stays at os.devnull, the ONLY way to stop a site or global pip.conf+ contributing (measured on pip 26.2: naming a real file suppresses the per-user file+ alone). What that removes is put back key by key by _pinned_pip_config_overrides()."""if not _is_pinned_index_cmd(cmd):relaxed = _relaxed_pip_policy_env(cmd)@@ -7765,13 +7891,176 @@ def _install_env_for_cmd(cmd: "list[str]") -> "dict[str, str] | None":env = os.environ.copy()for name in _UV_INDEX_ENV_VARS:env.pop(name, None)- for name in _PM_POLICY_ENV_VARS:+ for name in _PM_HASH_ENV_VARS + _PM_FORCE_SOURCE_ENV_VARS:env.pop(name, None)+ # Both config files go for the pin, so re-assert what they carried that it does not+ # conflict with, from the section belonging to THIS command.+ for name, value in _pinned_pip_config_overrides(_pip_subcommand_of(cmd)).items():+ # Not setdefault: pip ignores an EMPTY environment value and falls through to the+ # config file (verified with `pip config debug`), which devnull has just removed.+ if not env.get(name):+ env[name] = value+ elif name in _PINNED_PIP_ENV_ACCUMULATING:+ # pip adds the environment's entries after the file's, in that order (pip 26.2:+ # file :all: then env :none: allows an sdist), so an env value must not replace it.+ env[name] = f"{value},{env[name]}"env["UV_NO_CONFIG"] = "1"env["PIP_CONFIG_FILE"] = os.devnullreturn env+# What devnull must not take with it: transport, without which a private index cannot be+# reached, plus only-binary. An ALLOWLIST: a config file holds options for any subcommand.+# Absent on purpose: the source keys, no-binary and require-hashes.+_PINNED_PIP_CONFIG_KEEP_KEYS = (+ "cert",+ "client-cert",+ "proxy",+ "trusted-host",+ "timeout",+ "retries",+ "keyring-provider",+ "only-binary",+)++# pip config is per subcommand: measured on pip 26.2, `[download] no-index` stops a+# `pip download` and leaves `pip install` alone. A PIP_ variable is command-wide, so the+# only faithful translation reads `[global]` plus the section for the command being run.+# A uv command defaults to install, since the PIP_ vars only reach its pip fallback.+_PINNED_PIP_CONFIG_GLOBAL_SECTION = "global"+_PINNED_PIP_CONFIG_DEFAULT_SECTION = "install"++# Keys pip reads as a LIST, and the separator each is spelled with in the environment.+# Only these: elsewhere the newline `pip config list` renders is part of one value, and+# collapsing it would corrupt a path like `C:\Program Files\ca.pem`.+_PINNED_PIP_CONFIG_SEPARATORS = {"trusted-host": " ", "only-binary": ","}++# ...and of those, the one pip ACCUMULATES across sections instead of overriding. Asked of+# pip 26.2's parser with both sections set, `trusted_hosts` holds the install value alone+# (assigned per section) while `format_control` holds both (a callback that mutates in+# place). Accumulating trusted-host would re-trust a host install dropped, a TLS decision.+_PINNED_PIP_CONFIG_ACCUMULATING = frozenset({"only-binary"})+_PINNED_PIP_ENV_ACCUMULATING = frozenset(+ f"PIP_{option.upper().replace('-', '_')}" for option in _PINNED_PIP_CONFIG_ACCUMULATING+)++_PINNED_PIP_CONFIG_LISTING: "bytes | None" = None++# Failures are NOT memoised: a transient miss must not cost the operator their cert for the+# whole run, and a venv with no pip YET must be able to answer once it has one. Only a HANG+# is budgeted, which would otherwise pay the timeout once per pinned command.+_PINNED_PIP_CONFIG_TIMEOUT = 30+_PINNED_PIP_CONFIG_ATTEMPTS = 2+++def _pip_subcommand_of(cmd: "list[str]") -> str:+ """The pip subcommand ``cmd`` runs, or the default when it is not a pip command."""+ for name in ("install", "download", "wheel"):+ if _is_pip_subcommand(cmd, (name,)):+ return name+ return _PINNED_PIP_CONFIG_DEFAULT_SECTION+++def _pinned_pip_config_overrides(+ subcommand: str = _PINNED_PIP_CONFIG_DEFAULT_SECTION,+) -> "dict[str, str]":+ """pip's configured transport and binary policy, as PIP_ environment variables.++ ONLY a successful read is memoised, or a transient miss would cost the operator their+ cert and proxy for the rest of the run, and a venv with no pip yet could never answer+ once it has one. A HANG is budgeted instead, so a wedged pip costs the run one timeout+ budget rather than one per pinned command. Empty when pip cannot answer, the normal+ case early in a fresh venv. `:env:` rows are skipped: the child inherits those.+ """+ global _PINNED_PIP_CONFIG_LISTING, _PINNED_PIP_CONFIG_ATTEMPTS+ if _PINNED_PIP_CONFIG_LISTING is not None:+ return _parse_pinned_pip_config(_PINNED_PIP_CONFIG_LISTING, subcommand)+ if _PINNED_PIP_CONFIG_ATTEMPTS <= 0:+ return {}+ try:+ result = subprocess.run(+ [sys.executable, "-m", "pip", "config", "list"],+ stdout = subprocess.PIPE,+ stderr = subprocess.DEVNULL,+ # Dictate the child's encoding; see _decode_pip_output. Guessing loses a+ # non-ASCII cert path, and pip then fails rather than dropping the setting.+ env = {**os.environ, "PYTHONIOENCODING": "utf-8"},+ timeout = _PINNED_PIP_CONFIG_TIMEOUT,+ **_windows_hidden_subprocess_kwargs(),+ )+ except subprocess.TimeoutExpired:+ _PINNED_PIP_CONFIG_ATTEMPTS -= 1+ return {}+ except (OSError, subprocess.SubprocessError):+ return {}+ if result.returncode != 0:+ return {}+ _PINNED_PIP_CONFIG_LISTING = result.stdout or b""+ return _parse_pinned_pip_config(_PINNED_PIP_CONFIG_LISTING, subcommand)+++def _decode_pip_output(raw: bytes) -> str:+ r"""`pip config list` bytes as text.++ The child is told to write UTF-8 (see PYTHONIOENCODING above). The fallback covers a+ listing produced some other way, a pip old enough to ignore that variable among them,+ where the locale codec is the child's encoding since both share a locale. Sniffing+ replaces neither: cp1252 bytes can form valid UTF-8, so it is not recoverable after.+ """+ try:+ return raw.decode("utf-8")+ except UnicodeDecodeError:+ return raw.decode(locale.getpreferredencoding(False), "replace")+++def _parse_pinned_pip_config(+ stdout: bytes, subcommand: str = _PINNED_PIP_CONFIG_DEFAULT_SECTION+) -> "dict[str, str]":+ """`pip config list` output, filtered to the allowlist, as PIP_ variables.++ Reads `[global]` plus `[<subcommand>]`, the two sections pip itself would apply to that+ command, with the command section winning. Resolved by position rather than by the+ order the listing prints in. An unparseable line is skipped, never fatal.+ """+ sections = (_PINNED_PIP_CONFIG_GLOBAL_SECTION, subcommand)+ found: dict[str, dict[str, str]] = {}+ for line in _decode_pip_output(stdout).splitlines():+ name, separator, raw = line.partition("=")+ if not separator or name.startswith(":env:"):+ continue+ section, _, option = name.strip().rpartition(".")+ if section not in sections:+ continue+ if option not in _PINNED_PIP_CONFIG_KEEP_KEYS:+ continue+ try:+ value = ast.literal_eval(raw.strip())+ except (ValueError, SyntaxError):+ continue+ separator_for_key = _PINNED_PIP_CONFIG_SEPARATORS.get(option)+ if separator_for_key is None:+ text = str(value).strip()+ else:+ text = separator_for_key.join(str(value).split())+ if text:+ found.setdefault(option, {})[section] = text+ overrides: dict[str, str] = {}+ for option, by_section in found.items():+ separator_for_key = _PINNED_PIP_CONFIG_SEPARATORS.get(option)+ present = [by_section[name] for name in sections if name in by_section]+ if option not in _PINNED_PIP_CONFIG_ACCUMULATING:+ value = present[-1] # the command's section overrides global+ else:+ # Accumulates across sections, and pip applies the entries IN ORDER, so a+ # re-add outranks the `:none:` that empties the set. Measured on pip 26.2:+ # [global] a,b plus [install] :none:,a keeps a. Deduplicating left `:none:`+ # last instead, letting a pinned install build the forbidden sdist.+ parts = [part for chunk in present for part in chunk.split(separator_for_key) if part]+ value = separator_for_key.join(parts)+ overrides[f"PIP_{option.upper().replace('-', '_')}"] = value+ return overrides++def pip_install_try(label: str,*args: str,@@ -7793,9 +8082,9 @@ def pip_install_try(constraint_args_uv = ["-c", _uv_safe_path(CONSTRAINTS)]if USE_UV and not force_pip:- cmd = _build_uv_cmd(args) + constraint_args_uv+ cmd, env = _pinned_cmd_and_env(_build_uv_cmd(args) + constraint_args_uv)else:- cmd = _build_pip_cmd(args) + constraint_args_pip+ cmd, env = _pinned_cmd_and_env(_build_pip_cmd(args) + constraint_args_pip)if VERBOSE:_step(_LABEL, f"{label}...", _dim)@@ -7803,7 +8092,7 @@ def pip_install_try(cmd,stdout = subprocess.PIPE,stderr = subprocess.STDOUT,- env = _install_env_for_cmd(cmd),+ env = env,)if result.returncode == 0:# As pip_install below: `nobuild` only catches a build that reaches the log.@@ -7848,14 +8137,16 @@ def pip_install(try:if USE_UV:- uv_cmd = _build_uv_cmd(args) + constraint_args_uv + req_args_uv+ uv_cmd, uv_env = _pinned_cmd_and_env(+ _build_uv_cmd(args) + constraint_args_uv + req_args_uv+ )if VERBOSE:_safe_print(f" {label}...")result = subprocess.run(uv_cmd,stdout = subprocess.PIPE,stderr = subprocess.STDOUT,- env = _install_env_for_cmd(uv_cmd),+ env = uv_env,**_windows_hidden_subprocess_kwargs(),)if result.returncode == 0:@@ -7872,9 +8163,11 @@ def pip_install(if result.stdout:_safe_print(_redact_install_output(result.stdout))- pip_cmd = _build_pip_cmd(args) + constraint_args_pip + req_args_pip+ pip_cmd, pip_env = _pinned_cmd_and_env(+ _build_pip_cmd(args) + constraint_args_pip + req_args_pip+ )pip_label = f"{label} (pip)" if USE_UV else label- result = run(pip_label, pip_cmd, check = False)+ result = run(pip_label, pip_cmd, check = False, env = pip_env)if result.returncode != 0:# Retry once, and only after clearing something pip named as# unremovable: a blind retry of a failing install just doubles the wait.@@ -7882,7 +8175,7 @@ def pip_install(if not cleared:_report_failed_command(pip_label, result)_step(_LABEL, f"cleared half-written {', '.join(cleared)}, retrying...", _dim)- run(pip_label, pip_cmd)+ run(pip_label, pip_cmd, env = pip_env)finally:for temp_req in temp_reqs:temp_req.unlink(missing_ok = True)diff --git a/tests/python/test_cross_platform_parity.py b/tests/python/test_cross_platform_parity.pyindex afb14779cc2..f06566103be 100644--- a/tests/python/test_cross_platform_parity.py+++ b/tests/python/test_cross_platform_parity.py@@ -830,6 +830,12 @@ class TestPinnedIndexClearsUvEnvParity:"_install_env_for_cmd must point PIP_CONFIG_FILE at os.devnull for ""pinned installs (pip fallback isolation)")+ # devnull is all or nothing, so the transport and only-binary it removes are put+ # back key by key.+ assert "_pinned_pip_config_overrides()" in stack, (+ "the pinned scrub must re-assert the operator's transport and binary policy "+ "that PIP_CONFIG_FILE=devnull removes"+ )setup = SETUP_PS1.read_text(encoding = "utf-8")assert "$env:PIP_CONFIG_FILE = 'nul'" in setup, ("setup.ps1 Fast-Install pinned scrub must point PIP_CONFIG_FILE at nul "diff --git a/tests/python/test_install_python_stack.py b/tests/python/test_install_python_stack.pyindex 89b9cbfc51c..6572aced988 100644--- a/tests/python/test_install_python_stack.py+++ b/tests/python/test_install_python_stack.py@@ -10,6 +10,7 @@ import ioimport osimport reimport shutil+import subprocessimport sysimport typesfrom pathlib import Path@@ -67,6 +68,161 @@ sys.path.insert(0, str(STUDIO_DIR))import install_python_stack as ips+STACK_SOURCE = (STUDIO_DIR / "install_python_stack.py").read_text(encoding = "utf-8")+++# A CI image with its own /etc/pip.conf would leak into these assertions, and a test that+# mocks subprocess could poison the memoised read for whatever runs next under -p randomly.[email protected](autouse = True)+def _hermetic_pinned_pip_config(request):+ ips._PINNED_PIP_CONFIG_LISTING = None+ ips._PINNED_PIP_CONFIG_ATTEMPTS = 2+ if "reads_real_pip_config" in request.keywords:+ yield+ else:+ with mock.patch.object(ips, "_pinned_pip_config_overrides", lambda *a, **k: {}):+ yield+ ips._PINNED_PIP_CONFIG_LISTING = None+ ips._PINNED_PIP_CONFIG_ATTEMPTS = 2+++class TestUvOnlyBinaryOnPinnedCommands:+ """uv reads neither pip.conf nor PIP_ONLY_BINARY, and a pinned command runs with+ UV_NO_CONFIG=1, so restoring the policy in the environment alone leaves it unenforced+ on the leg that actually runs. Measured against uv 0.10.7: with PIP_ONLY_BINARY=:all:+ set, a pinned `uv pip install` builds the sdist anyway, and `--only-binary` refuses+ it."""++ PINNED = ("torch", "--index-url", "https://pin.example/whl")+ AMD = ("torch", "--index-url", "https://repo.amd.com/rocm/whl/gfx1151/")++ def _uv_cmd(self, args):+ return ips._pinned_cmd_and_env(ips._build_uv_cmd(args))[0]++ def _pip_cmd(self, args):+ return ips._pinned_cmd_and_env(ips._build_pip_cmd(args))[0]++ def test_a_pinned_uv_command_carries_only_binary_as_flags(self):+ with mock.patch.dict(os.environ, {"PIP_ONLY_BINARY": ":all:"}):+ cmd = self._uv_cmd(self.PINNED)+ assert cmd[-2:] == ["--only-binary", ":all:"]++ def test_each_entry_becomes_its_own_flag(self):+ """uv takes the option repeatably, not comma joined the way pip spells it."""+ with mock.patch.dict(os.environ, {"PIP_ONLY_BINARY": ":none:,numpy"}):+ cmd = self._uv_cmd(self.PINNED)+ assert cmd[-4:] == ["--only-binary", ":none:", "--only-binary", "numpy"]++ def test_an_amd_arch_pin_exempts_rocm_on_the_uv_leg(self):+ """Every torch on a gfx* index requires rocm[libraries], published as an sdist alone."""+ with mock.patch.dict(os.environ, {"PIP_ONLY_BINARY": ":all:"}):+ cmd = self._uv_cmd(self.AMD)+ assert cmd[-4:] == ["--only-binary", ":all:", "--no-binary", "rocm"]++ def test_an_amd_arch_pip_command_keeps_the_policy_in_env_and_the_exemption_in_argv(self):+ """pip reads PIP_ONLY_BINARY itself; the command line only has to exempt rocm."""+ with mock.patch.dict(os.environ, {"PIP_ONLY_BINARY": ":all:"}):+ cmd, env = ips._pinned_cmd_and_env(ips._build_pip_cmd(self.AMD))+ assert "--only-binary" not in cmd+ assert cmd[-2:] == ["--no-binary", "rocm"]+ assert env["PIP_ONLY_BINARY"] == ":all:"++ @pytest.mark.parametrize(+ "index",+ (+ "https://download.pytorch.org/whl/cu128",+ "https://download.pytorch.org/whl/cpu",+ "https://download.pytorch.org/whl/xpu",+ "https://download.pytorch.org/whl/rocm7.2",+ "https://mirror.corp/gfx1151/cu128",+ "https://mirror.corp/gfx-private",+ ),+ )+ @pytest.mark.parametrize("leg", ("uv", "pip"))+ def test_no_other_pin_is_exempted(self, index, leg):+ """Anywhere else the name would only let an index get a build past the policy."""+ with mock.patch.dict(os.environ, {"PIP_ONLY_BINARY": ":all:"}):+ args = ("torch", "--index-url", index)+ cmd = self._uv_cmd(args) if leg == "uv" else self._pip_cmd(args)+ assert "--no-binary" not in cmd++ @pytest.mark.parametrize(+ "index",+ (+ "https://mirror.corp/amd/gfx120X-all/",+ "https://mirror.corp/amd/gfx110X-all?token=x",+ ),+ )+ def test_a_mirrored_amd_arch_index_is_exempted_too(self, index):+ """UNSLOTH_AMD_ROCM_MIRROR / UNSLOTH_ROCM_WINDOWS_MIRROR keep the gfx leaf."""+ with mock.patch.dict(os.environ, {"PIP_ONLY_BINARY": ":all:"}):+ cmd = self._pip_cmd(("torch", "--index-url", index))+ assert cmd[-2:] == ["--no-binary", "rocm"]++ @pytest.mark.parametrize("policy", ("rocm", ":all:,rocm", ":all:,ROCm"))+ @pytest.mark.parametrize("leg", ("uv", "pip"))+ def test_a_package_the_operator_names_is_not_exempted(self, policy, leg):+ """A command-line --no-binary overrides the operator's own rule for that package."""+ with mock.patch.dict(os.environ, {"PIP_ONLY_BINARY": policy}):+ cmd = self._uv_cmd(self.AMD) if leg == "uv" else self._pip_cmd(self.AMD)+ assert "--no-binary" not in cmd++ @pytest.mark.reads_real_pip_config # stubbed below with a read that fails once+ @pytest.mark.parametrize("installer", ("pip_install", "pip_install_try"))+ def test_the_flag_and_the_environment_can_never_disagree(self, monkeypatch, installer):+ """Both come from one read. A failed read is not memoised, so asking twice let a+ transient miss leave the flag off the argv while the retry put the policy in the+ environment, which uv never reads."""+ answers = iter(({}, {"PIP_ONLY_BINARY": ":all:"}))+ monkeypatch.setattr(ips, "_pinned_pip_config_overrides", lambda *a, **k: next(answers, {}))+ monkeypatch.delenv("PIP_ONLY_BINARY", raising = False)+ monkeypatch.setattr(ips, "USE_UV", True)+ runs = []+ monkeypatch.setattr(+ ips.subprocess,+ "run",+ lambda cmd, **kwargs: runs.append((cmd, kwargs.get("env")))+ or subprocess.CompletedProcess(cmd, 0, b""),+ )+ getattr(ips, installer)("torch", *self.PINNED, constrain = False)+ ((cmd, env),) = runs+ flagged = cmd[cmd.index("--only-binary") + 1] if "--only-binary" in cmd else None+ assert flagged == (env or {}).get("PIP_ONLY_BINARY")++ @pytest.mark.reads_real_pip_config # stubbed below with reads that disagree+ def test_the_pip_fallback_runs_with_the_env_its_exemption_came_from(self, monkeypatch):+ """uv fails, so pip_install falls back. Had run() read the config again, a policy that+ appeared on that read would refuse rocm with no exemption on the argv."""+ answers = iter(({}, {}, {"PIP_ONLY_BINARY": ":all:"}))+ monkeypatch.setattr(ips, "_pinned_pip_config_overrides", lambda *a, **k: next(answers, {}))+ monkeypatch.delenv("PIP_ONLY_BINARY", raising = False)+ monkeypatch.setattr(ips, "USE_UV", True)+ runs = []++ def fake_run(cmd, **kwargs):+ runs.append((cmd, kwargs.get("env")))+ return subprocess.CompletedProcess(cmd, 1 if cmd[:1] == ["uv"] else 0, b"")++ monkeypatch.setattr(ips.subprocess, "run", fake_run)+ ips.pip_install("torch", *self.AMD, constrain = False)+ pip_cmd, pip_env = runs[-1]+ assert pip_cmd[:3] == [sys.executable, "-m", "pip"]+ assert ("--no-binary" in pip_cmd) == bool((pip_env or {}).get("PIP_ONLY_BINARY"))++ def test_a_non_pinned_command_is_left_alone(self):+ """It keeps its config file, so uv applies the operator's policy itself."""+ with mock.patch.dict(os.environ, {"PIP_ONLY_BINARY": ":all:"}):+ assert self._uv_cmd(("torch",)) == ips._build_uv_cmd(("torch",))+ assert self._pip_cmd(("torch",)) == ips._build_pip_cmd(("torch",))++ def test_no_policy_leaves_the_argv_untouched(self):+ """An unconfigured host must run exactly the command main ran, AMD indexes included."""+ env = {k: v for k, v in os.environ.items() if k != "PIP_ONLY_BINARY"}+ with mock.patch.dict(os.environ, env, clear = True):+ for args in (self.PINNED, self.AMD):+ assert self._uv_cmd(args) == ips._build_uv_cmd(args)+ assert self._pip_cmd(args) == ips._build_pip_cmd(args)+class TestBuildUvCmdTorchBackend:"""Verify _build_uv_cmd only adds --torch-backend when UV_TORCH_BACKEND is set."""@@ -312,10 +468,9 @@ class TestPinnedIndexClearsUvEnv:assert "UV_CONFIG_FILE" not in envdef test_pinned_cmd_disables_pip_config_files(self):- """The pip FALLBACK honours user/site pip config files (pip config set- global.extra-index-url) even with the PIP_* env vars stripped; pip loads- NO configuration files when PIP_CONFIG_FILE is os.devnull. Harmless for- uv, decisive for the fallback."""+ """devnull is the ONLY spelling that reaches a SITE or GLOBAL pip.conf. Measured+ on pip 26.2: naming a real file suppresses the per-user file alone, so a+ venv-level `no-index` still killed the pin."""env = ips._install_env_for_cmd(["uv", "pip", "install", "torch", "--index-url", "https://x/cu128"])@@ -506,18 +661,429 @@ class TestHardenedPipConfigRelaxation:assert ips._install_env_for_cmd(["python", "-m", "pip", "--version"]) is Noneassert ips._install_env_for_cmd(["python", "-m", "ensurepip", "--upgrade"]) is None- def test_pinned_cmd_strips_restrictive_policy_env(self):- """The pinned branch neutralises the config FILES, but an env var outranks a- config file, so a hardened shell could still fail a torch repair the pin was- supposed to make deterministic."""+ def test_pinned_cmd_clears_hash_mode_only(self):+ """Hash enforcement, which our unhashed requirements cannot satisfy, and nothing+ else the operator hardened: dropping only-binary would let a compromised mirror+ run a source build they had forbidden."""with mock.patch.dict(os.environ, self.HOSTILE):env = ips._install_env_for_cmd(["uv", "pip", "install", "torch", "--index-url", "https://x/cu128"])assert env is not None- for name in ("PIP_REQUIRE_HASHES", "PIP_ONLY_BINARY", "UV_NO_BUILD", "UV_EXCLUDE_NEWER"):- assert name not in env, f"{name} must be cleared for a pinned install"- assert env["UV_NO_CONFIG"] == "1" and env["PIP_CONFIG_FILE"] == os.devnull+ for name in ("PIP_REQUIRE_HASHES", "UV_REQUIRE_HASHES"):+ assert name not in env, f"{name} cannot be satisfied by an unhashed pin"+ assert env["PIP_ONLY_BINARY"] == ":all:"+ assert env["UV_NO_BUILD"] == "1" # inert for uv, but not ours to drop either+ assert env["UV_NO_CONFIG"] == "1"++ def test_pinned_cmd_clears_an_upload_cutoff_uv_alone_would_honour(self):+ """Only uv reads it, and pip_install falls back to pip whenever uv fails, so+ honouring it on the uv leg alone lets the fallback install past the cutoff."""+ with mock.patch.dict(os.environ, self.HOSTILE):+ env = ips._install_env_for_cmd(+ ["uv", "pip", "install", "torch", "--index-url", "https://x/cu128"]+ )+ assert "UV_EXCLUDE_NEWER" not in env++ def test_pinned_cmd_clears_forced_source_builds(self):+ """Dropping it is hardening: it would force torch to be BUILT from an sdist the+ pinned index does not serve."""+ with mock.patch.dict(os.environ, {"PIP_NO_BINARY": ":all:", "UV_NO_BINARY": ":all:"}):+ env = ips._install_env_for_cmd(+ ["python", "-m", "pip", "install", "torch", "--index-url", "https://x/cu128"]+ )+ assert env is not None+ assert "PIP_NO_BINARY" not in env and "UV_NO_BINARY" not in env++ LISTING = (+ "global.index-url='https://mirror/simple'\n"+ "global.extra-index-url='https://other/simple'\n"+ "global.no-index='true'\n"+ "global.no-binary=':all:'\n"+ "global.require-hashes='true'\n"+ "install.only-binary=':all:'\n"+ "global.cert='/etc/ssl/corp.pem'\n"+ "global.proxy='http://proxy.corp:3128'\n"+ "global.trusted-host='a.corp\\nb.corp'\n"+ "list.format='columns'\n"+ ":env:.no-binary=':all:'\n"+ )++ def _overrides(+ self,+ listing = None,+ subcommand = "install",+ ):+ return ips._parse_pinned_pip_config(+ (self.LISTING if listing is None else listing).encode(), subcommand+ )++ def test_devnull_gives_the_operators_policy_and_transport_back(self):+ """only-binary is the security half; cert / proxy / trusted-host are the half+ that makes a private index reachable, which devnull alone used to drop."""+ overrides = self._overrides()+ assert overrides["PIP_ONLY_BINARY"] == ":all:"+ assert overrides["PIP_CERT"] == "/etc/ssl/corp.pem"+ assert overrides["PIP_PROXY"] == "http://proxy.corp:3128"+ # Newline separated by `pip config list`, whitespace separated in the environment.+ assert overrides["PIP_TRUSTED_HOST"] == "a.corp b.corp"++ def test_the_pin_and_the_unsatisfiable_policy_never_come_back(self):+ """The pin replaces the source keys, no-binary would force a source build, and+ require-hashes cannot be met. None may come back."""+ overrides = self._overrides()+ for name in (+ "PIP_INDEX_URL",+ "PIP_EXTRA_INDEX_URL",+ "PIP_NO_INDEX",+ "PIP_NO_BINARY",+ "PIP_REQUIRE_HASHES",+ ):+ assert name not in overrides, f"{name} must not survive the pinned scrub"++ def test_options_from_unrelated_sections_are_not_translated(self):+ """`list.format` becoming PIP_FORMAT would apply it to install."""+ assert "PIP_FORMAT" not in self._overrides()++ def test_env_entries_are_skipped(self):+ """The child already inherits them, and re-asserting would undo the scrub."""+ assert "PIP_NO_BINARY" not in self._overrides()++ @pytest.mark.reads_real_pip_config+ @pytest.mark.parametrize(+ "outcome",+ [+ mock.Mock(returncode = 1, stdout = b""), # no pip in the venv yet+ mock.Mock(returncode = 0, stdout = None), # nothing captured+ OSError("no pip"),+ subprocess.TimeoutExpired("pip", 60), # a wedged pip+ ],+ )+ def test_a_pip_that_cannot_answer_changes_nothing(self, outcome):+ """On the path to every pinned install, so anything but a clean listing has to+ degrade to no overrides."""+ kwargs = (+ {"side_effect": outcome}+ if isinstance(outcome, Exception)+ else {"return_value": outcome}+ )+ with mock.patch.object(ips.subprocess, "run", **kwargs):+ assert ips._pinned_pip_config_overrides() == {}++ @pytest.mark.reads_real_pip_config+ def test_only_a_successful_read_is_cached(self):+ """A transient miss must not cost the operator their cert and proxy for the rest+ of the run."""+ listing = b"global.cert='/etc/ssl/corp.pem'\n"+ with mock.patch.object(ips.subprocess, "run", side_effect = OSError("wedged")):+ assert ips._pinned_pip_config_overrides() == {}+ with mock.patch.object(ips.subprocess, "run") as run:+ run.return_value = mock.Mock(returncode = 0, stdout = listing)+ assert ips._pinned_pip_config_overrides() == {"PIP_CERT": "/etc/ssl/corp.pem"}+ assert run.call_count == 1+ # ...and the success IS cached: N pinned commands, one subprocess.+ with mock.patch.object(ips.subprocess, "run") as run:+ assert ips._pinned_pip_config_overrides() == {"PIP_CERT": "/etc/ssl/corp.pem"}+ assert run.call_count == 0++ def test_garbage_in_the_listing_is_ignored_not_fatal(self):+ for listing in (+ b"",+ b"not a config listing\n",+ b"global.cert\n",+ b"=\n",+ b"global.cert=<unparseable>\n",+ b"\xff\xfe binary \x00\n",+ ):+ assert ips._parse_pinned_pip_config(listing) == {}++ def test_a_command_section_beats_global_for_a_scalar(self):+ """pip's own precedence for a single-valued option, resolved by position, not by+ the order the listing prints the two lines in."""+ for listing in (+ b"global.timeout='30'\ninstall.timeout='9'\n",+ b"install.timeout='9'\nglobal.timeout='30'\n",+ ):+ assert ips._parse_pinned_pip_config(listing)["PIP_TIMEOUT"] == "9"++ def test_the_section_read_is_the_one_pip_would_apply(self):+ """pip config is per subcommand: measured on pip 26.2, `[download] no-index` stops+ a `pip download` and leaves `pip install` alone. A PIP_ variable is command-wide,+ so reading `[install]` for a download both drops that command's own settings and+ imposes another command's."""+ listing = (+ b"global.timeout='30'\ninstall.only-binary=':all:'\n"+ b"install.timeout='9'\ndownload.timeout='5'\ndownload.cert='/etc/dl.pem'\n"+ )+ for_install = ips._parse_pinned_pip_config(listing, "install")+ assert for_install["PIP_TIMEOUT"] == "9" and for_install["PIP_ONLY_BINARY"] == ":all:"+ assert "PIP_CERT" not in for_install+ for_download = ips._parse_pinned_pip_config(listing, "download")+ assert for_download["PIP_TIMEOUT"] == "5" and for_download["PIP_CERT"] == "/etc/dl.pem"+ assert "PIP_ONLY_BINARY" not in for_download, "an install-only policy is not a download one"+ # A section belonging to neither is never read.+ assert "PIP_FORMAT" not in ips._parse_pinned_pip_config(+ b"list.format='columns'\n", "install"+ )++ @pytest.mark.parametrize(+ "cmd, expected",+ [+ (["python", "-m", "pip", "install", "x", "--index-url", "u"], "install"),+ (["python", "-m", "pip", "download", "x", "--index-url", "u"], "download"),+ (["python", "-m", "pip", "wheel", "x", "--index-url", "u"], "wheel"),+ # uv reads none of the PIP_ vars; they exist for its pip FALLBACK, an install.+ (["uv", "pip", "install", "x", "--index-url", "u"], "install"),+ (["python", "-m", "pip", "uninstall", "-y", "x"], "install"),+ ],+ )+ def test_the_subcommand_drives_which_section_is_read(self, cmd, expected):+ assert ips._pip_subcommand_of(cmd) == expected++ @pytest.mark.reads_real_pip_config+ def test_the_xpu_download_gets_the_download_section(self):+ """_ensure_xpu_triton's pinned fetch is a `pip download`, so a corporate+ `[download] cert` must reach it rather than an `[install]` one.++ PIP_CERT is cleared first: the caller's environment legitimately wins over the+ re-assertion, so a host that exports one would otherwise make this assert on the+ ambient value and pass or fail for reasons that have nothing to do with sections.+ """+ listing = b"download.cert='/etc/dl.pem'\ninstall.cert='/etc/inst.pem'\n"+ env_without_cert = {k: v for k, v in os.environ.items() if k != "PIP_CERT"}+ with (+ mock.patch.object(ips, "_PINNED_PIP_CONFIG_LISTING", listing),+ mock.patch.dict(os.environ, env_without_cert, clear = True),+ ):+ env = ips._install_env_for_cmd(+ ["python", "-m", "pip", "download", "triton", "--index-url", "https://x/xpu"]+ )+ assert env["PIP_CERT"] == "/etc/dl.pem"++ def test_an_empty_inherited_value_is_not_an_override(self):+ ambient = ""+ """pip ignores an EMPTY environment value and falls through to the config file+ (verified with `pip config debug`), which the pinned branch has just switched off+ with devnull. Treating it as set would lose the operator's cert entirely. Only a+ truly empty value: a whitespace one is a value pip would use, not ours to+ second-guess."""+ with (+ mock.patch.object(+ ips, "_pinned_pip_config_overrides", lambda *a, **k: {"PIP_CERT": "/etc/corp.pem"}+ ),+ mock.patch.dict(os.environ, {"PIP_CERT": ambient}),+ ):+ env = ips._install_env_for_cmd(+ ["uv", "pip", "install", "torch", "--index-url", "https://x/cu128"]+ )+ assert env["PIP_CERT"] == "/etc/corp.pem"++ def test_a_repeatable_option_accumulates_across_sections(self):+ """Measured on pip 26.2: `[global] only-binary = :all:` plus+ `[install] only-binary = numpy` still refuses an unrelated sdist, so pip+ accumulates the two rather than letting the command section replace the global+ one. Keeping only `numpy` would drop the operator's :all: policy on every pinned+ install, which is the control this change exists to preserve."""+ listing = b"global.only-binary=':all:'\ninstall.only-binary='numpy'\n"+ assert ips._parse_pinned_pip_config(listing, "install")["PIP_ONLY_BINARY"] == ":all:,numpy"+ # A scalar still takes the command section alone: two certs cannot be concatenated.+ certs = b"global.cert='/etc/g.pem'\ninstall.cert='/etc/i.pem'\n"+ assert ips._parse_pinned_pip_config(certs, "install")["PIP_CERT"] == "/etc/i.pem"++ def test_a_non_utf8_listing_is_decoded_the_way_the_child_wrote_it(self, monkeypatch):+ """A piped child encodes stdout with ITS locale encoding, which on Windows is the+ ANSI code page, not UTF-8. Getting that wrong does not merely lose the setting: it+ yields a cert path that exists nowhere, so pip fails the pinned install outright on+ exactly the corporate host the allowlist exists to serve. The read dictates the+ child's encoding (below) rather than sniffing it, since cp1252 bytes can form valid+ UTF-8; this covers the fallback, for a listing produced some other way."""+ path = "C:\\Soci\u00e9t\u00e9\\ca.pem"+ # repr, the way `pip config list` itself prints a value.+ listing = f"global.cert={path!r}\n".encode("cp1252")+ monkeypatch.setattr(ips.locale, "getpreferredencoding", lambda *a: "cp1252")+ assert ips._parse_pinned_pip_config(listing) == {"PIP_CERT": path}+ # UTF-8 is still tried first and strictly, so the POSIX case is untouched.+ assert (+ ips._decode_pip_output("cert='/etc/caf\u00e9/ca.pem'".encode())+ == "cert='/etc/caf\u00e9/ca.pem'"+ )+ # Undecodable under either codec is skipped, never fatal.+ assert ips._parse_pinned_pip_config(b"global.cert=\xff\xfe\x00") == {}++ @pytest.mark.reads_real_pip_config+ def test_a_wedged_pip_costs_the_run_one_budget_not_one_per_command(self, monkeypatch):+ """Failures are deliberately not memoised, so a transient miss cannot cost the+ operator their cert for the whole run. Unbounded, a HANG paid the timeout once per+ pinned command instead of once."""+ attempts = []++ def always_fails(cmd, **kwargs):+ attempts.append(kwargs.get("timeout"))+ raise subprocess.TimeoutExpired(cmd, kwargs.get("timeout"))++ monkeypatch.setattr(ips.subprocess, "run", always_fails)+ ips._PINNED_PIP_CONFIG_LISTING = None+ ips._PINNED_PIP_CONFIG_ATTEMPTS = 2+ for _ in range(25):+ assert ips._pinned_pip_config_overrides() == {}+ assert len(attempts) == 2, attempts+ assert set(attempts) == {ips._PINNED_PIP_CONFIG_TIMEOUT}+ # ...and small enough that the worst case is a wait, not a hang.+ assert ips._PINNED_PIP_CONFIG_TIMEOUT * 2 <= 60++ @pytest.mark.reads_real_pip_config+ def test_a_cheap_failure_does_not_spend_the_budget(self, monkeypatch):+ """A fresh venv has no pip for the first part of the run. That answer is instant,+ so budgeting it would mean the operator's cert is lost for the rest of the run the+ moment pip does appear."""+ calls = []++ def missing_then_present(cmd, **kwargs):+ calls.append(1)+ if len(calls) < 8:+ raise OSError("no pip yet")+ return subprocess.CompletedProcess(cmd, 0, b"global.cert='/etc/corp/ca.pem'\n")++ monkeypatch.setattr(ips.subprocess, "run", missing_then_present)+ ips._PINNED_PIP_CONFIG_LISTING = None+ ips._PINNED_PIP_CONFIG_ATTEMPTS = 2+ for _ in range(7):+ assert ips._pinned_pip_config_overrides() == {}+ assert ips._pinned_pip_config_overrides() == {"PIP_CERT": "/etc/corp/ca.pem"}++ @pytest.mark.reads_real_pip_config+ def test_the_read_dictates_the_child_encoding(self, monkeypatch):+ """Sniffing cannot recover an undictated encoding, so the child is told one."""+ seen = {}++ def fake_run(cmd, **kwargs):+ seen.update(kwargs.get("env") or {})+ return subprocess.CompletedProcess(cmd, 0, b"")++ monkeypatch.setattr(ips.subprocess, "run", fake_run)+ ips._PINNED_PIP_CONFIG_LISTING = None+ ips._PINNED_PIP_CONFIG_ATTEMPTS = 2+ ips._pinned_pip_config_overrides()+ assert seen.get("PYTHONIOENCODING") == "utf-8"++ def test_trusted_host_takes_section_precedence_instead(self):+ """Not every list key accumulates. Asked of pip 26.2's own parser with [global]+ and [install] both set, `trusted_hosts` comes back as the install value alone (an+ append option, assigned per section) while `format_control` holds both (a callback+ that mutates in place). Accumulating trusted-host would re-trust a host the+ install section had dropped, and that is a TLS decision."""+ hosts = b"global.trusted-host='global-a.corp'\ninstall.trusted-host='install-b.corp'\n"+ assert (+ ips._parse_pinned_pip_config(hosts, "install")["PIP_TRUSTED_HOST"] == "install-b.corp"+ )+ # Multiple hosts WITHIN the winning section are still space separated.+ many = rb"install.trusted-host='a.corp\nb.corp'"+ assert ips._parse_pinned_pip_config(many, "install")["PIP_TRUSTED_HOST"] == "a.corp b.corp"++ def test_a_reset_entry_keeps_its_order(self):+ """pip applies a repeatable option IN ORDER and `:none:` empties the set, so a+ re-add after a reset has to survive. Measured on pip 26.2: [global] a,b with+ [install] :none:,a still refuses a's sdist, and so does this concatenation, while+ deduplicating dropped the re-add and left `:none:` last, which empties the set and+ allowed the very build the operator forbade."""+ listing = (+ b"global.only-binary='probe-sdist,probe-wheel'\n"+ b"install.only-binary=':none:,probe-sdist'\n"+ )+ assert ips._parse_pinned_pip_config(listing, "install")["PIP_ONLY_BINARY"] == (+ "probe-sdist,probe-wheel,:none:,probe-sdist"+ )++ @pytest.mark.parametrize(+ "listing, expected",+ [+ # One value, passed through: collapsing whitespace breaks a real path.+ (+ b"global.cert='C:\\Program Files\\ca.pem'",+ {"PIP_CERT": "C:\\Program Files\\ca.pem"},+ ),+ (+ b"global.proxy='http://user:[email protected]:3128'",+ {"PIP_PROXY": "http://user:[email protected]:3128"},+ ),+ # Measured: rendered on ONE line with an escaped \n, which literal_eval undoes.+ (rb"global.trusted-host='a.corp\nb.corp'", {"PIP_TRUSTED_HOST": "a.corp b.corp"}),+ # COMMA separated, not whitespace. Verified: PIP_ONLY_BINARY="a,b" refuses both.+ (rb"global.only-binary='numpy\nscipy'", {"PIP_ONLY_BINARY": "numpy,scipy"}),+ (b"global.only-binary=':all:'", {"PIP_ONLY_BINARY": ":all:"}),+ ],+ )+ def test_each_key_is_joined_the_way_pip_reads_it(self, listing, expected):+ assert ips._parse_pinned_pip_config(listing) == expected++ def test_the_callers_own_environment_wins(self):+ """The re-assertion fills gaps; it never overwrites a variable the caller set."""+ with (+ mock.patch.object(+ ips,+ "_pinned_pip_config_overrides",+ lambda *a, **k: {"PIP_CERT": "/etc/ssl/corp.pem"},+ ),+ mock.patch.dict(os.environ, {"PIP_CERT": "/home/me/mine.pem"}),+ ):+ env = ips._install_env_for_cmd(+ ["uv", "pip", "install", "torch", "--index-url", "https://x/cu128"]+ )+ assert env["PIP_CERT"] == "/home/me/mine.pem"++ @pytest.mark.parametrize(+ "configured, exported, expected",+ [+ ("numpy", "scipy", "numpy,scipy"),+ (":all:", ":none:", ":all:,:none:"),+ (":none:", ":all:", ":none:,:all:"),+ ],+ )+ def test_only_binary_from_the_environment_adds_to_the_file(+ self, configured, exported, expected+ ):+ """only-binary accumulates: measured on pip 26.2, the file's entries apply first and the+ environment's after them. Letting the variable replace the file lost the file's rule."""+ with (+ mock.patch.object(+ ips,+ "_pinned_pip_config_overrides",+ lambda *a, **k: {"PIP_ONLY_BINARY": configured},+ ),+ mock.patch.dict(os.environ, {"PIP_ONLY_BINARY": exported}),+ ):+ cmd, env = ips._pinned_cmd_and_env(+ ["uv", "pip", "install", "torch", "--index-url", "https://x/cu128"]+ )+ assert env["PIP_ONLY_BINARY"] == expected+ flags = [cmd[i + 1] for i, arg in enumerate(cmd) if arg == "--only-binary"]+ assert flags == expected.split(",")++ def test_no_uv_env_var_is_invented_for_a_uv_toml_no_build(self):+ """Measured on uv 0.10.7: UV_NO_BUILD / UV_NO_BINARY / UV_ONLY_BINARY are not uv+ environment variables, so nothing here may pretend to carry a uv.toml no-build+ onto a pinned command."""+ src = STACK_SOURCE+ assert (+ "_uv_config_build_policy" not in src+ ), "re-asserting UV_NO_BUILD would promise a guarantee uv does not honour"+ # A NON-pinned uv command inherits everything, which is where source builds happen.+ with mock.patch.dict(os.environ, {"UV_EXCLUDE_NEWER": "2024-01-01T00:00:00Z"}):+ assert ips._install_env_for_cmd(["uv", "pip", "install", "-r", "extras.txt"]) is None++ @pytest.mark.parametrize(+ "cmd",+ [+ ["python", "-m", "pip", "uninstall", "-y", "install"],+ ["python", "-m", "pip", "config", "list"],+ [sys.executable, "/opt/tools/install.py", "--download"],+ ["uv", "pip", "install", "-r", "extras.txt"],+ ],+ )+ def test_only_a_real_pip_install_is_relaxed(self, cmd):+ """Keyed on the pip SUBCOMMAND, not on the word appearing anywhere in argv."""+ with mock.patch.dict(os.environ, self.HOSTILE):+ assert ips._install_env_for_cmd(cmd) is Nonedef test_the_parent_environment_is_never_mutated(self):"""The relaxation is a child-env override. Leaking it into os.environ would@@ -528,6 +1094,37 @@ class TestHardenedPipConfigRelaxation:assert os.environ["PIP_REQUIRE_HASHES"] == "1"assert os.environ["UV_NO_BUILD"] == "1"+ @pytest.mark.parametrize(+ "cmd, relaxed",+ [+ # os.path.basename keeps a backslash off-Windows: the naive-stem traps.+ ([r"C:\Python313\python.exe", "-m", "pip", "install", "x"], True),+ ([r"C:\venv\Scripts\pip.exe", "install", "x"], True),+ ([r"C:\venv\Scripts\pip3.13.exe", "download", "x"], True),+ ([r"C:\Program Files\venv\Scripts\python.exe", "-m", "pip", "wheel", "x"], True),+ (["/venv/bin/pip", "install", "x"], True),+ (["/venv/bin/pip3", "install", "x"], True),+ (["python", "-m", "pip", "-q", "install", "x"], True),+ (["python", "-m", "pip", "--isolated", "install", "x"], True),+ # ...and the ones that only LOOK like an install.+ (["python", "-m", "pip", "uninstall", "-y", "install"], False),+ (["python", "-m", "pip", "show", "wheel"], False),+ (["python", "-m", "pip", "check", "install.txt"], False),+ (["python", "/opt/tools/install.py"], False),+ ([r"C:\tools\installer.exe", "--download"], False),+ ],+ )+ def test_the_subcommand_test_reads_every_platform_spelling(self, cmd, relaxed):+ assert ips._is_pip_subcommand(cmd, ("install", "download", "wheel")) is relaxed++ @pytest.mark.parametrize(+ "cmd",+ [[], [""], ["uv"], ["python"], ["python", "-m"], ["python", "-m", "pip"]],+ )+ def test_a_degenerate_command_never_raises(self, cmd):+ """run() routes EVERY command through this, so a non-command must not IndexError."""+ assert ips._install_env_for_cmd(cmd) is None+def test_the_pip_fallback_receives_the_relaxation(self):"""End of the real path: uv fails, pip_install falls back through run(), andthat pip command is the one #8530 died on."""@@ -2449,6 +3046,7 @@ class TestRecordlessDistributionRecovery:*,quiet = True,check = True,+ env = None,):attempts.append(cmd)failed = len(attempts) == 1@@ -2475,6 +3073,7 @@ class TestRecordlessDistributionRecovery:*,quiet = True,check = True,+ env = None,):attempts.append(cmd)return types.SimpleNamespace(returncode = 1, stdout = b"ERROR: no matching distribution")diff --git a/tests/studio/test_xpu_triton_swap.py b/tests/studio/test_xpu_triton_swap.pyindex 51af2a51d36..2b4f242a9c6 100644--- a/tests/studio/test_xpu_triton_swap.py+++ b/tests/studio/test_xpu_triton_swap.py@@ -40,23 +40,62 @@ def _load_real_index_env_scrub():It is defined below the slice the swap comes from, so it is pulled in separately ratherthan stubbed -- a hand-written copy here would agree with a broken original forever."""+ import ast as _ast+ import atexit as _atexit+ import functools as _functools+ import locale as _localeimport os as _os+ import shutil as _shutil+ import subprocess as _subprocess+ import sys as _sys+ import tempfile as _tempfilesrc = STACK.read_text(encoding = "utf-8")- ns: dict = {"os": _os}+ ns: dict = {+ "os": _os,+ "ast": _ast,+ "atexit": _atexit,+ "functools": _functools,+ "shutil": _shutil,+ "subprocess": _subprocess,+ "locale": _locale,+ "sys": _sys,+ "tempfile": _tempfile,+ # The one dependency of the extracted code that is not a module.+ "_windows_hidden_subprocess_kwargs": dict,+ }for anchor, end, keep in (("_UV_INDEX_ENV_VARS = (", "\n)\n", 2),- # _install_env_for_cmd calls both of these, and they resolve from this namespace at CALL time, so omitting- # either only shows up as a NameError once a test actually invokes the scrub.- ("_PM_POLICY_ENV_VARS = (", "\n)\n", 2),+ # Resolved from this namespace at CALL time, so an omission is a NameError later.+ ("_PM_HASH_ENV_VARS = (", "\n)\n", 2),+ ("_PM_FORCE_SOURCE_ENV_VARS = (", "\n)\n", 2),+ # One line, so it ends at the first newline; "\n)\n" would swallow the file.+ ("_PINNED_PIP_CONFIG_KEEP_KEYS = (", "\n)\n", 2),+ ("_PINNED_PIP_CONFIG_GLOBAL_SECTION = ", "\n", 1),+ ("_PINNED_PIP_CONFIG_DEFAULT_SECTION = ", "\n", 1),+ ("_PINNED_PIP_CONFIG_SEPARATORS = ", "\n", 1),+ ("_PINNED_PIP_CONFIG_ACCUMULATING = ", "\n", 1),+ ("_PINNED_PIP_CONFIG_LISTING: ", "\n", 1),+ ("_PINNED_PIP_CONFIG_TIMEOUT = ", "\n", 1),+ ("_PINNED_PIP_CONFIG_ATTEMPTS = ", "\n", 1),+ ("def _pip_subcommand_of(", "\n\ndef ", 0),+ ("def _decode_pip_output(", "\n\ndef ", 0),+ ("def _pinned_pip_config_overrides(", "\n\ndef ", 0),+ # Omitting it made the exec'd scrub raise into a broad except and agree with+ # anything.+ ("def _parse_pinned_pip_config(", "\n\ndef ", 0),("def _relaxed_pip_policy_env(", "\n\ndef ", 0),+ ("def _is_pip_subcommand(", "\n\ndef ", 0),("def _is_pinned_index_cmd(", "\n\ndef ", 0),("def _install_env_for_cmd(", "\n\ndef ", 0),):start = src.index(anchor)exec(compile(src[start : src.index(end, start) + keep], str(STACK), "exec"), ns)assert "PIP_NO_INDEX" in ns["_UV_INDEX_ENV_VARS"], "extraction lost the pip vars"- assert "PIP_REQUIRE_HASHES" in ns["_PM_POLICY_ENV_VARS"], "extraction lost the policy vars"+ assert "PIP_REQUIRE_HASHES" in ns["_PM_HASH_ENV_VARS"], "extraction lost the hash vars"+ # Execute it once: a missing dependency here is otherwise an inert scrub that passes.+ parsed = ns["_parse_pinned_pip_config"](b"global.cert='/etc/corp/ca.pem'\n")+ assert parsed == {"PIP_CERT": "/etc/corp/ca.pem"}, f"extraction is inert: {parsed}"return ns["_install_env_for_cmd"]@@ -412,13 +451,27 @@ class TestTheFetchIgnoresTheUsersIndexEnvironment:assert var not in envdef test_the_fetch_neutralises_the_pip_config_file(self, monkeypatch, tmp_path):- # A pip.conf index-url outranks nothing on the CLI, but no-index in it does.+ # A config no-index outranks the CLI pin, and devnull is the only spelling that+ # reaches a SITE or GLOBAL file.mod, _ = _load(monkeypatch, tmp_path, spec = "pytorch-triton-xpu==3.5.0", generic = "3.7.1")mod.__dict__["_ensure_xpu_triton"]()env = mod.__dict__["_test_download_envs"][0]assert env["PIP_CONFIG_FILE"] == os.devnullassert env["UV_NO_CONFIG"] == "1"+ def test_the_fetch_keeps_the_operators_build_policy(self, monkeypatch, tmp_path):+ # The pin is a wheel, so only-binary costs it nothing; this is `pip download`.+ monkeypatch.setenv("PIP_ONLY_BINARY", ":all:")+ monkeypatch.setenv("PIP_REQUIRE_HASHES", "1")+ monkeypatch.setenv("UV_EXCLUDE_NEWER", "2024-01-01T00:00:00Z")+ mod, _ = _load(monkeypatch, tmp_path, spec = "pytorch-triton-xpu==3.5.0", generic = "3.7.1")+ mod.__dict__["_ensure_xpu_triton"]()+ env = mod.__dict__["_test_download_envs"][0]+ assert env["PIP_ONLY_BINARY"] == ":all:"+ assert "PIP_REQUIRE_HASHES" not in env+ # The pip leg cannot express an upload cutoff.+ assert "UV_EXCLUDE_NEWER" not in env+def test_unrelated_environment_survives(self, monkeypatch, tmp_path):# Scrub the index vars, not the environment: HTTPS_PROXY and friends are how a corporate host reaches the index# at all.