From 8f18e22eb41e087d3c8ed380beb29ea39d3e9436 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 4 Sep 2026 14:25:02 +1000 Subject: [PATCH] Correct error message for matrix length --- Tests/test_image_convert.py | 18 ++++++++++++------ src/_imaging.c | 1 + 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/Tests/test_image_convert.py b/Tests/test_image_convert.py index bf5034cc932..e1b06699c8f 100644 --- a/Tests/test_image_convert.py +++ b/Tests/test_image_convert.py @@ -46,7 +46,7 @@ def convert(im: Image.Image, mode: str) -> None: def test_unsupported_conversion() -> None: im = hopper() - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="image has wrong mode"): im.convert("INVALID") @@ -305,26 +305,33 @@ def test_matrix_illegal_conversion() -> None: assert im.mode != "RGB" # Act / Assert - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="illegal conversion"): im.convert(mode="CMYK", matrix=rgb2xyz_matrix) def test_matrix_wrong_mode() -> None: # Arrange im = hopper("L") - assert im.mode == "L" # Act / Assert - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="image has wrong mode"): im.convert(mode="L", matrix=rgb2xyz_matrix) +def test_matrix_truncated() -> None: + # Arrange + im = hopper() + + # Act / Assert + with pytest.raises(TypeError, match="matrix must be tuple of length 4 or 12"): + im.convert(mode="L", matrix=(0,)) + + @pytest.mark.parametrize("mode", ("RGB", "L")) def test_matrix_xyz(mode: str) -> None: # Arrange im = hopper("RGB") im.info["transparency"] = (255, 0, 0) - assert im.mode == "RGB" # Act # Convert an RGB image to the CIE XYZ colour space @@ -350,7 +357,6 @@ def test_matrix_identity() -> None: 0, 1, 0, 0, 0, 0, 1, 0, ) # fmt: skip - assert im.mode == "RGB" # Act # Convert with an identity matrix diff --git a/src/_imaging.c b/src/_imaging.c index e946bd37bb3..ee19f7f5dfc 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -1068,6 +1068,7 @@ _convert_matrix(ImagingObject *self, PyObject *args) { m + 10, m + 11 )) { + PyErr_SetString(PyExc_TypeError, "matrix must be tuple of length 4 or 12"); return NULL; } }