From d18efcbbf13204c4b54654928efed26a901ec5e8 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Sun, 7 Dec 2025 14:32:05 -0800 Subject: [PATCH] fix: support flexible type inputs for pages and unpaper_args Co-authored-by: aider (openrouter/anthropic/claude-sonnet-4) --- src/ocrmypdf/_jobcontext.py | 15 ++++++++++++--- src/ocrmypdf/_options.py | 4 ++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/ocrmypdf/_jobcontext.py b/src/ocrmypdf/_jobcontext.py index 798650ff..d3df8c9b 100644 --- a/src/ocrmypdf/_jobcontext.py +++ b/src/ocrmypdf/_jobcontext.py @@ -91,7 +91,11 @@ class PageContext: def __init__(self, pdf_context: PdfContext, pageno): self.work_folder = pdf_context.work_folder self.origin = pdf_context.origin - self.options = pdf_context.options + # Always use the legacy options for PageContext to ensure pickling works + if hasattr(pdf_context.options, 'to_namespace'): + self.options = pdf_context.options.to_namespace() + else: + self.options = pdf_context.options self.pageno = pageno self.pageinfo = pdf_context.pdfinfo[pageno] self.plugin_manager = pdf_context.plugin_manager @@ -107,8 +111,13 @@ class PageContext: def __getstate__(self): state = self.__dict__.copy() - state['options'] = copy(self.options) - # Handle both OCROptions and Namespace + # Convert OCROptions to Namespace for pickling compatibility + if hasattr(state['options'], 'to_namespace'): + state['options'] = state['options'].to_namespace() + else: + state['options'] = copy(state['options']) + + # Handle stream inputs if hasattr(state['options'], 'input_file'): if not isinstance(state['options'].input_file, str | bytes | os.PathLike): state['options'].input_file = 'stream' diff --git a/src/ocrmypdf/_options.py b/src/ocrmypdf/_options.py index 99da0617..0eced9e1 100644 --- a/src/ocrmypdf/_options.py +++ b/src/ocrmypdf/_options.py @@ -56,11 +56,11 @@ class OCROptions(BaseModel): remove_background: bool = False remove_vectors: bool = False oversample: int = 0 - unpaper_args: str | None = None + unpaper_args: str | list[str] | None = None # Can be string or list after validation # OCR behavior skip_big: float | None = None - pages: str | None = None + pages: str | set[int] | None = None # Can be string or set after validation invalidate_digital_signatures: bool = False # Metadata