Repository navigation
ROB: Skip a font resource whose reference resolves to nothing - #4481
Krushil-Dhankecha wants to merge 1 commit into
Conversation
PageObject._layout_mode_fonts() called get_object() on every entry of the /Font resources. A reference to an object the file does not hold resolves to None, so extract_text(extraction_mode="layout") raised AttributeError instead of skipping the font, while the plain mode already tolerated it. tika-957721.pdf fails this way on 92 of its 104 pages; its page 8 is the one test_extract_text__none_objects already covers for plain extraction.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4481 +/- ##
=======================================
Coverage 99.21% 99.21%
=======================================
Files 59 59
Lines 12425 12427 +2
Branches 2368 2369 +1
=======================================
+ Hits 12328 12330 +2
Misses 42 42
Partials 55 55 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| for font_name in font_resources: | ||
| font_object = font_resources[font_name].get_object() | ||
| # A dangling reference resolves to None, which has no get_object(). | ||
| font_object: PdfObject | None = font_resources[font_name] |
There was a problem hiding this comment.
The original call to get_object() was already redundant, because accessing a DictionaryObject by key will always return a resolved object. It will by definition return a PdfObject and never None.
| font_object = font_resources[font_name].get_object() | ||
| # A dangling reference resolves to None, which has no get_object(). | ||
| font_object: PdfObject | None = font_resources[font_name] | ||
| if font_object is not None: |
There was a problem hiding this comment.
... so this if branch can be removed.
|
Thanks for the PR! I sort of was expecting this one :-) I left two comments, but other than that the fix seems OK to me. Two questions though. Two: I can't help wondering what would happen if we'd add a default font instead of skipping the font entirely, by calling Hope this helps! |
Closes #4480.
_layout_mode_fonts()read each entry of the/Fontresources asfont_resources[font_name].get_object().DictionaryObject.__getitem__resolves thereference already, and
PdfReader.get_object()answersNonefor an object number thefile does not hold, so the second call landed on
Noneandextract_text(extraction_mode="layout")raisedAttributeError. Theisinstancecheck onthe next line shows such an entry was meant to be skipped, and
_extract_text()does skipit, because it runs the same lookup inside
except (AttributeError, TypeError). Plain andlayout extraction therefore disagreed on the same page.
The lookup now resolves only what it can resolve, and a reference to nothing falls through
to the existing
isinstancecheck.Two tests, both failing on the parent commit:
test_extract_text__none_objectsgains the layout-mode call on page 8 oftika-957721.pdf. That test exists for this file's unresolvable objects and alreadycovers plain extraction of the same page; 92 of the file's 104 pages raise without this
change.
test_page_object__layout_mode_fonts__dangling_font_referencebuilds the same shapeoffline, pointing
crazyones.pdf's font resources at an object number that is not in thefile.
AI use, as asked for in CONTRIBUTING: I used Claude Code (Claude Opus 5) while writing this change. The diff and the reasoning behind it are mine and I have checked both.