From 9d6f852a81d3eb7a862d8deee267f224b36f1a80 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Tue, 22 Sep 2026 13:33:43 +0300 Subject: [PATCH 1/4] Improve WebP encoder error handling Follows up on #5471 and #9993. Instead of reporting just "error 6", this PR adds human-readable error messages for the WebP encoder errors. The error messages are based on the WebP header. The memory-hungry `test_write_encoding_error_message` test case (that tested "partition is bigger than 512k") is no longer necessary, since `test_write_encoding_error_bad_dimension` covers the same code path. --- Tests/test_file_webp.py | 17 ++++------------ docs/releasenotes/13.1.0.rst | 6 ++++++ src/_webp.c | 39 +++++++++++++++++++++++++++--------- 3 files changed, 39 insertions(+), 23 deletions(-) diff --git a/Tests/test_file_webp.py b/Tests/test_file_webp.py index d74463790c3..ef610cc9824 100644 --- a/Tests/test_file_webp.py +++ b/Tests/test_file_webp.py @@ -2,7 +2,6 @@ import io import re -import sys import warnings from typing import Any @@ -163,21 +162,13 @@ def test_write_unsupported_mode_P(self, tmp_path: Path) -> None: self._roundtrip(tmp_path, "P", 50.0) - @pytest.mark.skipif(sys.maxsize <= 2**32, reason="Requires 64-bit system") - def test_write_encoding_error_message(self, tmp_path: Path) -> None: - im = Image.new("RGB", (15000, 15000)) - with pytest.raises(ValueError, match="encoding error 6"): - im.save(tmp_path / "temp.webp", method=0) - - @pytest.mark.skipif(sys.maxsize <= 2**32, reason="Requires 64-bit system") def test_write_encoding_error_bad_dimension(self, tmp_path: Path) -> None: im = Image.new("L", (16384, 1)) - with pytest.raises(ValueError) as e: + with pytest.raises( + ValueError, + match="encoding error 5: image size exceeds WebP limit of 16383 pixels", + ): im.save(tmp_path / "temp.webp") - assert ( - str(e.value) - == "encoding error 5: Image size exceeds WebP limit of 16383 pixels" - ) def test_WebPEncode_with_invalid_args(self) -> None: """ diff --git a/docs/releasenotes/13.1.0.rst b/docs/releasenotes/13.1.0.rst index 9427a607ac1..828a2c7738b 100644 --- a/docs/releasenotes/13.1.0.rst +++ b/docs/releasenotes/13.1.0.rst @@ -97,3 +97,9 @@ Minimum C version C11 is now the minimum version of C required to compile Pillow from source. Pillow requires Python 3.11 or later, and CPython 3.11 and later require C11. + +WebP encoding errors now include a description +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +When libwebp fails to encode an image, the raised :py:exc:`ValueError` now +consistently describes the error rather than only reporting a numeric code. diff --git a/src/_webp.c b/src/_webp.c index 1731ed9ed61..f89a56fa12d 100644 --- a/src/_webp.c +++ b/src/_webp.c @@ -73,6 +73,30 @@ HandleMuxError(WebPMuxError err, char *chunk) { return NULL; } +/* -------------------------------------------------------------------- */ +/* WebP Encoder Error Mapping */ +/* -------------------------------------------------------------------- */ + +#define WEBP_STR_HELPER(x) #x +#define WEBP_STR(x) WEBP_STR_HELPER(x) + +static const char *const kEncoderErrorMessages[VP8_ENC_ERROR_LAST] = { + "ok", + "out of memory allocating objects", + "out of memory re-allocating byte buffer", + "NULL parameter passed to function", + "configuration is invalid", + "image size exceeds WebP limit of " WEBP_STR(WEBP_MAX_DIMENSION) " pixels", + "partition #0 is bigger than 512K", + "partition is bigger than 16M", + "picture writer returned an I/O error", + "file would be bigger than 4G", + "encoding aborted by user" +}; + +#undef WEBP_STR +#undef WEBP_STR_HELPER + /* -------------------------------------------------------------------- */ /* Frame import */ /* -------------------------------------------------------------------- */ @@ -647,17 +671,12 @@ WebPEncode_wrapper(PyObject *self, PyObject *args) { ret_size = writer.size; if (!ok) { - int error_code = (&pic)->error_code; - char message[50] = ""; - if (error_code == VP8_ENC_ERROR_BAD_DIMENSION) { - sprintf( - message, - ": Image size exceeds WebP limit of %d pixels", - WEBP_MAX_DIMENSION - ); - } - PyErr_Format(PyExc_ValueError, "encoding error %d%s", error_code, message); free(output); + WebPEncodingError error_code = pic.error_code; + const char *message = error_code > VP8_ENC_OK && error_code < VP8_ENC_ERROR_LAST + ? kEncoderErrorMessages[error_code] + : "unknown error"; + PyErr_Format(PyExc_ValueError, "encoding error %d: %s", error_code, message); return NULL; } From 02a90bb53c997f2596eb64a4fda475666c2b28b1 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 7 Nov 2026 12:43:51 +1100 Subject: [PATCH 2/4] Update error messages to be closer to libwebp examples/cwebp.c --- src/_webp.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/_webp.c b/src/_webp.c index f89a56fa12d..68d976968e3 100644 --- a/src/_webp.c +++ b/src/_webp.c @@ -82,16 +82,16 @@ HandleMuxError(WebPMuxError err, char *chunk) { static const char *const kEncoderErrorMessages[VP8_ENC_ERROR_LAST] = { "ok", - "out of memory allocating objects", - "out of memory re-allocating byte buffer", + "Out of memory allocating objects", + "Out of memory re-allocating byte buffer", "NULL parameter passed to function", "configuration is invalid", "image size exceeds WebP limit of " WEBP_STR(WEBP_MAX_DIMENSION) " pixels", - "partition #0 is bigger than 512K", - "partition is bigger than 16M", - "picture writer returned an I/O error", - "file would be bigger than 4G", - "encoding aborted by user" + "Partition #0 is too big to fit 512k", + "Partition too big to fit 16M", + "Picture writer returned an I/O error", + "File would be too big to fit in 4G", + "encoding abort requested by user" }; #undef WEBP_STR From 34feabc18470a7e2d005b1de3e6159246f094e07 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 7 Nov 2026 12:59:14 +1100 Subject: [PATCH 3/4] Keep original capitalisation --- Tests/test_file_webp.py | 2 +- src/_webp.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/test_file_webp.py b/Tests/test_file_webp.py index ef610cc9824..15a5006243a 100644 --- a/Tests/test_file_webp.py +++ b/Tests/test_file_webp.py @@ -166,7 +166,7 @@ def test_write_encoding_error_bad_dimension(self, tmp_path: Path) -> None: im = Image.new("L", (16384, 1)) with pytest.raises( ValueError, - match="encoding error 5: image size exceeds WebP limit of 16383 pixels", + match="encoding error 5: Image size exceeds WebP limit of 16383 pixels", ): im.save(tmp_path / "temp.webp") diff --git a/src/_webp.c b/src/_webp.c index 68d976968e3..99c1a9681a9 100644 --- a/src/_webp.c +++ b/src/_webp.c @@ -86,7 +86,7 @@ static const char *const kEncoderErrorMessages[VP8_ENC_ERROR_LAST] = { "Out of memory re-allocating byte buffer", "NULL parameter passed to function", "configuration is invalid", - "image size exceeds WebP limit of " WEBP_STR(WEBP_MAX_DIMENSION) " pixels", + "Image size exceeds WebP limit of " WEBP_STR(WEBP_MAX_DIMENSION) " pixels", "Partition #0 is too big to fit 512k", "Partition too big to fit 16M", "Picture writer returned an I/O error", From 274e569c9488319a5b35d00d637f1280ccab1d3f Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 7 Nov 2026 13:06:17 +1100 Subject: [PATCH 4/4] Do not return "unknown error" --- src/_webp.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/_webp.c b/src/_webp.c index 99c1a9681a9..102a3f32a68 100644 --- a/src/_webp.c +++ b/src/_webp.c @@ -673,10 +673,16 @@ WebPEncode_wrapper(PyObject *self, PyObject *args) { if (!ok) { free(output); WebPEncodingError error_code = pic.error_code; - const char *message = error_code > VP8_ENC_OK && error_code < VP8_ENC_ERROR_LAST - ? kEncoderErrorMessages[error_code] - : "unknown error"; - PyErr_Format(PyExc_ValueError, "encoding error %d: %s", error_code, message); + if (error_code > VP8_ENC_OK && error_code < VP8_ENC_ERROR_LAST) { + PyErr_Format( + PyExc_ValueError, + "encoding error %d: %s", + error_code, + kEncoderErrorMessages[error_code] + ); + } else { + PyErr_Format(PyExc_ValueError, "encoding error %d", error_code); + } return NULL; }