PyObject *CListValue::Pyindex(PyObject *value) { PyObject *result = NULL; CValue* checkobj = ConvertPythonToValue(value, "val = cList[i]: CValueList, "); if (checkobj==NULL) return NULL; /* ConvertPythonToValue sets the error */ int numelem = GetCount(); for (int i=0;i<numelem;i++) { CValue* elem = GetValue(i); if (checkobj==elem || CheckEqual(checkobj,elem)) { result = PyLong_FromLong(i); break; } } checkobj->Release(); if (result==NULL) { PyErr_SetString(PyExc_ValueError, "CList.index(x): x not in CListValue"); } return result; }
CValue* CValue::ConvertPythonToValue(PyObject* pyobj, const char *error_prefix) { CValue* vallie = NULL; /* refcounting is broking here! - this crashes anyway, just store a python list for KX_GameObject */ #if 0 if (PyList_Check(pyobj)) { CListValue* listval = new CListValue(); bool error = false; Py_ssize_t i; Py_ssize_t numitems = PyList_GET_SIZE(pyobj); for (i=0;i<numitems;i++) { PyObject* listitem = PyList_GetItem(pyobj,i); /* borrowed ref */ CValue* listitemval = ConvertPythonToValue(listitem, error_prefix); if (listitemval) { listval->Add(listitemval); } else { error = true; } } if (!error) { // jippie! could be converted vallie = listval; } else { // list could not be converted... bad luck listval->Release(); } } else #endif if (PyFloat_Check(pyobj)) { vallie = new CFloatValue( (float)PyFloat_AsDouble(pyobj) ); } else if (PyLong_Check(pyobj)) { vallie = new CIntValue( (cInt)PyLong_AsLongLong(pyobj) ); } else if (PyUnicode_Check(pyobj)) { vallie = new CStringValue(_PyUnicode_AsString(pyobj),""); } else if (PyObject_TypeCheck(pyobj, &CValue::Type)) /* Note, dont let these get assigned to GameObject props, must check elsewhere */ { vallie = (static_cast<CValue *>(BGE_PROXY_REF(pyobj)))->AddRef(); } else { /* return an error value from the caller */ PyErr_Format(PyExc_TypeError, "%scould convert python value to a game engine property", error_prefix); } return vallie; }
PyObject *CListValue::Pyappend(PyObject *value) { CValue* objval = ConvertPythonToValue(value, "CList.append(i): CValueList, "); if (!objval) /* ConvertPythonToValue sets the error */ return NULL; if (!BGE_PROXY_PYOWNS(m_proxy)) { PyErr_SetString(PyExc_TypeError, "CList.append(i): this CValueList is used internally for the game engine and can't be modified"); return NULL; } Add(objval); Py_RETURN_NONE; }
PyObject *CListValue::Pycount(PyObject *value) { int numfound = 0; CValue* checkobj = ConvertPythonToValue(value, ""); /* error ignored */ if (checkobj==NULL) { /* in this case just return that there are no items in the list */ PyErr_Clear(); return PyLong_FromLong(0); } int numelem = GetCount(); for (int i=0;i<numelem;i++) { CValue* elem = GetValue(i); if (checkobj==elem || CheckEqual(checkobj,elem)) { numfound ++; } } checkobj->Release(); return PyLong_FromLong(numfound); }