diff options
author | Campbell Barton <ideasman42@gmail.com> | 2009-04-19 18:57:52 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2009-04-19 18:57:52 +0400 |
commit | 7dbc9dc719c3eb0823e4f9e7ae94a479f9427ea7 (patch) | |
tree | abaae0ea91ef351379f72f00c31f5a67ae0865af /source/gameengine/Expressions | |
parent | 8d2cb5bea44f4245dd17f2d82cbd0251d8090fd5 (diff) |
BGE Python API cleanup - no functionality changes
- comments to PyObjectPlus.h
- remove unused/commented junk.
- renamed PyDestructor to py_base_dealloc for consistency
- all the PyTypeObject's were still using the sizeof() their class, can use sizeof(PyObjectPlus_Proxy) now which is smaller too.
Diffstat (limited to 'source/gameengine/Expressions')
-rw-r--r-- | source/gameengine/Expressions/ListValue.cpp | 4 | ||||
-rw-r--r-- | source/gameengine/Expressions/PyObjectPlus.cpp | 93 | ||||
-rw-r--r-- | source/gameengine/Expressions/PyObjectPlus.h | 102 | ||||
-rw-r--r-- | source/gameengine/Expressions/Value.cpp | 4 |
4 files changed, 80 insertions, 123 deletions
diff --git a/source/gameengine/Expressions/ListValue.cpp b/source/gameengine/Expressions/ListValue.cpp index eaed2b5c400..5bb1eb0aeb9 100644 --- a/source/gameengine/Expressions/ListValue.cpp +++ b/source/gameengine/Expressions/ListValue.cpp @@ -223,10 +223,10 @@ PyTypeObject CListValue::Type = { PyObject_HEAD_INIT(NULL) 0, /*ob_size*/ "CListValue", /*tp_name*/ - sizeof(CListValue), /*tp_basicsize*/ + sizeof(PyObjectPlus_Proxy), /*tp_basicsize*/ 0, /*tp_itemsize*/ /* methods */ - PyDestructor, /*tp_dealloc*/ + py_base_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ diff --git a/source/gameengine/Expressions/PyObjectPlus.cpp b/source/gameengine/Expressions/PyObjectPlus.cpp index 2c5ba3f39fc..3c414c6b16d 100644 --- a/source/gameengine/Expressions/PyObjectPlus.cpp +++ b/source/gameengine/Expressions/PyObjectPlus.cpp @@ -59,10 +59,10 @@ PyTypeObject PyObjectPlus::Type = { PyObject_HEAD_INIT(NULL) 0, /*ob_size*/ "PyObjectPlus", /*tp_name*/ - sizeof(PyObjectPlus), /*tp_basicsize*/ + sizeof(PyObjectPlus_Proxy), /*tp_basicsize*/ 0, /*tp_itemsize*/ /* methods */ - PyDestructor, + py_base_dealloc, 0, 0, 0, @@ -85,7 +85,7 @@ PyObjectPlus::~PyObjectPlus() // assert(ob_refcnt==0); } -void PyObjectPlus::PyDestructor(PyObject *self) // python wrapper +void PyObjectPlus::py_base_dealloc(PyObject *self) // python wrapper { PyObjectPlus *self_plus= BGE_PROXY_REF(self); if(self_plus) { @@ -108,7 +108,7 @@ PyObjectPlus::PyObjectPlus(PyTypeObject *T) // constructor * PyObjectPlus Methods -- Every class, even the abstract one should have a Methods ------------------------------*/ PyMethodDef PyObjectPlus::Methods[] = { - {"isA", (PyCFunction) sPy_isA, METH_O}, + {"isA", (PyCFunction) sPyisA, METH_O}, {NULL, NULL} /* Sentinel */ }; @@ -130,6 +130,49 @@ PyParentObject PyObjectPlus::Parents[] = {&PyObjectPlus::Type, NULL}; /*------------------------------ * PyObjectPlus attributes -- attributes ------------------------------*/ + + +/* This should be the entry in Type since it takes the C++ class from PyObjectPlus_Proxy */ +PyObject *PyObjectPlus::py_base_getattro(PyObject * self, PyObject *attr) +{ + PyObjectPlus *self_plus= BGE_PROXY_REF(self); + if(self_plus==NULL) { + if(!strcmp("isValid", PyString_AsString(attr))) { + Py_RETURN_TRUE; + } + PyErr_SetString(PyExc_RuntimeError, BGE_PROXY_ERROR_MSG); + return NULL; + } + return self_plus->py_getattro(attr); +} + +/* This should be the entry in Type since it takes the C++ class from PyObjectPlus_Proxy */ +int PyObjectPlus::py_base_setattro(PyObject *self, PyObject *attr, PyObject *value) +{ + PyObjectPlus *self_plus= BGE_PROXY_REF(self); + if(self_plus==NULL) { + PyErr_SetString(PyExc_RuntimeError, BGE_PROXY_ERROR_MSG); + return -1; + } + + if (value==NULL) + return self_plus->py_delattro(attr); + + return self_plus->py_setattro(attr, value); +} + +PyObject *PyObjectPlus::py_base_repr(PyObject *self) // This should be the entry in Type. +{ + + PyObjectPlus *self_plus= BGE_PROXY_REF(self); + if(self_plus==NULL) { + PyErr_SetString(PyExc_RuntimeError, BGE_PROXY_ERROR_MSG); + return NULL; + } + + return self_plus->py_repr(); +} + PyObject *PyObjectPlus::py_getattro(PyObject* attr) { PyObject *descr = PyDict_GetItem(Type.tp_dict, attr); \ @@ -151,8 +194,6 @@ PyObject *PyObjectPlus::py_getattro(PyObject* attr) } /* end py_getattro_up copy */ } - //if (streq(attr, "type")) - // return Py_BuildValue("s", (*(GetParents()))->tp_name); } int PyObjectPlus::py_delattro(PyObject* attr) @@ -163,8 +204,6 @@ int PyObjectPlus::py_delattro(PyObject* attr) 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 PY_SET_ATTR_MISSING; } @@ -275,20 +314,6 @@ PyObject *PyObjectPlus::py_get_attrdef(void *self, const PyAttributeDef *attrdef } } -#if 0 -PyObject *PyObjectPlus::py_getattro_self(const PyAttributeDef attrlist[], void *self, PyObject *attr) -{ - char *attr_str= PyString_AsString(attr); - const PyAttributeDef *attrdef; - - for (attrdef=attrlist; attrdef->m_name != NULL; attrdef++) - if (!strcmp(attr_str, attrdef->m_name)) - return py_get_attrdef(self, attrdef); - - return NULL; -} -#endif - int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyObject *value) { void *undoBuffer = NULL; @@ -714,29 +739,7 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb return 0; } -#if 0 -int PyObjectPlus::py_setattro_self(const PyAttributeDef attrlist[], void *self, PyObject *attr, PyObject *value) -{ - const PyAttributeDef *attrdef; - char *attr_str= PyString_AsString(attr); - for (attrdef=attrlist; attrdef->m_name != NULL; attrdef++) - { - if (!strcmp(attr_str, attrdef->m_name)) - { - if (attrdef->m_access == KX_PYATTRIBUTE_RO || - attrdef->m_type == KX_PYATTRIBUTE_TYPE_DUMMY) - { - PyErr_SetString(PyExc_AttributeError, "property is read-only"); - return PY_SET_ATTR_FAIL; - } - - return py_set_attrdef(self, attrdef, value); - } - } - return PY_SET_ATTR_MISSING; -} -#endif /*------------------------------ * PyObjectPlus repr -- representations @@ -777,7 +780,7 @@ bool PyObjectPlus::isA(const char *mytypename) // check typename of each parent return false; } -PyObject *PyObjectPlus::Py_isA(PyObject *value) // Python wrapper for isA +PyObject *PyObjectPlus::PyisA(PyObject *self, PyObject *value) // Python wrapper for isA { if (PyType_Check(value)) { return PyBool_FromLong(isA((PyTypeObject *)value)); diff --git a/source/gameengine/Expressions/PyObjectPlus.h b/source/gameengine/Expressions/PyObjectPlus.h index 3be9a2f2bcb..9f30e6570ee 100644 --- a/source/gameengine/Expressions/PyObjectPlus.h +++ b/source/gameengine/Expressions/PyObjectPlus.h @@ -50,13 +50,13 @@ also in api2_2x/gen_utils.h */ #ifndef Py_RETURN_NONE -#define Py_RETURN_NONE return Py_BuildValue("O", Py_None) +#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None #endif #ifndef Py_RETURN_FALSE -#define Py_RETURN_FALSE return PyBool_FromLong(0) +#define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False #endif #ifndef Py_RETURN_TRUE -#define Py_RETURN_TRUE return PyBool_FromLong(1) +#define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True #endif /* for pre Py 2.5 */ @@ -92,7 +92,7 @@ typedef struct { #define BGE_PROXY_PYOWNS(_self) (((PyObjectPlus_Proxy *)_self)->py_owns) /* Note, sometimes we dont care what BGE type this is as long as its a proxy */ -#define BGE_PROXY_CHECK_TYPE(_self) ((_self)->ob_type->tp_dealloc == PyDestructor) +#define BGE_PROXY_CHECK_TYPE(_self) ((_self)->ob_type->tp_dealloc == py_base_dealloc) // This must be the first line of each @@ -429,80 +429,34 @@ public: PyObject *m_proxy; /* actually a PyObjectPlus_Proxy */ virtual ~PyObjectPlus(); // destructor - static void PyDestructor(PyObject *self); // python wrapper -// void INCREF(void) { -// Py_INCREF(this); -// }; // incref method -// void DECREF(void) { -// Py_DECREF(this); -// }; // decref method + /* These static functions are referenced by ALL PyObjectPlus_Proxy types + * they take the C++ reference from the PyObjectPlus_Proxy and call + * its own virtual py_getattro, py_setattro etc. functions. + */ + static void py_base_dealloc(PyObject *self); + static PyObject* py_base_getattro(PyObject * self, PyObject *attr); + static int py_base_setattro(PyObject *self, PyObject *attr, PyObject *value); + static PyObject* py_base_repr(PyObject *self); + + /* 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 int py_delattro(PyObject *attr); + virtual int py_setattro(PyObject *attr, PyObject *value); + virtual PyObject* py_repr(void); + + static PyObject* py_get_attrdef(void *self, const PyAttributeDef *attrdef); + static int py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyObject *value); - virtual PyObject *py_getattro(PyObject *attr); // py_getattro method - static PyObject *py_base_getattro(PyObject * self, PyObject *attr) // This should be the entry in Type. - { - PyObjectPlus *self_plus= BGE_PROXY_REF(self); - if(self_plus==NULL) { - if(!strcmp("isValid", PyString_AsString(attr))) { - Py_RETURN_TRUE; - } - PyErr_SetString(PyExc_RuntimeError, "data has been removed"); - return NULL; - } - return self_plus->py_getattro(attr); - } - - static PyObject* py_get_attrdef(void *self, const PyAttributeDef *attrdef); - static int py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyObject *value); - -#if 0 - static PyObject *py_getattro_self(const PyAttributeDef attrlist[], void *self, PyObject *attr); - static int py_setattro_self(const PyAttributeDef attrlist[], void *self, PyObject *attr, PyObject *value); -#endif - - virtual int py_delattro(PyObject *attr); - virtual int py_setattro(PyObject *attr, PyObject *value); // py_setattro method - static int py_base_setattro(PyObject *self, PyObject *attr, PyObject *value) // the PyType should reference this - { - PyObjectPlus *self_plus= BGE_PROXY_REF(self); - if(self_plus==NULL) { - PyErr_SetString(PyExc_RuntimeError, "data has been removed"); - return -1; - } - - if (value==NULL) - return self_plus->py_delattro(attr); - - return self_plus->py_setattro(attr, value); - } - - virtual PyObject *py_repr(void); // py_repr method - static PyObject *py_base_repr(PyObject *self) // This should be the entry in Type. - { - - PyObjectPlus *self_plus= BGE_PROXY_REF(self); - if(self_plus==NULL) { - PyErr_SetString(PyExc_RuntimeError, "data has been removed"); - return NULL; - } - - return self_plus->py_repr(); - } - - // isA methods + /* isA() methods, shonky replacement for pythons issubclass() + * which we cant use because we have our own subclass system */ bool isA(PyTypeObject *T); bool isA(const char *mytypename); - PyObject *Py_isA(PyObject *value); - static PyObject *sPy_isA(PyObject *self, PyObject *value) - { - PyObjectPlus *self_plus= BGE_PROXY_REF(self); - if(self_plus==NULL) { - PyErr_SetString(PyExc_RuntimeError, "data has been removed"); - return NULL; - } - - return self_plus->Py_isA(value); - } + PyObject *PyisA(PyObject *value); + //static PyObject *sPy_isA(PyObject *self, PyObject *value); + + KX_PYMETHOD_O(PyObjectPlus,isA); /* Kindof dumb, always returns True, the false case is checked for, before this function gets accessed */ static PyObject* pyattr_get_is_valid(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef); diff --git a/source/gameengine/Expressions/Value.cpp b/source/gameengine/Expressions/Value.cpp index 7958c16ca81..17813d0ab52 100644 --- a/source/gameengine/Expressions/Value.cpp +++ b/source/gameengine/Expressions/Value.cpp @@ -40,9 +40,9 @@ PyTypeObject CValue::Type = { PyObject_HEAD_INIT(NULL) 0, "CValue", - sizeof(CValue), + sizeof(PyObjectPlus_Proxy), 0, - PyDestructor, + py_base_dealloc, 0, 0, 0, |