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-04-23 04:32:33 +0400
committerCampbell Barton <ideasman42@gmail.com>2009-04-23 04:32:33 +0400
commit6a270ecb9435fd695988de5c685c65cac5a43c79 (patch)
tree6bc0b992e1a1ee44b739be6e76b860ae6c0c8aba /source/gameengine/Expressions/Value.cpp
parentd568794a9873eed10a9d240c1e1a1ba52aba929c (diff)
BGE Python API
CListValue fixes - Disable changing CValueLists that the BGE uses internally (scene.objects.append(1) would crash when drawing) - val=clist+list would modify clist in place, now return a new value. - clist.append([....]), was working like extend. - clist.append(val) didnt work for most CValue types like KX_GameObjects. Other changes - "isValid" was always returning True. - Set all errors for invalid proxy access to PyExc_SystemError (was using a mix of error types) - Added PyObjectPlus::InvalidateProxy() to manually invalidate, though if python ever gains access again, it will make a new valid proxy. This is so removing an object from a scene can invalidate the object even if its stored elsewhere in a CValueList for eg.
Diffstat (limited to 'source/gameengine/Expressions/Value.cpp')
-rw-r--r--source/gameengine/Expressions/Value.cpp28
1 files changed, 19 insertions, 9 deletions
diff --git a/source/gameengine/Expressions/Value.cpp b/source/gameengine/Expressions/Value.cpp
index b86fbe0b163..6e8f2ba1061 100644
--- a/source/gameengine/Expressions/Value.cpp
+++ b/source/gameengine/Expressions/Value.cpp
@@ -604,7 +604,7 @@ PyObject* CValue::py_getattro_dict() {
py_getattro_dict_up(PyObjectPlus);
}
-CValue* CValue::ConvertPythonToValue(PyObject* pyobj)
+CValue* CValue::ConvertPythonToValue(PyObject* pyobj, const char *error_prefix)
{
CValue* vallie = NULL;
@@ -620,7 +620,7 @@ CValue* CValue::ConvertPythonToValue(PyObject* pyobj)
for (i=0;i<numitems;i++)
{
PyObject* listitem = PyList_GetItem(pyobj,i); /* borrowed ref */
- CValue* listitemval = ConvertPythonToValue(listitem);
+ CValue* listitemval = ConvertPythonToValue(listitem, error_prefix);
if (listitemval)
{
listval->Add(listitemval);
@@ -657,13 +657,22 @@ CValue* CValue::ConvertPythonToValue(PyObject* pyobj)
{
vallie = new CStringValue(PyString_AsString(pyobj),"");
} else
- if (pyobj->ob_type==&CValue::Type || pyobj->ob_type==&CListValue::Type)
+ if (BGE_PROXY_CHECK_TYPE(pyobj)) /* Note, dont let these get assigned to GameObject props, must check elsewhere */
{
- vallie = ((CValue*) pyobj)->AddRef();
+ if (BGE_PROXY_REF(pyobj) && (BGE_PROXY_REF(pyobj))->isA(&CValue::Type))
+ {
+ vallie = (static_cast<CValue *>(BGE_PROXY_REF(pyobj)))->AddRef();
+ } else {
+
+ if(BGE_PROXY_REF(pyobj)) /* this is not a CValue */
+ PyErr_Format(PyExc_TypeError, "%sgame engine python type cannot be used as a property", error_prefix);
+ else /* PyObjectPlus_Proxy has been removed, cant use */
+ PyErr_Format(PyExc_SystemError, "%s"BGE_PROXY_ERROR_MSG, error_prefix);
+ }
} else
{
/* return an error value from the caller */
- PyErr_SetString(PyExc_TypeError, "This python type could not be converted to a to a game engine property");
+ PyErr_Format(PyExc_TypeError, "%scould convert python value to a game engine property", error_prefix);
}
return vallie;
@@ -682,10 +691,11 @@ int CValue::py_delattro(PyObject *attr)
int CValue::py_setattro(PyObject *attr, PyObject* pyobj)
{
char *attr_str= PyString_AsString(attr);
- CValue* oldprop = GetProperty(attr_str);
-
- CValue* vallie = ConvertPythonToValue(pyobj);
- if (vallie)
+ CValue* oldprop = GetProperty(attr_str);
+ CValue* vallie;
+
+ /* Dissallow python to assign GameObjects, Scenes etc as values */
+ if ((BGE_PROXY_CHECK_TYPE(pyobj)==0) && (vallie = ConvertPythonToValue(pyobj, "cvalue.attr = value: ")))
{
if (oldprop)
oldprop->SetValue(vallie);