diff options
Diffstat (limited to 'source/gameengine/Expressions/ListValue.cpp')
-rw-r--r-- | source/gameengine/Expressions/ListValue.cpp | 109 |
1 files changed, 48 insertions, 61 deletions
diff --git a/source/gameengine/Expressions/ListValue.cpp b/source/gameengine/Expressions/ListValue.cpp index 9ffdbb1223c..90a939af236 100644 --- a/source/gameengine/Expressions/ListValue.cpp +++ b/source/gameengine/Expressions/ListValue.cpp @@ -34,7 +34,12 @@ Py_ssize_t listvalue_bufferlen(PyObject* list) PyObject* listvalue_buffer_item(PyObject* list,Py_ssize_t index) { - if (index >= 0 && index < ((CListValue*) list)->GetCount()) + int count = ((CListValue*) list)->GetCount(); + + if (index < 0) + index = count+index; + + if (index >= 0 && index < count) { PyObject* pyobj = ((CListValue*) list)->GetValue(index)->ConvertValueToPython(); if (pyobj) @@ -64,8 +69,7 @@ PyObject* listvalue_mapping_subscript(PyObject* list,PyObject* pyindex) } PyObject *pyindex_str = PyObject_Repr(pyindex); /* new ref */ - STR_String index_str(PyString_AsString(pyindex_str)); - PyErr_Format(PyExc_KeyError, "'%s' not in list", index_str.Ptr()); + PyErr_Format(PyExc_KeyError, "'%s' not in list", PyString_AsString(pyindex_str)); Py_DECREF(pyindex_str); return NULL; } @@ -220,17 +224,19 @@ PyParentObject CListValue::Parents[] = { PyMethodDef CListValue::Methods[] = { - {"append", (PyCFunction)CListValue::sPyappend,METH_VARARGS}, - {"reverse", (PyCFunction)CListValue::sPyreverse,METH_VARARGS}, - {"index", (PyCFunction)CListValue::sPyindex,METH_VARARGS}, - {"count", (PyCFunction)CListValue::sPycount,METH_VARARGS}, + {"append", (PyCFunction)CListValue::sPyappend,METH_O}, + {"reverse", (PyCFunction)CListValue::sPyreverse,METH_NOARGS}, + {"index", (PyCFunction)CListValue::sPyindex,METH_O}, + {"count", (PyCFunction)CListValue::sPycount,METH_O}, {NULL,NULL} //Sentinel }; +PyAttributeDef CListValue::Attributes[] = { + { NULL } //Sentinel +}; - -PyObject* CListValue::_getattr(const STR_String& attr) { +PyObject* CListValue::_getattr(const char *attr) { _getattr_up(CValue); } @@ -399,34 +405,17 @@ void CListValue::MergeList(CListValue *otherlist) -PyObject* CListValue::Pyappend(PyObject* self, - PyObject* args, - PyObject* kwds) +PyObject* CListValue::Pyappend(PyObject* self, PyObject* value) { - - PyObject* pyobj = NULL; - if (PyArg_ParseTuple(args,"O",&pyobj)) - { - return listvalue_buffer_concat(self,pyobj); - } - else - { - return NULL; - } - - + return listvalue_buffer_concat(self, value); } -PyObject* CListValue::Pyreverse(PyObject* self, - PyObject* args, - PyObject* kwds) +PyObject* CListValue::Pyreverse(PyObject* self) { std::reverse(m_pValueArray.begin(),m_pValueArray.end()); - - Py_Return; - + Py_RETURN_NONE; } @@ -448,58 +437,56 @@ bool CListValue::CheckEqual(CValue* first,CValue* second) -PyObject* CListValue::Pyindex(PyObject* self, - PyObject* args, - PyObject* kwds) +PyObject* CListValue::Pyindex(PyObject* self, PyObject *value) { PyObject* result = NULL; - PyObject* pyobj = NULL; - if (PyArg_ParseTuple(args,"O",&pyobj)) + CValue* checkobj = ConvertPythonToValue(value); + if (checkobj==NULL) + return NULL; /* ConvertPythonToValue sets the error */ + + int numelem = GetCount(); + for (int i=0;i<numelem;i++) { - - CValue* checkobj = ConvertPythonToValue(pyobj); - int numelem = GetCount(); - for (int i=0;i<numelem;i++) + CValue* elem = GetValue(i); + if (CheckEqual(checkobj,elem)) { - CValue* elem = GetValue(i); - if (CheckEqual(checkobj,elem)) - { - result = PyInt_FromLong(i); - break; - } + result = PyInt_FromLong(i); + break; } - checkobj->Release(); } + checkobj->Release(); + if (result==NULL) { + PyErr_SetString(PyExc_ValueError, "ValueError: list.index(x): x not in CListValue"); + } return result; } -PyObject* CListValue::Pycount(PyObject* self, - PyObject* args, - PyObject* kwds) +PyObject* CListValue::Pycount(PyObject* self, PyObject* value) { - int numfound = 0; - PyObject* pyobj = NULL; - if (PyArg_ParseTuple(args,"O",&pyobj)) + CValue* checkobj = ConvertPythonToValue(value); + + if (checkobj==NULL) { /* in this case just return that there are no items in the list */ + PyErr_Clear(); + PyInt_FromLong(0); + } + + int numelem = GetCount(); + for (int i=0;i<numelem;i++) { - CValue* checkobj = ConvertPythonToValue(pyobj); - int numelem = GetCount(); - for (int i=0;i<numelem;i++) + CValue* elem = GetValue(i); + if (CheckEqual(checkobj,elem)) { - CValue* elem = GetValue(i); - if (CheckEqual(checkobj,elem)) - { - numfound ++; - } + numfound ++; } - checkobj->Release(); } + checkobj->Release(); return PyInt_FromLong(numfound); } |