Skip to content

Commit

Permalink
Fixing test_iss1601
Browse files Browse the repository at this point in the history
  • Loading branch information
Cimon Lucas (LCM) committed Aug 20, 2023
1 parent f9b05a6 commit 1688837
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
3 changes: 2 additions & 1 deletion pypdf/generic/_data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,7 @@ class ContentStream(DecodedStreamObject):
* 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 @@ -1232,7 +1233,7 @@ 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
self.get_data() # this ensures ._data is rebuilt
super().write_to_stream(stream, encryption_key)


Expand Down
17 changes: 17 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,20 @@ def get_object(self) -> "DummyObj":

def get_reference(self, obj):
return IndirectObject(idnum=1, generation=1, pdf=self)


def is_sublist(child_list, parent_list):
"""
Check if child_list is a sublist of parent_list, with respect to
* elements order
* elements repetition
Elements are compared using `==`
"""
if len(child_list) == 0:
return True
if len(parent_list) == 0:
return False
if parent_list[0] == child_list[0]:
return is_sublist(child_list[1:], parent_list[1:])
return is_sublist(child_list, parent_list[1:])
20 changes: 20 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
)
from pypdf.errors import DeprecationError, PdfReadError, PdfStreamError

from . import is_sublist

TESTS_ROOT = Path(__file__).parent.resolve()
PROJECT_ROOT = TESTS_ROOT.parent
RESOURCE_ROOT = PROJECT_ROOT / "resources"
Expand Down Expand Up @@ -351,3 +353,21 @@ def test_parse_datetime_err():
parse_iso8824_date("D:20210408T054711Z")
assert ex.value.args[0] == "Can not convert date: D:20210408T054711Z"
assert parse_iso8824_date("D:20210408054711").tzinfo is None


def test_is_sublist():
# Basic checks:
assert is_sublist([0, 1], [0, 1, 2]) is True
assert is_sublist([0, 2], [0, 1, 2]) is True
assert is_sublist([1, 2], [0, 1, 2]) is True
assert is_sublist([0, 3], [0, 1, 2]) is False
# Ensure order is checked:
assert is_sublist([1, 0], [0, 1, 2]) is False
# Ensure duplicates are handled:
assert is_sublist([0, 1, 1], [0, 1, 1, 2]) is True
assert is_sublist([0, 1, 1], [0, 1, 2]) is False
# Edge cases with empty lists:
assert is_sublist([], [0, 1, 2]) is True
assert is_sublist([0, 1], []) is False
# Self-sublist edge case:
assert is_sublist([0, 1, 2], [0, 1, 2]) is True
17 changes: 7 additions & 10 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
TextStringObject,
)

from . import get_data_from_url
from . import get_data_from_url, is_sublist

TESTS_ROOT = Path(__file__).parent.resolve()
PROJECT_ROOT = TESTS_ROOT.parent
Expand Down Expand Up @@ -1238,23 +1238,20 @@ def test_iss1601():
url = "https://github.com/py-pdf/pypdf/files/10579503/badges-38.pdf"
name = "badge-38.pdf"
reader = PdfReader(BytesIO(get_data_from_url(url, name=name)))
original_cs_operations = ContentStream(reader.pages[0].get_contents(), reader).operations
writer = PdfWriter()
page_1 = writer.add_blank_page(
reader.pages[0].mediabox[2], reader.pages[0].mediabox[3]
)
page_1.merge_transformed_page(reader.pages[0], Transformation())
assert (
ContentStream(reader.pages[0].get_contents(), reader).get_data()
in page_1.get_contents().get_data()
)
page_1_cs_operations = page_1.get_contents().operations
assert is_sublist(original_cs_operations, page_1_cs_operations)
page_1 = writer.add_blank_page(
reader.pages[0].mediabox[2], reader.pages[0].mediabox[3]
)
page_1.merge_page(reader.pages[0])
assert (
ContentStream(reader.pages[0].get_contents(), reader).get_data()
in page_1.get_contents().get_data()
)
page_1_cs_operations = page_1.get_contents().operations
assert is_sublist(original_cs_operations, page_1_cs_operations)


def test_attachments():
Expand Down Expand Up @@ -1539,7 +1536,7 @@ def test_watermark():


@pytest.mark.enable_socket()
@pytest.mark.timeout(2) # this was a lot slower before PR #2086
@pytest.mark.timeout(4) # this was a lot slower before PR #2086
def test_watermarking_speed():
url = "https://github.com/py-pdf/pypdf/files/11985889/bg.pdf"
name = "bgwatermark.pdf"
Expand Down

0 comments on commit 1688837

Please sign in to comment.