From fe89be5dc07f0e9f740cbc0a47446b01e12b0fda Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Wed, 27 Nov 2024 15:44:12 -0800 Subject: [PATCH] Fix test broken in commit 85d6fb8c --- src/ocrmypdf/_exec/ghostscript.py | 17 ++++++++++++----- tests/test_ghostscript.py | 2 +- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/ocrmypdf/_exec/ghostscript.py b/src/ocrmypdf/_exec/ghostscript.py index e581f68ea..ee8a5d407 100644 --- a/src/ocrmypdf/_exec/ghostscript.py +++ b/src/ocrmypdf/_exec/ghostscript.py @@ -137,11 +137,12 @@ def rasterize_pdf( p = run(args_gs, stdout=PIPE, stderr=PIPE, check=True) except CalledProcessError as e: log.error(e.stderr.decode(errors='replace')) - raise SubprocessOutputError('Ghostscript rasterizing failed') from e - else: - stderr = p.stderr.decode(errors='replace') - if _gs_error_reported(stderr): - log.error(stderr) + Path(output_file).unlink(missing_ok=True) + raise SubprocessOutputError("Ghostscript rasterizing failed") from e + + stderr = p.stderr.decode(errors='replace') + if _gs_error_reported(stderr): + log.error(stderr) try: with Image.open(output_file) as im: @@ -164,6 +165,12 @@ def rasterize_pdf( "an invalid page image file." ) raise + except OSError as e: + log.error( + f"Ghostscript (using {raster_device} at {raster_dpi} dpi) produced " + "an invalid page image file." + ) + raise UnidentifiedImageError() from e class GhostscriptFollower: diff --git a/tests/test_ghostscript.py b/tests/test_ghostscript.py index 1a511c449..0c2a69bda 100644 --- a/tests/test_ghostscript.py +++ b/tests/test_ghostscript.py @@ -139,7 +139,7 @@ def test_ghostscript_mandatory_color_conversion(resources, outpdf): def test_rasterize_pdf_errors(resources, no_outpdf, caplog): with patch('ocrmypdf._exec.ghostscript.run') as mock: - # ghostscript can produce + # ghostscript can produce empty files with return code 0 mock.return_value = subprocess.CompletedProcess( ['fakegs'], returncode=0, stdout=b'', stderr=b'error this is an error' )