From 217bbb7800679899302ddb058f9ceea1d00c7ce1 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Mon, 20 Apr 2009 23:17:52 +0000 Subject: BGE Python API Separate getting a normal attribute and getting __dict__, was having to do too a check for __dict__ on each class (multiple times per getattro call from python) when its not used that often. --- source/gameengine/Expressions/ListValue.cpp | 4 ++++ source/gameengine/Expressions/ListValue.h | 1 + source/gameengine/Expressions/PyObjectPlus.cpp | 15 +++++++++++---- source/gameengine/Expressions/PyObjectPlus.h | 11 ++++------- source/gameengine/Expressions/Value.cpp | 4 ++++ source/gameengine/Expressions/Value.h | 1 + 6 files changed, 25 insertions(+), 11 deletions(-) (limited to 'source/gameengine/Expressions') diff --git a/source/gameengine/Expressions/ListValue.cpp b/source/gameengine/Expressions/ListValue.cpp index dd9b296dce1..7c31a29f4ac 100644 --- a/source/gameengine/Expressions/ListValue.cpp +++ b/source/gameengine/Expressions/ListValue.cpp @@ -273,6 +273,10 @@ PyObject* CListValue::py_getattro(PyObject* attr) { py_getattro_up(CValue); } +PyObject* CListValue::py_getattro_dict() { + py_getattro_dict_up(CValue); +} + ////////////////////////////////////////////////////////////////////// // Construction/Destruction diff --git a/source/gameengine/Expressions/ListValue.h b/source/gameengine/Expressions/ListValue.h index 2af5a330c43..3d88b5aea9c 100644 --- a/source/gameengine/Expressions/ListValue.h +++ b/source/gameengine/Expressions/ListValue.h @@ -60,6 +60,7 @@ public: bool CheckEqual(CValue* first,CValue* second); virtual PyObject* py_getattro(PyObject* attr); + virtual PyObject* py_getattro_dict(); virtual PyObject* py_repr(void) { PyObject *py_proxy= this->GetProxy(); PyObject *py_list= PySequence_List(py_proxy); diff --git a/source/gameengine/Expressions/PyObjectPlus.cpp b/source/gameengine/Expressions/PyObjectPlus.cpp index 6cfa14ddc80..c4daaff2dd1 100644 --- a/source/gameengine/Expressions/PyObjectPlus.cpp +++ b/source/gameengine/Expressions/PyObjectPlus.cpp @@ -143,7 +143,13 @@ PyObject *PyObjectPlus::py_base_getattro(PyObject * self, PyObject *attr) PyErr_SetString(PyExc_RuntimeError, BGE_PROXY_ERROR_MSG); return NULL; } - return self_plus->py_getattro(attr); + + PyObject *ret= self_plus->py_getattro(attr); + + if(ret==NULL && (strcmp(PyString_AsString(attr), "__dict__")==0)) + ret= self_plus->py_getattro_dict(); + + return ret; } /* This should be the entry in Type since it takes the C++ class from PyObjectPlus_Proxy */ @@ -177,9 +183,6 @@ PyObject *PyObjectPlus::py_getattro(PyObject* attr) { PyObject *descr = PyDict_GetItem(Type.tp_dict, attr); \ if (descr == NULL) { - if (strcmp(PyString_AsString(attr), "__dict__")==0) { - return py_getattr_dict(NULL, Type.tp_dict); /* no Attributes yet */ - } PyErr_Format(PyExc_AttributeError, "attribute \"%s\" not found", PyString_AsString(attr)); return NULL; } else { @@ -196,6 +199,10 @@ PyObject *PyObjectPlus::py_getattro(PyObject* attr) } } +PyObject* PyObjectPlus::py_getattro_dict() { + return py_getattr_dict(NULL, Type.tp_dict); +} + int PyObjectPlus::py_delattro(PyObject* attr) { PyErr_SetString(PyExc_AttributeError, "attribute cant be deleted"); diff --git a/source/gameengine/Expressions/PyObjectPlus.h b/source/gameengine/Expressions/PyObjectPlus.h index 370717a919b..b0ddfa04e32 100644 --- a/source/gameengine/Expressions/PyObjectPlus.h +++ b/source/gameengine/Expressions/PyObjectPlus.h @@ -130,16 +130,12 @@ typedef struct { } \ } else { \ PyErr_Clear(); \ - PyObject *rvalue= Parent::py_getattro(attr); \ - \ - if (strcmp(PyString_AsString(attr), "__dict__")==0) { \ - return py_getattr_dict(rvalue, Type.tp_dict); \ - } \ - \ - return rvalue; \ + return Parent::py_getattro(attr); \ } \ return NULL; +#define py_getattro_dict_up(Parent) \ + return py_getattr_dict(Parent::py_getattro_dict(), Type.tp_dict); /* * nonzero values are an error for setattr @@ -434,6 +430,7 @@ public: /* These are all virtual python methods that are defined in each class * Our own fake subclassing calls these on each class, then calls the parent */ virtual PyObject* py_getattro(PyObject *attr); + virtual PyObject* py_getattro_dict(); virtual int py_delattro(PyObject *attr); virtual int py_setattro(PyObject *attr, PyObject *value); virtual PyObject* py_repr(void); diff --git a/source/gameengine/Expressions/Value.cpp b/source/gameengine/Expressions/Value.cpp index 7cb97909119..106bd1256a6 100644 --- a/source/gameengine/Expressions/Value.cpp +++ b/source/gameengine/Expressions/Value.cpp @@ -612,6 +612,10 @@ PyObject* CValue::py_getattro(PyObject *attr) py_getattro_up(PyObjectPlus); } +PyObject* CValue::py_getattro_dict() { + py_getattro_dict_up(PyObjectPlus); +} + CValue* CValue::ConvertPythonToValue(PyObject* pyobj) { diff --git a/source/gameengine/Expressions/Value.h b/source/gameengine/Expressions/Value.h index a687e1a493c..88186fa95c0 100644 --- a/source/gameengine/Expressions/Value.h +++ b/source/gameengine/Expressions/Value.h @@ -225,6 +225,7 @@ public: virtual PyObject* py_getattro(PyObject *attr); + virtual PyObject* py_getattro_dict(); virtual PyObject* ConvertValueToPython() { return NULL; } -- cgit v1.2.3