From 405e31fcb016080c774b2a3acb72631f0e105afc Mon Sep 17 00:00:00 2001 From: hyperammonemia <279652652+hyperammonemia@users.noreply.github.com> Date: Fri, 30 Oct 2026 11:05:40 +0800 Subject: [PATCH 1/2] Preserve Ghostscript diagnostics with non-UTF-8 stderr Decode failed-repair diagnostics with replacement and cover invalid bytes, valid UTF-8, and the successful output path. Prepared with OpenAI Codex assistance. --- CHANGELOG.md | 1 + README.md | 1 + pdfplumber/repair.py | 2 +- tests/test_repair.py | 27 +++++++++++++++++++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f9d161e9..7c682a29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file. The format ## [Unreleased] ### Fixed +- Preserve readable Ghostscript diagnostics when a failed repair emits non-UTF-8 bytes, instead of raising a `UnicodeDecodeError` while decoding the error message. - Initialize PDFium's form environment in `get_page_image` so that filled AcroForm field content is included when rendering pages via `Page.to_image()`. ([#1367](https://github.com/jsvine/pdfplumber/issues/1367)) - Fix `make venv`, which created the virtual environment at `venv/` but then installed into `${VENV}` (default `.venv/`), causing the target to fail on a fresh checkout (h/t @soodoku). ([ec96f72](https://github.com/jsvine/pdfplumber/commit/ec96f72)) - Include the wrapped exception's class name in `PdfminerException`'s message when `pdfminer.six` raises an exception without one (e.g. `PDFPasswordIncorrect`), which previously surfaced as a blank error message. diff --git a/README.md b/README.md index 012086c2..e62bde34 100644 --- a/README.md +++ b/README.md @@ -577,6 +577,7 @@ Many thanks to the following users who've contributed ideas, features, and fixes - [Sebastian Cao](https://github.com/cycsmail) - [Kaspar Naraghi](https://github.com/kaninaba94) - [Siddharth Gaur](https://github.com/siddharthgaur1) +- [@hyperammonemia](https://github.com/hyperammonemia) ## Contributing diff --git a/pdfplumber/repair.py b/pdfplumber/repair.py index 2e4df9aa..20d3362c 100644 --- a/pdfplumber/repair.py +++ b/pdfplumber/repair.py @@ -55,7 +55,7 @@ def _repair( stdout, stderr = proc.communicate(stdin.read() if stdin else None) if proc.returncode: - raise Exception(f"{stderr.decode('utf-8')}") + raise Exception(stderr.decode("utf-8", errors="replace")) return BytesIO(stdout) diff --git a/tests/test_repair.py b/tests/test_repair.py index 60a2c33e..a6316231 100644 --- a/tests/test_repair.py +++ b/tests/test_repair.py @@ -3,6 +3,8 @@ import shutil import tempfile import unittest +from io import BytesIO +from unittest.mock import patch import pytest @@ -12,6 +14,31 @@ class Test(unittest.TestCase): + def test_repair_failure_diagnostics(self): + diagnostics = [ + (b"Ghostscript: invalid input \xae", "Ghostscript: invalid input \ufffd"), + ("Ghostscript: \u00e9chec".encode("utf-8"), "Ghostscript: \u00e9chec"), + ] + for stderr, expected in diagnostics: + with self.subTest(stderr=stderr): + with patch("pdfplumber.repair.subprocess.Popen") as popen: + popen.return_value.returncode = 1 + popen.return_value.communicate.return_value = (b"", stderr) + with pytest.raises(Exception) as exc: + pdfplumber.repair(BytesIO(b"input"), gs_path="ghostscript") + assert type(exc.value) is Exception + assert str(exc.value) == expected + + def test_repair_success_with_non_utf8_diagnostics(self): + with patch("pdfplumber.repair.subprocess.Popen") as popen: + popen.return_value.returncode = 0 + popen.return_value.communicate.return_value = ( + b"repaired PDF bytes", + b"Ghostscript warning \xae", + ) + with pdfplumber.repair(BytesIO(b"input"), gs_path="ghostscript") as result: + assert result.read() == b"repaired PDF bytes" + def test_from_issue_932(self): path = os.path.join(HERE, "pdfs/malformed-from-issue-932.pdf") with pdfplumber.open(path) as pdf: From beeecc16240d5855c866a697ab210796ad887080 Mon Sep 17 00:00:00 2001 From: hyperammonemia <279652652+hyperammonemia@users.noreply.github.com> Date: Fri, 30 Oct 2026 12:10:39 +0800 Subject: [PATCH 2/2] Use a mock target compatible with Python 3.10 --- tests/test_repair.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_repair.py b/tests/test_repair.py index a6316231..76d64987 100644 --- a/tests/test_repair.py +++ b/tests/test_repair.py @@ -21,7 +21,7 @@ def test_repair_failure_diagnostics(self): ] for stderr, expected in diagnostics: with self.subTest(stderr=stderr): - with patch("pdfplumber.repair.subprocess.Popen") as popen: + with patch("subprocess.Popen") as popen: popen.return_value.returncode = 1 popen.return_value.communicate.return_value = (b"", stderr) with pytest.raises(Exception) as exc: @@ -30,7 +30,7 @@ def test_repair_failure_diagnostics(self): assert str(exc.value) == expected def test_repair_success_with_non_utf8_diagnostics(self): - with patch("pdfplumber.repair.subprocess.Popen") as popen: + with patch("subprocess.Popen") as popen: popen.return_value.returncode = 0 popen.return_value.communicate.return_value = ( b"repaired PDF bytes",