Skip to content

ROB: Do not crash on removing objects when the resources are not a dictionary - #4329

Merged
stefan6419846 merged 2 commits into
py-pdf:mainfrom
RavSinghChandan:remove-objects-resources-not-a-dictionary
Nov 2, 2026
Merged

stefan6419846 merged 2 commits into
py-pdf:mainfrom
RavSinghChandan:remove-objects-resources-not-a-dictionary

Conversation

@RavSinghChandan

Copy link
Copy Markdown
Contributor

writer.remove_images() and writer.remove_text() raised TypeError (for example 'NumberObject' object is not subscriptable) when a page's /Resources is a number, array or string, and AttributeError: 'NumberObject' object has no attribute 'items' when its /XObject entry is a number. _remove_objects_from_page__clean_forms() looked up /Resources -> /XObject and only caught KeyError.

It now handles them like #4272 does for page.images: it logs "Resources are not a dictionary: ..." or "XObject resources are not a dictionary: ...", treats the page as having no XObject resources and still removes the inline images or the text in the content stream. A null /Resources or /XObject is treated as missing, without a warning.

Added test_remove_objects__resources_are_not_a_dictionary, covering both methods with number, array, string and null resources and number and null /XObject entries

@codecov

codecov Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.79%. Comparing base (dbe8728) to head (2ab6d05).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4329   +/-   ##
=======================================
  Coverage   98.79%   98.79%           
=======================================
  Files          59       59           
  Lines       12114    12120    +6     
  Branches     2304     2308    +4     
=======================================
+ Hits        11968    11974    +6     
  Misses         70       70           
  Partials       76       76           

☔ 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 merged commit f1e5e3c into py-pdf:main Nov 2, 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