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
2021module'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+
547600def 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