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.
This commit is contained in:
Campbell Barton
2009-05-26 16:15:40 +00:00
parent 7e48820a97
commit 33b974ee43
7 changed files with 132 additions and 22 deletions

View File

@@ -1190,7 +1190,11 @@ PyMethodDef KX_GameObject::Methods[] = {
KX_PYMETHODTABLE_O(KX_GameObject, getDistanceTo),
KX_PYMETHODTABLE_O(KX_GameObject, getVectTo),
KX_PYMETHODTABLE(KX_GameObject, sendMessage),
// dict style access for props
{"has_key",(PyCFunction) KX_GameObject::sPyhas_key, METH_O},
{"get",(PyCFunction) KX_GameObject::sPyget, METH_VARARGS},
// deprecated
{"getPosition", (PyCFunction) KX_GameObject::sPyGetPosition, METH_NOARGS},
{"setPosition", (PyCFunction) KX_GameObject::sPySetPosition, METH_O},
@@ -1899,6 +1903,8 @@ int KX_GameObject::py_setattro(PyObject *attr, PyObject *value) // py_setattro m
int KX_GameObject::py_delattro(PyObject *attr)
{
ShowDeprecationWarning("del ob.attr", "del ob['attr'] for user defined properties");
char *attr_str= PyString_AsString(attr);
if (RemoveProperty(attr_str)) // XXX - should call CValues instead but its only 2 lines here
@@ -2737,6 +2743,48 @@ KX_PYMETHODDEF_DOC_VARARGS(KX_GameObject, sendMessage,
Py_RETURN_NONE;
}
/* dict style access */
/* Matches python dict.get(key, [default]) */
PyObject* KX_GameObject::Pyget(PyObject *args)
{
PyObject *key;
PyObject* def = Py_None;
PyObject* ret;
if (!PyArg_ParseTuple(args, "O|O:get", &key, &def))
return NULL;
if(PyString_Check(key)) {
CValue *item = GetProperty(PyString_AsString(key));
if (item)
return item->GetProxy();
}
if (m_attr_dict && (ret=PyDict_GetItem(m_attr_dict, key))) {
Py_INCREF(ret);
return ret;
}
Py_INCREF(def);
return def;
}
/* Matches python dict.has_key() */
PyObject* KX_GameObject::Pyhas_key(PyObject* value)
{
if(PyString_Check(value) && GetProperty(PyString_AsString(value)))
Py_RETURN_TRUE;
if (m_attr_dict && PyDict_GetItem(m_attr_dict, value))
Py_RETURN_TRUE;
Py_RETURN_FALSE;
}
/* ---------------------------------------------------------------------
* Some stuff taken from the header
* --------------------------------------------------------------------- */