Skip to content

Commit

Permalink
ROB: Invalid float object; use 0 as fallback (py-pdf#782)
Browse files Browse the repository at this point in the history
Users report the following error:
    decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]
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 py-pdf#386

Co-authored-by: James Campbell <[email protected]>
  • Loading branch information
2 people authored and VictorCarlquist committed Apr 29, 2022
1 parent 574e2e7 commit 6870b65
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion PyPDF2/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]"))
Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit 6870b65

Please sign in to comment.