Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions pypdf/_doc_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
IndirectObject,
NameObject,
NullObject,
NumberObject,
PdfObject,
TextStringObject,
TreeObject,
Expand Down Expand Up @@ -1007,24 +1006,18 @@ def get_destination_page_number(self, destination: Destination) -> Optional[int]
def _build_destination(
self,
title: Union[str, bytes],
array: Optional[
list[
Union[NumberObject, IndirectObject, NullObject, DictionaryObject, None]
]
],
array: Optional[ArrayObject],
) -> Destination:
page, typ = None, None
# handle outline items with missing or invalid destination
if (
isinstance(array, (NullObject, str))
or (isinstance(array, ArrayObject) and len(array) < 2)
or array is None
):
# A valid destination is an array of at least a page and a fit type.
# Anything else (a name, a bare number, a NullObject, None, ...) cannot
# be unpacked below, so treat it as a missing destination.
if not isinstance(array, ArrayObject) or len(array) < 2:
page = NullObject()
return Destination(title, page, Fit.fit())
page, typ, *array = array # type: ignore[assignment]
try:
return Destination(title, page, Fit(fit_type=typ, fit_args=array)) # type: ignore[arg-type]
return Destination(title, page, Fit(fit_type=typ, fit_args=array))
except PdfReadError:
logger_warning("Unknown destination: %(title)r %(array)s", source=__name__, title=title, array=array)
if self.strict:
Expand Down
39 changes: 36 additions & 3 deletions tests/test_doc_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,21 +194,54 @@ def test_viewer_preferences__indirect_reference():


def test_build_destination__short_array():
"""A one-element array degrades to a null destination instead of raising on
the unpacking; a valid array with extra fit args still builds.
"""
writer = PdfWriter()
writer.add_blank_page(width=72, height=72)

# An array too short to hold a page reference and a fit type degrades to a
# null destination instead of raising on the unpacking.
dest = writer._build_destination("title", ArrayObject([NumberObject(0)]))
assert isinstance(dest["/Page"], NullObject)

# A trailing fit type with the wrong number of arguments still builds.
dest = writer._build_destination(
"title", ArrayObject([NumberObject(0), NameObject("/FitR"), NumberObject(1), NumberObject(2)])
)
assert dest["/Type"] == "/FitR"


def test_build_destination__non_array():
"""A non-array destination degrades to a null destination rather than
raising TypeError while unpacking.
"""
writer = PdfWriter()
writer.add_blank_page(width=72, height=72)

dest = writer._build_destination("title", NumberObject(5))
assert isinstance(dest["/Page"], NullObject)


def test_named_destinations__non_array_value():
"""A bare number where a destination array is expected is skipped instead
of crashing named_destinations with a TypeError.
"""
writer = PdfWriter()
writer.add_blank_page(width=72, height=72)
names = DictionaryObject()
names[NameObject("/Names")] = ArrayObject(
[TextStringObject("foo"), NumberObject(5)]
)
dests = DictionaryObject()
dests[NameObject("/Dests")] = names
writer.root_object[NameObject("/Names")] = dests

stream = BytesIO()
writer.write(stream)
stream.seek(0)
reader = PdfReader(stream)

assert "foo" in reader.named_destinations


def _reader_with_button_field(field: DictionaryObject) -> PdfReader:
writer = PdfWriter()
writer.add_blank_page(width=72, height=72)
Expand Down
Loading