diff options
author | Campbell Barton <ideasman42@gmail.com> | 2009-04-12 13:56:30 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2009-04-12 13:56:30 +0400 |
commit | 5b306b7541bfc60342b70bcc55456d5c453a294a (patch) | |
tree | 267232e135a123f3f75051a9732a9ab35370d769 /source/gameengine/Expressions | |
parent | 55d2b184ec1b2c1adfe182ead937cedae1a8208d (diff) |
BGE Python API
added defines PY_SET_ATTR_FAIL, PY_SET_ATTR_MISSING and PY_SET_ATTR_SUCCESS
This is useful when objects that have user defined attributes (GameObject and Scene)
When calling setattr on the parent, a return value of PY_SET_ATTR_FAIL means the attribute exists but failed to be set, so don't set the custom attribute.
Diffstat (limited to 'source/gameengine/Expressions')
-rw-r--r-- | source/gameengine/Expressions/PyObjectPlus.cpp | 6 | ||||
-rw-r--r-- | source/gameengine/Expressions/PyObjectPlus.h | 14 | ||||
-rw-r--r-- | source/gameengine/Expressions/Value.cpp | 4 |
3 files changed, 17 insertions, 7 deletions
diff --git a/source/gameengine/Expressions/PyObjectPlus.cpp b/source/gameengine/Expressions/PyObjectPlus.cpp index 4526f8f649b..33335ebef3e 100644 --- a/source/gameengine/Expressions/PyObjectPlus.cpp +++ b/source/gameengine/Expressions/PyObjectPlus.cpp @@ -137,7 +137,7 @@ int PyObjectPlus::py_setattro(PyObject *attr, PyObject* value) //return PyObject::py_setattro(attr,value); //cerr << "Unknown attribute" << endl; PyErr_SetString(PyExc_AttributeError, "attribute cant be set"); - return 1; + return PY_SET_ATTR_MISSING; } PyObject *PyObjectPlus::py_get_attrdef(void *self, const PyAttributeDef *attrdef) @@ -699,13 +699,13 @@ int PyObjectPlus::py_setattro_self(const PyAttributeDef attrlist[], void *self, attrdef->m_type == KX_PYATTRIBUTE_TYPE_DUMMY) { PyErr_SetString(PyExc_AttributeError, "property is read-only"); - return 1; + return PY_SET_ATTR_FAIL; } return py_set_attrdef(self, attrdef, value); } } - return -1; + return PY_SET_ATTR_MISSING; } #endif diff --git a/source/gameengine/Expressions/PyObjectPlus.h b/source/gameengine/Expressions/PyObjectPlus.h index 42168461634..f61b9f8d0b8 100644 --- a/source/gameengine/Expressions/PyObjectPlus.h +++ b/source/gameengine/Expressions/PyObjectPlus.h @@ -124,6 +124,16 @@ static inline void Py_Fatal(const char *M) { return NULL; +/* + * nonzero values are an error for setattr + * however because of the nested lookups we need to know if the errors + * was because the attribute didnt exits of if there was some problem setting the value + */ + +#define PY_SET_ATTR_FAIL 1 +#define PY_SET_ATTR_MISSING -1 +#define PY_SET_ATTR_SUCCESS 0 + #define py_setattro_up(Parent) \ PyObject *descr = PyDict_GetItem(Type.tp_dict, attr); \ \ @@ -132,14 +142,14 @@ static inline void Py_Fatal(const char *M) { const PyAttributeDef* attrdef= reinterpret_cast<const PyAttributeDef *>(PyCObject_AsVoidPtr(descr)); \ if (attrdef->m_access == KX_PYATTRIBUTE_RO) { \ PyErr_Format(PyExc_AttributeError, "\"%s\" is read only", PyString_AsString(attr)); \ - return -1; \ + return PY_SET_ATTR_FAIL; \ } \ else { \ return py_set_attrdef((void *)this, attrdef, value); \ } \ } else { \ PyErr_Format(PyExc_AttributeError, "\"%s\" cannot be set", PyString_AsString(attr)); \ - return -1; \ + return PY_SET_ATTR_FAIL; \ } \ } else { \ PyErr_Clear(); \ diff --git a/source/gameengine/Expressions/Value.cpp b/source/gameengine/Expressions/Value.cpp index 47f0686c4c3..f1e367d6e59 100644 --- a/source/gameengine/Expressions/Value.cpp +++ b/source/gameengine/Expressions/Value.cpp @@ -804,11 +804,11 @@ int CValue::py_setattro(PyObject *attr, PyObject* pyobj) vallie->Release(); } else { - return 1; /* ConvertPythonToValue sets the error message */ + return PY_SET_ATTR_FAIL; /* ConvertPythonToValue sets the error message */ } //PyObjectPlus::py_setattro(attr,value); - return 0; + return PY_SET_ATTR_SUCCESS; }; PyObject* CValue::ConvertKeysToPython( void ) |