Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ROB: Raise PdfReadError when missing /Root in trailer. #2808

Merged
merged 2 commits into from
Aug 23, 2024
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
5 changes: 4 additions & 1 deletion pypdf/_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,10 @@ def close(self) -> None:
@property
def root_object(self) -> DictionaryObject:
"""Provide access to "/Root". Standardized with PdfWriter."""
return cast(DictionaryObject, self.trailer[TK.ROOT].get_object())
root = self.trailer[TK.ROOT]
if root is None:
raise PdfReadError('Cannot find "/Root" key in trailer')
return cast(DictionaryObject, root.get_object())

@property
def _info(self) -> Optional[DictionaryObject]:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,9 +607,9 @@ def test_read_unknown_zero_pages(caplog):
"startxref on same line as offset",
]
assert normalize_warnings(caplog.text) == warnings
with pytest.raises(AttributeError) as exc:
with pytest.raises(PdfReadError) as exc:
len(reader.pages)
assert exc.value.args[0] == "'NoneType' object has no attribute 'get_object'"
assert exc.value.args[0] == 'Cannot find "/Root" key in trailer'


def test_read_encrypted_without_decryption():
Expand Down
Loading