Skip to content

Commit ab7e054

Browse files
authored
Correct error message for matrix length (#9956)
1 parent d3a33e9 commit ab7e054

2 files changed

Lines changed: 13 additions & 6 deletions

File tree

Tests/test_image_convert.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def convert(im: Image.Image, mode: str) -> None:
4646

4747
def test_unsupported_conversion() -> None:
4848
im = hopper()
49-
with pytest.raises(ValueError):
49+
with pytest.raises(ValueError, match="image has wrong mode"):
5050
im.convert("INVALID")
5151

5252

@@ -305,26 +305,33 @@ def test_matrix_illegal_conversion() -> None:
305305
assert im.mode != "RGB"
306306

307307
# Act / Assert
308-
with pytest.raises(ValueError):
308+
with pytest.raises(ValueError, match="illegal conversion"):
309309
im.convert(mode="CMYK", matrix=rgb2xyz_matrix)
310310

311311

312312
def test_matrix_wrong_mode() -> None:
313313
# Arrange
314314
im = hopper("L")
315-
assert im.mode == "L"
316315

317316
# Act / Assert
318-
with pytest.raises(ValueError):
317+
with pytest.raises(ValueError, match="image has wrong mode"):
319318
im.convert(mode="L", matrix=rgb2xyz_matrix)
320319

321320

321+
def test_matrix_truncated() -> None:
322+
# Arrange
323+
im = hopper()
324+
325+
# Act / Assert
326+
with pytest.raises(TypeError, match="matrix must be tuple of length 4 or 12"):
327+
im.convert(mode="L", matrix=(0,))
328+
329+
322330
@pytest.mark.parametrize("mode", ("RGB", "L"))
323331
def test_matrix_xyz(mode: str) -> None:
324332
# Arrange
325333
im = hopper("RGB")
326334
im.info["transparency"] = (255, 0, 0)
327-
assert im.mode == "RGB"
328335

329336
# Act
330337
# Convert an RGB image to the CIE XYZ colour space
@@ -350,7 +357,6 @@ def test_matrix_identity() -> None:
350357
0, 1, 0, 0,
351358
0, 0, 1, 0,
352359
) # fmt: skip
353-
assert im.mode == "RGB"
354360

355361
# Act
356362
# Convert with an identity matrix

src/_imaging.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,7 @@ _convert_matrix(ImagingObject *self, PyObject *args) {
10781078
m + 10,
10791079
m + 11
10801080
)) {
1081+
PyErr_SetString(PyExc_TypeError, "matrix must be tuple of length 4 or 12");
10811082
return NULL;
10821083
}
10831084
}

0 commit comments

Comments
 (0)