Skip to content

Commit affa059

Browse files
authored
Merge pull request #5504 from radarhere/tuple_size
Added specific error messages when ink has incorrect number of bands
2 parents cc2db17 + a141268 commit affa059

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

Tests/test_image_access.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,24 @@ def test_putpixel_type_error1(self, mode):
355355
with pytest.raises(TypeError, match="color must be int or tuple"):
356356
im.putpixel((0, 0), v)
357357

358+
@pytest.mark.parametrize(
359+
("mode", "band_numbers", "match"),
360+
(
361+
("L", (0, 2), "color must be int or single-element tuple"),
362+
("LA", (0, 3), "color must be int, or tuple of one or two elements"),
363+
(
364+
"RGB",
365+
(0, 2, 5),
366+
"color must be int, or tuple of one, three or four elements",
367+
),
368+
),
369+
)
370+
def test_putpixel_invalid_number_of_bands(self, mode, band_numbers, match):
371+
im = hopper(mode)
372+
for band_number in band_numbers:
373+
with pytest.raises(TypeError, match=match):
374+
im.putpixel((0, 0), (0,) * band_number)
375+
358376
@pytest.mark.parametrize("mode", IMAGE_MODES2)
359377
def test_putpixel_type_error2(self, mode):
360378
im = hopper(mode)

src/_imaging.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ getink(PyObject *color, Imaging im, char *ink) {
498498
be cast to either UINT8 or INT32 */
499499

500500
int rIsInt = 0;
501-
if (PyTuple_Check(color) && PyTuple_Size(color) == 1) {
501+
if (PyTuple_Check(color) && PyTuple_GET_SIZE(color) == 1) {
502502
color = PyTuple_GetItem(color, 0);
503503
}
504504
if (im->type == IMAGING_TYPE_UINT8 || im->type == IMAGING_TYPE_INT32 ||
@@ -527,7 +527,10 @@ getink(PyObject *color, Imaging im, char *ink) {
527527
if (im->bands == 1) {
528528
/* unsigned integer, single layer */
529529
if (rIsInt != 1) {
530-
if (!PyArg_ParseTuple(color, "L", &r)) {
530+
if (PyTuple_GET_SIZE(color) != 1) {
531+
PyErr_SetString(PyExc_TypeError, "color must be int or single-element tuple");
532+
return NULL;
533+
} else if (!PyArg_ParseTuple(color, "L", &r)) {
531534
return NULL;
532535
}
533536
}
@@ -542,13 +545,20 @@ getink(PyObject *color, Imaging im, char *ink) {
542545
g = (UINT8)(r >> 8);
543546
r = (UINT8)r;
544547
} else {
548+
int tupleSize = PyTuple_GET_SIZE(color);
545549
if (im->bands == 2) {
546-
if (!PyArg_ParseTuple(color, "L|i", &r, &a)) {
550+
if (tupleSize != 1 && tupleSize != 2) {
551+
PyErr_SetString(PyExc_TypeError, "color must be int, or tuple of one or two elements");
552+
return NULL;
553+
} else if (!PyArg_ParseTuple(color, "L|i", &r, &a)) {
547554
return NULL;
548555
}
549556
g = b = r;
550557
} else {
551-
if (!PyArg_ParseTuple(color, "Lii|i", &r, &g, &b, &a)) {
558+
if (tupleSize != 3 && tupleSize != 4) {
559+
PyErr_SetString(PyExc_TypeError, "color must be int, or tuple of one, three or four elements");
560+
return NULL;
561+
} else if (!PyArg_ParseTuple(color, "Lii|i", &r, &g, &b, &a)) {
552562
return NULL;
553563
}
554564
}

0 commit comments

Comments
 (0)