fix(tools): report deeply nested patch advisor JSON and TOML as errors #12787

Merged
xXPBSXx merged 3 commits from upstream-pr/12787 into main 2026-11-10 19:29:01 +00:00
xXPBSXx commented 2026-10-29 09:05:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-29T09:05:15Z.


What

suggest_patches.py reads the title's param.json, the catalog's index.json and each entry's patch file with json.loads and tomllib.loads and reports an unreadable file as Patch advisor: ... with exit code 2, or rejects only the entry whose patch file is unreadable, but only for OSError, ValueError, json.JSONDecodeError and tomllib.TOMLDecodeError. A file nested deeper than the interpreter's recursion limit raises RecursionError, which is none of these, so the tool printed a traceback and exited with 1. This catches RecursionError in the top-level handler, so param.json and index.json get the usual message and exit code 2, and in the per-entry handler, so one catalog entry whose patch file is nested too deeply for the TOML decoder is rejected on its own and the remaining entries are still checked, like any other unreadable patch file. The new tests pick their nesting depth from sys.getrecursionlimit() and check that the decoder really fails on it, so they do not depend on the standard library's message text.

Tested

  • Windows 10 Pro, Python 3.12.10: with this change (head 668f600596) all 37 tests of tools/tests/test_suggest_patches.py pass (2 skipped, as before). At the first commit (4e6047eccf) the nested-JSON test failed with the suggest_patches.py of main 14b3441f3d, differing only in the except clause; the new patch-file test fails when the per-entry except lacks RecursionError.
  • Ubuntu 24.04 under WSL2 (a virtualised kernel, not bare-metal), Python 3.12.3: 37 of 37 pass; with the first commit's suggest_patches.py the patch-file test fails.
  • A trial merge with the current main (5454cd2e45, which now has the offline catalog validator) merges without conflict, and tools/tests/test_suggest_patches.py together with tools/tests/test_validate_patch_catalog.py passes (52 tests, 3 skipped).
  • check_conventions --base upstream/main passes.
  • Measured on Python 3.12 only. A reviewer ran the first commit on macOS arm64 with Python 3.12 to 3.15; the second commit was not run on newer versions.

Overlap

Two open PRs change tools/suggest_patches.py: #12340 (symbolic link loops, also its test) and #12769 (JSON output). I took the changed files of each of the 1418 open PRs from git diff --name-only against its merge base, and re-checked these two against this head and main 5454cd2e45. git merge-tree of this branch with #12340 reports one content conflict in tools/suggest_patches.py, next to the except clause this change edits, while #12340 is clean on its own against main; whichever lands second needs a one-line keep-both resolution. #12769 merges cleanly with this branch. Neither is a prerequisite, so there is no Depends on. #12263, which I had listed in the first version, has been merged; its validate_patch_catalog.py reports deeply nested index.json data on its own and added no RecursionError handling to suggest_patches.py.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5065240338 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12787). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-29T09:05:15Z. --- ### What `suggest_patches.py` reads the title's `param.json`, the catalog's `index.json` and each entry's patch file with `json.loads` and `tomllib.loads` and reports an unreadable file as `Patch advisor: ...` with exit code 2, or rejects only the entry whose patch file is unreadable, but only for `OSError`, `ValueError`, `json.JSONDecodeError` and `tomllib.TOMLDecodeError`. A file nested deeper than the interpreter's recursion limit raises `RecursionError`, which is none of these, so the tool printed a traceback and exited with 1. This catches `RecursionError` in the top-level handler, so `param.json` and `index.json` get the usual message and exit code 2, and in the per-entry handler, so one catalog entry whose patch file is nested too deeply for the TOML decoder is rejected on its own and the remaining entries are still checked, like any other unreadable patch file. The new tests pick their nesting depth from `sys.getrecursionlimit()` and check that the decoder really fails on it, so they do not depend on the standard library's message text. ### Tested - Windows 10 Pro, Python 3.12.10: with this change (head `668f600596`) all 37 tests of `tools/tests/test_suggest_patches.py` pass (2 skipped, as before). At the first commit (`4e6047eccf`) the nested-JSON test failed with the `suggest_patches.py` of `main` `14b3441f3d`, differing only in the `except` clause; the new patch-file test fails when the per-entry `except` lacks `RecursionError`. - Ubuntu 24.04 under WSL2 (a virtualised kernel, not bare-metal), Python 3.12.3: 37 of 37 pass; with the first commit's `suggest_patches.py` the patch-file test fails. - A trial merge with the current `main` (`5454cd2e45`, which now has the offline catalog validator) merges without conflict, and `tools/tests/test_suggest_patches.py` together with `tools/tests/test_validate_patch_catalog.py` passes (52 tests, 3 skipped). - `check_conventions --base upstream/main` passes. - Measured on Python 3.12 only. A reviewer ran the first commit on macOS arm64 with Python 3.12 to 3.15; the second commit was not run on newer versions. ### Overlap Two open PRs change `tools/suggest_patches.py`: #12340 (symbolic link loops, also its test) and #12769 (JSON output). I took the changed files of each of the 1418 open PRs from `git diff --name-only` against its merge base, and re-checked these two against this head and `main` `5454cd2e45`. `git merge-tree` of this branch with #12340 reports one content conflict in `tools/suggest_patches.py`, next to the `except` clause this change edits, while #12340 is clean on its own against `main`; whichever lands second needs a one-line keep-both resolution. #12769 merges cleanly with this branch. Neither is a prerequisite, so there is no `Depends on`. #12263, which I had listed in the first version, has been merged; its `validate_patch_catalog.py` reports deeply nested `index.json` data on its own and added no `RecursionError` handling to `suggest_patches.py`. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code).
rooklz commented 2026-10-29 10:28:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-29T10:28:56Z.

Review: COMMENTED


I checked head 4e6047eccf on macOS arm64, where the body says it was not run.

What I confirmed

  • tools/tests/test_suggest_patches.py passes 36/36 on Python 3.12.13, 3.13.13, 3.14.8 and 3.15.1. The new test fails on the main suggest_patches.py with the expected RecursionError (2 errors, 3.13 and 3.15). The system Python 3.9.6 can't import the module because there is no tomllib, and that is the same on main.
  • check_conventions --base upstream/main, git diff --check and git merge-tree against main are all clean.

1. A deeply nested patch.toml still escapes the per-entry handling (non-blocking, maybe a follow-up)
tomllib raises RecursionError too. On all four interpreters, a = [[[…]]] fails at every depth I tried from 1,000 up. That happens in validate_entry (L158), and the per-entry except (OSError, ValueError, tomllib.TOMLDecodeError) at L213 doesn't catch it. My test catalog had two entries: entry 1 points at a broken patch file and entry 2 is the valid fixture entry.

entry 1 patch file main this PR
malformed TOML (not toml [[[) rejected: Expected '='…, entry 2 Recommended, rc 0 same
200,000-deep array traceback Patch advisor: maximum recursion depth exceeded, rc 2, entry 2 never checked

So the PR turns the traceback into a clean error, which is better. But one over-nested patch file now fails the whole run, while any other unreadable patch file only rejects its own entry. You could add RecursionError to the L213 tuple too, either here or in a follow-up. The title only mentions JSON, so I'm leaving that call to you.

2. Overlap paragraph
#12769 (StoneDC, opened 08:40Z, before this PR) rewrites the same top-level except block (print → emit). git merge-tree --write-tree pr-12769 pr-12787 reports CONFLICT (content): Merge conflict in tools/suggest_patches.py, and the bot's overlap comment lists it as well. The body says each open PR merges cleanly with this branch, so it may have used an older PR list. The conflict is a one-line keep-both, but it should be listed.

Nit
If an assertion inside the subTest loop fails, target.write_text(original, …) is skipped. The next subtest then runs with the nested file still in place. self.addCleanup or a try/finally would keep a failure from spreading to the second target.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5696228824 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12787#pullrequestreview-5696228824). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-29T10:28:56Z. **Review:** COMMENTED --- I checked head `4e6047eccf` on macOS arm64, where the body says it was not run. **What I confirmed** - `tools/tests/test_suggest_patches.py` passes 36/36 on Python 3.12.13, 3.13.13, 3.14.8 and 3.15.1. The new test fails on the `main` `suggest_patches.py` with the expected `RecursionError` (2 errors, 3.13 and 3.15). The system Python 3.9.6 can't import the module because there is no `tomllib`, and that is the same on `main`. - `check_conventions --base upstream/main`, `git diff --check` and `git merge-tree` against `main` are all clean. **1. A deeply nested `patch.toml` still escapes the per-entry handling (non-blocking, maybe a follow-up)** `tomllib` raises `RecursionError` too. On all four interpreters, `a = [[[…]]]` fails at every depth I tried from 1,000 up. That happens in `validate_entry` (L158), and the per-entry `except (OSError, ValueError, tomllib.TOMLDecodeError)` at L213 doesn't catch it. My test catalog had two entries: entry 1 points at a broken patch file and entry 2 is the valid fixture entry. | entry 1 patch file | main | this PR | |---|---|---| | malformed TOML (`not toml [[[`) | `rejected: Expected '='…`, entry 2 **Recommended**, rc 0 | same | | 200,000-deep array | traceback | `Patch advisor: maximum recursion depth exceeded`, rc 2, entry 2 never checked | So the PR turns the traceback into a clean error, which is better. But one over-nested patch file now fails the whole run, while any other unreadable patch file only rejects its own entry. You could add `RecursionError` to the L213 tuple too, either here or in a follow-up. The title only mentions JSON, so I'm leaving that call to you. **2. Overlap paragraph** #12769 (StoneDC, opened 08:40Z, before this PR) rewrites the same top-level `except` block (`print` → `emit`). `git merge-tree --write-tree pr-12769 pr-12787` reports `CONFLICT (content): Merge conflict in tools/suggest_patches.py`, and the bot's overlap comment lists it as well. The body says each open PR merges cleanly with this branch, so it may have used an older PR list. The conflict is a one-line keep-both, but it should be listed. **Nit** If an assertion inside the `subTest` loop fails, `target.write_text(original, …)` is skipped. The next subtest then runs with the nested file still in place. `self.addCleanup` or a `try/finally` would keep a failure from spreading to the second target. AI-assisted: yes
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
boykopovar/AnyPS5!12787
No description provided.