Skip to content

Cache page lists only after loading succeeds - #1417

Open
FanWu-ai wants to merge 1 commit into
jsvine:developfrom
FanWu-ai:fix/cache-complete-page-lists
Open

FanWu-ai wants to merge 1 commit into
jsvine:developfrom
FanWu-ai:fix/cache-complete-page-lists

Conversation

@FanWu-ai

Copy link
Copy Markdown

PDF.pages assigns its cache before page iteration/construction finishes. If loading a later page raises, the next access returns the incomplete cached list without reporting the error. This can silently turn a failed document load into an empty or truncated document.

Build the page list locally and publish the cache only after loading succeeds. Successful loading, page selection and cache reuse are unchanged.

Ten regressions fail against the original source and pass with the fix:

  • Eight cases cover errors in page iteration and construction, on the first and second pages, with all pages or a selected page. They check repeated errors, recovery after the underlying failure is removed, and caching after successful recovery.
  • Two existing OSS-Fuzz PDF fixtures reproduce the real malformed-bounding-box case: repeated accesses must consistently raise MalformedPDFException, not return an empty list after the first failure.

This does not attempt to repair malformed PDFs; it prevents failed loads from being cached as successful results. Changelog and contributor credit are included as requested by CONTRIBUTING.md.

Validation on Linux, Python 3.12.14:

  • Original full make tests: 173 passed, including Ghostscript repair tests and HTML coverage generation.
  • Original source plus the new regressions: all 10 new cases fail.
  • Final full make tests: 183 passed, no skips, including coverage HTML generation.
  • Original and final make lint: Flake8, strict mypy, Black and isort passed.
  • Original and final wheel/sdist builds, strict Twine checks and the CI-style setup.py build/sdist command passed.

The test command retains the project's -n auto with PYTEST_XDIST_AUTO_NUM_WORKERS=2 to bound concurrency; no tests are excluded. Other Python/platform combinations and hosted checks are not claimed as tested locally.

Prepared and tested with OpenAI AI assistance.

@FanWu-ai
FanWu-ai marked this pull request as ready for review October 21, 2026 22:21
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.

1 participant