From 6870b65bd78abc3e687667511da14a6a132f5a26 Mon Sep 17 00:00:00 2001 From: Martin Thoma Date: Tue, 19 Apr 2022 18:38:15 +0200 Subject: [PATCH] ROB: Invalid float object; use 0 as fallback (#782) Users report the following error: decimal.InvalidOperation: [] and: InvalidOperation: Invalid Literal for Decimal '0.0000-74251147' Ghostscript when it encounters this reference, treats the value as if it was "0.0". PyPDF2 will now do the same. Closes #386 Co-authored-by: James Campbell --- PyPDF2/generic.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/PyPDF2/generic.py b/PyPDF2/generic.py index 3bb8a98e06..094aa14306 100644 --- a/PyPDF2/generic.py +++ b/PyPDF2/generic.py @@ -36,6 +36,7 @@ import decimal import re import warnings +import logging from PyPDF2.constants import FilterTypes as FT from PyPDF2.constants import StreamAttributes as SA @@ -57,6 +58,7 @@ u_, ) +logger = logging.getLogger(__name__) ObjectPrefix = b_('/<[tf(n%') NumberSigns = b_('+-') IndirectPattern = re.compile(b_(r"[+-]?(\d+)\s+(\d+)\s+R[^a-zA-Z]")) @@ -237,7 +239,13 @@ def __new__(cls, value="0", context=None): try: return decimal.Decimal.__new__(cls, utils.str_(value), context) except Exception: - return decimal.Decimal.__new__(cls, str(value)) + try: + return decimal.Decimal.__new__(cls, str(value)) + except decimal.InvalidOperation: + # If this isn't a valid decimal (happens in malformed PDFs) + # fallback to 0 + logger.warning("Invalid FloatObject {}".format(value)) + return decimal.Decimal.__new__(cls, "0") def __repr__(self): if self == self.to_integral():