gamedata_reader: fix --root filter, sanitize --out filenames, and write JSON atomically (GAMEDATA-READER-FILTER-AND-OUT-IO) #1557

Merged
leonida/mandy
quietfold merged 1 commit from fix/gamedata-reader-filter-and-out-io into main 2026-11-28 10:50:27 +00:00

Pull request successfully merged and closed

quietfold commented 2026-11-25 04:30:19 +00:00

What

Fix --root filtering, unsafe export filenames, and --out I/O handling in tools/gamedata_reader.py:

  1. Namespaced root tags crashed --out export (_safe_stem, run):
    • ET.iterparse expands XML namespaces to Clark notation ({http://example.com/ns}Root), so f"{tag}_{name}.json" contained / and : and crashed with FileNotFoundError / OSError.
    • Sanitize export stems by replacing path/Windows-illegal filename characters ([<>:"/\\|?*\x00-\x1f]) with _ while leaving ordinary ASCII tag/file stems unchanged.
  2. --root missed valid documents without a matching root_hint (run):
    • Skipping immediately when hint != root_filter dropped documents whose root_hint was empty (e.g. a self-closing root after <!DOCTYPE ...>) or lacked the namespace prefix of a Clark-notation --root filter, and never verified the parsed tag against root_filter.
    • Keep the fast pre-parse skip when hint is non-empty and matches neither root_filter nor its local name (root_filter.endswith("}" + hint)), and verify tag == root_filter after parsing.
  3. Empty/whitespace --out, atomic LF-terminated writes, and accurate write error diagnostics (main, run):
    • Reject empty or whitespace-only --out paths with ERROR: --out path must not be empty (exit 2) instead of silently ignoring "" or creating a whitespace-named folder.
    • Write exported JSON files atomically through sibling temp files (newline="\n", trailing "\n", file_mode.published_mode, replace_retry.replace), and report write OSErrors as ERROR: cannot write ... instead of ERROR: cannot read ....

Claimed in the coordination issue as

GAMEDATA-READER-FILTER-AND-OUT-IO — verified 0 open PRs and 0 prior claims; unable to post a comment on #14 because GitHub disabled commenting once #14 reached its 2,500-comment cap (and no successor "Coordinating who works on what" issue is open yet).

Testing

  • python3 -I -W error tests/test_gamedata_reader.py -v: 25/25 OK (including 3 new regression tests).

  • python3 -I tests/test_text_write_line_endings.py: 3/3 OK.

  • python3 -I scripts/check_no_game_data.py tools/gamedata_reader.py tests/test_gamedata_reader.py: clean.

  • python3 -m compileall -q tools/gamedata_reader.py tests/test_gamedata_reader.py: clean.

  • Mutation check against unpatched main:tools/gamedata_reader.py: all 3 new tests fail on main (FAILED (failures=3, errors=2)).

  • AI-assisted: yes

  • GPUs tested: none (CPU-only XML reader tests on macOS; no GPU or game files needed)

## What Fix `--root` filtering, unsafe export filenames, and `--out` I/O handling in `tools/gamedata_reader.py`: 1. **Namespaced root tags crashed `--out` export (`_safe_stem`, `run`)**: - `ET.iterparse` expands XML namespaces to Clark notation (`{http://example.com/ns}Root`), so `f"{tag}_{name}.json"` contained `/` and `:` and crashed with `FileNotFoundError` / `OSError`. - Sanitize export stems by replacing path/Windows-illegal filename characters (`[<>:"/\\|?*\x00-\x1f]`) with `_` while leaving ordinary ASCII tag/file stems unchanged. 2. **`--root` missed valid documents without a matching `root_hint` (`run`)**: - Skipping immediately when `hint != root_filter` dropped documents whose `root_hint` was empty (e.g. a self-closing root after `<!DOCTYPE ...>`) or lacked the namespace prefix of a Clark-notation `--root` filter, and never verified the parsed `tag` against `root_filter`. - Keep the fast pre-parse skip when `hint` is non-empty and matches neither `root_filter` nor its local name (`root_filter.endswith("}" + hint)`), and verify `tag == root_filter` after parsing. 3. **Empty/whitespace `--out`, atomic LF-terminated writes, and accurate write error diagnostics (`main`, `run`)**: - Reject empty or whitespace-only `--out` paths with `ERROR: --out path must not be empty` (exit `2`) instead of silently ignoring `""` or creating a whitespace-named folder. - Write exported JSON files atomically through sibling temp files (`newline="\n"`, trailing `"\n"`, `file_mode.published_mode`, `replace_retry.replace`), and report write `OSError`s as `ERROR: cannot write ...` instead of `ERROR: cannot read ...`. ## Claimed in the coordination issue as `GAMEDATA-READER-FILTER-AND-OUT-IO` — verified 0 open PRs and 0 prior claims; unable to post a comment on `#14` because GitHub disabled commenting once `#14` reached its 2,500-comment cap (and no successor `"Coordinating who works on what"` issue is open yet). ## Testing - `python3 -I -W error tests/test_gamedata_reader.py -v`: **25/25 OK** (including 3 new regression tests). - `python3 -I tests/test_text_write_line_endings.py`: **3/3 OK**. - `python3 -I scripts/check_no_game_data.py tools/gamedata_reader.py tests/test_gamedata_reader.py`: clean. - `python3 -m compileall -q tools/gamedata_reader.py tests/test_gamedata_reader.py`: clean. - Mutation check against unpatched `main:tools/gamedata_reader.py`: all 3 new tests fail on `main` (`FAILED (failures=3, errors=2)`). - AI-assisted: yes - GPUs tested: none (CPU-only XML reader tests on macOS; no GPU or game files needed)
copilot-pull-request-reviewer[bot] reviewed 2026-11-25 04:30:32 +00:00
copilot-pull-request-reviewer[bot] left a comment
No description provided.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
MoonEyes commented 2026-11-28 10:50:21 +00:00

Red/green: the PR's tests fail on main and pass with this PR (Linux, Python 3.13.18).
Targeted batch of 19 PRs (their tests, the tests importing the changed modules, the docs tests) on main 3ca0707: no new failure, Linux / Python 3.13.
AI-assisted (Claude Code, Claude Opus 5.5)

Red/green: the PR's tests fail on main and pass with this PR (Linux, Python 3.13.18). Targeted batch of 19 PRs (their tests, the tests importing the changed modules, the docs tests) on main 3ca0707: no new failure, Linux / Python 3.13. AI-assisted (Claude Code, Claude Opus 5.5)
Sign in to join this conversation.
No description provided.