Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2009-05-26 20:15:40 +0400
committerCampbell Barton <ideasman42@gmail.com>2009-05-26 20:15:40 +0400
commit33b974ee43c2d6c131860efbe02fd478197b9fda (patch)
tree459ef7b438e969938baa3f6049e5d0668ea62926 /source/gameengine/Expressions
parent7e48820a97a1cc5d806caa2c7ea5b47f4809aa58 (diff)
BGE Py API
- Deprecation warnings for using attribute access - Added dictionary functions to KX_GameObject and ListValue ob.get(key, default=None) ob.has_key(key) ob.has_key is important since there was no way to do something like hasattr(ob, "attr") which can be replaced by ob.has_key("attr") - (both still work of course). ob.get is just useful in many cases where you want a property if it exists but can fallback to a default. - CListValue::FindValue was adding a reference but the ~3 places it was used were releasing the reference. added a FindValue that accepts a const char* type to avoid converting python strings to STR_String.
Diffstat (limited to 'source/gameengine/Expressions')
-rw-r--r--source/gameengine/Expressions/ListValue.cpp65
-rw-r--r--source/gameengine/Expressions/ListValue.h3
-rw-r--r--source/gameengine/Expressions/Value.cpp6
3 files changed, 54 insertions, 20 deletions
diff --git a/source/gameengine/Expressions/ListValue.cpp b/source/gameengine/Expressions/ListValue.cpp
index 75ae2cb787f..c741a6d8809 100644
--- a/source/gameengine/Expressions/ListValue.cpp
+++ b/source/gameengine/Expressions/ListValue.cpp
@@ -76,13 +76,9 @@ PyObject* listvalue_mapping_subscript(PyObject* self, PyObject* pyindex)
if (PyString_Check(pyindex))
{
- const char *index = PyString_AsString(pyindex);
- CValue *item = ((CListValue*) list)->FindValue(index);
+ CValue *item = ((CListValue*) list)->FindValue(PyString_AsString(pyindex));
if (item)
- {
- item->Release(); /* FindValue() AddRef's */
return item->GetProxy();
- }
}
else if (PyInt_Check(pyindex))
{
@@ -279,10 +275,17 @@ PyParentObject CListValue::Parents[] = {
PyMethodDef CListValue::Methods[] = {
+ /* List style access */
{"append", (PyCFunction)CListValue::sPyappend,METH_O},
{"reverse", (PyCFunction)CListValue::sPyreverse,METH_NOARGS},
{"index", (PyCFunction)CListValue::sPyindex,METH_O},
{"count", (PyCFunction)CListValue::sPycount,METH_O},
+
+ /* Dict style access */
+ {"get", (PyCFunction)CListValue::sPyget,METH_VARARGS},
+ {"has_key", (PyCFunction)CListValue::sPyhas_key,METH_O},
+
+ /* Own cvalue funcs */
{"from_id", (PyCFunction)CListValue::sPyfrom_id,METH_O},
{NULL,NULL} //Sentinel
@@ -395,25 +398,23 @@ void CListValue::ReleaseAndRemoveAll()
-CValue* CListValue::FindValue(const char * name)
+CValue* CListValue::FindValue(const STR_String & name)
{
- CValue* resultval = NULL;
- int i=0;
+ for (int i=0; i < GetCount(); i++)
+ if (GetValue(i)->GetName() == name)
+ return GetValue(i);
- while (!resultval && i < GetCount())
- {
- CValue* myval = GetValue(i);
-
- if (myval->GetName() == name)
- resultval = GetValue(i)->AddRef(); // add referencecount
- else
- i++;
-
- }
- return resultval;
+ return NULL;
}
-
+CValue* CListValue::FindValue(const char * name)
+{
+ for (int i=0; i < GetCount(); i++)
+ if (GetValue(i)->GetName() == name)
+ return GetValue(i);
+
+ return NULL;
+}
bool CListValue::SearchValue(CValue *val)
{
@@ -564,7 +565,31 @@ PyObject* CListValue::Pycount(PyObject* value)
return PyInt_FromLong(numfound);
}
+/* Matches python dict.get(key, [default]) */
+PyObject* CListValue::Pyget(PyObject *args)
+{
+ char *key;
+ PyObject* def = Py_None;
+
+ if (!PyArg_ParseTuple(args, "s|O:get", &key, &def))
+ return NULL;
+
+ CValue *item = FindValue((const char *)key);
+ if (item)
+ return item->GetProxy();
+
+ Py_INCREF(def);
+ return def;
+}
+/* Matches python dict.has_key() */
+PyObject* CListValue::Pyhas_key(PyObject* value)
+{
+ if (PyString_Check(value) && FindValue((const char *)PyString_AsString(value)))
+ Py_RETURN_TRUE;
+
+ Py_RETURN_FALSE;
+}
PyObject* CListValue::Pyfrom_id(PyObject* value)
{
diff --git a/source/gameengine/Expressions/ListValue.h b/source/gameengine/Expressions/ListValue.h
index ad918cbb925..68e900e25e0 100644
--- a/source/gameengine/Expressions/ListValue.h
+++ b/source/gameengine/Expressions/ListValue.h
@@ -45,6 +45,7 @@ public:
void SetReleaseOnDestruct(bool bReleaseContents);
bool SearchValue(CValue* val);
+ CValue* FindValue(const STR_String & name);
CValue* FindValue(const char *name);
void ReleaseAndRemoveAll();
@@ -74,6 +75,8 @@ public:
KX_PYMETHOD_NOARGS(CListValue,reverse);
KX_PYMETHOD_O(CListValue,index);
KX_PYMETHOD_O(CListValue,count);
+ KX_PYMETHOD_VARARGS(CListValue,get);
+ KX_PYMETHOD_O(CListValue,has_key);
KX_PYMETHOD_O(CListValue,from_id);
diff --git a/source/gameengine/Expressions/Value.cpp b/source/gameengine/Expressions/Value.cpp
index 08249f92902..7f4ed388442 100644
--- a/source/gameengine/Expressions/Value.cpp
+++ b/source/gameengine/Expressions/Value.cpp
@@ -556,6 +556,8 @@ PyAttributeDef CValue::Attributes[] = {
PyObject* CValue::py_getattro(PyObject *attr)
{
+ ShowDeprecationWarning("val = ob.attr", "val = ob['attr']");
+
char *attr_str= PyString_AsString(attr);
CValue* resultattr = GetProperty(attr_str);
if (resultattr)
@@ -655,6 +657,8 @@ CValue* CValue::ConvertPythonToValue(PyObject* pyobj, const char *error_prefix)
int CValue::py_delattro(PyObject *attr)
{
+ ShowDeprecationWarning("del ob.attr", "del ob['attr']");
+
char *attr_str= PyString_AsString(attr);
if (RemoveProperty(attr_str))
return 0;
@@ -665,6 +669,8 @@ int CValue::py_delattro(PyObject *attr)
int CValue::py_setattro(PyObject *attr, PyObject* pyobj)
{
+ ShowDeprecationWarning("ob.attr = val", "ob['attr'] = val");
+
char *attr_str= PyString_AsString(attr);
CValue* oldprop = GetProperty(attr_str);
CValue* vallie;