Skip to content

gamedata_reader: continue batches after conversion depth failures - #7512

Open
Kosmabb8 wants to merge 1 commit into
ItsNotRexed:mainfrom
Kosmabb8:codex/nov23-reader-depth
Open

Kosmabb8 wants to merge 1 commit into
ItsNotRexed:mainfrom
Kosmabb8:codex/nov23-reader-depth

Conversation

@Kosmabb8

Copy link
Copy Markdown
Contributor

A well-formed XML document can exceed the recursive Reader conversion depth. run() currently lets its RecursionError escape, so the CLI prints a traceback and aborts the batch before later ordinary documents are converted or exported. Count this as a per-document conversion failure and continue, using the existing not_well_formed/parse_errors accounting.

Claim: #7333 (comment) (new narrow per-document conversion-depth failure handling item).

The change adds RecursionError only to the exception tuple around reader.read(). It keeps the current behavior for mixed batches (exit 0 when documents are read) and all-failed batches (exit 1 with the existing no-document error). No recursion-limit changes, invented depth limit, mock Reader, or fragment-recovery changes. Independent of #4291's opt-in recovery work and #3264's index/CLI diagnostics.

Validation on Windows 11, portable CPython 3.12.10, CPU only, against main 6f58bd2:

  • Negative control with the new real-Reader tests: 1 API error and 2 CLI failures before the source correction.
  • All targeted suites ran with ResourceWarning treated as an error: test_gamedata_reader.py 25 passed; test_gamedata_xml.py 14 ran/13 passed/1 existing skip; test_gamedata_index.py 30 passed; test_content_definitions.py 12 passed.
  • python -m compileall -q make_game.py tools tests: passed.
  • python -I scripts/check_no_game_data.py tools/gamedata_reader.py tests/test_gamedata_reader.py: passed.
  • git diff --check: passed.
  • Both changed files are unchanged between the pinned base and newer main 86d1c86.

The fixture has max(2000, sys.getrecursionlimit()+100) nested nodes. ElementTree parses it successfully, while conversion exceeds Python's existing call-depth limit. API and real subprocess CLI checks verify that a later shallow document is counted and exported with correct JSON content; shallow input alone succeeds; two depth failures produce the existing all-failed status without a traceback or output directory.

Synthetic XML only; no game files, GPU/driver validation or C++ runtime changes. AI-assisted with Codex.

@morodirule

Copy link
Copy Markdown

Tested b573fff on Arch Linux, Python 3.14.10, CPU only: pass — tests/test_gamedata_reader.py 25/25 OK. Negative control: merge-base tool → 2 failures + 1 error (depth-failure counting, all-failures status, continue-after-failure); restored, green again. No game files. AI-assisted.

@airp1ane

Copy link
Copy Markdown
Contributor

Tested b573fff on Windows 11, Python 3.15.2, NVIDIA RTX 4060 Ti (driver 621.24; CPU-only checks, no game files, no GPU/llvm-mc/MSVC used): pass.

Ran the test files this PR adds or changes, from a clean checkout of the PR head: test_gamedata_reader.py - all exit 0.

AI-assisted (Claude Code).

@AsgerCode

Copy link
Copy Markdown

Tested b573fff on Windows 11 (NTFS), Python 3.12.10 (CPU only, no GPU, no game files): pass

What I ran:

  • python -I -W error::ResourceWarning tests/test_gamedata_reader.py: 25 tests, OK.
  • The other tests that use the reader: test_gamedata_xml.py 14 tests OK (1 skipped), test_gamedata_index.py 30 tests OK, test_content_definitions.py 12 tests OK, test_world_grid_atlas.py 28 tests OK (1 skipped). world_grid_atlas.py calls Reader.read directly, not run(), so this change does not affect it.
  • Negative control (merge base's tools/gamedata_reader.py): 1 error and 2 failures in ConversionDepth, each with RecursionError: maximum recursion depth exceeded.
  • Merge with current main (dde82f2): clean, and main has not changed either file since the merge base. The 4 reader test files above also pass on the merged tree.

The earlier comments ran the test file and the negative control. I added these checks with hand-made XML only.

The depth limit is unchanged. No setrecursionlimit call exists in gamedata_reader.py or gamedata_xml.py. A binary search with Reader().read() found the same deepest nesting on the merge base and on the PR head: 331 for plain <node> nesting, 330 for <Item type="T"> nesting, and 331 for <node name="n"> nesting. sys.getrecursionlimit() is 1000 before and after.

The failed item is reported, and later items are converted (CLI, --out):

Inputs, in order Exit First stdout line Files written
deep <node> x2000, shallow A, deep <Item type="T"> x2000, shallow B 0 2 documents read, 2 roots, 2 not well-formed {'maximum recursion depth exceeded': 2} A and B, correct JSON
deep x2000, deep typed x2000 1 0 documents read, 0 roots, 2 not well-formed {...: 2} none, and --out is not created; stderr ERROR: no document could be read; 2 not well-formed
--root CDeep with deep + shallow 1 0 documents read, 0 roots, 1 not well-formed {...: 1}
--root CShallowA with deep + shallow 0 1 documents read, 1 roots, 0 not well-formed
<CHuge> nested 100,000 and 1,000,000 deep, then shallow 0 1 documents read, 1 roots, 1 not well-formed {...: 1} shallow only. No crash, under 2 s
<Item type> and <node> nesting at depths 320, 325, 328, 329, 330 and 331, each then shallow 0 2 documents read, 2 roots, 0 not well-formed (only typed 331: 1 documents read ... 1 not well-formed) 2 (1 for typed 331)

The last row checks that the JSON export (outside the new except) does not reach the limit when conversion only just succeeds. It does not.

Nits, none blocking:

  1. The partial counts of a failed document stay in the summary. After the first row above, the summary prints type classes: 1 (331 typed elements), but both documents that were read have no type. _convert counts self.types[t] += 1 before it recurses. The merge base does the same for a content-array ValueError (a 3-deep typed <Item> with a bad vec3v_array adds 3), so this is not new. But each depth failure now adds about 330. One possible fix is to give each document its own Counter and merge it only after a successful read.

Overlap: #4291 (gamedata_reader: recover intact sections and items from truncated XML payloads) also changes run(). It merges cleanly with this PR, but with both merged, --recover still aborts a batch on a depth failure. The input was <CRoot><Item> + 400 nested <Item> + <Item><x value= (truncated). reader.read raises ParseError, and then recover_fragments -> _recover_prefix_items converts the complete deep <Item> and raises RecursionError from inside the except block in run(): exit 1, last stderr line RecursionError: maximum recursion depth exceeded, and the later shallow document is not read. Without --recover, the same input is counted as unclosed token, exit 0. The two except (ET.ParseError, ValueError) blocks in #4291 (lines 356 and 428) need RecursionError as well, in whichever PR lands second. The other open PRs on this file (#3856, #6568, #7706) do not catch RecursionError and merge cleanly with this one.

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.

4 participants