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> | 2010-11-10 15:36:28 -0200 |
commit | c948d05782056d9b12ab0ccf04fa4e1b30478296 (patch) | |
tree | 41593b720751d52a8efa582a8284ee53729072f9 /libshiboken | |
parent | 47006421772b9183d8a6a0fd84ff73f5c35b3f07 (diff) | |
download | shiboken-c948d05782056d9b12ab0ccf04fa4e1b30478296.tar.gz shiboken-c948d05782056d9b12ab0ccf04fa4e1b30478296.tar.xz shiboken-c948d05782056d9b12ab0ccf04fa4e1b30478296.zip |
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 297c2b48..50bf6941 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 |