aboutsummaryrefslogtreecommitdiffstats
path: root/libshiboken/helper.cpp
diff options
context:
space:
mode:
authorLauro Neto <lauro.neto@openbossa.org>2010-02-18 17:53:53 -0300
committerLauro Neto <lauro.neto@openbossa.org>2010-02-19 12:59:34 -0300
commit4b0fa3fd45ed42cf881a25897ed66b5c0cf3a3fa (patch)
tree5a5ee9e41f223cdc4cba13ec864972d64067a117 /libshiboken/helper.cpp
parentf425ded9520c965202b62b1c2d172608f803497b (diff)
Adding sequenceToIntArray helper function
Diffstat (limited to 'libshiboken/helper.cpp')
-rw-r--r--libshiboken/helper.cpp37
1 files changed, 37 insertions, 0 deletions
diff --git a/libshiboken/helper.cpp b/libshiboken/helper.cpp
index 4523a2059..3346484e4 100644
--- a/libshiboken/helper.cpp
+++ b/libshiboken/helper.cpp
@@ -69,4 +69,41 @@ PySequenceToArgcArgv(PyObject* argList, int* argc, char*** argv, const char* def
return true;
}
+int*
+sequenceToIntArray(PyObject* obj, bool zeroTerminated)
+{
+ int* array = NULL;
+ int i;
+ int size;
+
+ if (!PySequence_Check(obj)) {
+ PyErr_SetString(PyExc_TypeError, "Sequence of ints expected");
+ return NULL;
+ }
+
+ size = PySequence_Size(obj);
+
+ array = new int[size + zeroTerminated ? 1 : 0];
+
+ for (i = 0; i < size; i++) {
+ PyObject* item = PySequence_GetItem(obj, i);
+ if (!PyInt_Check(item)) {
+ PyErr_SetString(PyExc_TypeError, "Sequence of ints expected");
+ Py_DECREF(item);
+ if (array)
+ delete array;
+ return NULL;
+ } else {
+ array[i] = PyInt_AsLong(item);
+ Py_DECREF(item);
+ }
+ }
+
+ if (zeroTerminated)
+ array[i] = 0;
+
+ return array;
+}
+
+
} // namespace Shiboken