- should fix compiling with older python versions (<2.5)
authorCampbell Barton <ideasman42@gmail.com>
Sat, 4 Apr 2009 08:20:52 +0000 (08:20 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Sat, 4 Apr 2009 08:20:52 +0000 (08:20 +0000)
- made the isA() function accept python types as well as strings.
- renamed _getattr_dict to py_getattr_dict

source/gameengine/Expressions/PyObjectPlus.cpp
source/gameengine/Expressions/PyObjectPlus.h
source/gameengine/Ketsji/KX_GameObject.cpp
source/gameengine/Ketsji/KX_Scene.cpp

index ed6932b414ac4cf643694bafa932f1f276f42446..ac1871300d0ecabdf0238e8990f5372006fca262 100644 (file)
@@ -703,35 +703,40 @@ PyObject *PyObjectPlus::py_repr(void)
 ------------------------------*/
 bool PyObjectPlus::isA(PyTypeObject *T)                // if called with a Type, use "typename"
 {
-  return isA(T->tp_name);
+       int i;
+       PyParentObject  P;
+       PyParentObject *Ps = GetParents();
+
+       for (P = Ps[i=0]; P != NULL; P = Ps[i++])
+               if (P==T)
+                       return true;
+
+       return false;
 }
 
 
 bool PyObjectPlus::isA(const char *mytypename)         // check typename of each parent
 {
-  int i;
-  PyParentObject  P;
-  PyParentObject *Ps = GetParents();
+       int i;
+       PyParentObject  P;
+       PyParentObject *Ps = GetParents();
   
-  for (P = Ps[i=0]; P != NULL; P = Ps[i++])
-  {
-      if (strcmp(P->tp_name, mytypename)==0)
-                 return true;
-  }
-       
-  return false;
+       for (P = Ps[i=0]; P != NULL; P = Ps[i++])
+               if (strcmp(P->tp_name, mytypename)==0)
+                       return true;
+
+       return false;
 }
 
 PyObject *PyObjectPlus::Py_isA(PyObject *value)                // Python wrapper for isA
 {
-  if (!PyString_Check(value)) {
-    PyErr_SetString(PyExc_TypeError, "expected a string");
-    return NULL;
-  }
-  if(isA(PyString_AsString(value)))
-    Py_RETURN_TRUE;
-  else
-    Py_RETURN_FALSE;
+       if (PyType_Check(value)) {
+               return PyBool_FromLong(isA((PyTypeObject *)value));
+       } else if (PyString_Check(value)) {
+               return PyBool_FromLong(isA(PyString_AsString(value)));
+       }
+    PyErr_SetString(PyExc_TypeError, "expected a type or a string");
+    return NULL;       
 }
 
 /* Utility function called by the macro py_getattro_up()
@@ -742,7 +747,7 @@ PyObject *PyObjectPlus::Py_isA(PyObject *value)             // Python wrapper for isA
  * Other then making dir() useful the value returned from __dict__() is not useful
  * since every value is a Py_None
  * */
-PyObject *_getattr_dict(PyObject *pydict, PyMethodDef *meth, PyAttributeDef *attrdef)
+PyObject *py_getattr_dict(PyObject *pydict, PyMethodDef *meth, PyAttributeDef *attrdef)
 {
     if(pydict==NULL) { /* incase calling __dict__ on the parent of this object raised an error */
        PyErr_Clear();
index 77963c092ebf0de353ddca5746c2784c79e70689..f178d03e131dec920160a7f24d98e3f0b96dae02 100644 (file)
@@ -62,6 +62,7 @@
 /*  for pre Py 2.5 */
 #if PY_VERSION_HEX < 0x02050000
 typedef int Py_ssize_t;
+typedef Py_ssize_t (*lenfunc)(PyObject *);
 #define PY_SSIZE_T_MAX INT_MAX
 #define PY_SSIZE_T_MIN INT_MIN
 #define PY_METHODCHAR char *
@@ -74,6 +75,7 @@ typedef int Py_ssize_t;
 
 #include "descrobject.h"
 
+
 static inline void Py_Fatal(const char *M) {
        fprintf(stderr, "%s\n", M);
        exit(-1);
@@ -108,7 +110,7 @@ static inline void Py_Fatal(const char *M) {
        } \
        \
        if (strcmp(PyString_AsString(attr), "__dict__")==0) {\
-               rvalue = _getattr_dict(rvalue, Methods, Attributes); \
+               rvalue = py_getattr_dict(rvalue, Methods, Attributes); \
        } \
        return rvalue; \
 
@@ -404,7 +406,7 @@ public:
        }
 };
 
-PyObject *_getattr_dict(PyObject *pydict, PyMethodDef *meth, PyAttributeDef *attrdef);
+PyObject *py_getattr_dict(PyObject *pydict, PyMethodDef *meth, PyAttributeDef *attrdef);
 
 #endif //  _adr_py_lib_h_
 
index e79ef5ab72b41a5d1d94afe658d82517abd1da9e..5bba9190bd670882132ee1572870676283c92ded 100644 (file)
@@ -1421,7 +1421,7 @@ PyObject* KX_GameObject::pyattr_get_dir_dict(void *self_v, const KX_PYATTRIBUTE_
 {
        KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
        PyObject *dict_str = PyString_FromString("__dict__");
-       PyObject *dict= _getattr_dict(self->SCA_IObject::py_getattro(dict_str), KX_GameObject::Methods, KX_GameObject::Attributes);
+       PyObject *dict= py_getattr_dict(self->SCA_IObject::py_getattro(dict_str), KX_GameObject::Methods, KX_GameObject::Attributes);
        Py_DECREF(dict_str);
        
        if(dict==NULL)
index 94ea234f1bf3ce56821b95daa06f0f29a13d0e93..254717894df7a494da2a16671dd0c45a63e64c6c 100644 (file)
@@ -1574,7 +1574,7 @@ PyObject* KX_Scene::pyattr_get_dir_dict(void *self_v, const KX_PYATTRIBUTE_DEF *
        KX_Scene* self= static_cast<KX_Scene*>(self_v);
        /* Useually done by py_getattro_up but in this case we want to include m_attrlist dict */
        PyObject *dict_str= PyString_FromString("__dict__");
-       PyObject *dict= _getattr_dict(self->PyObjectPlus::py_getattro(dict_str), KX_Scene::Methods, KX_Scene::Attributes);
+       PyObject *dict= py_getattr_dict(self->PyObjectPlus::py_getattro(dict_str), KX_Scene::Methods, KX_Scene::Attributes);
        Py_DECREF(dict_str);
        
        PyDict_Update(dict, self->m_attrlist);