diff options
author | Hugo Parente Lima <hugo.pl@gmail.com> | 2010-11-09 11:08:59 -0200 |
---|---|---|
committer | Hugo Parente Lima <hugo.pl@gmail.com> | 2012-03-08 16:08:56 -0300 |
commit | c3bfbea370fcb19075cd5761ae22691696eaafcd (patch) | |
tree | ce126374011cfedc6d908ea41c91eea88303d14f /libshiboken | |
parent | b4957bcc1cde9fa1bb491a166cf163872a6708cc (diff) |
Refactor on sequenceToIntArray.
Diffstat (limited to 'libshiboken')
-rw-r--r-- | libshiboken/helper.cpp | 33 |
1 files changed, 11 insertions, 22 deletions
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 |