upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tests/test_tool_stdout_encoding.py
diff options
context:
space:
mode:
authorAAyberkU <97833630+AAyberkU@users.noreply.github.com>2026-11-22 19:55:36 +0300
committerAAyberkU <97833630+AAyberkU@users.noreply.github.com>2026-11-22 19:55:36 +0300
commita1343528f7de7fa086aefd2e13942ebe83e6db0a (patch)
tree948cb580f3aac461473b74a3885098358c925334 /tests/test_tool_stdout_encoding.py
parent49475796c226c8511b9470bf97a4daf57b8af323 (diff)
scripts, viewer: a redirected stdout no longer dies on a path outside the code page
Under `python -I` on Windows a redirected stdout is strict ANSI, so printing a path with a character outside the code page raised UnicodeEncodeError. tools/ guards against it at the start of every __main__ block and tests/test_tool_stdout_encoding.py enforces that, but only for tools/. viewer/serve.py died before it started serving and scripts/audit_shader4_regs.py exited 1 after writing its report; bench_wave64.py and run_gpu_tests.py print the same kind of output unguarded. All four get the guard, and the check now covers scripts/ and viewer/. Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016ABh36DZ2sZNq3ChN6EZJs
Diffstat (limited to 'tests/test_tool_stdout_encoding.py')
-rw-r--r--tests/test_tool_stdout_encoding.py66
1 files changed, 55 insertions, 11 deletions
diff --git a/tests/test_tool_stdout_encoding.py b/tests/test_tool_stdout_encoding.py
index df27a596f..3f4f99cf6 100644
--- a/tests/test_tool_stdout_encoding.py
+++ b/tests/test_tool_stdout_encoding.py
@@ -4,11 +4,12 @@ On Windows a redirected stdout (tool.py > log.txt, or a pipe from make_game.py)
4code page, and `python -I` ignores PYTHONUTF8 and PYTHONIOENCODING. A caller path with a character outside that 4code page, and `python -I` ignores PYTHONUTF8 and PYTHONIOENCODING. A caller path with a character outside that
5code page, such as a Cyrillic, Greek or CJK user name, or a Polish one under a code page without it, reached print() 5code page, such as a Cyrillic, Greek or CJK user name, or a Polish one under a code page without it, reached print()
6as UnicodeEncodeError and ended the run. stderr escapes such characters (backslashreplace); stdout was strict. #1406 6as UnicodeEncodeError and ended the run. stderr escapes such characters (backslashreplace); stdout was strict. #1406
7fixed make_game.py. Every CLI tool now starts its __main__ block by setting errors="replace" on stdout. 7fixed make_game.py. Every CLI tool now starts its __main__ block by setting errors="replace" on stdout, and so do
8the programs under scripts/ and viewer/, which are run the same way.
8 9
9The paths below use a private-use character that no ANSI code page maps, so before the fix these tools died on any 10The paths below use a private-use character that no ANSI code page maps, so before the fix these tools died on any
10Windows locale. On a UTF-8 console the character prints, and the test passes either way. Standard library only; 11Windows locale. On a UTF-8 console the character prints, and the test passes either way. Standard library only;
11no game files (the inputs are zero bytes). 12no game files (the inputs are zero bytes or an empty folder).
12""" 13"""
13import os 14import os
14import re 15import re
@@ -19,21 +20,29 @@ import unittest
19 20
20ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 21ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
21TOOLS = os.path.join(ROOT, "tools") 22TOOLS = os.path.join(ROOT, "tools")
23PROGRAM_DIRS = ("tools", "scripts", "viewer")
24# Still without the guard because other open pull requests change these files; each gets it there or after them.
25NOT_YET_GUARDED = {
26 "scripts/shader_stats.py", # guard added separately, see #2135's file
27 "scripts/run_wine_tests.py", # guard added separately; open Wine runner pull requests change this file
28}
22MARK = "" 29MARK = ""
23 30
24 31
25class EveryToolGuardsStdout(unittest.TestCase): 32class EveryToolGuardsStdout(unittest.TestCase):
26 def test_every_main_block_reconfigures_stdout(self): 33 def test_every_main_block_reconfigures_stdout(self):
27 missing = [] 34 missing = []
28 for name in sorted(os.listdir(TOOLS)): 35 for folder in PROGRAM_DIRS:
29 if not name.endswith(".py"): 36 for name in sorted(os.listdir(os.path.join(ROOT, folder))):
30 continue 37 rel = folder + "/" + name
31 with open(os.path.join(TOOLS, name), encoding="utf-8") as f: 38 if not name.endswith(".py") or rel in NOT_YET_GUARDED:
32 text = f.read() 39 continue
33 m = re.search(r"^if __name__ == ['\"]__main__['\"]:\n", text, re.M) 40 with open(os.path.join(ROOT, folder, name), encoding="utf-8") as f:
34 if m and 'sys.stdout.reconfigure(errors="replace")' not in text[m.end():m.end() + 400]: 41 text = f.read()
35 missing.append(name) 42 m = re.search(r"^if __name__ == ['\"]__main__['\"]:\n", text, re.M)
36 self.assertEqual(missing, [], "tools whose __main__ block doesn't set errors='replace' on stdout") 43 if m and 'sys.stdout.reconfigure(errors="replace")' not in text[m.end():m.end() + 400]:
44 missing.append(rel)
45 self.assertEqual(missing, [], "programs whose __main__ block doesn't set errors='replace' on stdout")
37 46
38 47
39class MeasuredCrashes(unittest.TestCase): 48class MeasuredCrashes(unittest.TestCase):
@@ -59,5 +68,40 @@ class MeasuredCrashes(unittest.TestCase):
59 self.assertNotIn("UnicodeEncodeError", err) 68 self.assertNotIn("UnicodeEncodeError", err)
60 69
61 70
71class ScriptsAndViewer(unittest.TestCase):
72 # Both died with UnicodeEncodeError on Windows 11 with stdout redirected: the audit after writing its report
73 # (exit 1), the viewer before it started serving.
74 def test_audit_with_a_report_path_outside_the_code_page(self):
75 with tempfile.TemporaryDirectory() as tmp:
76 disasm = os.path.join(tmp, "disasm-" + MARK)
77 os.makedirs(disasm)
78 report = os.path.join(tmp, "rep-" + MARK + ".json")
79 r = subprocess.run([sys.executable, "-I", os.path.join(ROOT, "scripts", "audit_shader4_regs.py"),
80 disasm, "--json", report], capture_output=True, cwd=ROOT, timeout=300)
81 err = r.stderr.decode("utf-8", "replace")
82 self.assertNotIn("Traceback", err)
83 self.assertEqual(r.returncode, 0, err)
84 self.assertTrue(os.path.isfile(report))
85 self.assertIn(b"wrote ", r.stdout)
86
87 def test_viewer_with_a_world_path_outside_the_code_page(self):
88 with tempfile.TemporaryDirectory() as tmp:
89 world = os.path.join(tmp, "world-" + MARK)
90 os.makedirs(world)
91 proc = subprocess.Popen([sys.executable, "-I", os.path.join(ROOT, "viewer", "serve.py"),
92 "--world", world, "--port", "0"],
93 stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=ROOT)
94 try:
95 line = proc.stdout.readline() # the viewer prints this line once it is listening
96 running = proc.poll() is None
97 finally:
98 proc.kill()
99 _, err = proc.communicate(timeout=60)
100 err = err.decode("utf-8", "replace")
101 self.assertNotIn("Traceback", err)
102 self.assertIn(b"viewer: http://127.0.0.1:", line)
103 self.assertTrue(running, err)
104
105
62if __name__ == "__main__": 106if __name__ == "__main__":
63 unittest.main() 107 unittest.main()