From 132663a881bfe2bfc5265e7e05de7113f06501a5 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 22 Aug 2024 07:36:52 +1000 Subject: [PATCH 1/4] Updated error message for invalid width or height --- Tests/test_file_webp.py | 8 ++++++++ src/_webp.c | 7 ++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Tests/test_file_webp.py b/Tests/test_file_webp.py index 6ccd489bb..ad08da364 100644 --- a/Tests/test_file_webp.py +++ b/Tests/test_file_webp.py @@ -157,6 +157,14 @@ class TestFileWebp: im.save(temp_file, method=0) assert str(e.value) == "encoding error 6" + @pytest.mark.skipif(sys.maxsize <= 2**32, reason="Requires 64-bit system") + def test_write_encoding_error_bad_dimension(self, tmp_path: Path) -> None: + temp_file = str(tmp_path / "temp.webp") + im = Image.new("L", (16384, 16384)) + with pytest.raises(ValueError) as e: + im.save(temp_file) + assert str(e.value) == "encoding error 5: Image size exceeds WebP limit" + def test_WebPEncode_with_invalid_args(self) -> None: """ Calling encoder functions with no arguments should result in an error. diff --git a/src/_webp.c b/src/_webp.c index d1943b3e0..0d2d6f023 100644 --- a/src/_webp.c +++ b/src/_webp.c @@ -672,7 +672,12 @@ WebPEncode_wrapper(PyObject *self, PyObject *args) { WebPPictureFree(&pic); if (!ok) { - PyErr_Format(PyExc_ValueError, "encoding error %d", (&pic)->error_code); + int error_code = (&pic)->error_code; + const char *message = ""; + if (error_code == VP8_ENC_ERROR_BAD_DIMENSION) { + message = ": Image size exceeds WebP limit"; + } + PyErr_Format(PyExc_ValueError, "encoding error %d%s", error_code, message); return NULL; } output = writer.mem; From d49ea378115f4ae32e12f1f0c3e6146ebdc68fe0 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 22 Aug 2024 17:50:42 +1000 Subject: [PATCH 2/4] Include limit in error message --- Tests/test_file_webp.py | 4 +++- src/_webp.c | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Tests/test_file_webp.py b/Tests/test_file_webp.py index ad08da364..a86757e64 100644 --- a/Tests/test_file_webp.py +++ b/Tests/test_file_webp.py @@ -163,7 +163,9 @@ class TestFileWebp: im = Image.new("L", (16384, 16384)) with pytest.raises(ValueError) as e: im.save(temp_file) - assert str(e.value) == "encoding error 5: Image size exceeds WebP limit" + assert ( + str(e.value) == "encoding error 5: Image size exceeds WebP limit of 16383" + ) def test_WebPEncode_with_invalid_args(self) -> None: """ diff --git a/src/_webp.c b/src/_webp.c index 0d2d6f023..c0e1a6f63 100644 --- a/src/_webp.c +++ b/src/_webp.c @@ -673,9 +673,11 @@ WebPEncode_wrapper(PyObject *self, PyObject *args) { WebPPictureFree(&pic); if (!ok) { int error_code = (&pic)->error_code; - const char *message = ""; + char message[50] = ""; if (error_code == VP8_ENC_ERROR_BAD_DIMENSION) { - message = ": Image size exceeds WebP limit"; + sprintf( + message, ": Image size exceeds WebP limit of %d", WEBP_MAX_DIMENSION + ); } PyErr_Format(PyExc_ValueError, "encoding error %d%s", error_code, message); return NULL; From 658b60e3a33d6bf4e3ff3a2053cb73faa466772d Mon Sep 17 00:00:00 2001 From: Andrew Murray <3112309+radarhere@users.noreply.github.com> Date: Sat, 24 Aug 2024 21:26:56 +1000 Subject: [PATCH 3/4] Include units Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- 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 a86757e64..4a048f2c2 100644 --- a/Tests/test_file_webp.py +++ b/Tests/test_file_webp.py @@ -164,7 +164,7 @@ class TestFileWebp: with pytest.raises(ValueError) as e: im.save(temp_file) assert ( - str(e.value) == "encoding error 5: Image size exceeds WebP limit of 16383" + 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/src/_webp.c b/src/_webp.c index c0e1a6f63..b4cf9c329 100644 --- a/src/_webp.c +++ b/src/_webp.c @@ -676,7 +676,7 @@ WebPEncode_wrapper(PyObject *self, PyObject *args) { char message[50] = ""; if (error_code == VP8_ENC_ERROR_BAD_DIMENSION) { sprintf( - message, ": Image size exceeds WebP limit of %d", WEBP_MAX_DIMENSION + message, ": Image size exceeds WebP limit of %d pixels", WEBP_MAX_DIMENSION ); } PyErr_Format(PyExc_ValueError, "encoding error %d%s", error_code, message); From a3e3639a59ffaf33430b08b089da1eedf04286ab Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 24 Aug 2024 11:27:40 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- Tests/test_file_webp.py | 3 ++- src/_webp.c | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Tests/test_file_webp.py b/Tests/test_file_webp.py index 4a048f2c2..247fc6021 100644 --- a/Tests/test_file_webp.py +++ b/Tests/test_file_webp.py @@ -164,7 +164,8 @@ class TestFileWebp: with pytest.raises(ValueError) as e: im.save(temp_file) assert ( - str(e.value) == "encoding error 5: Image size exceeds WebP limit of 16383 pixels" + 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/src/_webp.c b/src/_webp.c index b4cf9c329..f59ad3036 100644 --- a/src/_webp.c +++ b/src/_webp.c @@ -676,7 +676,9 @@ WebPEncode_wrapper(PyObject *self, PyObject *args) { char message[50] = ""; if (error_code == VP8_ENC_ERROR_BAD_DIMENSION) { sprintf( - message, ": Image size exceeds WebP limit of %d pixels", WEBP_MAX_DIMENSION + message, + ": Image size exceeds WebP limit of %d pixels", + WEBP_MAX_DIMENSION ); } PyErr_Format(PyExc_ValueError, "encoding error %d%s", error_code, message);