aboutsummaryrefslogtreecommitdiffstats
path: root/libshiboken/helper.cpp
diff options
context:
space:
mode:
authorHugo Parente Lima <hugo.pl@gmail.com>2010-11-09 11:08:59 -0200
committerHugo Parente Lima <hugo.pl@gmail.com>2012-03-08 16:08:56 -0300
commitc3bfbea370fcb19075cd5761ae22691696eaafcd (patch)
treece126374011cfedc6d908ea41c91eea88303d14f /libshiboken/helper.cpp
parentb4957bcc1cde9fa1bb491a166cf163872a6708cc (diff)
Refactor on sequenceToIntArray.
Diffstat (limited to 'libshiboken/helper.cpp')
-rw-r--r--libshiboken/helper.cpp33
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