From c3bfbea370fcb19075cd5761ae22691696eaafcd Mon Sep 17 00:00:00 2001 From: Hugo Parente Lima Date: Tue, 9 Nov 2010 11:08:59 -0200 Subject: Refactor on sequenceToIntArray. --- libshiboken/helper.cpp | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) (limited to 'libshiboken') diff --git a/libshiboken/helper.cpp b/libshiboken/helper.cpp index 297c2b48f..50bf6941c 100644 --- a/libshiboken/helper.cpp +++ b/libshiboken/helper.cpp @@ -63,41 +63,30 @@ bool sequenceToArgcArgv(PyObject* argList, int* argc, char*** argv, const char* return true; } -int* -sequenceToIntArray(PyObject* obj, bool zeroTerminated) +int* sequenceToIntArray(PyObject* obj, bool zeroTerminated) { - int* array = NULL; - int i; - int size; + AutoDecRef seq(PySequence_Fast(obj, "Sequence of ints expected")); + if (seq.isNull()) + return 0; - if (!PySequence_Check(obj)) { - PyErr_SetString(PyExc_TypeError, "Sequence of ints expected"); - return NULL; - } - - size = PySequence_Size(obj); + Py_ssize_t size = PySequence_Fast_GET_SIZE(seq.object()); + int* array = new int[size + (zeroTerminated ? 1 : 0)]; - array = new int[size + (zeroTerminated ? 1 : 0)]; - - for (i = 0; i < size; i++) { - PyObject* item = PySequence_GetItem(obj, i); + for (int i = 0; i < size; i++) { + PyObject* item = PySequence_Fast_GET_ITEM(seq.object(), i); if (!PyInt_Check(item)) { PyErr_SetString(PyExc_TypeError, "Sequence of ints expected"); - Py_DECREF(item); - if (array) - delete[] array; - return NULL; + delete[] array; + return 0; } else { array[i] = PyInt_AsLong(item); - Py_DECREF(item); } } if (zeroTerminated) - array[i] = 0; + array[size] = 0; return array; } - } // namespace Shiboken -- cgit v1.2.3