Repository navigation
BUG: Fix AttributeError for /XRefStm in a cross-reference stream - #4451
Merged
stefan6419846 merged 2 commits intoNov 20, 2026
Merged
Conversation
PdfReader could not open a file whose cross-reference stream dictionary contains /XRefStm. _process_xref_stream used self.stream, which is only assigned after read() has returned. Use the stream that is being read, as the code did before py-pdf#2847. Co-authored-by: Claude <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4451 +/- ##
==========================================
+ Coverage 99.21% 99.25% +0.04%
==========================================
Files 59 59
Lines 12425 12425
Branches 2368 2368
==========================================
+ Hits 12328 12333 +5
+ Misses 42 38 -4
+ Partials 55 54 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
stefan6419846
approved these changes
Nov 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PdfReadercannot open a PDF whose cross-reference stream dictionary has an/XRefStmentry:_process_xref_streamreadsself.stream, but_initialize_streamonly sets that attribute afterread()has returned, so it does not exist yet when the method runs. Before #2847 this code was inline and used the localstream. This PR passes the stream into the method again.I found this while running pypdf over PDFs from the Common Crawl PDF corpus (CC-MAIN-2021-31-PDF-UNTRUNCATED). The file is
3472143.pdfin archive3472.zip, SHA-256700cb76ee7148438cb2860ed27be9f86935afa5ff9be8ca8bd7c8b7bd89058eb. It was written by Word 2010 as a hybrid-reference file and later updated by another tool, which copied/XRefStminto its cross-reference stream. poppler and PDFium open it. pypdf 5.0.0 opens it, 5.0.1 and later do not. With this change it opens again and has 2 pages.The test builds a small PDF where the page object is only listed in the cross-reference stream that
/XRefStmpoints to. It fails onmainand passes with the change.I can attach the file to an issue if you would like a test with the real file.
AI disclosure: I used Claude Code (Claude Fable 5.2) for the corpus run, the diagnosis, the fix and the test. I reviewed the diff and ran the tests myself.