Skip to content

Commit 132663a

Browse files
committed
Updated error message for invalid width or height
1 parent f0d8fd3 commit 132663a

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

Tests/test_file_webp.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,14 @@ def test_write_encoding_error_message(self, tmp_path: Path) -> None:
157157
im.save(temp_file, method=0)
158158
assert str(e.value) == "encoding error 6"
159159

160+
@pytest.mark.skipif(sys.maxsize <= 2**32, reason="Requires 64-bit system")
161+
def test_write_encoding_error_bad_dimension(self, tmp_path: Path) -> None:
162+
temp_file = str(tmp_path / "temp.webp")
163+
im = Image.new("L", (16384, 16384))
164+
with pytest.raises(ValueError) as e:
165+
im.save(temp_file)
166+
assert str(e.value) == "encoding error 5: Image size exceeds WebP limit"
167+
160168
def test_WebPEncode_with_invalid_args(self) -> None:
161169
"""
162170
Calling encoder functions with no arguments should result in an error.

src/_webp.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,12 @@ WebPEncode_wrapper(PyObject *self, PyObject *args) {
672672

673673
WebPPictureFree(&pic);
674674
if (!ok) {
675-
PyErr_Format(PyExc_ValueError, "encoding error %d", (&pic)->error_code);
675+
int error_code = (&pic)->error_code;
676+
const char *message = "";
677+
if (error_code == VP8_ENC_ERROR_BAD_DIMENSION) {
678+
message = ": Image size exceeds WebP limit";
679+
}
680+
PyErr_Format(PyExc_ValueError, "encoding error %d%s", error_code, message);
676681
return NULL;
677682
}
678683
output = writer.mem;

0 commit comments

Comments
 (0)