Skip to content

ROB: Do not crash on images when the resources are not a dictionary - #4272

Merged
stefan6419846 merged 3 commits into
py-pdf:mainfrom
RavSinghChandan:resources-not-a-dictionary-images
Oct 31, 2026
Merged

stefan6419846 merged 3 commits into
py-pdf:mainfrom
RavSinghChandan:resources-not-a-dictionary-images

Conversation

@RavSinghChandan

Copy link
Copy Markdown
Contributor

page.images raised TypeError: argument of type 'NumberObject' is not iterable when a page's /Resources is not a dictionary, from the /XObject membership test in _get_ids_image(). #4030 made a malformed /XObject entry warn and return no images; this does the same one level up.

_get_ids_image() now logs "Resources are not a dictionary: ..." and treats the page as having no XObject resources, so inline images are still listed. _get_image() also caught only KeyError when looking up /XObject, so reading such an inline image raised the same TypeError; it now catches TypeError too. Added test_images__resources_are_not_a_dictionary (number, array and string cases, as in test_images__xobject_is_not_a_dictionary) and a test that the inline image is still returned.

@codecov

codecov Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.65%. Comparing base (d6584db) to head (7dc2901).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4272   +/-   ##
=======================================
  Coverage   98.65%   98.65%           
=======================================
  Files          59       59           
  Lines       11957    11962    +5     
  Branches     2267     2270    +3     
=======================================
+ Hits        11796    11801    +5     
  Misses         84       84           
  Partials       77       77           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@stefan6419846 stefan6419846 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please review the merge conflict.

Comment thread pypdf/_page.py Resolved
Check the resources type in _get_image instead of catching TypeError.
@RavSinghChandan

Copy link
Copy Markdown
Contributor Author

Only when an inline image (0) is requested and /Resources is not a dictionary: obj[PG.RESOURCES][RES.XOBJECT] then subscripts a number, array or string. Catching it was too broad, so I replaced the try/except with an explicit isinstance check on the resources. The KeyError for non-inline images is unchanged.

@RavSinghChandan

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I have resolved the merge conflict.

@stefan6419846
stefan6419846 merged commit c55cde4 into py-pdf:main Oct 31, 2026
20 checks passed
AP24110011695 added a commit to AP24110011695/pypdf that referenced this pull request Nov 4, 2026
_get_fonts_walk and the inline-image color-space lookup checked the
presence of /Resources but not its type, raising TypeError when the
value is not a dictionary. The same class was fixed for images (py-pdf#4272)
and for removing objects (py-pdf#4329); these two siblings follow the same
pattern: warn and treat as an empty dictionary, keeping the intended
PdfReadError for unresolvable color-space names.
AP24110011695 added a commit to AP24110011695/pypdf that referenced this pull request Nov 9, 2026
_get_fonts_walk and the inline-image color-space lookup checked the
presence of /Resources but not its type, raising TypeError when the
value is not a dictionary. The same class was fixed for images (py-pdf#4272)
and for removing objects (py-pdf#4329); these two siblings follow the same
pattern: warn and treat as an empty dictionary, keeping the intended
PdfReadError for unresolvable color-space names.
stefan6419846 pushed a commit that referenced this pull request Nov 10, 2026
* ROB: Guard the remaining non-dictionary /Resources accesses

_get_fonts_walk and the inline-image color-space lookup checked the
presence of /Resources but not its type, raising TypeError when the
value is not a dictionary. The same class was fixed for images (#4272)
and for removing objects (#4329); these two siblings follow the same
pattern: warn and treat as an empty dictionary, keeping the intended
PdfReadError for unresolvable color-space names.

* TST: Cover the resolution and null branches of the non-dictionary /Resources guards

The inline-image color-space lookup was only tested on its short-circuit
path (empty resources): the membership check and the return of the
resolved color space had no coverage. /Resources that resolve to null
skip the warning by design; pin that too.

* fix: end test files with a single newline (pre-commit end-of-file hook)

Signed-off-by: Ayush Kumar Saha <[email protected]>

* MAINT: Apply the review suggestions

Walrus operator for the color-space lookup, house test naming,
anchored regex match, and a trimmed docstring.

---------

Signed-off-by: Ayush Kumar Saha <[email protected]>
Co-authored-by: Ayush Kumar Saha <[email protected]>
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.

2 participants