Skip to content

Commit

Permalink
Added specific error message for invalid width or height
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Aug 21, 2024
1 parent f0d8fd3 commit cd4f2da
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Tests/test_file_webp.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ def test_write_encoding_error_message(self, tmp_path: Path) -> None:
im.save(temp_file, method=0)
assert str(e.value) == "encoding error 6"

def test_write_encoding_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) == "Image size exceeds WebP limit"

def test_WebPEncode_with_invalid_args(self) -> None:
"""
Calling encoder functions with no arguments should result in an error.
Expand Down
7 changes: 6 additions & 1 deletion src/_webp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
if (error_code == VP8_ENC_ERROR_BAD_DIMENSION) {
PyErr_Format(PyExc_ValueError, "Image size exceeds WebP limit");
} else {
PyErr_Format(PyExc_ValueError, "encoding error %d", error_code);
}
return NULL;
}
output = writer.mem;
Expand Down

0 comments on commit cd4f2da

Please sign in to comment.