Skip to content

tools: guard stdout in five tools, and unpack bvh_slot_check's six-tuple - #7176

Open
thiefyzheng wants to merge 3 commits into
ItsNotRexed:mainfrom
thiefyzheng:fix/stdout-guard-and-bvh-unpack
Open

thiefyzheng wants to merge 3 commits into
ItsNotRexed:mainfrom
thiefyzheng:fix/stdout-guard-and-bvh-unpack

Conversation

@thiefyzheng

Copy link
Copy Markdown

What

Two things the per-file sweep of main reported on the coordination issue: five tools missing the stdout guard, and one tool that cannot run at all.

1. Five tools had no errors="replace" on stdout — tests/test_tool_stdout_encoding.py names them:

AssertionError: Lists differ: ['tools/awc_cue_labels.py', 'tools/import_call_sites.py',
  'tools/op_reach.py', 'tools/world_drawable_lookup.py', 'tools/world_drawable_verify.py'] != []

Under python -I on Windows a redirected stdout is encoded with the console's ANSI code page and PYTHONUTF8/PYTHONIOENCODING are ignored, so a caller path with a character outside that code page (a Cyrillic, Greek or CJK user name) ended the run with UnicodeEncodeError. Each __main__ block now sets it, the shape every other tool already uses:

    try:  # a redirected stdout is a strict ANSI code page under -I; write '?' for what it cannot encode
        sys.stdout.reconfigure(errors="replace")
    except (AttributeError, ValueError, OSError):
        pass

2. tools/bvh_slot_check.py:152 unpacked five values from a six-tuple

$ python3 -I tests/test_bvh_slot_check.py
ERROR: test_census_with_known_source_and_swapped_blocks
  File "tools/bvh_slot_check.py", line 293, in census
  File "tools/bvh_slot_check.py", line 152, in match_exports
ValueError: too many values to unpack (expected 5)

bvh_match.model_geometries returns (mesh index, model, geometry, triangles, shader hash, render bucket) — its docstring says so, and the other callers unpack six — so the call site was stale, not the helper. It now unpacks all six (the two extra values are unused here, as they are in match_exports' neighbours).

Verification

python3 -I tests/test_tool_stdout_encoding.py   -> 4 tests OK
python3 -I tests/test_bvh_slot_check.py         -> 9 tests OK
python3 -m compileall -q tools scripts viewer   -> clean

No game files, no GPU. The remaining failures from that sweep are separate: test_float_nan_fract (translator, fix in #7173) and the two f64 output-modifier tests.

AI-assisted: yes — found by an automated sweep, diagnosed and fixed with a coding agent; the commands above were run, not assumed.

tests/test_tool_stdout_encoding.py requires every tool's __main__ block to set
errors="replace" on stdout: under -I on Windows a redirected stdout is encoded with the
console's ANSI code page, and a caller path outside it ended the run with
UnicodeEncodeError. Five tools were missing the guard: awc_cue_labels, import_call_sites,
op_reach, world_drawable_lookup, world_drawable_verify.

tools/bvh_slot_check.py:152 unpacked five values from bvh_match.model_geometries, which
returns six (its docstring: mesh index, model, geometry, triangles, shader hash, render
bucket), so match_exports raised ValueError and the tool's own census test could not run.

Both gates green: tests/test_tool_stdout_encoding.py 4 tests OK,
tests/test_bvh_slot_check.py 9 tests OK, compileall clean over tools/ scripts/ viewer/.

Found by the per-file sweep of main reported in ItsNotRexed#6910. AI-assisted.
@best2135

Copy link
Copy Markdown

Tested 9ca4568 on Linux (kernel 7.2), Python 3.14.8, CPU only: pass

On main (26ecd9f) test_bvh_slot_check fail; at this head and with this head merged onto 26ecd9f they pass.

python -I tests/test_bvh_slot_check.py

Note: this conflicts with #7174 in tools/op_reach.py (both add the stdout guard there), so whichever lands second needs a rebase.

AI-assisted check (Claude Code); counts and pass/fail only, no game data.

@duudiis

duudiis commented Nov 23, 2026

Copy link
Copy Markdown

Tested 9ca4568 merged onto main e36d4c4 on Windows 11 Pro, Intel i7-14700KF, CPython 3.11.0 (venv: pytest 9.2.0, numpy 2.4.6, Pillow 13.0.0; numpy is below the 2.5.5 pin because 2.5 needs Python 3.12), CPU only: pass, with notes.

The merge is clean (one auto-merge in tools/world_drawable_lookup.py). Every test was run as python -I tests/<file>.py:

  • test_bvh_slot_check.py: 9 tests OK on the merge. When I put main's tools/bvh_slot_check.py back into the merge, it fails with errors=3, so the existing tests cover the six-tuple unpack fix.
  • test_awc_cue_labels.py (2), test_import_call_sites.py (8), test_op_reach.py (10), test_world_drawable_lookup.py (2), test_world_drawable_verify.py (2): all OK.
  • test_tool_stdout_encoding.py: FAILED (failures=1) on both main and the merge. On main it lists eight unguarded programs. On the merge only ['tools/wave_diff.py', 'scripts/dev.py'] are left. Both files reached main after this PR's base (09b9ba8), so this PR didn't cause the failure, but the test stays red after merging until those two get the same guard too. Adding them here would make it green.
  • python -I -m compileall -q on the six changed files: clean.

Notes:

  • The title and body say five tools, but the diff guards six: tools/bvh_slot_check.py gets the stdout guard too, alongside the unpack fix.
  • tools/bvh_slot_check.py:340: the comment reads "is strict ANSI code page" and is missing an "a" ("is a strict ANSI code page").
  • tools/awc_cue_labels.py:3-4: import sys now sits above import argparse. This is minor ordering only.

AI-assisted (Claude Code).

agent added 2 commits November 23, 2026 17:03
…d#7176's base

Same guard as ItsNotRexed#7176: set errors="replace" on stdout at the start of each
__main__ block, so a path outside the console code page prints '?' instead
of ending the run with UnicodeEncodeError under -I.

Files: tools/div_fixup_diff.py, tools/gbuffer_census.py,
tools/navmesh_tile_obj.py, tools/wave_diff.py (all landed after 09b9ba8),
and scripts/dev.py.

AI-assisted (Pi/DeepSeek V4.1 Flash); measured by running
tests/test_tool_stdout_encoding.py under python3 -I.
@thiefyzheng

Copy link
Copy Markdown
Author

Verified and updated — thanks @duudiis. All measurements below were run locally in fresh worktrees (AI-assisted; every command and result is reproducible from this PR's branches):

  • Your claim reproduces: merging this PR's previous head 9ca4568c onto current main leaves tests/test_tool_stdout_encoding.py red.
  • In fact the test now reports five unguarded programs — tools/div_fixup_diff.py, tools/gbuffer_census.py, tools/navmesh_tile_obj.py, tools/wave_diff.py, scripts/dev.py — main gained three more since your run.
  • The branch is updated (fast-forward, no rebase): new head 9137ca182 merges current main and extends the guard to all five. At that head: test_tool_stdout_encoding.py 4/4 OK, test_bvh_slot_check.py 9/9 OK, tests/test_wave_diff.py OK, compileall clean.
  • The one remaining failure at this head, tests/test_text_write_line_endings.py (leonida_tui.py:753, wave_diff.py:373), reproduces bit-identically on plain main — pre-existing, no pin touched here; our tests: accept both write shapes for an f64 result, and pin three newlines #7209 covers those two spots.
  • On the overlap @best2135 flagged: op_reach, wave_diff, scripts/dev.py: guard stdout like every other tool (test_tool_stdout_encoding) #7174 changes op_reach.py + wave_diff.py + scripts/dev.py, which does intersect this PR's op_reach.py changes — happy to split or adjust whichever way maintainers prefer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants