Skip to content

Commit

Permalink
Fixing test_extra_test_iss1541
Browse files Browse the repository at this point in the history
  • Loading branch information
Cimon Lucas (LCM) committed Aug 20, 2023
1 parent c7ca7a6 commit f9b05a6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pypdf/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2194,7 +2194,7 @@ def clean(content: ContentStream) -> None:
i += 1
else:
i += 1
content.get_data() # this ensures ._data is rebuilt
content.get_data() # this ensures ._data is rebuilt from the .operations

try:
d = cast(dict, cast(DictionaryObject, page["/Resources"])["/XObject"])
Expand Down
24 changes: 24 additions & 0 deletions pypdf/generic/_data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,22 @@ def set_data(self, data: bytes) -> None: # deprecated


class ContentStream(DecodedStreamObject):
"""
In order to be fast, this datastructure can contain either:
* raw data in ._data
* parsed stream operations in ._operations
At any time, ContentStream object can either have one or both of those fields defined,
and zero or one of those fields set to None.
Those fields are "rebuilt" lazily, when accessed:
* when .get_data() is called, if ._data is None, it is rebuilt from ._operations
* when .operations is called, if ._operations is None, it is rebuilt from ._data
On the other side, those fields can be invalidated:
* when .set_data() is called, ._operations is set to None
* when .operations is set, ._data is set to None
"""
def __init__(
self,
stream: Any,
Expand Down Expand Up @@ -1211,6 +1227,14 @@ def operations(self, operations: List[Tuple[Any, Any]]) -> None:
self._operations = operations
self._data = b""

# This overrides the parent method:
def write_to_stream(
self, stream: StreamType, encryption_key: Union[None, str, bytes] = None
) -> None:
if not self._data and self._operations:
self.get_data() # this ensures ._data is rebuilt for ContentStream
super().write_to_stream(stream, encryption_key)


def read_object(
stream: StreamType,
Expand Down

0 comments on commit f9b05a6

Please sign in to comment.