odoo/odoo#193129

Created by fw-bot
Merged at 1d53bc8612229fd0422df2f28c5db4125d6b65b5

Statuses:

label
odoo-dev:saas-17.2-17.0-pypdf-5.1-inconsistent-metadata-xmo-O2th-fw
head
3fc36ab5ffb040c4ced6afae71b729ee286aea99
merged
5 months ago by Framework (ORM), Xavier Morel (xmo)
odoo/odoo
17.0 #193086
saas-17.2 #193129
saas-17.4 #193143
18.0 #193155
saas-18.1 #193158
saas-18.2
saas-18.3
master #193159

[FW][FIX] core: inconsistent pypdf metadata in 5.1

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 #185673

Description of the issue/feature this PR addresses:

Current behavior before PR:

Desired behavior after PR is merged:


I confirm I have signed the CLA and read the PR guidelines at www.odoo.com/submit-pr

Forward-Port-Of: #193086