Skip to content

Commit fc74fe8

Browse files
xmo-odoojuwu-odoo
andcommittedJan 10, 2025
[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 closes #193143 X-original-commit: 1fb35ad Signed-off-by: Xavier Morel (xmo) <xmo@odoo.com> Co-authored-by: Junqi <juwu@odoo.com>
1 parent ed69289 commit fc74fe8

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed
 

‎odoo/tools/pdf/_pypdf.py

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Dict, Any
2+
13
import pypdf
24
from pypdf import errors, filters, generic, PdfReader as _Reader, PdfWriter as _Writer
35
from pypdf.generic import create_string_object
@@ -45,6 +47,11 @@ def getFormTextFields(self):
4547

4648

4749
class PdfWriter(_Writer):
50+
def add_metadata(self, infos: Dict[str, Any]) -> None:
51+
if hasattr(self, '_info') and self._info is None:
52+
self._info = generic.DictionaryObject()
53+
super().add_metadata(infos)
54+
4855
def getPage(self, pageNumber):
4956
return self.pages[pageNumber]
5057

0 commit comments

Comments
 (0)