From d71e50e83d95892b111cded61eea380deb83901b Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Wed, 18 Nov 2020 11:52:17 -0800 Subject: [PATCH] Fix "readLinearizationData for file that is not linearized" pikepdf 2.1.0 throws wrong type of exception in this case, so special-case it. Closes #680 Closes #681 --- src/ocrmypdf/_sync.py | 7 +------ src/ocrmypdf/helpers.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/ocrmypdf/_sync.py b/src/ocrmypdf/_sync.py index 2525fe05..a24c05e7 100644 --- a/src/ocrmypdf/_sync.py +++ b/src/ocrmypdf/_sync.py @@ -55,6 +55,7 @@ from ocrmypdf._validation import ( ) from ocrmypdf.exceptions import ExitCode, ExitCodeException from ocrmypdf.helpers import ( + NeverRaise, available_cpu_count, check_pdf, pikepdf_enable_mmap, @@ -301,12 +302,6 @@ def exec_concurrent(context: PdfContext): copy_final(pdf, options.output_file, context) -class NeverRaise(Exception): - """An exception that is never raised""" - - pass # pylint: disable=unnecessary-pass - - def configure_debug_logging(log_filename, prefix: str = ''): """ Create a debug log file at a specified location. diff --git a/src/ocrmypdf/helpers.py b/src/ocrmypdf/helpers.py index 42d5e725..b26e4021 100644 --- a/src/ocrmypdf/helpers.py +++ b/src/ocrmypdf/helpers.py @@ -58,6 +58,10 @@ class Resolution(namedtuple('Resolution', ('x', 'y'))): return f"Resolution({self.x}x{self.y} dpi)" +class NeverRaise(Exception): + """An exception that is never raised""" + + def safe_symlink(input_file: os.PathLike, soft_link_name: os.PathLike): """ Helper function: relinks soft symbolic link if necessary @@ -191,6 +195,12 @@ def check_pdf(input_file: Path) -> bool: pdf.check_linearization(sio) except RuntimeError: pass + except ( + getattr(pikepdf, 'ForeignObjectError') + if pikepdf.__version__ == '2.1.0' # This version may throw wrong exception + else NeverRaise + ): + pass else: linearize = sio.getvalue() if linearize: