diff --git a/pypdf/_reader.py b/pypdf/_reader.py index 1ffcd436d..1452661a5 100644 --- a/pypdf/_reader.py +++ b/pypdf/_reader.py @@ -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]: diff --git a/tests/test_reader.py b/tests/test_reader.py index 0413a9135..d2394f95d 100644 --- a/tests/test_reader.py +++ b/tests/test_reader.py @@ -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():