diff options
author | Campbell Barton <ideasman42@gmail.com> | 2012-09-26 03:28:15 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2012-09-26 03:28:15 +0400 |
commit | d08abbee694166429f67d59f6d86962b8d1bed4e (patch) | |
tree | 404721427eee1cbfba068a15bf740189a7ea634a /source | |
parent | 3b2962f2ab755b9185381b9ff29c8abcd775c701 (diff) |
add back game engine python api slicing, (was missing / regression, since move to py3x)
not many people must have used it since it would crash with non-zero start slice values.
Diffstat (limited to 'source')
-rw-r--r-- | source/gameengine/Expressions/ListValue.cpp | 96 |
1 files changed, 52 insertions, 44 deletions
diff --git a/source/gameengine/Expressions/ListValue.cpp b/source/gameengine/Expressions/ListValue.cpp index 88b0a986a05..43f84d273fd 100644 --- a/source/gameengine/Expressions/ListValue.cpp +++ b/source/gameengine/Expressions/ListValue.cpp @@ -316,7 +316,37 @@ static PyObject *listvalue_buffer_item(PyObject *self, Py_ssize_t index) return cval->GetProxy(); } -static PyObject *listvalue_mapping_subscript(PyObject *self, PyObject *pyindex) + +/* just slice it into a python list... */ +static PyObject *listvalue_buffer_slice(CListValue *list, Py_ssize_t start, Py_ssize_t stop) +{ + PyObject *newlist; + Py_ssize_t i, j; + + /* caller needs to validate negative index */ +#if 0 + Py_ssize_t len = list->GetCount(); + + if (start > len) start = len; + if (stop > len) stop = len; +#endif + + newlist = PyList_New(stop - start); + if (!newlist) + return NULL; + + for (i = start, j = 0; i < stop; i++, j++) { + PyObject *pyobj = list->GetValue(i)->ConvertValueToPython(); + if (!pyobj) { + pyobj = list->GetValue(i)->GetProxy(); + } + PyList_SET_ITEM(newlist, j, pyobj); + } + return newlist; +} + + +static PyObject *listvalue_mapping_subscript(PyObject *self, PyObject *key) { CListValue *list= static_cast<CListValue *>(BGE_PROXY_REF(self)); if (list==NULL) { @@ -324,9 +354,8 @@ static PyObject *listvalue_mapping_subscript(PyObject *self, PyObject *pyindex) return NULL; } - if (PyUnicode_Check(pyindex)) - { - CValue *item = ((CListValue*) list)->FindValue(_PyUnicode_AsString(pyindex)); + if (PyUnicode_Check(key)) { + CValue *item = ((CListValue*) list)->FindValue(_PyUnicode_AsString(key)); if (item) { PyObject *pyobj = item->ConvertValueToPython(); if (pyobj) @@ -335,54 +364,33 @@ static PyObject *listvalue_mapping_subscript(PyObject *self, PyObject *pyindex) return item->GetProxy(); } } - else if (PyLong_Check(pyindex)) - { - int index = PyLong_AsSsize_t(pyindex); + else if (PyIndex_Check(key)) { + int index = PyLong_AsSsize_t(key); return listvalue_buffer_item(self, index); /* wont add a ref */ } + else if (PySlice_Check(key)) { + Py_ssize_t start, stop, step, slicelength; - PyErr_Format(PyExc_KeyError, - "CList[key]: '%R' key not in list", pyindex); - return NULL; -} - + if (PySlice_GetIndicesEx(key, list->GetCount(), &start, &stop, &step, &slicelength) < 0) + return NULL; -/* just slice it into a python list... */ -static PyObject *listvalue_buffer_slice(PyObject *self,Py_ssize_t ilow, Py_ssize_t ihigh) -{ - CListValue *list= static_cast<CListValue *>(BGE_PROXY_REF(self)); - if (list==NULL) { - PyErr_SetString(PyExc_SystemError, "val = CList[i:j], "BGE_PROXY_ERROR_MSG); - return NULL; + if (slicelength <= 0) { + return PyList_New(0); + } + else if (step == 1) { + return listvalue_buffer_slice(list, start, stop); + } + else { + PyErr_SetString(PyExc_TypeError, "CList[slice]: slice steps not supported"); + return NULL; + } } - - int i, j; - PyObject *newlist; - - if (ilow < 0) ilow = 0; - - int n = ((CListValue*) list)->GetCount(); - if (ihigh >= n) - ihigh = n; - if (ihigh < ilow) - ihigh = ilow; - - newlist = PyList_New(ihigh - ilow); - if (!newlist) - return NULL; - - for (i = ilow, j = 0; i < ihigh; i++, j++) - { - PyObject *pyobj = list->GetValue(i)->ConvertValueToPython(); - if (!pyobj) - pyobj = list->GetValue(i)->GetProxy(); - PyList_SET_ITEM(newlist, i, pyobj); - } - return newlist; + PyErr_Format(PyExc_KeyError, + "CList[key]: '%R' key not in list", key); + return NULL; } - /* clist + list, return a list that python owns */ static PyObject *listvalue_buffer_concat(PyObject *self, PyObject *other) { |