Merge pull request #3098 from uploadcare/getlist-fixes

Raise error if it is occurred during conversion in getlist
This commit is contained in:
Alexander Karpinsky 2018-04-14 18:07:57 +03:00 committed by GitHub
commit 9d404e9979
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 8 deletions

View File

@ -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)

View File

@ -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;
}