svn merge -r 21041:21301 https://svn.blender.org/svnroot/bf-blender/branches/blender2...
[blender.git] / source / gameengine / Expressions / PyObjectPlus.cpp
index e6b49332273e5dfe1fe379d3a9c34b0a5797cdaf..729fff31052a9d848af62935b1aa7dedd1ab7cf1 100644 (file)
@@ -50,6 +50,7 @@
 #include "stdlib.h"
 #include "PyObjectPlus.h"
 #include "STR_String.h"
+#include "MT_Vector3.h"
 /*------------------------------
  * PyObjectPlus Type           -- Every class, even the abstract one should have a Type
 ------------------------------*/
@@ -73,11 +74,13 @@ PyTypeObject PyObjectPlus::Type = {
        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
+       Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+       0,0,0,0,0,0,0,
+       Methods,
+       0,
+       0,
+       NULL // no subtype
 };
 
 
@@ -90,150 +93,146 @@ PyObjectPlus::~PyObjectPlus()
 //     assert(ob_refcnt==0);
 }
 
+
+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_SystemError, BGE_PROXY_ERROR_MSG);
+               return NULL;
+       }
+       
+       return self_plus->py_repr();  
+}
+
+
+PyObject * PyObjectPlus::py_base_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+{
+       PyTypeObject *base_type;
+       PyObjectPlus_Proxy *base = NULL;
+
+       if (!PyArg_ParseTuple(args, "O:Base PyObjectPlus", &base))
+               return NULL;
+
+       /* the 'base' PyObject may be subclassed (multiple times even)
+        * we need to find the first C++ defined class to check 'type'
+        * is a subclass of the base arguments type.
+        *
+        * This way we can share one tp_new function for every PyObjectPlus
+        *
+        * eg.
+        *
+        * # CustomOb is called 'type' in this C code
+        * class CustomOb(GameTypes.KX_GameObject):
+        *     pass
+        *
+        * # this calls py_base_new(...), the type of 'CustomOb' is checked to be a subclass of the 'cont.owner' type
+        * ob = CustomOb(cont.owner)
+        *
+        * */
+       base_type= Py_TYPE(base);
+       while(base_type && !BGE_PROXY_CHECK_TYPE(base_type))
+               base_type= base_type->tp_base;
+
+       if(base_type==NULL || !BGE_PROXY_CHECK_TYPE(base_type)) {
+               PyErr_SetString(PyExc_TypeError, "can't subclass from a blender game type because the argument given is not a game class or subclass");
+               return NULL;
+       }
+
+       /* use base_type rather then Py_TYPE(base) because we could alredy be subtyped */
+       if(!PyType_IsSubtype(type, base_type)) {
+               PyErr_Format(PyExc_TypeError, "can't subclass blender game type <%s> from <%s> because it is not a subclass", base_type->tp_name, type->tp_name);
+               return NULL;
+       }
+
+       /* invalidate the existing base and return a new subclassed one,
+        * this is a bit dodgy in that it also attaches its self to the existing object
+        * which is not really 'correct' python OO but for our use its OK. */
+
+       PyObjectPlus_Proxy *ret = (PyObjectPlus_Proxy *) type->tp_alloc(type, 0); /* starts with 1 ref, used for the return ref' */
+       ret->ref= base->ref;
+       base->ref= NULL;                /* invalidate! disallow further access */
+
+       ret->py_owns= base->py_owns;
+
+       ret->ref->m_proxy= NULL;
+
+       /* 'base' may be free'd after this func finished but not necessarily
+        * there is no reference to the BGE data now so it will throw an error on access */
+       Py_DECREF(base);
+
+       ret->ref->m_proxy= (PyObject *)ret; /* no need to add a ref because one is added when creating. */
+       Py_INCREF(ret); /* we return a new ref but m_proxy holds a ref so we need to add one */
+
+
+       /* 'ret' will have 2 references.
+        * - One ref is needed because ret->ref->m_proxy holds a refcount to the current proxy.
+        * - Another is needed for returning the value.
+        *
+        * So we should be ok with 2 refs, but for some reason this crashes. so adding a new ref...
+        * */
+
+       return (PyObject *)ret;
+}
+
 void PyObjectPlus::py_base_dealloc(PyObject *self)                             // python wrapper
 {
        PyObjectPlus *self_plus= BGE_PROXY_REF(self);
        if(self_plus) {
                if(BGE_PROXY_PYOWNS(self)) { /* Does python own this?, then delete it  */
+                       self_plus->m_proxy = NULL; /* Need this to stop ~PyObjectPlus from decrefing m_proxy otherwise its decref'd twice and py-debug crashes */
                        delete self_plus;
                }
-               
+
                BGE_PROXY_REF(self)= NULL; // not really needed
        }
+
+#if 0
+       /* is ok normally but not for subtyping, use tp_free instead. */
        PyObject_DEL( self );
+#else
+       Py_TYPE(self)->tp_free(self);
+#endif
 };
 
-PyObjectPlus::PyObjectPlus(PyTypeObject *T)                            // constructor
+PyObjectPlus::PyObjectPlus() : SG_QList()                              // constructor
 {
-       MT_assert(T != NULL);
        m_proxy= NULL;
 };
-  
+
 /*------------------------------
  * PyObjectPlus Methods        -- Every class, even the abstract one should have a Methods
 ------------------------------*/
 PyMethodDef PyObjectPlus::Methods[] = {
-  {"isA",               (PyCFunction) sPyisA,                  METH_O},
   {NULL, NULL}         /* Sentinel */
 };
 
+#define attr_invalid (&(PyObjectPlus::Attributes[0]))
 PyAttributeDef PyObjectPlus::Attributes[] = {
        KX_PYATTRIBUTE_RO_FUNCTION("invalid",           PyObjectPlus, pyattr_get_invalid),
        {NULL} //Sentinel
 };
 
-PyObject* PyObjectPlus::pyattr_get_invalid(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
-{      
-       Py_RETURN_FALSE;
-}
-
-/*------------------------------
- * PyObjectPlus Parents                -- Every class, even the abstract one should have parents
-------------------------------*/
-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)
+PyObject* PyObjectPlus::pyattr_get_invalid(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
-       PyObjectPlus *self_plus= BGE_PROXY_REF(self);
-       if(self_plus==NULL) {
-               if(!strcmp("invalid", PyString_AsString(attr))) {
-                       Py_RETURN_TRUE;
-               }
-               PyErr_SetString(PyExc_SystemError, BGE_PROXY_ERROR_MSG);
-               return NULL;
-       }
-       
-       PyObject *ret= self_plus->py_getattro(attr);
-       
-       /* Attribute not found, was this a __dict__ lookup?, otherwise set an error if none is set */
-       if(ret==NULL) {
-               char *attr_str= PyString_AsString(attr);
-               
-               if (strcmp(attr_str, "__dict__")==0)
-               {
-                       /* the error string will probably not
-                        * be set but just incase clear it */
-                       PyErr_Clear(); 
-                       ret= self_plus->py_getattro_dict();
-               }
-               else if (!PyErr_Occurred()) {
-                       /* We looked for an attribute but it wasnt found
-                        * since py_getattro didnt set the error, set it here */
-                       PyErr_Format(PyExc_AttributeError, "'%s' object has no attribute '%s'", self->ob_type->tp_name, attr_str);
-               }
-       }
-       return ret;
+       return PyBool_FromLong(self_v ? 1:0);
 }
 
-/* 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)
+/* note, this is called as a python 'getset, where the PyAttributeDef is the closure */
+PyObject *PyObjectPlus::py_get_attrdef(PyObject *self_py, const PyAttributeDef *attrdef)
 {
-       PyObjectPlus *self_plus= BGE_PROXY_REF(self);
-       if(self_plus==NULL) {
-               PyErr_SetString(PyExc_SystemError, BGE_PROXY_ERROR_MSG);
-               return -1;
-       }
-       
-       if (value==NULL)
-               return self_plus->py_delattro(attr);
-       
-       return self_plus->py_setattro(attr, value); 
-}
+       void *self= (void *)(BGE_PROXY_REF(self_py));
+       if(self==NULL) {
+               if(attrdef == attr_invalid)
+                       Py_RETURN_TRUE; // dont bother running the function
 
-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_SystemError, 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); \
-       if (descr == NULL) {
-               return NULL; /* py_base_getattro sets the error, this way we can avoid setting the error at many levels */
-       } else {
-               /* Copied from py_getattro_up */
-               if (PyCObject_Check(descr)) {
-                       return py_get_attrdef((void *)this, (const PyAttributeDef*)PyCObject_AsVoidPtr(descr));
-               } else if (descr->ob_type->tp_descr_get) {
-                       return PyCFunction_New(((PyMethodDescrObject *)descr)->d_method, this->m_proxy);
-               } else {
-                       return NULL;
-               }
-               /* end py_getattro_up copy */
-       }
-}
-
-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");
-       return 1;
-}
-
-int PyObjectPlus::py_setattro(PyObject *attr, PyObject* value)
-{
-       PyErr_SetString(PyExc_AttributeError, "attribute cant be set");
-       return PY_SET_ATTR_MISSING;
-}
 
-PyObject *PyObjectPlus::py_get_attrdef(void *self, const PyAttributeDef *attrdef)
-{
        if (attrdef->m_type == KX_PYATTRIBUTE_TYPE_DUMMY)
        {
                // fake attribute, ignore
@@ -257,14 +256,14 @@ PyObject *PyObjectPlus::py_get_attrdef(void *self, const PyAttributeDef *attrdef
                                {
                                        bool *val = reinterpret_cast<bool*>(ptr);
                                        ptr += sizeof(bool);
-                                       PyList_SET_ITEM(resultlist,i,PyInt_FromLong(*val));
+                                       PyList_SET_ITEM(resultlist,i,PyLong_FromSsize_t(*val));
                                        break;
                                }
                        case KX_PYATTRIBUTE_TYPE_SHORT:
                                {
                                        short int *val = reinterpret_cast<short int*>(ptr);
                                        ptr += sizeof(short int);
-                                       PyList_SET_ITEM(resultlist,i,PyInt_FromLong(*val));
+                                       PyList_SET_ITEM(resultlist,i,PyLong_FromSsize_t(*val));
                                        break;
                                }
                        case KX_PYATTRIBUTE_TYPE_ENUM:
@@ -279,7 +278,7 @@ PyObject *PyObjectPlus::py_get_attrdef(void *self, const PyAttributeDef *attrdef
                                {
                                        int *val = reinterpret_cast<int*>(ptr);
                                        ptr += sizeof(int);
-                                       PyList_SET_ITEM(resultlist,i,PyInt_FromLong(*val));
+                                       PyList_SET_ITEM(resultlist,i,PyLong_FromSsize_t(*val));
                                        break;
                                }
                        case KX_PYATTRIBUTE_TYPE_FLOAT:
@@ -303,12 +302,12 @@ PyObject *PyObjectPlus::py_get_attrdef(void *self, const PyAttributeDef *attrdef
                case KX_PYATTRIBUTE_TYPE_BOOL:
                        {
                                bool *val = reinterpret_cast<bool*>(ptr);
-                               return PyInt_FromLong(*val);
+                               return PyLong_FromSsize_t(*val);
                        }
                case KX_PYATTRIBUTE_TYPE_SHORT:
                        {
                                short int *val = reinterpret_cast<short int*>(ptr);
-                               return PyInt_FromLong(*val);
+                               return PyLong_FromSsize_t(*val);
                        }
                case KX_PYATTRIBUTE_TYPE_ENUM:
                        // enum are like int, just make sure the field size is the same
@@ -320,17 +319,32 @@ PyObject *PyObjectPlus::py_get_attrdef(void *self, const PyAttributeDef *attrdef
                case KX_PYATTRIBUTE_TYPE_INT:
                        {
                                int *val = reinterpret_cast<int*>(ptr);
-                               return PyInt_FromLong(*val);
+                               return PyLong_FromSsize_t(*val);
                        }
                case KX_PYATTRIBUTE_TYPE_FLOAT:
                        {
                                float *val = reinterpret_cast<float*>(ptr);
                                return PyFloat_FromDouble(*val);
                        }
+               case KX_PYATTRIBUTE_TYPE_VECTOR:
+                       {
+                               MT_Vector3 *val = reinterpret_cast<MT_Vector3*>(ptr);
+#ifdef USE_MATHUTILS
+                               float fval[3]= {(*val)[0], (*val)[1], (*val)[2]};
+                               return newVectorObject(fval, 3, Py_NEW, NULL);
+#else
+                               PyObject* resultlist = PyList_New(3);
+                               for (unsigned int i=0; i<3; i++)
+                               {
+                                       PyList_SET_ITEM(resultlist,i,PyFloat_FromDouble((*val)[i]));
+                               }
+                               return resultlist;
+#endif
+                       }
                case KX_PYATTRIBUTE_TYPE_STRING:
                        {
                                STR_String *val = reinterpret_cast<STR_String*>(ptr);
-                               return PyString_FromString(*val);
+                               return PyUnicode_FromString(*val);
                        }
                default:
                        return NULL;
@@ -338,8 +352,15 @@ PyObject *PyObjectPlus::py_get_attrdef(void *self, const PyAttributeDef *attrdef
        }
 }
 
-int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyObject *value)
+/* note, this is called as a python getset */
+int PyObjectPlus::py_set_attrdef(PyObject *self_py, PyObject *value, const PyAttributeDef *attrdef)
 {
+       void *self= (void *)(BGE_PROXY_REF(self_py));
+       if(self==NULL) {
+               PyErr_SetString(PyExc_SystemError, BGE_PROXY_ERROR_MSG);
+               return PY_SET_ATTR_FAIL;
+       }
+
        void *undoBuffer = NULL;
        void *sourceBuffer = NULL;
        size_t bufferSize = 0;
@@ -350,12 +371,12 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                if (!PySequence_Check(value)) 
                {
                        PyErr_Format(PyExc_TypeError, "expected a sequence for attribute \"%s\"", attrdef->m_name);
-                       return 1;
+                       return PY_SET_ATTR_FAIL;
                }
                if (PySequence_Size(value) != attrdef->m_length)
                {
                        PyErr_Format(PyExc_TypeError, "incorrect number of elements in sequence for attribute \"%s\"", attrdef->m_name);
-                       return 1;
+                       return PY_SET_ATTR_FAIL;
                }
                switch (attrdef->m_type) 
                {
@@ -363,7 +384,7 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                        if (attrdef->m_setFunction == NULL) 
                        {
                                PyErr_Format(PyExc_AttributeError, "function attribute without function for attribute \"%s\", report to blender.org", attrdef->m_name);
-                               return 1;
+                               return PY_SET_ATTR_FAIL;
                        }
                        return (*attrdef->m_setFunction)(self, attrdef, value);
                case KX_PYATTRIBUTE_TYPE_BOOL:
@@ -382,7 +403,7 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                default:
                        // should not happen
                        PyErr_Format(PyExc_AttributeError, "Unsupported attribute type for attribute \"%s\", report to blender.org", attrdef->m_name);
-                       return 1;
+                       return PY_SET_ATTR_FAIL;
                }
                // let's implement a smart undo method
                bufferSize *= attrdef->m_length;
@@ -404,9 +425,9 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                                {
                                        bool *var = reinterpret_cast<bool*>(ptr);
                                        ptr += sizeof(bool);
-                                       if (PyInt_Check(item)) 
+                                       if (PyLong_Check(item)) 
                                        {
-                                               *var = (PyInt_AsLong(item) != 0);
+                                               *var = (PyLong_AsSsize_t(item) != 0);
                                        } 
                                        else if (PyBool_Check(item))
                                        {
@@ -423,9 +444,9 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                                {
                                        short int *var = reinterpret_cast<short int*>(ptr);
                                        ptr += sizeof(short int);
-                                       if (PyInt_Check(item)) 
+                                       if (PyLong_Check(item)) 
                                        {
-                                               long val = PyInt_AsLong(item);
+                                               long val = PyLong_AsSsize_t(item);
                                                if (attrdef->m_clamp)
                                                {
                                                        if (val < attrdef->m_imin)
@@ -459,9 +480,9 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                                {
                                        int *var = reinterpret_cast<int*>(ptr);
                                        ptr += sizeof(int);
-                                       if (PyInt_Check(item)) 
+                                       if (PyLong_Check(item)) 
                                        {
-                                               long val = PyInt_AsLong(item);
+                                               long val = PyLong_AsSsize_t(item);
                                                if (attrdef->m_clamp)
                                                {
                                                        if (val < attrdef->m_imin)
@@ -519,6 +540,10 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                {
                        if ((*attrdef->m_checkFunction)(self, attrdef) != 0)
                        {
+                               // if the checing function didnt set an error then set a generic one here so we dont set an error with no exception
+                               if (PyErr_Occurred()==0)
+                                       PyErr_Format(PyExc_AttributeError, "type check error for attribute \"%s\", reasion unknown", attrdef->m_name);
+                               
                                // post check returned an error, restore values
                        UNDO_AND_ERROR:
                                if (undoBuffer)
@@ -526,12 +551,12 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                                        memcpy(sourceBuffer, undoBuffer, bufferSize);
                                        free(undoBuffer);
                                }
-                               return 1;
+                               return PY_SET_ATTR_FAIL;
                        }
                }
                if (undoBuffer)
                        free(undoBuffer);
-               return 0;
+               return PY_SET_ATTR_SUCCESS;
        }
        else    // simple attribute value
        {
@@ -540,11 +565,11 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                        if (attrdef->m_setFunction == NULL)
                        {
                                PyErr_Format(PyExc_AttributeError, "function attribute without function \"%s\", report to blender.org", attrdef->m_name);
-                               return 1;
+                               return PY_SET_ATTR_FAIL;
                        }
                        return (*attrdef->m_setFunction)(self, attrdef, value);
                }
-               if (attrdef->m_checkFunction != NULL)
+               if (attrdef->m_checkFunction != NULL || attrdef->m_type == KX_PYATTRIBUTE_TYPE_VECTOR)
                {
                        // post check function is provided, prepare undo buffer
                        sourceBuffer = ptr;
@@ -568,9 +593,12 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                                if (sourceBuffer)
                                        bufferSize = strlen(reinterpret_cast<char*>(sourceBuffer))+1;
                                break;
+                       case KX_PYATTRIBUTE_TYPE_VECTOR:
+                               bufferSize = sizeof(MT_Vector3);
+                               break;
                        default:
                                PyErr_Format(PyExc_AttributeError, "unknown type for attribute \"%s\", report to blender.org", attrdef->m_name);
-                               return 1;
+                               return PY_SET_ATTR_FAIL;
                        }
                        if (bufferSize)
                        {
@@ -587,9 +615,9 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                case KX_PYATTRIBUTE_TYPE_BOOL:
                        {
                                bool *var = reinterpret_cast<bool*>(ptr);
-                               if (PyInt_Check(value)) 
+                               if (PyLong_Check(value)) 
                                {
-                                       *var = (PyInt_AsLong(value) != 0);
+                                       *var = (PyLong_AsSsize_t(value) != 0);
                                } 
                                else if (PyBool_Check(value))
                                {
@@ -605,9 +633,9 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                case KX_PYATTRIBUTE_TYPE_SHORT:
                        {
                                short int *var = reinterpret_cast<short int*>(ptr);
-                               if (PyInt_Check(value)) 
+                               if (PyLong_Check(value)) 
                                {
-                                       long val = PyInt_AsLong(value);
+                                       long val = PyLong_AsSsize_t(value);
                                        if (attrdef->m_clamp)
                                        {
                                                if (val < attrdef->m_imin)
@@ -640,9 +668,9 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                case KX_PYATTRIBUTE_TYPE_INT:
                        {
                                int *var = reinterpret_cast<int*>(ptr);
-                               if (PyInt_Check(value)) 
+                               if (PyLong_Check(value)) 
                                {
-                                       long val = PyInt_AsLong(value);
+                                       long val = PyLong_AsSsize_t(value);
                                        if (attrdef->m_clamp)
                                        {
                                                if (val < attrdef->m_imin)
@@ -688,12 +716,48 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                                *var = (float)val;
                                break;
                        }
+               case KX_PYATTRIBUTE_TYPE_VECTOR:
+                       {
+                               if (!PySequence_Check(value) || PySequence_Size(value) != 3) 
+                               {
+                                       PyErr_Format(PyExc_TypeError, "expected a sequence of 3 floats for attribute \"%s\"", attrdef->m_name);
+                                       return PY_SET_ATTR_FAIL;
+                               }
+                               MT_Vector3 *var = reinterpret_cast<MT_Vector3*>(ptr);
+                               for (int i=0; i<3; i++)
+                               {
+                                       PyObject *item = PySequence_GetItem(value, i); /* new ref */
+                                       // we can decrement the reference immediately, the reference count
+                                       // is at least 1 because the item is part of an array
+                                       Py_DECREF(item);
+                                       double val = PyFloat_AsDouble(item);
+                                       if (val == -1.0 && PyErr_Occurred())
+                                       {
+                                               PyErr_Format(PyExc_TypeError, "expected a sequence of 3 floats for attribute \"%s\"", attrdef->m_name);
+                                               goto RESTORE_AND_ERROR;
+                                       }
+                                       else if (attrdef->m_clamp)
+                                       {
+                                               if (val < attrdef->m_fmin)
+                                                       val = attrdef->m_fmin;
+                                               else if (val > attrdef->m_fmax)
+                                                       val = attrdef->m_fmax;
+                                       }
+                                       else if (val < attrdef->m_fmin || val > attrdef->m_fmax)
+                                       {
+                                               PyErr_Format(PyExc_ValueError, "value out of range for attribute \"%s\"", attrdef->m_name);
+                                               goto RESTORE_AND_ERROR;
+                                       }
+                                       (*var)[i] = (MT_Scalar)val;
+                               }
+                               break;
+                       }
                case KX_PYATTRIBUTE_TYPE_STRING:
                        {
                                STR_String *var = reinterpret_cast<STR_String*>(ptr);
-                               if (PyString_Check(value)) 
+                               if (PyUnicode_Check(value)) 
                                {
-                                       char *val = PyString_AsString(value);
+                                       char *val = _PyUnicode_AsString(value);
                                        if (attrdef->m_clamp)
                                        {
                                                if (strlen(val) < attrdef->m_imin)
@@ -774,48 +838,6 @@ PyObject *PyObjectPlus::py_repr(void)
        return NULL;
 }
 
-/*------------------------------
- * PyObjectPlus isA            -- the isA functions
-------------------------------*/
-bool PyObjectPlus::isA(PyTypeObject *T)                // if called with a Type, use "typename"
-{
-       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();
-  
-       for (P = Ps[i=0]; P != NULL; P = Ps[i++])
-               if (strcmp(P->tp_name, mytypename)==0)
-                       return true;
-
-       return false;
-}
-
-PyObject *PyObjectPlus::PyisA(PyObject *value)         // Python wrapper for isA
-{
-       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, "object.isA(value): expected a type or a string");
-    return NULL;       
-}
-
-
 void PyObjectPlus::ProcessReplica()
 {
        /* Clear the proxy, will be created again if needed with GetProxy()
@@ -840,27 +862,6 @@ void PyObjectPlus::InvalidateProxy()               // check typename of each parent
        }
 }
 
-/* Utility function called by the macro py_getattro_up()
- * for getting ob.__dict__() values from our PyObject
- * this is used by python for doing dir() on an object, so its good
- * if we return a list of attributes and methods.
- * 
- * Other then making dir() useful the value returned from __dict__() is not useful
- * since every value is a Py_None
- * */
-PyObject *py_getattr_dict(PyObject *pydict, PyObject *tp_dict)
-{
-    if(pydict==NULL) { /* incase calling __dict__ on the parent of this object raised an error */
-       PyErr_Clear();
-       pydict = PyDict_New();
-    }
-       
-       PyDict_Update(pydict, tp_dict);
-       return pydict;
-}
-
-
-
 PyObject *PyObjectPlus::GetProxy_Ext(PyObjectPlus *self, PyTypeObject *tp)
 {
        if (self->m_proxy==NULL)
@@ -904,15 +905,16 @@ PyObject *PyObjectPlus::NewProxy_Ext(PyObjectPlus *self, PyTypeObject *tp, bool
 ///////////////////////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////////////////////
 /* deprecation warning management */
+
 bool PyObjectPlus::m_ignore_deprecation_warnings(false);
 void PyObjectPlus::SetDeprecationWarnings(bool ignoreDeprecationWarnings)
 {
        m_ignore_deprecation_warnings = ignoreDeprecationWarnings;
 }
 
-void PyObjectPlus::ShowDeprecationWarning(const char* old_way,const char* new_way)
+void PyObjectPlus::ShowDeprecationWarning_func(const char* old_way,const char* new_way)
 {
-       if (!m_ignore_deprecation_warnings) {
+       {
                printf("Method %s is deprecated, please use %s instead.\n", old_way, new_way);
                
                // import sys; print '\t%s:%d' % (sys._getframe(0).f_code.co_filename, sys._getframe(0).f_lineno)
@@ -930,7 +932,7 @@ void PyObjectPlus::ShowDeprecationWarning(const char* old_way,const char* new_wa
                                        co_filename= PyObject_GetAttrString(f_code, "co_filename");
                                        if (co_filename) {
                                                
-                                               printf("\t%s:%d\n", PyString_AsString(co_filename), (int)PyInt_AsLong(f_lineno));
+                                               printf("\t%s:%d\n", _PyUnicode_AsString(co_filename), (int)PyLong_AsSsize_t(f_lineno));
                                                
                                                Py_DECREF(f_lineno);
                                                Py_DECREF(f_code);
@@ -951,6 +953,29 @@ void PyObjectPlus::ShowDeprecationWarning(const char* old_way,const char* new_wa
        }
 }
 
+void PyObjectPlus::ClearDeprecationWarning()
+{
+       WarnLink *wlink_next;
+       WarnLink *wlink = GetDeprecationWarningLinkFirst();
+       
+       while(wlink)
+       {
+               wlink->warn_done= false; /* no need to NULL the link, its cleared before adding to the list next time round */
+               wlink_next= reinterpret_cast<WarnLink *>(wlink->link);
+               wlink->link= NULL;
+               wlink= wlink_next;
+       }
+       NullDeprecationWarning();
+}
+
+WarnLink*              m_base_wlink_first= NULL;
+WarnLink*              m_base_wlink_last= NULL;
+
+WarnLink*              PyObjectPlus::GetDeprecationWarningLinkFirst(void) {return m_base_wlink_first;}
+WarnLink*              PyObjectPlus::GetDeprecationWarningLinkLast(void) {return m_base_wlink_last;}
+void                   PyObjectPlus::SetDeprecationWarningFirst(WarnLink* wlink) {m_base_wlink_first= wlink;}
+void                   PyObjectPlus::SetDeprecationWarningLinkLast(WarnLink* wlink) {m_base_wlink_last= wlink;}
+void                   PyObjectPlus::NullDeprecationWarning() {m_base_wlink_first= m_base_wlink_last= NULL;}
 
 #endif //NO_EXP_PYTHON_EMBEDDING