Skip to content

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.

Ask Python for the process image table when the native helper is unavailable (#11115)

Daniel Han7e107f886cbc
diff --git a/.github/workflows/cross-platform-parity-ci.yml b/.github/workflows/cross-platform-parity-ci.yml
index dd130709148..8166d90fc42 100644
--- a/.github/workflows/cross-platform-parity-ci.yml
+++ b/.github/workflows/cross-platform-parity-ci.yml
@@ -107,6 +107,7 @@ on:
- 'tests/studio/test_nvidia_smi_discovery.ps1'
- 'tests/studio/test_install_lock_excludes_aliases.ps1'
- 'tests/studio/test_early_python_path_resolver.ps1'
+ - 'tests/studio/test_early_python_process_image.ps1'
- 'tests/studio/test_application_control_cli_fallback.ps1'
# test_application_control_cli_fallback.ps1 pins install.ps1's trampoline against
# process.rs's, so a change to either spelling has to run here.
@@ -220,6 +221,7 @@ on:
- 'tests/studio/test_nvidia_smi_discovery.ps1'
- 'tests/studio/test_install_lock_excludes_aliases.ps1'
- 'tests/studio/test_early_python_path_resolver.ps1'
+ - 'tests/studio/test_early_python_process_image.ps1'
- 'tests/studio/test_application_control_cli_fallback.ps1'
# test_application_control_cli_fallback.ps1 pins install.ps1's trampoline against
# process.rs's, so a change to either spelling has to run here.
@@ -400,6 +402,14 @@ jobs:
if: runner.os == 'Windows'
shell: powershell
run: powershell -NoProfile -File tests\studio\test_early_python_path_resolver.ps1
+ # 5.1 too: the runner builds the command line by hand there (no ArgumentList).
+ - name: Early Python process image tests
+ shell: pwsh
+ run: pwsh -NoProfile -File tests/studio/test_early_python_process_image.ps1
+ - name: Early Python process image tests (Windows PowerShell 5.1)
+ if: runner.os == 'Windows'
+ shell: powershell
+ run: powershell -NoProfile -File tests\studio\test_early_python_process_image.ps1
- name: Install lock alias-exclusion tests (Windows PowerShell 5.1)
if: runner.os == 'Windows'
shell: powershell
diff --git a/install.ps1 b/install.ps1
index 38ded796bd2..80b5fc12993 100644
--- a/install.ps1
+++ b/install.ps1
@@ -2839,13 +2839,34 @@ exit 1
$script = "import pathlib,sys" + [char]10 +
"sys.exit(2) if sys.version_info < (3,8) else None" + [char]10 +
"sys.stdout.buffer.write(str(pathlib.Path(sys.argv[1]).resolve(strict=True)).encode('utf-8'))"
+ # Any error, incl. from Test-Path, returns $null (lexical rung).
+ try {
+ # Verbatim: Trim() would drop a trailing U+00A0, which NTFS names keep.
+ $answer = "$(Invoke-StudioEarlyPythonScript -Exe $Exe -Script $script -ScriptArgs @($Path) -TimeoutMs $TimeoutMs)"
+ if ([string]::IsNullOrWhiteSpace($answer)) { return $null }
+ if (-not [System.IO.Path]::IsPathRooted($answer)) { return $null }
+ if (-not (Test-Path -LiteralPath $answer)) { return $null }
+ return $answer
+ } catch {
+ return $null
+ }
+ }
+
+ function Invoke-StudioEarlyPythonScript {
+ param(
+ [Parameter(Mandatory = $true)][string]$Exe,
+ [Parameter(Mandatory = $true)][string]$Script,
+ [string[]]$ScriptArgs = @(),
+ [int]$TimeoutMs = 10000
+ )
$proc = $null
$clock = [System.Diagnostics.Stopwatch]::StartNew()
try {
$psi = New-Object System.Diagnostics.ProcessStartInfo
$psi.FileName = $Exe
# -S as well as -I: -I still imports site, so a sitecustomize could print or hang.
- $argv = @("-I", "-S", "-c", $script, $Path)
+ # ArgumentList is .NET Core only; Windows PowerShell 5.1 lacks it.
+ $argv = @("-I", "-S", "-c", $Script) + $ScriptArgs
if ($null -ne $psi.PSObject.Properties["ArgumentList"]) {
foreach ($a in $argv) { $null = $psi.ArgumentList.Add($a) }
} else {
@@ -2871,12 +2892,7 @@ exit 1
# A leftover child can hold stdout open, so the deadline bounds the read too.
$left = [Math]::Max(0, $TimeoutMs - [int]$clock.ElapsedMilliseconds)
if (-not $stdout.Wait($left)) { return $null }
- # Verbatim: Trim() would drop a trailing U+00A0, which NTFS names keep.
- $answer = "$($stdout.Result)"
- if ([string]::IsNullOrWhiteSpace($answer)) { return $null }
- if (-not [System.IO.Path]::IsPathRooted($answer)) { return $null }
- if (-not (Test-Path -LiteralPath $answer)) { return $null }
- return $answer
+ return "$($stdout.Result)"
} catch {
return $null
} finally {
@@ -6347,6 +6363,57 @@ exit 0
$script:StudioProcessImageTable = $null
$script:StudioProcessImageWarned = $false
+ # PID -> image path via ctypes in a child, so no type is defined in this script.
+ $script:StudioPythonProcessImageTable = $null
+ $script:StudioPythonProcessImageProbed = $false
+
+ function Get-StudioPythonProcessImageTable {
+ $exe = Get-StudioEarlyPython
+ if (-not $exe) { return $null }
+ if (-not ($env:OS -eq "Windows_NT")) { return $null }
+ $probe = "import ctypes,sys" + [char]10 +
+ "from ctypes import wintypes" + [char]10 +
+ "k32=ctypes.WinDLL('kernel32',use_last_error=True)" + [char]10 +
+ "psapi=ctypes.WinDLL('psapi',use_last_error=True)" + [char]10 +
+ "k32.OpenProcess.restype=wintypes.HANDLE" + [char]10 +
+ "k32.OpenProcess.argtypes=[wintypes.DWORD,wintypes.BOOL,wintypes.DWORD]" + [char]10 +
+ "k32.CloseHandle.argtypes=[wintypes.HANDLE]" + [char]10 +
+ "k32.QueryFullProcessImageNameW.argtypes=[wintypes.HANDLE,wintypes.DWORD,wintypes.LPWSTR,ctypes.POINTER(wintypes.DWORD)]" + [char]10 +
+ "n=1024" + [char]10 +
+ "while True:" + [char]10 +
+ " a=(wintypes.DWORD*n)();b=wintypes.DWORD()" + [char]10 +
+ " if not psapi.EnumProcesses(ctypes.byref(a),ctypes.sizeof(a),ctypes.byref(b)): sys.exit(3)" + [char]10 +
+ " if b.value < ctypes.sizeof(a): break" + [char]10 +
+ " n*=2" + [char]10 +
+ "out=[]" + [char]10 +
+ "for pid in a[:b.value//ctypes.sizeof(wintypes.DWORD)]:" + [char]10 +
+ " if not pid: continue" + [char]10 +
+ " h=k32.OpenProcess(0x1000,False,pid)" + [char]10 +
+ " if not h: continue" + [char]10 +
+ " try:" + [char]10 +
+ " buf=ctypes.create_unicode_buffer(32768);sz=wintypes.DWORD(32768)" + [char]10 +
+ " if k32.QueryFullProcessImageNameW(h,0,buf,ctypes.byref(sz)): out.append(str(pid)+'|'+buf.value)" + [char]10 +
+ " finally:" + [char]10 +
+ " k32.CloseHandle(h)" + [char]10 +
+ "sys.stdout.buffer.write('\n'.join(out).encode('utf-8'))"
+ $raw = Invoke-StudioEarlyPythonScript -Exe $exe -Script $probe -TimeoutMs 20000
+ if ([string]::IsNullOrWhiteSpace($raw)) { return $null }
+ $table = @{}
+ foreach ($line in ($raw -split "`r?`n")) {
+ if ([string]::IsNullOrWhiteSpace($line)) { continue }
+ $split = $line.IndexOf('|')
+ if ($split -lt 1) { continue }
+ $pidText = $line.Substring(0, $split)
+ $path = $line.Substring($split + 1)
+ $parsed = 0
+ if (-not [int]::TryParse($pidText, [ref]$parsed)) { continue }
+ if ($parsed -le 0) { continue }
+ if (-not [string]::IsNullOrWhiteSpace($path)) { $table[$parsed] = $path }
+ }
+ if ($table.Count -eq 0) { return $null }
+ return $table
+ }
+
function Get-StudioProcessImagePath {
param([Parameter(Mandatory = $true)][int]$ProcessId)
if (Initialize-StudioProcessImageNativeType) {
@@ -6367,6 +6434,19 @@ exit 0
if (-not [string]::IsNullOrWhiteSpace($process.Path)) { return $process.Path }
} catch {}
}
+ # PROCESS_QUERY_LIMITED_INFORMATION works where PROCESS_VM_READ does not; one child per run.
+ if (-not $script:StudioPythonProcessImageProbed) {
+ $script:StudioPythonProcessImageProbed = $true
+ # A failure falls through to the WMI rung, never past it.
+ try { $script:StudioPythonProcessImageTable = Get-StudioPythonProcessImageTable } catch {
+ $script:StudioPythonProcessImageTable = $null
+ }
+ }
+ if ($script:StudioPythonProcessImageTable -and
+ $script:StudioPythonProcessImageTable.ContainsKey($ProcessId)) {
+ return $script:StudioPythonProcessImageTable[$ProcessId]
+ }
+ # Per-run PID snapshots: a reused PID reads stale; accepted.
# Queried once per run, not once per process: this is the slow rung.
if ($null -eq $script:StudioProcessImageTable) {
$script:StudioProcessImageTable = @{}
diff --git a/tests/python/test_windows_installer_concurrency_guard.py b/tests/python/test_windows_installer_concurrency_guard.py
index d43580ce3ef..575e75e1be5 100644
--- a/tests/python/test_windows_installer_concurrency_guard.py
+++ b/tests/python/test_windows_installer_concurrency_guard.py
@@ -98,6 +98,7 @@ _FINAL_PATH_CHAIN = (
"Get-StudioSubstTarget",
"Get-ElevationState",
"Get-StudioEarlyPython",
+ "Invoke-StudioEarlyPythonScript",
"Test-StudioPlainFile",
"Test-UnslothCmdShimFile",
"Invoke-StudioEarlyPython",
@@ -139,6 +140,7 @@ def _mutex_helpers(source: str) -> str:
"Get-StudioSubstTarget",
"Get-ElevationState",
"Get-StudioEarlyPython",
+ "Invoke-StudioEarlyPythonScript",
"Test-StudioPlainFile",
"Test-UnslothCmdShimFile",
"Invoke-StudioEarlyPython",
@@ -182,6 +184,7 @@ def _process_helpers(source: str) -> str:
"Get-StudioSubstTarget",
"Get-ElevationState",
"Get-StudioEarlyPython",
+ "Invoke-StudioEarlyPythonScript",
"Test-StudioPlainFile",
"Test-UnslothCmdShimFile",
"Invoke-StudioEarlyPython",
@@ -191,6 +194,7 @@ def _process_helpers(source: str) -> str:
"Get-StudioFinalPath",
"Test-StudioProtectedPathMatch",
"Initialize-StudioProcessImageNativeType",
+ "Get-StudioPythonProcessImageTable",
"Get-StudioNativeProcessImagePath",
"Get-StudioProcessImagePath",
"Get-RunningStudioVenvProcesses",
diff --git a/tests/python/test_windows_installer_native_type_fallback.py b/tests/python/test_windows_installer_native_type_fallback.py
index a99311f391a..7fb5f0ff782 100644
--- a/tests/python/test_windows_installer_native_type_fallback.py
+++ b/tests/python/test_windows_installer_native_type_fallback.py
@@ -110,6 +110,7 @@ LOCK_CHAIN = (
"Get-StudioSubstTarget",
"Get-ElevationState",
"Get-StudioEarlyPython",
+ "Invoke-StudioEarlyPythonScript",
"Test-StudioPlainFile",
"Test-UnslothCmdShimFile",
"Invoke-StudioEarlyPython",
diff --git a/tests/studio/test_early_python_path_resolver.ps1 b/tests/studio/test_early_python_path_resolver.ps1
index d8bd302f287..f2288c2833b 100644
--- a/tests/studio/test_early_python_path_resolver.ps1
+++ b/tests/studio/test_early_python_path_resolver.ps1
@@ -18,7 +18,8 @@ $tokens = $null; $errors = $null
$ast = [System.Management.Automation.Language.Parser]::ParseFile($installPs1, [ref]$tokens, [ref]$errors)
if ($errors) { $errors | ForEach-Object { $_.ToString() }; throw "install.ps1 has parse errors" }
foreach ($name in @(
- "Get-ElevationState", "Get-StudioEarlyPython", "Invoke-StudioEarlyPython", "Get-StudioPythonFinalPath",
+ "Get-ElevationState", "Get-StudioEarlyPython", "Invoke-StudioEarlyPythonScript", "Invoke-StudioEarlyPython",
+ "Get-StudioPythonFinalPath",
"Resolve-StudioLinkTarget", "Get-StudioSubstTarget", "Get-StudioLexicalPath",
"Resolve-StudioFinalPathInfo", "Test-StudioPlainFile", "Test-UnslothCmdShimFile"
)) {
@@ -271,12 +272,12 @@ try {
Check "-S is what turns site off" ("$isoPlus".Trim() -eq "1")
# Read from the source: a planted sitecustomize is shadowed by the host's own.
- $resolverFn = @($ast.FindAll({ param($n)
+ $launcherFn = @($ast.FindAll({ param($n)
$n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and
- $n.Name -eq "Invoke-StudioEarlyPython"
+ $n.Name -eq "Invoke-StudioEarlyPythonScript"
}, $true))[0].Extent.Text
- Check "the resolver runs the probe with -S as well as -I" (
- $resolverFn -match '@\("-I",\s*"-S",\s*"-c"')
+ Check "the launcher runs every probe with -S as well as -I" (
+ $launcherFn -match '@\("-I",\s*"-S",\s*"-c"')
# A miss recorded before $VenvDir existed (the --tauri path) is probed again once it does.
$venvHome = Join-Path $tmp "venvhome"
$venvBin = if ($IsWindows -or $env:OS -eq "Windows_NT") { "Scripts" } else { "bin" }
diff --git a/tests/studio/test_early_python_process_image.ps1 b/tests/studio/test_early_python_process_image.ps1
new file mode 100644
index 00000000000..388563b928c
--- /dev/null
+++ b/tests/studio/test_early_python_process_image.ps1
@@ -0,0 +1,192 @@
+#!/usr/bin/env pwsh
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+# ctypes process-image rung between Get-Process and WMI; the table uses a stubbed runner.
+# Run: pwsh -NoProfile -File tests/studio/test_early_python_process_image.ps1
+
+$ErrorActionPreference = "Stop"
+$root = (Resolve-Path ([System.IO.Path]::Combine($PSScriptRoot, "..", ".."))).Path
+$installPs1 = Join-Path $root "install.ps1"
+
+$failures = 0
+function Check($name, $cond) {
+ if ($cond) { Write-Host " PASS $name" }
+ else { Write-Host " FAIL $name" -ForegroundColor Red; $script:failures++ }
+}
+
+$tokens = $null; $errors = $null
+$ast = [System.Management.Automation.Language.Parser]::ParseFile($installPs1, [ref]$tokens, [ref]$errors)
+if ($errors) { $errors | ForEach-Object { $_.ToString() }; throw "install.ps1 has parse errors" }
+foreach ($name in @(
+ "Invoke-StudioEarlyPythonScript", "Invoke-StudioEarlyPython", "Get-StudioEarlyPython",
+ "Get-StudioPythonProcessImageTable", "Get-StudioProcessImagePath"
+)) {
+ $fn = $ast.FindAll({ param($n)
+ $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq $name
+ }, $true)
+ if ($fn.Count -lt 1) { throw "expected $name in install.ps1, found none" }
+ Invoke-Expression $fn[0].Extent.Text
+}
+# A standard user: an elevated run declines this probe, and hosted Windows runners are elevated.
+function Get-ElevationState { return "false" }
+
+function Initialize-StudioProcessImageNativeType { return $false }
+function Get-StudioNativeProcessImagePath { param([int]$ProcessId) return $null }
+function Get-Process { param($Id, $ErrorAction) throw "no such process" }
+function Get-CimInstance { param($ClassName, $ErrorAction) throw "WMI is unavailable" }
+function Write-StudioLine { param([string]$Line, [string]$ForegroundColor = "") }
+
+function Reset-RungState {
+ $script:StudioPythonProcessImageTable = $null
+ $script:StudioPythonProcessImageProbed = $false
+ $script:StudioProcessImageTable = $null
+ $script:StudioProcessImageWarned = $true
+}
+
+# ---------------------------------------------------------------- the generic runner, for real
+
+$script:StudioEarlyPythonProbed = $false
+$script:StudioEarlyPython = $null
+$exe = Get-StudioEarlyPython
+if (-not $exe) {
+ Write-Host " SKIP no Python on this host, which is the fallback case and not a failure" -ForegroundColor Yellow
+ exit 0
+}
+Write-Host " interpreter: $exe"
+
+$out = Invoke-StudioEarlyPythonScript -Exe $exe -Script "import sys;sys.stdout.write('ok')"
+Check "the runner returns the child's stdout" ($out -eq "ok")
+
+# The 5.1 branch quotes by hand: a space, and a trailing backslash that could escape its quote.
+$probeArgs = @("a b", "c\")
+$echo = Invoke-StudioEarlyPythonScript -Exe $exe `
+ -Script "import sys;sys.stdout.write('|'.join(sys.argv[1:]))" -ScriptArgs $probeArgs
+Check "arguments survive quoting, including a space and a trailing backslash" ($echo -eq "a b|c\")
+
+$nonZero = Invoke-StudioEarlyPythonScript -Exe $exe -Script "import sys;sys.stdout.write('x');sys.exit(4)"
+Check "a non-zero exit is refused even though the child printed" ($null -eq $nonZero)
+
+$zero = Invoke-StudioEarlyPythonScript -Exe $exe -Script "import sys;sys.stdout.write('x')"
+Check "control: the identical script exiting 0 does answer" ($zero -eq "x")
+
+$started = [System.Diagnostics.Stopwatch]::StartNew()
+$hung = Invoke-StudioEarlyPythonScript -Exe $exe -Script "import time;time.sleep(90)" -TimeoutMs 2000
+$started.Stop()
+Check "a hung child is killed, not waited on" ($null -eq $hung -and $started.Elapsed.TotalSeconds -lt 30)
+
+$utf8 = Invoke-StudioEarlyPythonScript -Exe $exe `
+ -Script "import sys;sys.stdout.buffer.write('\u00e9\u4e2d'.encode('utf-8'))"
+Check "non-ASCII output survives the host's console codepage" ($utf8 -eq ([char]0xE9 + [string][char]0x4E2D))
+
+$missing = Invoke-StudioEarlyPythonScript -Exe (Join-Path $root "no-such-interpreter") -Script "pass"
+Check "an interpreter that does not exist yields null, not a throw" ($null -eq $missing)
+
+# ------------------------------------------------------- the table, through a stubbed runner
+
+$script:RunnerCalls = 0
+$script:RunnerOutput = "10|C:\a\python.exe`n20|C:\b\python.exe"
+function Invoke-StudioEarlyPythonScript {
+ param([string]$Exe, [string]$Script, [string[]]$ScriptArgs = @(), [int]$TimeoutMs = 10000)
+ $script:RunnerCalls++
+ return $script:RunnerOutput
+}
+
+$savedOs = $env:OS
+try {
+ $env:OS = "Windows_NT"
+
+ Reset-RungState
+ $table = Get-StudioPythonProcessImageTable
+ Check "pid|path lines become a table" (
+ $table -and $table.Count -eq 2 -and $table[10] -eq "C:\a\python.exe" -and $table[20] -eq "C:\b\python.exe")
+
+ Reset-RungState
+ $script:RunnerOutput = "30|C:\c\python.exe`r`n`r`nnot-a-row`r`n|no-pid`r`n40|"
+ $table = Get-StudioPythonProcessImageTable
+ Check "blank, pid-less, path-less and malformed rows are dropped, the good one kept" (
+ $table -and $table.Count -eq 1 -and $table[30] -eq "C:\c\python.exe")
+
+ Reset-RungState
+ $script:RunnerOutput = ""
+ Check "a child that answers nothing yields null, so the WMI rung is still reached" (
+ $null -eq (Get-StudioPythonProcessImageTable))
+
+ # A $null table must not re-probe: "not asked" and "asked, got nothing" need the probed flag.
+ Reset-RungState
+ $script:RunnerCalls = 0
+ $script:RunnerOutput = ""
+ $null = Get-StudioProcessImagePath -ProcessId 1
+ $null = Get-StudioProcessImagePath -ProcessId 2
+ $null = Get-StudioProcessImagePath -ProcessId 3
+ Check "a declining probe runs once per installer run, not once per process" ($script:RunnerCalls -eq 1)
+
+ Reset-RungState
+ $script:RunnerCalls = 0
+ $script:RunnerOutput = "77|C:\d\python.exe"
+ $first = Get-StudioProcessImagePath -ProcessId 77
+ $second = Get-StudioProcessImagePath -ProcessId 77
+ Check "the ladder returns the Python answer when the rungs above decline" (
+ $first -eq "C:\d\python.exe" -and $second -eq "C:\d\python.exe")
+ Check "an answering probe is also cached" ($script:RunnerCalls -eq 1)
+
+ Reset-RungState
+ $script:RunnerOutput = "77|C:\d\python.exe"
+ Check "control: an unknown pid falls through to the rung below" (
+ $null -eq (Get-StudioProcessImagePath -ProcessId 999))
+
+ # Interpreter discovery can throw; the WMI rung below this one must still get its turn.
+ Reset-RungState
+ $savedTable = ${function:Get-StudioPythonProcessImageTable}
+ function Get-StudioPythonProcessImageTable { throw "interpreter discovery failed" }
+ function Get-CimInstance { param($ClassName, $ErrorAction)
+ return @([pscustomobject]@{ ProcessId = 55; ExecutablePath = "C:\w\python.exe" }) }
+ $threw = $null; $viaWmi = $null
+ try { $viaWmi = Get-StudioProcessImagePath -ProcessId 55 } catch { $threw = $_ }
+ Check "a table probe that throws still reaches the WMI rung" (
+ $null -eq $threw -and $viaWmi -eq "C:\w\python.exe")
+ function Get-CimInstance { param($ClassName, $ErrorAction) throw "WMI is unavailable" }
+ ${function:Get-StudioPythonProcessImageTable} = $savedTable
+
+ # Validating the answer is part of the null-on-error contract: an access error declines.
+ $script:RunnerOutput = [System.IO.Path]::GetTempPath()
+ function Test-Path { param($LiteralPath, $PathType, $ErrorAction)
+ throw [System.UnauthorizedAccessException]::new("Access to the path is denied.") }
+ $threw = $null; $answer = "unset"
+ try { $answer = Invoke-StudioEarlyPython -Exe "python3" -Path $root } catch { $threw = $_ }
+ Remove-Item Function:Test-Path -ErrorAction SilentlyContinue
+ Check "an access error validating the answer declines instead of throwing" (
+ $null -eq $threw -and $null -eq $answer)
+
+ $env:OS = "Linux"
+ Reset-RungState
+ $script:RunnerCalls = 0
+ Check "off Windows the rung declines: QueryFullProcessImageNameW does not exist there" (
+ $null -eq (Get-StudioPythonProcessImageTable) -and $script:RunnerCalls -eq 0)
+} finally {
+ if ($null -eq $savedOs) { Remove-Item Env:OS -ErrorAction SilentlyContinue } else { $env:OS = $savedOs }
+}
+
+# ------------------------------------------------------------------------ the embedded probe
+
+$probeFn = $ast.FindAll({ param($n)
+ $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and
+ $n.Name -eq "Get-StudioPythonProcessImageTable"
+}, $true)[0]
+$probeText = $probeFn.Extent.Text
+
+# ctypes defaults restype to int, which truncates a HANDLE and closes the wrong one.
+Check "the probe declares OpenProcess's return type" (
+ $probeText -match "OpenProcess\.restype\s*=\s*wintypes\.HANDLE")
+Check "the probe declares CloseHandle's argument type" (
+ $probeText -match "CloseHandle\.argtypes")
+# 0x400 is refused by protected and cross-session processes; 0x1000 is not.
+Check "the probe asks for the limited-information right only" ($probeText -match "OpenProcess\(0x1000,")
+# EnumProcesses filling the buffer exactly may mean truncation; only a smaller count is complete.
+Check "the probe grows its buffer until the enumeration is provably complete" (
+ $probeText -match "b\.value\s*<\s*ctypes\.sizeof\(a\)")
+
+if ($failures -gt 0) {
+ Write-Host "$failures check(s) failed" -ForegroundColor Red
+ exit 1
+}
+Write-Host "all checks passed" -ForegroundColor Green
diff --git a/tests/studio/test_native_path_resolver_degrades.ps1 b/tests/studio/test_native_path_resolver_degrades.ps1
index df90e9f0719..984c9bd2f03 100644
--- a/tests/studio/test_native_path_resolver_degrades.ps1
+++ b/tests/studio/test_native_path_resolver_degrades.ps1
@@ -100,10 +100,13 @@ $fns = @(
"Test-StudioCanDefineNativeTypes", "Test-StudioEmitInChildProcess", "New-StudioDynamicAssembly",
"New-StudioEmittedNativeType",
"Initialize-StudioFinalPathNativeType", "Get-StudioNativeFinalPath",
- "Get-ElevationState", "Get-StudioEarlyPython", "Invoke-StudioEarlyPython", "Get-StudioPythonFinalPath",
+ "Get-ElevationState", "Get-StudioEarlyPython", "Invoke-StudioEarlyPythonScript", "Invoke-StudioEarlyPython",
+ "Get-StudioPythonFinalPath",
"Resolve-StudioLinkTarget", "Get-StudioSubstTarget", "Get-StudioLexicalPath",
"Resolve-StudioFinalPathInfo",
"Initialize-StudioProcessImageNativeType", "Get-StudioNativeProcessImagePath",
+ # Reached only by the below-Get-Process scenario at the end of the harness.
+ "Get-StudioPythonProcessImageTable",
"Get-StudioProcessImagePath"
)
$src = @()
@@ -232,6 +235,15 @@ try { `$fallback = Get-StudioProcessImagePath -ProcessId `$PID } catch { `$fallb
Write-Host "PROCIMG_FALLBACK_THREW: `$fallbackThrew"
Write-Host "PROCIMG_FALLBACK_ANSWERED: `$(-not [string]::IsNullOrWhiteSpace(`$fallback))"
Write-Host "PROCIMG_WARNED: `$script:StudioProcessImageWarned"
+
+# The rungs below Get-Process, which the check above never reaches because this shell can read
+# its own .Path. A Get-Process with no answer stands in for an uninspectable process.
+function Get-Process { param(`$Id, `$ErrorAction) return `$null }
+`$script:StudioPythonProcessImageProbed = `$false
+`$script:StudioPythonProcessImageTable = `$null
+`$deepThrew = `$false
+try { `$null = Get-StudioProcessImagePath -ProcessId `$PID } catch { `$deepThrew = `$true; Write-Host "DEEP_ERROR: `$(`$_.Exception.Message)" }
+Write-Host "PROCIMG_DEEP_THREW: `$deepThrew"
"@
$file = Join-Path ([System.IO.Path]::GetTempPath()) ("uns_native_" + [guid]::NewGuid().ToString("N") + ".ps1")
Set-Content -LiteralPath $file -Value $harness -Encoding utf8
@@ -273,6 +285,8 @@ Write-Host "PROCIMG_WARNED: `$script:StudioProcessImageWarned"
# And with the type forced away, the rungs below it still answer, which is what
# keeps a host from finding no running processes and overwriting an open venv.
Check "the fallback rung does not throw" ($out -match "PROCIMG_FALLBACK_THREW: False")
+ # Catches a helper missing from the extraction list.
+ Check "the rungs below Get-Process do not throw either" ($out -match "PROCIMG_DEEP_THREW: False")
Check "the fallback rung still names our own image" ($out -match "PROCIMG_FALLBACK_ANSWERED: True")
Check "and says out loud that it is degraded" ($out -match "PROCIMG_WARNED: True")
}