Skip to content

Commit 860fc8c

Browse files
authored
Merge pull request #352 from PhysShell/claude/p022-step7-tool-encoding
fix(step7): decode a tool's bytes by the code page that wrote them
2 parents 6194d7e + b8fdddf commit 860fc8c

3 files changed

Lines changed: 97 additions & 2 deletions

File tree

‎docs/notes/p022-263a-step7-environment-capture.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,16 @@ envcapture-reason-fits-platform an 'unavailable' reason names THIS platform
151151
envcapture-windows-fixture the schema holds off Linux
152152
envcapture-frozen-untouched this addition moved none of the three frozen digests
153153
envcapture-ci-provenance a CI-taken manifest says so and cannot hide it
154+
envcapture-tool-encoding a tool's bytes decode by the code page that wrote them
154155
```
155156

157+
`envcapture-tool-encoding` postdates the mutation campaign below and was not
158+
scored by it. It was added with the locale-decoding fix: `text=True` decoded a
159+
console tool's output with the ANSI code page while the tool wrote in the
160+
console's, so `power_policy` — identity-bearing — arrived as mojibake that moved
161+
with the ambient code page, which is drift the capture invented rather than
162+
observed.
163+
156164
Fifteen mutations, each declaring in advance which control must catch it, and
157165
each scored on a `FAIL` line from **that** control rather than on a non-zero
158166
exit. Two of the fifteen mutate the **control file**, not the tool: the guard is

‎scripts/step7/envcapture.py‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,19 +117,52 @@ def _text(path: str) -> str | None:
117117
return None
118118

119119

120+
def _tool_encoding() -> str:
121+
"""The encoding a console tool's bytes actually arrive in.
122+
123+
`text=True` decodes with the locale's preferred encoding, which on a
124+
non-English Windows is the ANSI code page — while a console tool writes in
125+
the CONSOLE output code page. The two disagree, so `powercfg` output arrived
126+
as mojibake whose bytes changed with the ambient code page, and
127+
`power_policy` is identity-bearing: that is drift on a machine that never
128+
moved. Decoding by the producing code page makes the value the same string
129+
whichever console the capture is taken from.
130+
"""
131+
if os.name == "nt":
132+
import ctypes # only needed on the Windows path
133+
134+
try:
135+
# ctypes.windll is defined only on Windows; same treatment as above.
136+
kernel32 = ctypes.windll.kernel32 # type: ignore[attr-defined] # Windows-only
137+
for query in ("GetConsoleOutputCP", "GetOEMCP"):
138+
code_page = getattr(kernel32, query)()
139+
if code_page:
140+
return f"cp{code_page}"
141+
except (AttributeError, OSError, ValueError):
142+
pass
143+
return "utf-8"
144+
145+
120146
def _tool(argv: list[str]) -> tuple[int, str] | None:
121147
"""Run a version query. NOT timed, and no shell.
122148
123149
Returns (returncode, stdout+stderr) or None when the tool is absent. The
124150
return code is handed back because `systemd-detect-virt` reports "none" with
125151
a non-zero exit, and reading that as a failure would turn a real answer into
126152
an unavailable.
153+
154+
Bytes are decoded by `_tool_encoding()` rather than by the locale, with the
155+
same `errors="replace"` `_text` uses: an undecodable byte becomes a visible
156+
replacement character instead of raising inside a probe.
127157
"""
128158
try:
129-
proc = subprocess.run(argv, capture_output=True, text=True, check=False)
159+
proc = subprocess.run(argv, capture_output=True, check=False)
130160
except (OSError, ValueError):
131161
return None
132-
return proc.returncode, (proc.stdout + proc.stderr).strip()
162+
encoding = _tool_encoding()
163+
out = proc.stdout.decode(encoding, errors="replace")
164+
err = proc.stderr.decode(encoding, errors="replace")
165+
return proc.returncode, (out + err).strip()
133166

134167

135168
def _host_fingerprint() -> dict[str, object]:

‎tests/test_step7_envcapture.py‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
envcapture-windows-fixture the schema holds off Linux; the capture path does not
1616
envcapture-frozen-untouched this addition moved none of the three frozen digests
1717
envcapture-ci-provenance a CI-taken manifest says so and cannot hide it
18+
envcapture-tool-encoding a tool's bytes decode by the code page that wrote them
1819
1920
`envcapture-no-measurement` walks the AST rather than the text, because this
2021
module's own docstring names `perf_counter` and `wait4` to say it does not use
@@ -544,6 +545,58 @@ def always_raises() -> None:
544545
"and this probe prints no FAIL line of its own on a green run")
545546

546547

548+
def control_tool_encoding() -> None:
549+
"""A console tool's bytes are decoded by the code page that produced them.
550+
551+
`powercfg` writes in the console OUTPUT code page. `text=True` decoded with
552+
the locale's preferred encoding — the ANSI code page — so on a Russian
553+
Windows `power_policy` arrived as mojibake, and mojibake whose bytes moved
554+
with the ambient code page: the same unchanged machine produced two
555+
different identity values. Identity fields compare whole, so that is drift
556+
the capture invents rather than observes.
557+
558+
The invariant is not "the bytes are UTF-8" — they are whatever the console
559+
is set to. It is that the decoded VALUE is the same string whichever code
560+
page produced it.
561+
"""
562+
expected = "Высокая производительность"
563+
original = ec._tool_encoding
564+
seen: dict[str, str] = {}
565+
try:
566+
for code_page in ("cp866", "cp1251", "utf-8"):
567+
ec._tool_encoding = lambda page=code_page: page # type: ignore[assignment]
568+
emitted = ec._tool([sys.executable, "-c",
569+
"import sys; sys.stdout.buffer.write("
570+
f"{expected.encode(code_page)!r})"])
571+
if emitted is None:
572+
fail("envcapture-tool-encoding", f"the {code_page} probe did not run at all")
573+
return
574+
seen[code_page] = emitted[1]
575+
finally:
576+
ec._tool_encoding = original # type: ignore[assignment]
577+
578+
wrong = {page: text for page, text in seen.items() if text != expected}
579+
if wrong:
580+
fail("envcapture-tool-encoding",
581+
f"decoded {wrong!r}, expected {expected!r} from every code page: the producing "
582+
"code page was ignored, so an identity-bearing value moves with the console")
583+
return
584+
585+
resolved = original()
586+
if os.name == "nt":
587+
if not (resolved.startswith("cp") and resolved[2:].isdigit()):
588+
fail("envcapture-tool-encoding",
589+
f"on Windows the resolver named {resolved!r}, which is not a console code page")
590+
return
591+
elif resolved != "utf-8":
592+
fail("envcapture-tool-encoding",
593+
f"off Windows the resolver named {resolved!r} rather than utf-8")
594+
return
595+
ok("envcapture-tool-encoding",
596+
f"the same text decodes identically from cp866, cp1251 and utf-8; this host resolves "
597+
f"{resolved}")
598+
599+
547600
def run() -> int:
548601
guarded("envcapture-guard-reports", control_guard_reports)
549602
guarded("envcapture-schema", control_schema)
@@ -556,6 +609,7 @@ def run() -> int:
556609
guarded("envcapture-windows-fixture", control_windows_fixture)
557610
guarded("envcapture-frozen-untouched", control_frozen_untouched)
558611
guarded("envcapture-ci-provenance", control_ci_provenance)
612+
guarded("envcapture-tool-encoding", control_tool_encoding)
559613
print()
560614
print(f"step 7 environment capture controls: {len(_PASSES)} passed, {len(_FAILURES)} failed")
561615
return 1 if _FAILURES else 0

0 commit comments

Comments
 (0)