Skip to content

awc_cue_labels: guard stdout with errors='replace' in __main__ - #7152

Open
kapitche wants to merge 1 commit into
ItsNotRexed:mainfrom
kapitche:pyitem-r3
Open

kapitche wants to merge 1 commit into
ItsNotRexed:mainfrom
kapitche:pyitem-r3

Conversation

@kapitche

Copy link
Copy Markdown
Contributor

What broke: tools/awc_cue_labels.py (added in #6819) has no stdout guard in its __main__ block, so tests/test_tool_stdout_encoding.py::EveryToolGuardsStdout::test_every_main_block_reconfigures_stdout lists it on main f25ba44ed:

~/src/Leonida/.venv/bin/python -I tests/test_tool_stdout_encoding.py
... ['tools/awc_cue_labels.py', 'tools/bvh_slot_check.py', 'tools/import_call_sites.py', ...]

Fix: the same sys.stdout.reconfigure(errors="replace") guard every other tool starts its __main__ block with. The tool itself prints nothing to stdout today; the guard keeps it consistent with the rule the test enforces, and covers any later print of a path.

Not touched: bvh_slot_check (#6508) and import_call_sites, world_drawable_lookup, world_drawable_verify (#7032 / #7046) already have open PRs; this PR only removes awc_cue_labels from the list.

Tested: Linux (WSL2, kernel 5.15), Python 3.12.16, python -I. tests/test_tool_stdout_encoding.py: awc_cue_labels no longer in the missing list (the other four names remain, owned by the PRs above, so the test as a whole stays red until those land). tests/test_awc_cue_labels.py still passes.

Negative control: git checkout upstream/main -- tools/awc_cue_labels.py puts tools/awc_cue_labels.py back in the missing list (5 names instead of 4).

AI-assisted.

@tikD1337

Copy link
Copy Markdown
Contributor

Review: awc_cue_labels: guard stdout with errors='replace' in __main__

Verdict: Approve. This is a 5-line change that makes the last tool from #6819 follow the project's stdout rule, and it removes tools/awc_cue_labels.py from the list that tests/test_tool_stdout_encoding.py flags. It changes nothing in how the tool runs.

Environment

  • Windows 11 Pro, CPython 3.13, python -I
  • Checked out at the PR head 1aba0be51 (base main @ f25ba44ed)

What changed

tools/awc_cue_labels.py:

  1. Adds import sys, placed in alphabetical order with the other imports.
  2. Before main() runs, the __main__ block now tries sys.stdout.reconfigure(errors="replace") and catches (AttributeError, ValueError, OSError).

The comment, the call and the exception tuple match the guard the other tools already use, character for character. For example, tools/ab_control_flow.py:344-348 is identical, and 216 files under tools/ carry the same guard.

Why it is sound

  • What it fixes. Under python -I on Windows, a redirected sys.stdout uses the ANSI code page with errors="strict". Printing a character the code page cannot encode (a non-ASCII path, say) raises UnicodeEncodeError and the tool dies. With errors="replace" that character becomes ? and the run continues.

  • The exception tuple is right.

    • AttributeError: sys.stdout was replaced by an object without reconfigure (a test harness, an IDE shim).
    • ValueError: the stream was closed or detached.
    • OSError: the underlying descriptor is not valid.

    Catching these and moving on is correct, because the guard is optional hardening.

  • No change in behaviour.

    • Only the error handler is changed. The encoding and newline translation stay as they were.
    • The tool writes nothing to stdout today. Its only output is the label files, which it writes with encoding="utf-8", newline="\n", so they are unaffected.
    • --help text is pure ASCII.
  • Where it runs. It sits in __main__, not at import time, so tests/test_awc_cue_labels.py imports convert() without touching the test runner's stdout.

  • stderr (not a blocker). The error path is parser.exit(1, f"error: {exc}\n"), which writes to stderr. stderr already defaults to errors="backslashreplace", so an OSError that names a non-ASCII path still prints. The tool is therefore safe on both streams, and the PR is right to touch stdout only, which is all the rule asks for.

  • Scope. The PR touches only this one file. It leaves bvh_slot_check (bvh_slot_check: reconfigure stdout with errors='replace' in __main__ #6508) and import_call_sites / world_drawable_lookup / world_drawable_verify (import_call_sites, world_drawable_lookup, world_drawable_verify: guard stdout like every other tool #7032 / leonida_tui's log names its newline; import_call_sites and world_drawable_lookup/verify guard stdout (lint tests red on main) #7046) to the PRs that already handle them.

Test results (no game files needed)

Check Result
git diff --check PASS
python -m compileall -q make_game.py tools tests PASS
scripts/check_no_game_data.py PASS
python -I make_game.py --help PASS
python -I tests/test_awc_cue_labels.py PASS: test_channel_roundtrip and test_reject_invalid_channel_before_publishing, 2 tests in 0.184s
python -I tests/test_tool_stdout_encoding.py Expected FAIL: test_every_main_block_reconfigures_stdout

The test_tool_stdout_encoding.py failure is the expected result, not a regression. tools/awc_cue_labels.py is no longer in the missing list. The four names that remain (bvh_slot_check, import_call_sites, world_drawable_lookup, world_drawable_verify) are exactly the ones the PR description says other open PRs own. This matches the author's negative control: with the base file the list has 5 names, and with this PR it has 4.

Adherence to project rules

  • No game data: the diff contains no archives, executables, shaders, audio, cue sheets or logs that quote them.
  • No keys, decryption or protection workarounds: none.
  • Nothing online: no network access or service domains.
  • Fix, don't disable: the tool gets the guard the test enforces. Nothing is added to a skip list and the test is not loosened.
  • Conventions: standard library only, works on Windows first, and the PR says it was AI-assisted and how it was tested.

Nits (non-blocking)

  • None in the code.
  • For the merge, the commit subject could reference the coordination item ID if the coordinator assigned one. The body already explains the dependency on the other four PRs well.

AI-assisted.

@duudiis

duudiis commented Nov 23, 2026

Copy link
Copy Markdown

Tested 1aba0be 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.

  • git merge --no-ff refs/pr/7152 onto main: clean, touches only tools/awc_cue_labels.py (+5).
  • python -m pytest -q tests/test_tool_stdout_encoding.py
    • main: 1 failed, 3 passed; test_every_main_block_reconfigures_stdout lists 8 programs, including tools/awc_cue_labels.py.
    • merge: 1 failed, 3 passed; the same test now lists 7 programs, and tools/awc_cue_labels.py is no longer among them.
  • python -I tests/test_awc_cue_labels.py on the merge: 2 tests, OK.
  • python -I tools/awc_cue_labels.py --help on the merge: exit 0.
  • git diff --check on the merge diff: clean.

Notes:

  • The guard at tools/awc_cue_labels.py:56-59 follows the same pattern as the other tools: it wraps sys.stdout.reconfigure(errors="replace") in a try that catches AttributeError/ValueError/OSError, and runs before main().
  • The PR description says four programs remain without the guard. On current main, seven remain, and none of them is caused by this PR: tools/bvh_slot_check.py, tools/import_call_sites.py, tools/op_reach.py, tools/wave_diff.py, tools/world_drawable_lookup.py, tools/world_drawable_verify.py and scripts/dev.py. So test_every_main_block_reconfigures_stdout still fails after this merge. It might be worth updating that count in the description.

AI-assisted (Claude Code).

@aarushkha

Copy link
Copy Markdown

Tested 1aba0be merged onto main 86a7807dd on Windows 10 Pro 22H2 (19045), Python 3.14.3: pass (its part).

  • python -I tests/test_tool_stdout_encoding.py: Ran 4 tests in 2.568s only the other PRs' entries left

Fixes its entry (tools/awc_cue_labels.py); with #6508, #7174 and #7032 also applied, test_tool_stdout_encoding passes.

It merges cleanly together with #7092 #6049 #7120 #6513 #7242 #7209 #6254 #7214 #6957 #7309 #4771 #7029 #7094 #7129 #6508 #7174 #7032 onto main; with all of them applied, the only standalone test still red on Windows is test_first_look_paths (claimed by Racurs156). Summary in #7333.

No GPU or game files needed. AI-assisted (Claude Code); I read the diff before running it.

@Daazd

Daazd commented Nov 23, 2026

Copy link
Copy Markdown

Independent check (Linux, Python 3.12.3, Node 24.13, CPU only, no game files): head 1aba0be51 merged onto main 6a4299563: merges clean.

  • python3 -I tests/test_tool_stdout_encoding.py: Ran 4 tests in 0.165s FAILED (failures=1) (exit 1)

Clears awc_cue_labels in test_tool_stdout_encoding. With #6508, #7032, #7174 and #7650 also merged, the whole test passes.

AI-assisted (Claude Code).

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.

5 participants