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

Open
opened 2026-11-26 20:21:45 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1557
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/gamedata-reader-filter-and-out-io


📝 Commits (1)

  • b6721dc gamedata_reader: fix --root filter, sanitize --out filenames, and write JSON atomically

📊 Changes

2 files changed (+124 additions, -18 deletions)

View changed files

📝 tests/test_gamedata_reader.py (+53 -0)
📝 tools/gamedata_reader.py (+71 -18)

📄 Description

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)


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1557 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/gamedata-reader-filter-and-out-io` --- ### 📝 Commits (1) - [`b6721dc`](https://github.com/ubiquitarianism/leonida/commit/b6721dc0baeee7b542b47d977be9818ffb824e13) gamedata_reader: fix --root filter, sanitize --out filenames, and write JSON atomically ### 📊 Changes **2 files changed** (+124 additions, -18 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_gamedata_reader.py` (+53 -0) 📝 `tools/gamedata_reader.py` (+71 -18) </details> ### 📄 Description ## 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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1580
No description provided.