Skip to content

Commit

Permalink
[FIX] core: inconsistent pypdf metadata in 5.1
Browse files Browse the repository at this point in the history
Even if 5.1 is not technically supported, debuntu might switch to it
any moment and we don't know if / when upstream will fix it, so it's
not worth the time bomb.

While technically it was always typed such, since py-pdf/pypdf#2820 it
looks like `_info` is a lot more likely to be `None` as
e.g. `clone_reader_document_root` now starts with unsetting
`_info_obj` and never re-sets it.

Except `add_metadata` was not updated to handle this case, likely
because mypy interprets `assert isinstance(self._info,
DictionaryObject)` as a type narrowing and trusts the developer, thus
does not report the type mismatch... and the assertion ends up blowing
in the user's face at runtime with a simple

    >>> r = pypdf.PdfReader(some_pdf_document)
    >>> w = pypdf.PdfWriter()
    >>> w.clone_reader_document_root(r)
    >>> w.add_metadata({"/foo": "bar"})
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "lib/python3.12/site-packages/pypdf/_writer.py", line 1622, in add_metadata
        assert isinstance(self._info, DictionaryObject)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    AssertionError

This is rather inconsiderate, so monkeypatch `add_metadata` to handle
the case where `_info` exists and is `None`.

Most of the credit goes to juwu for uncovering the issue.

opw-4372052
opw-4426881
Fixes odoo#185673

X-original-commit: 1fb35ad
Co-authored-by: Junqi <juwu@odoo.com>
  • Loading branch information
xmo-odoo and juwu-odoo committed Jan 9, 2025
1 parent f60fbd8 commit 0a61ee2
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions odoo/tools/pdf/_pypdf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Dict, Any

import pypdf
from pypdf import errors, filters, generic, PdfReader as _Reader, PdfWriter as _Writer
from pypdf.generic import create_string_object
Expand Down Expand Up @@ -45,6 +47,11 @@ def getFormTextFields(self):


class PdfWriter(_Writer):
def add_metadata(self, infos: Dict[str, Any]) -> None:
if hasattr(self, '_info') and self._info is None:
self._info = generic.DictionaryObject()
super().add_metadata(infos)

def getPage(self, pageNumber):
return self.pages[pageNumber]

Expand Down

0 comments on commit 0a61ee2

Please # to comment.