Skip to content

root_spill_census: report an unreadable index as one error line, not a traceback - #7429

Closed
bobcatchris wants to merge 1 commit into
ItsNotRexed:mainfrom
bobcatchris:fix/root-spill-census-read-errors
Closed

bobcatchris wants to merge 1 commit into
ItsNotRexed:mainfrom
bobcatchris:fix/root-spill-census-read-errors

Conversation

@bobcatchris

Copy link
Copy Markdown
Contributor

Summary

tools/root_spill_census.py printed a traceback for every input it could not read. The __main__ block guards only the stdout reconfigure, so main()'s json.load calls were unguarded:

python -I tools/root_spill_census.py <missing>.json
FileNotFoundError: [Errno 2] No such file or directory: 'absent.json'

python -I tools/root_spill_census.py <not-utf8>.json
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 8: invalid start byte

python -I tools/root_spill_census.py <empty>.json
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

All three were measured on main 1924c8f91 with synthetic files only. A deeply nested file did the same through RecursionError.

Change

  • One read_json(path) helper reads both --index and --carve. It keeps the existing utf-8-sig handling (a BOM-saved index is still JSON, tested today) and turns OSError, UnicodeDecodeError, json.JSONDecodeError and RecursionError into one line: error: cannot read <path>: <reason> or <path> is not valid JSON: <detail>.
  • main() prints that line to stderr and returns 2.
  • Nothing else moves: the census arithmetic, the --base overrides, --expect-no-spill's exit 1, the "not a translator index" usage error and the --json overwrite/folder guards are untouched, and a readable index still produces byte-identical output.

Tests

tests/test_root_spill_census.py, 21 tests, all passing with python -I tests/test_root_spill_census.py:

  • new: four unreadable --index cases (missing, not UTF-8, empty, not JSON), one unreadable --carve, and an index nested 100,000 levels deep. Each asserts exit 2, the error: line naming the path, no Traceback, and that no report was written.
  • existing: the six census tests and nine CLI tests, including the BOM reads, the partial --base default, the write-failure line and the input-overwrite refusals, unchanged and still green.

The same three commands against main's copy of the tool exit 1 with the tracebacks above, which is the negative control for the new tests.

No game files, no GPU: the tests build their indexes in a temporary folder.

AI-assisted.

…a traceback

`main`'s `__main__` guard wraps only the stdout reconfigure, so a missing
--index/--carve, bytes that are not UTF-8, an empty file, text that is not
JSON or a file nested deeper than the parser can hold all end in a
traceback (FileNotFoundError, UnicodeDecodeError, JSONDecodeError,
RecursionError) instead of a diagnostic and exit 2.

Both indexes now go through one reader that keeps the existing UTF-8 BOM
handling and turns each failure into `error: cannot read <path>: <reason>`
or `<path> is not valid JSON: <detail>`, returning 2. The census, the
--json overwrite guard and every verdict are unchanged; a successful read
still yields exactly the same summary.

Tests: four read failures for --index, one for --carve, and a deeply
nested index, each asserting no traceback and no report written; the nine
existing CLI tests and six census tests still pass (21 total).
@bobcatchris
bobcatchris force-pushed the fix/root-spill-census-read-errors branch from 6331184 to 5d93ea3 Compare November 23, 2026 16:46
@neoisg8

neoisg8 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Independent Review of PR #7429

Verdict: LGTM / Approve.

Environment

  • Windows 11 Pro x64, CPython 3.14.3.
  • CPU only; zero game data or GPU hardware needed.
  • Evaluated PR head commit 5d93ea30e cleanly rebased onto origin/main (26ecd9f96).

Negative Control Reproduction on main

On current upstream main (26ecd9f96), tools/root_spill_census.py directly invoked with open(a.index, encoding="utf-8-sig") as f: json.load(f) without handling input read errors:

python -I tools/root_spill_census.py nonexistent.json
# Fails with exit code 1 and an unhandled traceback:
# FileNotFoundError: [Errno 2] No such file or directory: 'nonexistent.json'

The same traceback occurred for invalid UTF-8 bytes, empty JSON files, malformed JSON, and deeply nested JSON (RecursionError).

Verification with PR #7429 Applied

  • read_json(path) encapsulates input reading with utf-8-sig encoding, catching OSError, UnicodeDecodeError, json.JSONDecodeError, and RecursionError and returning a clean diagnostic line:
    python -I tools/root_spill_census.py nonexistent.json
    # Outputs to stderr: error: cannot read nonexistent.json: No such file or directory
    # Exits 2 cleanly without a Python traceback
  • All unit tests pass:
    python -I tests/test_root_spill_census.py
    # Ran 21 tests in 3.238s - OK
  • Checked hygiene:
    • python -m compileall -q tools/root_spill_census.py tests/test_root_spill_census.py -> exit 0.
    • python -I scripts/check_no_game_data.py tools/root_spill_census.py tests/test_root_spill_census.py -> clean, exit 0.
    • Temporary synthetic test files only; zero game data.

AI-assisted.

@gut0to

gut0to commented Nov 23, 2026

Copy link
Copy Markdown

Independent Windows/Python 3.10 validation on 5d93ea30e167bc26fb6923bcb6e137f872f3b8c5:

  • git diff --check HEAD^ HEAD is clean.
  • Passed python -I tests/test_root_spill_census.py: 21/21.
  • Compileall and the no-game-data check passed for both changed files.

The shared read_json() helper preserves BOM input handling and turns each supported read/parse failure into the existing CLI error contract. Both index and carve use the same boundary; the tests also ensure no report is emitted before an unreadable input is rejected.

@garuspl

garuspl commented Nov 23, 2026

Copy link
Copy Markdown

Tested 5d93ea3 on Windows 11 (CPU only, no GPU needed): pass

Windows 11 Pro, Python 3.14.7, CPU only (no GPU or game files involved); pytest/numpy/Pillow pinned from tests/requirements.txt in a fresh venv. Method: PR head merged onto main 51e7c9d, the PR's test files run with python -m pytest before and after the merge, plus python -m compileall -q on the changed files (clean).

  • tests/test_root_spill_census.py: main 18 passed; with the PR 21 passed, no failures.

AI-assisted (Claude Code, Claude Opus 5.5).

@skorex

skorex commented Nov 23, 2026

Copy link
Copy Markdown

Duplicate of #5712

#5712 makes the same change and was opened earlier (#6105 is duplicates too). Topic: root_spill_census: unreadable index = one error line.

Suggest closing this PR in favour of #5712, and porting anything this one adds that #5712 lacks there.

@bobcatchris

Copy link
Copy Markdown
Contributor Author

Closing as a duplicate: #6105 (open, earlier) and #5712 cover this item, and main's #6701 (22037f702) already turned a missing index into ERROR: cannot read ...: no such file + exit 2. skorex spotted it on this PR and is right.

What this PR had that #6105 does not, for its author to take if useful:

  • RecursionError. root_spill_census: an unreadable or malformed index is one ERROR line and exit 2 #6105 wraps both reads in except (OSError, ValueError), which catches the missing file, a permission error, non-UTF-8 bytes and malformed JSON, but json.load on a deeply nested file raises RecursionError, which is neither. Measured on main 7541333a0 with "[" * 100000 + "]" * 100000: traceback. except (OSError, ValueError, RecursionError) and one line <path> is nested too deeply to read covers it.
  • three test cases missing from its matrix: non-UTF-8 bytes, an empty file, and the nested file (its tests cover missing, {bad, and a missing --carve). All three exit 2 with ERROR: cannot read <path>: ... and no Traceback, and none writes a report.

No code from this branch is needed beyond that. AI-assisted.

@bobcatchris

Copy link
Copy Markdown
Contributor Author

Three PRs cover this topic. Facts at current main (976b3ec91):

I'm not closing this while it is the only mergeable, complete fix; the maintainer should pick the base. If #5712 is preferred, the unique pieces here are read_json's RecursionError clause and the "nothing was measured, so no report" assertion.

AI-assisted.

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