inline_scan: support UTF-8 BOM, clean up dumpbin Popen on early close, and validate --base #2273

Merged
kirigayakazima merged 2 commits from upstream-pr/2273 into main 2026-11-28 15:08:03 +00:00
kirigayakazima commented 2026-11-26 11:59:26 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kirigayakazima on 2026-11-26T11:59:26Z.


Claimed in the coordination issue as: INLINE-SCAN-BOM-DUMPBIN-CLEANUP-AND-CLI-SAFETY (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7191397017)

What and why

tools/inline_scan.py previously:

  • Opened text listings with "utf-8" instead of "utf-8-sig", causing line 1 to miss LINE matching when a file started with a UTF-8 BOM.
  • Spawned dumpbin via subprocess.Popen without encoding="utf-8" or try/finally cleanup, risking locale-dependent decoding and leaking/leaving dumpbin running if iteration stopped early.
  • Defined main() with no argv parameter, accepted negative --base values (like -0x1000), and let OSError from reading listings crash unhandled instead of exiting 2.

This uses "utf-8-sig" for listings, adds encoding="utf-8" and try/finally pipe/process cleanup in read_lines(), adds _parse_base rejecting negative hex bases, accepts argv=None in main(), handles OSError cleanly, and adds unit/CLI tests in tests/test_inline_scan.py.

Tested

  • python -I tests/test_inline_scan.py -v (17 passed)
  • Detached upstream/main baseline negative control: failed with the new tests before the fix
  • python -I tests/test_ci_registration.py, python -I tests/test_text_write_line_endings.py, python -I scripts/check_no_game_data.py, python -I -m compileall -q, and git diff --cached --check

GPUs tested: n/a

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5509622637 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2273). Originally posted by [kirigayakazima](https://github.com/kirigayakazima) on 2026-11-26T11:59:26Z. --- Claimed in the coordination issue as: INLINE-SCAN-BOM-DUMPBIN-CLEANUP-AND-CLI-SAFETY (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7191397017) ### What and why `tools/inline_scan.py` previously: - Opened text listings with `"utf-8"` instead of `"utf-8-sig"`, causing line 1 to miss `LINE` matching when a file started with a UTF-8 BOM. - Spawned `dumpbin` via `subprocess.Popen` without `encoding="utf-8"` or `try/finally` cleanup, risking locale-dependent decoding and leaking/leaving `dumpbin` running if iteration stopped early. - Defined `main()` with no `argv` parameter, accepted negative `--base` values (like `-0x1000`), and let `OSError` from reading listings crash unhandled instead of exiting 2. This uses `"utf-8-sig"` for listings, adds `encoding="utf-8"` and `try/finally` pipe/process cleanup in `read_lines()`, adds `_parse_base` rejecting negative hex bases, accepts `argv=None` in `main()`, handles `OSError` cleanly, and adds unit/CLI tests in `tests/test_inline_scan.py`. ### Tested - `python -I tests/test_inline_scan.py -v` (17 passed) - Detached `upstream/main` baseline negative control: failed with the new tests before the fix - `python -I tests/test_ci_registration.py`, `python -I tests/test_text_write_line_endings.py`, `python -I scripts/check_no_game_data.py`, `python -I -m compileall -q`, and `git diff --cached --check` GPUs tested: n/a AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed.
real-rusher commented 2026-11-26 12:23:21 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by real-rusher on 2026-11-26T12:23:21Z.


Checked on main a73a57e1 (Ubuntu 24.04 x86-64, Python 3.13, no GPU). Merges cleanly, compileall clean, but one of the new tests fails with this PR:

python -I tests/test_inline_scan.py: 17 run, FAILED (failures=1), in test_negative_base_and_oserror_return_2:

AssertionError: 'non-negative' not found in 'usage: test_inline_scan.py [-h] [--base BASE] [--sites] source
test_inline_scan.py: error: argument --base: expected one argument'

argparse only treats decimal negatives like -1 as values, so "--base", "-0x1000" is parsed as a missing argument followed by an unknown option, and the new non-negative check in _parse_base is never reached. The same happens on the command line: python -I tools/inline_scan.py x.txt --base -0x1000 prints "argument --base: expected one argument". With the test changed to inline_scan.main([path, "--base=-0x1000"]), all 17 pass, and the new tests fail on main (1 failure, 2 errors) as they should. If negative bases should be caught as written in the usage line, the check may need the --base=... form documented or a different parser setup.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7191937401 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2273#issuecomment-7191937401). Originally posted by [real-rusher](https://github.com/real-rusher) on 2026-11-26T12:23:21Z. --- Checked on main a73a57e1 (Ubuntu 24.04 x86-64, Python 3.13, no GPU). Merges cleanly, compileall clean, but **one of the new tests fails with this PR**: python -I tests/test_inline_scan.py: 17 run, FAILED (failures=1), in test_negative_base_and_oserror_return_2: AssertionError: 'non-negative' not found in 'usage: test_inline_scan.py [-h] [--base BASE] [--sites] source test_inline_scan.py: error: argument --base: expected one argument' argparse only treats decimal negatives like -1 as values, so "--base", "-0x1000" is parsed as a missing argument followed by an unknown option, and the new non-negative check in _parse_base is never reached. The same happens on the command line: python -I tools/inline_scan.py x.txt --base -0x1000 prints "argument --base: expected one argument". With the test changed to inline_scan.main([path, "--base=-0x1000"]), all 17 pass, and the new tests fail on main (1 failure, 2 errors) as they should. If negative bases should be caught as written in the usage line, the check may need the --base=... form documented or a different parser setup. AI-assisted: yes (Claude Code)
DeadlyChirp commented 2026-11-26 12:52:26 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by DeadlyChirp on 2026-11-26T12:52:26Z.


Tested on Windows 11, Python 3.12.10, with this PR merged onto current upstream main.

  • tests/test_inline_scan.py: 1 failed, 16 passed.
  • Failing test: test_negative_base_and_oserror_return_2. inline_scan.main([path, "--base", "-0x1000"]) exits 2, but from argparse itself (argument --base: expected one argument), so "non-negative" is not in stderr.
  • Cause: on Python 3.12, argparse reads only decimal numbers like -1 as negative values. -0x1000 looks like an option, so --base gets no value. This is not Windows-specific. --base=-0x1000 in the test (or a decimal value such as -4096) would reach the tool's own check.

(AI-assisted test run; output checked by hand.)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7192573216 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2273#issuecomment-7192573216). Originally posted by [DeadlyChirp](https://github.com/DeadlyChirp) on 2026-11-26T12:52:26Z. --- Tested on Windows 11, Python 3.12.10, with this PR merged onto current upstream main. - `tests/test_inline_scan.py`: 1 failed, 16 passed. - Failing test: `test_negative_base_and_oserror_return_2`. `inline_scan.main([path, "--base", "-0x1000"])` exits 2, but from argparse itself (`argument --base: expected one argument`), so `"non-negative"` is not in stderr. - Cause: on Python 3.12, argparse reads only decimal numbers like `-1` as negative values. `-0x1000` looks like an option, so `--base` gets no value. This is not Windows-specific. `--base=-0x1000` in the test (or a decimal value such as `-4096`) would reach the tool's own check. (AI-assisted test run; output checked by hand.)
tiguan26 commented 2026-11-27 04:38:41 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by tiguan26 on 2026-11-27T04:38:41Z.

Review: CHANGES_REQUESTED


Requesting one change in the new test; the code change itself looks right.

test_negative_base_and_oserror_return_2 calls inline_scan.main([path, "--base", "-0x1000"]) and fails on its own head (757ffc364) with Python 3.13: argparse only treats decimal negatives such as -5 as values, so -0x1000 is taken for an unknown option and the run stops with "argument --base: expected one argument" before _parse_base is called. The exit code is still 2, but the "non-negative" assertion fails:

AssertionError: 'non-negative' not found in 'usage: __main__.py [-h] [--base BASE] [--sites] source\n__main__.py: error: argument --base: expected one argument\n'

Writing it as "--base=-0x1000" passes the value on every Python and actually reaches the new check. The other tests pass (16 of 17 in tests/test_inline_scan.py, on main d1dff7949 plus this PR). AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6083551724 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2273#pullrequestreview-6083551724). Originally posted by [tiguan26](https://github.com/tiguan26) on 2026-11-27T04:38:41Z. **Review:** CHANGES_REQUESTED --- Requesting one change in the new test; the code change itself looks right. `test_negative_base_and_oserror_return_2` calls `inline_scan.main([path, "--base", "-0x1000"])` and fails on its own head (757ffc364) with Python 3.13: argparse only treats decimal negatives such as `-5` as values, so `-0x1000` is taken for an unknown option and the run stops with "argument --base: expected one argument" before `_parse_base` is called. The exit code is still 2, but the "non-negative" assertion fails: ``` AssertionError: 'non-negative' not found in 'usage: __main__.py [-h] [--base BASE] [--sites] source\n__main__.py: error: argument --base: expected one argument\n' ``` Writing it as `"--base=-0x1000"` passes the value on every Python and actually reaches the new check. The other tests pass (16 of 17 in `tests/test_inline_scan.py`, on `main` `d1dff7949` plus this PR). AI-assisted (Claude Code, Claude Opus 5.5).
kirigayakazima commented 2026-11-27 11:55:08 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kirigayakazima on 2026-11-27T11:55:08Z.


Thanks @tiguan26, @real-rusher, and @DeadlyChirp — fixed in the test by passing --base=-0x1000 (and also testing decimal --base -16) so argparse delivers the negative value to _parse_base. python -I tests/test_inline_scan.py -v and the 5 repository checks pass.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7217625418 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2273#issuecomment-7217625418). Originally posted by [kirigayakazima](https://github.com/kirigayakazima) on 2026-11-27T11:55:08Z. --- Thanks @tiguan26, @real-rusher, and @DeadlyChirp — fixed in the test by passing `--base=-0x1000` (and also testing decimal `--base -16`) so `argparse` delivers the negative value to `_parse_base`. `python -I tests/test_inline_scan.py -v` and the 5 repository checks pass.
tiguan26 commented 2026-11-27 18:42:28 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by tiguan26 on 2026-11-27T18:42:28Z.

Review: APPROVED


Approving now. 2de87e5ea fixes what I asked for: with --base=-0x1000 (and --base -16, which argparse accepts as a negative number) the value reaches _parse_base, so the test now checks the real "image base must be non-negative" error instead of argparse's unknown-option one.

Checked on 2de87e5ea merged onto current main (43a9d562d), no conflicts: tests/test_inline_scan.py 17 passed (2 subtests). From the command line, both tools/inline_scan.py <file> --base=-0x1000 and --base -16 print argument --base: image base must be non-negative and exit 2.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6092202972 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2273#pullrequestreview-6092202972). Originally posted by [tiguan26](https://github.com/tiguan26) on 2026-11-27T18:42:28Z. **Review:** APPROVED --- Approving now. 2de87e5ea fixes what I asked for: with `--base=-0x1000` (and `--base -16`, which argparse accepts as a negative number) the value reaches `_parse_base`, so the test now checks the real "image base must be non-negative" error instead of argparse's unknown-option one. Checked on 2de87e5ea merged onto current main (43a9d562d), no conflicts: `tests/test_inline_scan.py` 17 passed (2 subtests). From the command line, both `tools/inline_scan.py <file> --base=-0x1000` and `--base -16` print `argument --base: image base must be non-negative` and exit 2.
MoonEyes commented 2026-11-28 15:08:00 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T15:08:00Z.


Red/green: pytest tests/test_inline_scan.py fails on main (5 failed, 14 passed) and passes on head 2de87e5ea1 merged with main (17 passed, 2 subtests passed).
Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main d48a957bb in GitHub Actions: no new failure.
AI-assisted (Claude Code, Claude Opus 5.5)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7241226005 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2273#issuecomment-7241226005). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T15:08:00Z. --- Red/green: `pytest tests/test_inline_scan.py` fails on main (5 failed, 14 passed) and passes on head 2de87e5ea1e7 merged with main (17 passed, 2 subtests passed). Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main d48a957bb in GitHub Actions: no new failure. AI-assisted (Claude Code, Claude Opus 5.5)
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
ProjectLeonida/mandy!2273
No description provided.