diff options
author | Campbell Barton <ideasman42@gmail.com> | 2009-06-12 16:56:12 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2009-06-12 16:56:12 +0400 |
commit | 1c2ce9535caa7ef0ed70aea5bd25c0f281322cf6 (patch) | |
tree | 8c8203d3b4d9ce40fc52aec23099c99f8d82c759 /source/gameengine/Expressions/ListValue.cpp | |
parent | 96c5f36eff4ee85aad90c507070447a6ad5bdda9 (diff) |
use contains for ListValue and KX_GameObject types (has_key is deprecated by python)
eg.
if 'prop' in gameOb: ...
if 'GameOb' in sce.objects: ...
Diffstat (limited to 'source/gameengine/Expressions/ListValue.cpp')
-rw-r--r-- | source/gameengine/Expressions/ListValue.cpp | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/source/gameengine/Expressions/ListValue.cpp b/source/gameengine/Expressions/ListValue.cpp index a0d73c75d60..ea097ddff5b 100644 --- a/source/gameengine/Expressions/ListValue.cpp +++ b/source/gameengine/Expressions/ListValue.cpp @@ -209,6 +209,30 @@ static PyObject *listvalue_buffer_concat(PyObject * self, PyObject * other) return listval_new->NewProxy(true); /* python owns this list */ } +static int listvalue_buffer_contains(PyObject *self_v, PyObject *value) +{ + CListValue *self= static_cast<CListValue *>(BGE_PROXY_REF(self_v)); + + if (self==NULL) { + PyErr_SetString(PyExc_SystemError, "val in CList, "BGE_PROXY_ERROR_MSG); + return -1; + } + + if (PyString_Check(value)) { + if (self->FindValue((const char *)PyString_AsString(value))) { + return 1; + } + } + else if (BGE_PROXY_CHECK_TYPE(value)) { /* not dict like at all but this worked before __contains__ was used */ + CValue *item= static_cast<CValue *>(BGE_PROXY_REF(value)); + for (int i=0; i < self->GetCount(); i++) + if (self->GetValue(i) == item) // Com + return 1; + + } // not using CheckEqual + + return 0; +} static PySequenceMethods listvalue_as_sequence = { @@ -225,6 +249,7 @@ static PySequenceMethods listvalue_as_sequence = { NULL, /*sq_ass_item*/ NULL, /*sq_ass_slice*/ #endif + (objobjproc)listvalue_buffer_contains, /* sq_contains */ }; @@ -264,7 +289,9 @@ PyTypeObject CListValue::Type = { 0, py_base_getattro, py_base_setattro, - 0,0,0,0,0,0,0,0,0, + 0, + Py_TPFLAGS_DEFAULT, + 0,0,0,0,0,0,0, Methods }; @@ -499,7 +526,7 @@ PyObject* CListValue::Pyreverse() bool CListValue::CheckEqual(CValue* first,CValue* second) { bool result = false; - + CValue* eqval = ((CValue*)first)->Calc(VALUE_EQL_OPERATOR,(CValue*)second); if (eqval==NULL) @@ -528,7 +555,7 @@ PyObject* CListValue::Pyindex(PyObject *value) for (int i=0;i<numelem;i++) { CValue* elem = GetValue(i); - if (CheckEqual(checkobj,elem)) + if (checkobj==elem || CheckEqual(checkobj,elem)) { result = PyInt_FromLong(i); break; @@ -560,7 +587,7 @@ PyObject* CListValue::Pycount(PyObject* value) for (int i=0;i<numelem;i++) { CValue* elem = GetValue(i); - if (CheckEqual(checkobj,elem)) + if (checkobj==elem || CheckEqual(checkobj,elem)) { numfound ++; } |