Skip to content

Commit 112a09f

Browse files
committed
Add bespoke error messages where suitable
1 parent affd16a commit 112a09f

25 files changed

Lines changed: 167 additions & 79 deletions

Tests/test_color_lut.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def test_correct_args(
137137
def test_wrong_mode(
138138
self, image_mode: str, lut_mode: str, table_channels: int, table_size: int
139139
) -> None:
140-
with pytest.raises(ValueError, match="wrong mode"):
140+
with pytest.raises(ValueError, match="bands"):
141141
im = Image.new(image_mode, (10, 10), 0)
142142
im.im.color_lut_3d(
143143
lut_mode,

Tests/test_image_paste.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,9 @@ def test_overflow(self, box: tuple[int, int, int, int]) -> None:
368368
im = Image.new("1", (1, 1))
369369
im.paste(1, box)
370370

371-
with pytest.raises(ValueError, match="images do not match"):
371+
with pytest.raises(
372+
ValueError, match="image dimensions and pixel size must match"
373+
):
372374
im.paste(im.copy(), box)
373375

374376
def test_incorrect_abbreviated_form(self) -> None:

src/libImaging/AlphaComposite.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@ ImagingAlphaComposite(Imaging imDst, Imaging imSrc) {
2424
Imaging imOut;
2525

2626
/* Check arguments */
27-
if (!imDst || !imSrc ||
28-
(imDst->mode != IMAGING_MODE_RGBA && imDst->mode != IMAGING_MODE_LA)) {
29-
return ImagingError_ModeError(NULL);
27+
if (!imDst || !imSrc) {
28+
return ImagingError_ValueError("imDst and imSrc must not be NULL");
29+
}
30+
if (imDst->mode != IMAGING_MODE_RGBA && imDst->mode != IMAGING_MODE_LA) {
31+
return ImagingError_ModeError("destination image must have alpha channel");
3032
}
3133

3234
if (imDst->mode != imSrc->mode || imDst->xsize != imSrc->xsize ||
3335
imDst->ysize != imSrc->ysize) {
34-
return ImagingError_Mismatch(NULL);
36+
return ImagingError_Mismatch("image modes and dimensions must match");
3537
}
3638

3739
imOut = ImagingNewDirty(imDst->mode, imDst->xsize, imDst->ysize);

src/libImaging/Bands.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ ImagingGetBand(Imaging imIn, int band) {
2424

2525
/* Check arguments */
2626
if (!imIn || imIn->type != IMAGING_TYPE_UINT8) {
27-
return (Imaging)ImagingError_ModeError(NULL);
27+
return (Imaging)ImagingError_NotSupportedError(NULL);
2828
}
2929

3030
if (band < 0 || band >= imIn->bands) {
@@ -74,7 +74,7 @@ ImagingSplit(Imaging imIn, Imaging bands[4]) {
7474

7575
/* Check arguments */
7676
if (!imIn || imIn->type != IMAGING_TYPE_UINT8) {
77-
(void)ImagingError_ModeError(NULL);
77+
(void)ImagingError_NotSupportedError(NULL);
7878
return 0;
7979
}
8080

@@ -178,17 +178,20 @@ ImagingPutBand(Imaging imOut, Imaging imIn, int band) {
178178
int x, y;
179179

180180
/* Check arguments */
181-
if (!imIn || imIn->bands != 1 || !imOut) {
181+
if (!imIn || !imOut) {
182182
return (Imaging)ImagingError_ModeError(NULL);
183183
}
184+
if (imIn->bands != 1) {
185+
return (Imaging)ImagingError_ModeError("source image must have exactly 1 band");
186+
}
184187

185188
if (band < 0 || band >= imOut->bands) {
186189
return (Imaging)ImagingError_ValueError("band index out of range");
187190
}
188191

189192
if (imIn->type != imOut->type || imIn->xsize != imOut->xsize ||
190193
imIn->ysize != imOut->ysize) {
191-
return (Imaging)ImagingError_Mismatch(NULL);
194+
return (Imaging)ImagingError_Mismatch("image types and sizes must match");
192195
}
193196

194197
/* Shortcuts */
@@ -224,8 +227,12 @@ ImagingFillBand(Imaging imOut, int band, int color) {
224227
int x, y;
225228

226229
/* Check arguments */
227-
if (!imOut || imOut->type != IMAGING_TYPE_UINT8) {
228-
return (Imaging)ImagingError_ModeError(NULL);
230+
if (!imOut) {
231+
return (Imaging)ImagingError_ValueError(NULL);
232+
}
233+
234+
if (imOut->type != IMAGING_TYPE_UINT8) {
235+
return (Imaging)ImagingError_NotSupportedError("only 8-bit images supported");
229236
}
230237

231238
if (band < 0 || band >= imOut->bands) {
@@ -270,11 +277,13 @@ ImagingMerge(const ModeID mode, Imaging bands[4]) {
270277
break;
271278
}
272279
if (bands[i]->bands != 1) {
273-
return (Imaging)ImagingError_ModeError(NULL);
280+
return (Imaging)ImagingError_ModeError(
281+
"source image must have exactly 1 band"
282+
);
274283
}
275284
if (bands[i]->xsize != firstBand->xsize ||
276285
bands[i]->ysize != firstBand->ysize) {
277-
return (Imaging)ImagingError_Mismatch(NULL);
286+
return (Imaging)ImagingError_Mismatch("image sizes must match");
278287
}
279288
}
280289
bandsCount = i;

src/libImaging/Blend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ ImagingBlend(Imaging imIn1, Imaging imIn2, float alpha) {
3131

3232
if (imIn1->type != imIn2->type || imIn1->bands != imIn2->bands ||
3333
imIn1->xsize != imIn2->xsize || imIn1->ysize != imIn2->ysize) {
34-
return ImagingError_Mismatch(NULL);
34+
return ImagingError_Mismatch("image types, band count and sizes must match");
3535
}
3636

3737
/* Shortcuts */

src/libImaging/BoxBlur.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,18 +251,20 @@ ImagingBoxBlur(Imaging imOut, Imaging imIn, float xradius, float yradius, int n)
251251
if (imIn->mode != imOut->mode || imIn->type != imOut->type ||
252252
imIn->bands != imOut->bands || imIn->xsize != imOut->xsize ||
253253
imIn->ysize != imOut->ysize) {
254-
return ImagingError_Mismatch(NULL);
254+
return ImagingError_Mismatch("image mode, type, bands and size must match");
255255
}
256256

257257
if (imIn->type != IMAGING_TYPE_UINT8) {
258-
return ImagingError_ModeError(NULL);
258+
return ImagingError_NotSupportedError("non-uint8 images not supported");
259259
}
260260

261261
if (imIn->mode != IMAGING_MODE_RGB && imIn->mode != IMAGING_MODE_RGBA &&
262262
imIn->mode != IMAGING_MODE_RGBa && imIn->mode != IMAGING_MODE_RGBX &&
263263
imIn->mode != IMAGING_MODE_CMYK && imIn->mode != IMAGING_MODE_L &&
264264
imIn->mode != IMAGING_MODE_LA && imIn->mode != IMAGING_MODE_La) {
265-
return ImagingError_ModeError(NULL);
265+
return ImagingError_NotSupportedError(
266+
"BoxBlur operation not supported in this mode"
267+
);
266268
}
267269

268270
/* Apply blur in one dimension.

src/libImaging/Chops.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ create(Imaging im1, Imaging im2, const ModeID mode) {
7575
return (Imaging)ImagingError_ModeError(NULL);
7676
}
7777
if (im1->type != im2->type || im1->bands != im2->bands) {
78-
return (Imaging)ImagingError_Mismatch(NULL);
78+
return (Imaging)ImagingError_Mismatch("image types and band count must match");
7979
}
8080

8181
xsize = (im1->xsize < im2->xsize) ? im1->xsize : im2->xsize;

src/libImaging/ColorLUT.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,23 @@ ImagingColorLUT3D_linear(
8787
return NULL;
8888
}
8989

90-
if (imIn->type != IMAGING_TYPE_UINT8 || imOut->type != IMAGING_TYPE_UINT8 ||
91-
imIn->bands < 3 || imOut->bands < table_channels) {
92-
return (Imaging)ImagingError_ModeError(NULL);
90+
if (imIn->type != IMAGING_TYPE_UINT8 || imOut->type != IMAGING_TYPE_UINT8) {
91+
return (Imaging)ImagingError_NotSupportedError(
92+
"only uint8 images are supported"
93+
);
94+
}
95+
if (imIn->bands < 3) {
96+
return (Imaging)ImagingError_ModeError("input image needs at least 3 bands");
97+
}
98+
if (imOut->bands < table_channels) {
99+
return (Imaging)ImagingError_ValueError(
100+
"output image needs at least as many bands as the table"
101+
);
93102
}
94-
95-
/* In case we have one extra band in imOut and don't have in imIn.*/
96103
if (imOut->bands > table_channels && imOut->bands > imIn->bands) {
97-
return (Imaging)ImagingError_ModeError(NULL);
104+
return (Imaging)ImagingError_ModeError(
105+
"output image has more bands than input image and table"
106+
);
98107
}
99108

100109
ImagingSectionEnter(&cookie);

src/libImaging/Convert.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1747,7 +1747,9 @@ ImagingConvertInPlace(Imaging imIn, const ModeID mode) {
17471747
} else if (imIn->mode == IMAGING_MODE_1 && mode == IMAGING_MODE_L) {
17481748
convert = bit2l;
17491749
} else {
1750-
return ImagingError_ModeError(NULL);
1750+
return ImagingError_NotSupportedError(
1751+
"in-place conversion not supported between these formats"
1752+
);
17511753
}
17521754

17531755
ImagingSectionEnter(&cookie);

src/libImaging/Dib.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ ImagingNewDIB(const ModeID mode, int xsize, int ysize) {
5959

6060
/* Check mode */
6161
if (mode != IMAGING_MODE_1 && mode != IMAGING_MODE_L && mode != IMAGING_MODE_RGB) {
62-
return (ImagingDIB)ImagingError_ModeError(NULL);
62+
return (ImagingDIB)ImagingError_NotSupportedError(
63+
"only modes I/L/RGB supported"
64+
);
6365
}
6466

6567
const int pixelsize = mode == IMAGING_MODE_RGB ? 3 : 1;

0 commit comments

Comments
 (0)