[PR #1567] ptr_truncation_census: guard output paths/overwrites, handle scan/listing OSError, and write JSON atomically (PTR-TRUNCATION-CENSUS-GUARDS-AND-IO) #1591

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

📋 Pull Request Information

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

Base: main ← Head: fix/ptr-truncation-census-guards-and-io


📝 Commits (1)

  • 9dcc3e7 ptr_truncation_census: guard output overwrites/paths, catch OSError in scan_one/listings, and write JSON atomically

📊 Changes

2 files changed (+163 additions, -10 deletions)

View changed files

📝 tests/test_ptr_truncation_census.py (+86 -0)
📝 tools/ptr_truncation_census.py (+77 -10)

📄 Description

Item ID: PTR-TRUNCATION-CENSUS-GUARDS-AND-IO (new item; coordination issue #14 is at GitHub's 2,500-comment cap with no open successor at PR time — confirmed 0 open PRs touch tools/ptr_truncation_census.py)

Summary

  • Validate --json and --index-out before scanning: reject empty/whitespace paths (previously "" was silently ignored and " " created a literal " " file in cwd), existing directories, missing parent directories, and any output path that aliases src, a scanned .s listing, <root>/index.json, <root>/<name>.dxbc, or adjacent native_paths (.fssm/.code/.json).
  • Write --json and --index-out atomically: stage via NamedTemporaryFile + file_mode.published_mode + replace_retry.replace, enforce allow_nan=False and trailing LF \n, and clean up temp files on failure so existing outputs are never truncated.
  • Handle OSError cleanly in scan_one() and main(): record unreadable listings as cannot read listing: <ExcType> (never leaking the filename or path into aggregate counts) instead of aborting the pool, and exit 2 cleanly when listings(src) raises OSError.
  • Deterministic tie-breaking in summarise()["not_reached"]: sort by (-count, error) instead of insertion-order-dependent Counter.most_common().

Testing

  • AI-assisted: yes
  • GPUs tested: none (CPU/Python stdlib tool; no GPU, no game files)
  • python3 -W error -I tests/test_ptr_truncation_census.py -v: 16/16 passed
  • python3 -W error -I tests/test_text_write_line_endings.py -v: 3/3 passed
  • python3 -I scripts/check_no_game_data.py tools/ptr_truncation_census.py tests/test_ptr_truncation_census.py: passed
  • python3 -m compileall -q make_game.py tools tests: passed
  • Mutation check against unpatched main:tools/ptr_truncation_census.py: all 5 new/updated tests fail (FAILED (failures=19, errors=2)).

🔄 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/1567 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/ptr-truncation-census-guards-and-io` --- ### 📝 Commits (1) - [`9dcc3e7`](https://github.com/ubiquitarianism/leonida/commit/9dcc3e75dce29b5a8751ee03d9f7cc6df9f43968) ptr_truncation_census: guard output overwrites/paths, catch OSError in scan_one/listings, and write JSON atomically ### 📊 Changes **2 files changed** (+163 additions, -10 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_ptr_truncation_census.py` (+86 -0) 📝 `tools/ptr_truncation_census.py` (+77 -10) </details> ### 📄 Description Item ID: `PTR-TRUNCATION-CENSUS-GUARDS-AND-IO` (new item; coordination issue #14 is at GitHub's 2,500-comment cap with no open successor at PR time — confirmed 0 open PRs touch `tools/ptr_truncation_census.py`) ## Summary - **Validate `--json` and `--index-out` before scanning**: reject empty/whitespace paths (previously `""` was silently ignored and `" "` created a literal `" "` file in cwd), existing directories, missing parent directories, and any output path that aliases `src`, a scanned `.s` listing, `<root>/index.json`, `<root>/<name>.dxbc`, or adjacent `native_paths` (`.fssm`/`.code`/`.json`). - **Write `--json` and `--index-out` atomically**: stage via `NamedTemporaryFile` + `file_mode.published_mode` + `replace_retry.replace`, enforce `allow_nan=False` and trailing LF `\n`, and clean up temp files on failure so existing outputs are never truncated. - **Handle `OSError` cleanly in `scan_one()` and `main()`**: record unreadable listings as `cannot read listing: <ExcType>` (never leaking the filename or path into aggregate counts) instead of aborting the pool, and exit 2 cleanly when `listings(src)` raises `OSError`. - **Deterministic tie-breaking in `summarise()["not_reached"]`**: sort by `(-count, error)` instead of insertion-order-dependent `Counter.most_common()`. ## Testing - AI-assisted: yes - GPUs tested: none (CPU/Python stdlib tool; no GPU, no game files) - `python3 -W error -I tests/test_ptr_truncation_census.py -v`: **16/16 passed** - `python3 -W error -I tests/test_text_write_line_endings.py -v`: **3/3 passed** - `python3 -I scripts/check_no_game_data.py tools/ptr_truncation_census.py tests/test_ptr_truncation_census.py`: passed - `python3 -m compileall -q make_game.py tools tests`: passed - Mutation check against unpatched `main:tools/ptr_truncation_census.py`: all 5 new/updated tests fail (`FAILED (failures=19, errors=2)`). --- <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#1591
No description provided.