Skip to content

Commit

Permalink
BUG: getcontents() shall return None if contents is NullObject
Browse files Browse the repository at this point in the history
  • Loading branch information
pubpub-zz committed Sep 6, 2023
1 parent 05f2a65 commit c84777b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pypdf/_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,11 @@ def get_contents(self) -> Optional[ContentStream]:
pdf = cast(IndirectObject, self.indirect_reference).pdf
except AttributeError:
pdf = None
return ContentStream(self[PG.CONTENTS].get_object(), pdf)
obj = self[PG.CONTENTS].get_object()
if isinstance(obj, NullObject):
return None
else:
return ContentStream(obj, pdf)
else:
return None

Expand Down
8 changes: 8 additions & 0 deletions tests/test_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -1225,3 +1225,11 @@ def create_stamp_pdf() -> BytesIO:
assert isinstance(
writer._objects[contents.indirect_reference.idnum - 1], NullObject
)


def test_get_contents_from_nullobject():
"""Issue #2157"""
writer = PdfWriter()
p = writer.add_blank_page(100, 100)
p[NameObject("/Contents")] = writer._add_object(NullObject())
p.get_contents()

0 comments on commit c84777b

Please sign in to comment.