From d573e9c5390a438b6e606a12d05dc2c6ad06a174 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 3 Apr 2009 04:12:20 +0000 Subject: BGE Python api Added the method into the PyType so python knows about the methods (its supposed to work this way). This means in the future the api can use PyType_Ready() to store the methods in the types dictionary. Python3 removes Py_FindMethod and we should not be using it anyway since its not that efficient. --- source/gameengine/GameLogic/SCA_RandomSensor.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'source/gameengine/GameLogic/SCA_RandomSensor.cpp') diff --git a/source/gameengine/GameLogic/SCA_RandomSensor.cpp b/source/gameengine/GameLogic/SCA_RandomSensor.cpp index 5354c120f52..d88f8f500c0 100644 --- a/source/gameengine/GameLogic/SCA_RandomSensor.cpp +++ b/source/gameengine/GameLogic/SCA_RandomSensor.cpp @@ -136,13 +136,10 @@ PyTypeObject SCA_RandomSensor::Type = { 0, __getattr, __setattr, - 0, //&MyPyCompare, - __repr, - 0, //&cvalue_as_number, - 0, 0, - 0, - 0 + __repr, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + Methods }; PyParentObject SCA_RandomSensor::Parents[] = { -- cgit v1.2.3 From fd2b1156783d52dbb7c93c53fe008d9e14cbffdd Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 3 Apr 2009 14:51:06 +0000 Subject: Python BGE API - Initialize python types with PyType_Ready, which adds methods to the type dictionary. - use Pythons get/setattro (uses a python string for the attribute rather then char*). Using basic C strings seems nice but internally python converts them to python strings and discards them for most functions that accept char arrays. - Method lookups use the PyTypes dictionary (should be faster then Py_FindMethod) - Renamed __getattr -> py_base_getattro, _getattr -> py_getattro, __repr -> py_base_repr, py_delattro, py_getattro_self etc. From here is possible to put all the parent classes methods into each python types dictionary to avoid nested lookups (api has 4 levels of lookups in some places), tested this but its not ready yet. Simple tests for getting a method within a loop show this to be between 0.5 and 3.2x faster then using Py_FindMethod() --- source/gameengine/GameLogic/SCA_RandomSensor.cpp | 32 ++++++++++++++---------- 1 file changed, 19 insertions(+), 13 deletions(-) (limited to 'source/gameengine/GameLogic/SCA_RandomSensor.cpp') diff --git a/source/gameengine/GameLogic/SCA_RandomSensor.cpp b/source/gameengine/GameLogic/SCA_RandomSensor.cpp index d88f8f500c0..84a9ef95e84 100644 --- a/source/gameengine/GameLogic/SCA_RandomSensor.cpp +++ b/source/gameengine/GameLogic/SCA_RandomSensor.cpp @@ -127,18 +127,21 @@ bool SCA_RandomSensor::Evaluate(CValue* event) /* Integration hooks ------------------------------------------------------- */ PyTypeObject SCA_RandomSensor::Type = { - PyObject_HEAD_INIT(&PyType_Type) + PyObject_HEAD_INIT(NULL) 0, "SCA_RandomSensor", sizeof(SCA_RandomSensor), 0, PyDestructor, 0, - __getattr, - __setattr, 0, - __repr, - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0, + 0, + py_base_repr, + 0,0,0,0,0,0, + py_base_getattro, + py_base_setattro, + 0,0,0,0,0,0,0,0,0, Methods }; @@ -162,22 +165,25 @@ PyAttributeDef SCA_RandomSensor::Attributes[] = { {NULL} //Sentinel }; -PyObject* SCA_RandomSensor::_getattr(const char *attr) { - PyObject* object = _getattr_self(Attributes, this, attr); +PyObject* SCA_RandomSensor::py_getattro(PyObject *attr) { + PyObject* object = py_getattro_self(Attributes, this, attr); if (object != NULL) return object; - if (!strcmp(attr,"seed")) { + + char *attr_str= PyString_AsString(attr); + if (!strcmp(attr_str,"seed")) { return PyInt_FromLong(m_basegenerator->GetSeed()); } - _getattr_up(SCA_ISensor); + py_getattro_up(SCA_ISensor); } -int SCA_RandomSensor::_setattr(const char *attr, PyObject *value) +int SCA_RandomSensor::py_setattro(PyObject *attr, PyObject *value) { - int ret = _setattr_self(Attributes, this, attr, value); + int ret = py_setattro_self(Attributes, this, attr, value); if (ret >= 0) return ret; - if (!strcmp(attr,"seed")) { + char *attr_str= PyString_AsString(attr); + if (!strcmp(attr_str,"seed")) { if (PyInt_Check(value)) { int ival = PyInt_AsLong(value); m_basegenerator->SetSeed(ival); @@ -187,7 +193,7 @@ int SCA_RandomSensor::_setattr(const char *attr, PyObject *value) return 1; } } - return SCA_ISensor::_setattr(attr, value); + return SCA_ISensor::py_setattro(attr, value); } /* 1. setSeed */ -- cgit v1.2.3 From 6be69211843ee37ab9be763df0621db94dbfd69b Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sat, 4 Apr 2009 02:57:35 +0000 Subject: moved more attributes from getattr into PyAttributeDef's --- source/gameengine/GameLogic/SCA_RandomSensor.cpp | 35 +++++++++++++----------- 1 file changed, 19 insertions(+), 16 deletions(-) (limited to 'source/gameengine/GameLogic/SCA_RandomSensor.cpp') diff --git a/source/gameengine/GameLogic/SCA_RandomSensor.cpp b/source/gameengine/GameLogic/SCA_RandomSensor.cpp index 84a9ef95e84..bdee64430fa 100644 --- a/source/gameengine/GameLogic/SCA_RandomSensor.cpp +++ b/source/gameengine/GameLogic/SCA_RandomSensor.cpp @@ -162,6 +162,7 @@ PyMethodDef SCA_RandomSensor::Methods[] = { PyAttributeDef SCA_RandomSensor::Attributes[] = { KX_PYATTRIBUTE_BOOL_RO("lastDraw",SCA_RandomSensor,m_lastdraw), + KX_PYATTRIBUTE_RW_FUNCTION("seed", SCA_RandomSensor, pyattr_get_seed, pyattr_set_seed), {NULL} //Sentinel }; @@ -169,11 +170,6 @@ PyObject* SCA_RandomSensor::py_getattro(PyObject *attr) { PyObject* object = py_getattro_self(Attributes, this, attr); if (object != NULL) return object; - - char *attr_str= PyString_AsString(attr); - if (!strcmp(attr_str,"seed")) { - return PyInt_FromLong(m_basegenerator->GetSeed()); - } py_getattro_up(SCA_ISensor); } @@ -182,17 +178,6 @@ int SCA_RandomSensor::py_setattro(PyObject *attr, PyObject *value) int ret = py_setattro_self(Attributes, this, attr, value); if (ret >= 0) return ret; - char *attr_str= PyString_AsString(attr); - if (!strcmp(attr_str,"seed")) { - if (PyInt_Check(value)) { - int ival = PyInt_AsLong(value); - m_basegenerator->SetSeed(ival); - return 0; - } else { - PyErr_SetString(PyExc_TypeError, "expected an integer"); - return 1; - } - } return SCA_ISensor::py_setattro(attr, value); } @@ -234,4 +219,22 @@ PyObject* SCA_RandomSensor::PyGetLastDraw(PyObject* self, PyObject* args, PyObje return PyInt_FromLong(m_lastdraw); } + +PyObject* SCA_RandomSensor::pyattr_get_seed(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) +{ + SCA_RandomSensor* self= static_cast(self_v); + return PyInt_FromLong(self->m_basegenerator->GetSeed()); +} + +int SCA_RandomSensor::pyattr_set_seed(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value) +{ + SCA_RandomSensor* self= static_cast(self_v); + if (!PyInt_Check(value)) { + PyErr_SetString(PyExc_TypeError, "expected an integer"); + return -1; + } + self->m_basegenerator->SetSeed(PyInt_AsLong(value)); + return 0; +} + /* eof */ -- cgit v1.2.3 From 5d64dd019e7e8150db40505097d1b4048f4e0153 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 7 Apr 2009 11:06:35 +0000 Subject: BGE Python API Use each types dictionary to store attributes PyAttributeDef's so it uses pythons hash lookup (which it was already doing for methods) rather then doing a string lookup on the array each time. This also means attributes can be found in the type without having to do a dir() on the instance. --- source/gameengine/GameLogic/SCA_RandomSensor.cpp | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'source/gameengine/GameLogic/SCA_RandomSensor.cpp') diff --git a/source/gameengine/GameLogic/SCA_RandomSensor.cpp b/source/gameengine/GameLogic/SCA_RandomSensor.cpp index bdee64430fa..fe4d88e2797 100644 --- a/source/gameengine/GameLogic/SCA_RandomSensor.cpp +++ b/source/gameengine/GameLogic/SCA_RandomSensor.cpp @@ -167,18 +167,12 @@ PyAttributeDef SCA_RandomSensor::Attributes[] = { }; PyObject* SCA_RandomSensor::py_getattro(PyObject *attr) { - PyObject* object = py_getattro_self(Attributes, this, attr); - if (object != NULL) - return object; py_getattro_up(SCA_ISensor); } int SCA_RandomSensor::py_setattro(PyObject *attr, PyObject *value) { - int ret = py_setattro_self(Attributes, this, attr, value); - if (ret >= 0) - return ret; - return SCA_ISensor::py_setattro(attr, value); + py_setattro_up(SCA_ISensor); } /* 1. setSeed */ -- cgit v1.2.3 From 2fff90bbb4a922f454c15dbf2d6215bd4d8c519c Mon Sep 17 00:00:00 2001 From: Andre Susano Pinto Date: Fri, 10 Apr 2009 16:45:19 +0000 Subject: Added function name to many of the PyArg_ParseTuple calls in gameengine This way python raises more useful messages. --- source/gameengine/GameLogic/SCA_RandomSensor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source/gameengine/GameLogic/SCA_RandomSensor.cpp') diff --git a/source/gameengine/GameLogic/SCA_RandomSensor.cpp b/source/gameengine/GameLogic/SCA_RandomSensor.cpp index fe4d88e2797..3179c8522f9 100644 --- a/source/gameengine/GameLogic/SCA_RandomSensor.cpp +++ b/source/gameengine/GameLogic/SCA_RandomSensor.cpp @@ -185,7 +185,7 @@ const char SCA_RandomSensor::SetSeed_doc[] = PyObject* SCA_RandomSensor::PySetSeed(PyObject* self, PyObject* args, PyObject* kwds) { ShowDeprecationWarning("setSeed()", "the seed property"); long seedArg; - if(!PyArg_ParseTuple(args, "i", &seedArg)) { + if(!PyArg_ParseTuple(args, "i:setSeed", &seedArg)) { return NULL; } -- cgit v1.2.3 From 7dbc9dc719c3eb0823e4f9e7ae94a479f9427ea7 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sun, 19 Apr 2009 14:57:52 +0000 Subject: 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. --- source/gameengine/GameLogic/SCA_RandomSensor.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source/gameengine/GameLogic/SCA_RandomSensor.cpp') diff --git a/source/gameengine/GameLogic/SCA_RandomSensor.cpp b/source/gameengine/GameLogic/SCA_RandomSensor.cpp index 3179c8522f9..b0bc518825b 100644 --- a/source/gameengine/GameLogic/SCA_RandomSensor.cpp +++ b/source/gameengine/GameLogic/SCA_RandomSensor.cpp @@ -130,9 +130,9 @@ PyTypeObject SCA_RandomSensor::Type = { PyObject_HEAD_INIT(NULL) 0, "SCA_RandomSensor", - sizeof(SCA_RandomSensor), + sizeof(PyObjectPlus_Proxy), 0, - PyDestructor, + py_base_dealloc, 0, 0, 0, -- cgit v1.2.3 From 6bc162e679d8b52b28e205de76985a1735abbf0a Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sun, 19 Apr 2009 17:29:07 +0000 Subject: BGE Python API removed redundant (PyObject *self) argument from python functions that are not exposed to python directly. --- source/gameengine/GameLogic/SCA_RandomSensor.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'source/gameengine/GameLogic/SCA_RandomSensor.cpp') diff --git a/source/gameengine/GameLogic/SCA_RandomSensor.cpp b/source/gameengine/GameLogic/SCA_RandomSensor.cpp index b0bc518825b..e04d2a8ab90 100644 --- a/source/gameengine/GameLogic/SCA_RandomSensor.cpp +++ b/source/gameengine/GameLogic/SCA_RandomSensor.cpp @@ -155,8 +155,8 @@ PyParentObject SCA_RandomSensor::Parents[] = { PyMethodDef SCA_RandomSensor::Methods[] = { {"setSeed", (PyCFunction) SCA_RandomSensor::sPySetSeed, METH_VARARGS, (PY_METHODCHAR)SetSeed_doc}, - {"getSeed", (PyCFunction) SCA_RandomSensor::sPyGetSeed, METH_VARARGS, (PY_METHODCHAR)GetSeed_doc}, - {"getLastDraw", (PyCFunction) SCA_RandomSensor::sPyGetLastDraw, METH_VARARGS, (PY_METHODCHAR)GetLastDraw_doc}, + {"getSeed", (PyCFunction) SCA_RandomSensor::sPyGetSeed, METH_NOARGS, (PY_METHODCHAR)GetSeed_doc}, + {"getLastDraw", (PyCFunction) SCA_RandomSensor::sPyGetLastDraw, METH_NOARGS, (PY_METHODCHAR)GetLastDraw_doc}, {NULL,NULL} //Sentinel }; @@ -182,7 +182,7 @@ const char SCA_RandomSensor::SetSeed_doc[] = "\tSet the initial seed of the generator. Equal seeds produce\n" "\tequal series. If the seed is 0, the generator will produce\n" "\tthe same value on every call.\n"; -PyObject* SCA_RandomSensor::PySetSeed(PyObject* self, PyObject* args, PyObject* kwds) { +PyObject* SCA_RandomSensor::PySetSeed(PyObject* args) { ShowDeprecationWarning("setSeed()", "the seed property"); long seedArg; if(!PyArg_ParseTuple(args, "i:setSeed", &seedArg)) { @@ -199,7 +199,7 @@ const char SCA_RandomSensor::GetSeed_doc[] = "getSeed()\n" "\tReturns the initial seed of the generator. Equal seeds produce\n" "\tequal series.\n"; -PyObject* SCA_RandomSensor::PyGetSeed(PyObject* self, PyObject* args, PyObject* kwds) { +PyObject* SCA_RandomSensor::PyGetSeed() { ShowDeprecationWarning("getSeed()", "the seed property"); return PyInt_FromLong(m_basegenerator->GetSeed()); } @@ -208,7 +208,7 @@ PyObject* SCA_RandomSensor::PyGetSeed(PyObject* self, PyObject* args, PyObject* const char SCA_RandomSensor::GetLastDraw_doc[] = "getLastDraw()\n" "\tReturn the last value that was drawn.\n"; -PyObject* SCA_RandomSensor::PyGetLastDraw(PyObject* self, PyObject* args, PyObject* kwds) { +PyObject* SCA_RandomSensor::PyGetLastDraw() { ShowDeprecationWarning("getLastDraw()", "the lastDraw property"); return PyInt_FromLong(m_lastdraw); } -- cgit v1.2.3 From f5fc4ebdd8ede5263f4b34f161ebe139d40466dc Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sun, 19 Apr 2009 21:01:12 +0000 Subject: BGE Python API - More verbose error messages. - BL_Shader wasnt setting error messages on some errors - FilterNormal depth attribute was checking for float which is bad because scripts often expect ints assigned to float attributes. - Added a check to PyVecTo for a tuple rather then always using a generic python sequence. On my system this is over 2x faster with an optmized build. --- source/gameengine/GameLogic/SCA_RandomSensor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source/gameengine/GameLogic/SCA_RandomSensor.cpp') diff --git a/source/gameengine/GameLogic/SCA_RandomSensor.cpp b/source/gameengine/GameLogic/SCA_RandomSensor.cpp index e04d2a8ab90..5ead82db428 100644 --- a/source/gameengine/GameLogic/SCA_RandomSensor.cpp +++ b/source/gameengine/GameLogic/SCA_RandomSensor.cpp @@ -224,7 +224,7 @@ int SCA_RandomSensor::pyattr_set_seed(void *self_v, const KX_PYATTRIBUTE_DEF *at { SCA_RandomSensor* self= static_cast(self_v); if (!PyInt_Check(value)) { - PyErr_SetString(PyExc_TypeError, "expected an integer"); + PyErr_SetString(PyExc_TypeError, "sensor.seed = int: Random Sensor, expected an integer"); return -1; } self->m_basegenerator->SetSeed(PyInt_AsLong(value)); -- cgit v1.2.3