diff --git a/Tests/test_color_lut.py b/Tests/test_color_lut.py index f9d35c83c..733aba2a2 100644 --- a/Tests/test_color_lut.py +++ b/Tests/test_color_lut.py @@ -66,6 +66,14 @@ class TestColorLut3DCoreAPI(PillowTestCase): im.im.color_lut_3d('RGB', Image.LINEAR, 3, 2, 2, 2, [0, 0, 0] * 9) + with self.assertRaises(TypeError): + im.im.color_lut_3d('RGB', Image.LINEAR, + 3, 2, 2, 2, [0, 0, "0"] * 8) + + with self.assertRaises(TypeError): + im.im.color_lut_3d('RGB', Image.LINEAR, + 3, 2, 2, 2, 16) + def test_correct_args(self): im = Image.new('RGB', (10, 10), 0) diff --git a/src/_imaging.c b/src/_imaging.c index 544e54d87..808917ad1 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -379,12 +379,12 @@ getlist(PyObject* arg, Py_ssize_t* length, const char* wrong_length, int type) PyObject* seq; PyObject* op; - if (!PySequence_Check(arg)) { + if ( ! PySequence_Check(arg)) { PyErr_SetString(PyExc_TypeError, must_be_sequence); return NULL; } - n = PyObject_Length(arg); + n = PySequence_Size(arg); if (length && wrong_length && n != *length) { PyErr_SetString(PyExc_ValueError, wrong_length); return NULL; @@ -393,13 +393,12 @@ getlist(PyObject* arg, Py_ssize_t* length, const char* wrong_length, int type) /* malloc check ok, type & ff is just a sizeof(something) calloc checks for overflow */ list = calloc(n, type & 0xff); - if (!list) + if ( ! list) return PyErr_NoMemory(); seq = PySequence_Fast(arg, must_be_sequence); - if (!seq) { + if ( ! seq) { free(list); - PyErr_SetString(PyExc_TypeError, must_be_sequence); return NULL; } @@ -427,12 +426,16 @@ getlist(PyObject* arg, Py_ssize_t* length, const char* wrong_length, int type) } } + Py_DECREF(seq); + + if (PyErr_Occurred()) { + free(list); + return NULL; + } + if (length) *length = n; - PyErr_Clear(); - Py_DECREF(seq); - return list; }