svn merge -r 21041:21301 https://svn.blender.org/svnroot/bf-blender/branches/blender2...
[blender.git] / source / gameengine / Expressions / PyObjectPlus.cpp
index 0db2e8991fc73005e1b6a1996cf166e93dc409fb..729fff31052a9d848af62935b1aa7dedd1ab7cf1 100644 (file)
 #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
 ------------------------------*/
 
+
 PyTypeObject PyObjectPlus::Type = {
-       PyObject_HEAD_INIT(NULL)
+#if (PY_VERSION_HEX >= 0x02060000)
+       PyVarObject_HEAD_INIT(NULL, 0)
+#else
+       /* python 2.5 and below */
+       PyObject_HEAD_INIT( NULL )  /* required py macro */
        0,                              /*ob_size*/
+#endif
        "PyObjectPlus",                 /*tp_name*/
-       sizeof(PyObjectPlus),           /*tp_basicsize*/
+       sizeof(PyObjectPlus_Proxy),             /*tp_basicsize*/
        0,                              /*tp_itemsize*/
        /* methods */
-       PyDestructor,
+       py_base_dealloc,
        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
+       Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+       0,0,0,0,0,0,0,
+       Methods,
+       0,
+       0,
+       NULL // no subtype
 };
 
+
 PyObjectPlus::~PyObjectPlus()
 {
-       if (ob_refcnt)
-       {
-               _Py_ForgetReference(this);
+       if(m_proxy) {
+               Py_DECREF(m_proxy);                     /* Remove own reference, python may still have 1 */
+               BGE_PROXY_REF(m_proxy)= NULL;
        }
 //     assert(ob_refcnt==0);
 }
 
-PyObjectPlus::PyObjectPlus(PyTypeObject *T)                            // constructor
+
+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() : SG_QList()                              // constructor
 {
-       MT_assert(T != NULL);
-       this->ob_type = T; 
-       _Py_NewReference(this);
-       SetZombie(false);
+       m_proxy= NULL;
 };
-  
+
 /*------------------------------
  * PyObjectPlus Methods        -- Every class, even the abstract one should have a Methods
 ------------------------------*/
 PyMethodDef PyObjectPlus::Methods[] = {
-  {"isA",               (PyCFunction) sPy_isA,                 METH_O},
   {NULL, NULL}         /* Sentinel */
 };
 
+#define attr_invalid (&(PyObjectPlus::Attributes[0]))
 PyAttributeDef PyObjectPlus::Attributes[] = {
-       KX_PYATTRIBUTE_RO_FUNCTION("isValid",           PyObjectPlus, pyattr_get_is_valid),
+       KX_PYATTRIBUTE_RO_FUNCTION("invalid",           PyObjectPlus, pyattr_get_invalid),
        {NULL} //Sentinel
 };
 
-PyObject* PyObjectPlus::pyattr_get_is_valid(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
-{      
-       Py_RETURN_TRUE;
-}
 
-/*------------------------------
- * PyObjectPlus Parents                -- Every class, even the abstract one should have parents
-------------------------------*/
-PyParentObject PyObjectPlus::Parents[] = {&PyObjectPlus::Type, NULL};
 
-/*------------------------------
- * PyObjectPlus attributes     -- attributes
-------------------------------*/
-PyObject *PyObjectPlus::py_getattro(PyObject* attr)
+PyObject* PyObjectPlus::pyattr_get_invalid(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
-       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 {
-               /* 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, (PyObject *)this);
-               } else {
-                       fprintf(stderr, "Unknown attribute type (PyObjectPlus::py_getattro)");
-                       return descr;
-               }
-               /* end py_getattro_up copy */
-       }
-  //if (streq(attr, "type"))
-  //  return Py_BuildValue("s", (*(GetParents()))->tp_name);
+       return PyBool_FromLong(self_v ? 1:0);
 }
 
-int PyObjectPlus::py_delattro(PyObject* attr)
+/* 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)
 {
-       PyErr_SetString(PyExc_AttributeError, "attribute cant be deleted");
-       return 1;
-}
+       void *self= (void *)(BGE_PROXY_REF(self_py));
+       if(self==NULL) {
+               if(attrdef == attr_invalid)
+                       Py_RETURN_TRUE; // dont bother running the function
+
+               PyErr_SetString(PyExc_SystemError, BGE_PROXY_ERROR_MSG);
+               return NULL;
+       }
 
-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;
-}
 
-PyObject *PyObjectPlus::py_get_attrdef(void *self, const PyAttributeDef *attrdef)
-{
        if (attrdef->m_type == KX_PYATTRIBUTE_TYPE_DUMMY)
        {
                // fake attribute, ignore
@@ -181,14 +256,14 @@ PyObject *PyObjectPlus::py_get_attrdef(void *self, const PyAttributeDef *attrdef
                                {
                                        bool *val = reinterpret_cast<bool*>(ptr);
                                        ptr += sizeof(bool);
-                                       PyList_SetItem(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_SetItem(resultlist,i,PyInt_FromLong(*val));
+                                       PyList_SET_ITEM(resultlist,i,PyLong_FromSsize_t(*val));
                                        break;
                                }
                        case KX_PYATTRIBUTE_TYPE_ENUM:
@@ -203,14 +278,14 @@ PyObject *PyObjectPlus::py_get_attrdef(void *self, const PyAttributeDef *attrdef
                                {
                                        int *val = reinterpret_cast<int*>(ptr);
                                        ptr += sizeof(int);
-                                       PyList_SetItem(resultlist,i,PyInt_FromLong(*val));
+                                       PyList_SET_ITEM(resultlist,i,PyLong_FromSsize_t(*val));
                                        break;
                                }
                        case KX_PYATTRIBUTE_TYPE_FLOAT:
                                {
                                        float *val = reinterpret_cast<float*>(ptr);
                                        ptr += sizeof(float);
-                                       PyList_SetItem(resultlist,i,PyFloat_FromDouble(*val));
+                                       PyList_SET_ITEM(resultlist,i,PyFloat_FromDouble(*val));
                                        break;
                                }
                        default:
@@ -227,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
@@ -244,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;
@@ -262,22 +352,15 @@ PyObject *PyObjectPlus::py_get_attrdef(void *self, const PyAttributeDef *attrdef
        }
 }
 
-#if 0
-PyObject *PyObjectPlus::py_getattro_self(const PyAttributeDef attrlist[], void *self, PyObject *attr)
+/* note, this is called as a python getset */
+int PyObjectPlus::py_set_attrdef(PyObject *self_py, PyObject *value, const PyAttributeDef *attrdef)
 {
-       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
+       void *self= (void *)(BGE_PROXY_REF(self_py));
+       if(self==NULL) {
+               PyErr_SetString(PyExc_SystemError, BGE_PROXY_ERROR_MSG);
+               return PY_SET_ATTR_FAIL;
+       }
 
-int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyObject *value)
-{
        void *undoBuffer = NULL;
        void *sourceBuffer = NULL;
        size_t bufferSize = 0;
@@ -287,21 +370,21 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
        {
                if (!PySequence_Check(value)) 
                {
-                       PyErr_SetString(PyExc_TypeError, "expected a sequence");
-                       return 1;
+                       PyErr_Format(PyExc_TypeError, "expected a sequence for attribute \"%s\"", attrdef->m_name);
+                       return PY_SET_ATTR_FAIL;
                }
                if (PySequence_Size(value) != attrdef->m_length)
                {
-                       PyErr_SetString(PyExc_TypeError, "incorrect number of elements in sequence");
-                       return 1;
+                       PyErr_Format(PyExc_TypeError, "incorrect number of elements in sequence for attribute \"%s\"", attrdef->m_name);
+                       return PY_SET_ATTR_FAIL;
                }
                switch (attrdef->m_type) 
                {
                case KX_PYATTRIBUTE_TYPE_FUNCTION:
                        if (attrdef->m_setFunction == NULL) 
                        {
-                               PyErr_SetString(PyExc_AttributeError, "function attribute without function, report to blender.org");
-                               return 1;
+                               PyErr_Format(PyExc_AttributeError, "function attribute without function for attribute \"%s\", report to blender.org", attrdef->m_name);
+                               return PY_SET_ATTR_FAIL;
                        }
                        return (*attrdef->m_setFunction)(self, attrdef, value);
                case KX_PYATTRIBUTE_TYPE_BOOL:
@@ -319,8 +402,8 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                        break;
                default:
                        // should not happen
-                       PyErr_SetString(PyExc_AttributeError, "Unsupported attribute type, report to blender.org");
-                       return 1;
+                       PyErr_Format(PyExc_AttributeError, "Unsupported attribute type for attribute \"%s\", report to blender.org", attrdef->m_name);
+                       return PY_SET_ATTR_FAIL;
                }
                // let's implement a smart undo method
                bufferSize *= attrdef->m_length;
@@ -342,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))
                                        {
@@ -352,7 +435,7 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                                        }
                                        else
                                        {
-                                               PyErr_SetString(PyExc_TypeError, "expected an integer or a bool");
+                                               PyErr_Format(PyExc_TypeError, "expected an integer or a bool for attribute \"%s\"", attrdef->m_name);
                                                goto UNDO_AND_ERROR;
                                        }
                                        break;
@@ -361,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)
@@ -373,14 +456,14 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                                                }
                                                else if (val < attrdef->m_imin || val > attrdef->m_imax)
                                                {
-                                                       PyErr_SetString(PyExc_ValueError, "item value out of range");
+                                                       PyErr_Format(PyExc_ValueError, "item value out of range for attribute \"%s\"", attrdef->m_name);
                                                        goto UNDO_AND_ERROR;
                                                }
                                                *var = (short int)val;
                                        }
                                        else
                                        {
-                                               PyErr_SetString(PyExc_TypeError, "expected an integer");
+                                               PyErr_Format(PyExc_TypeError, "expected an integer for attribute \"%s\"", attrdef->m_name);
                                                goto UNDO_AND_ERROR;
                                        }
                                        break;
@@ -389,7 +472,7 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                                // enum are equivalent to int, just make sure that the field size matches:
                                if (sizeof(int) != attrdef->m_size)
                                {
-                                       PyErr_SetString(PyExc_AttributeError, "attribute size check error, report to blender.org");
+                                       PyErr_Format(PyExc_AttributeError, "Size check error for attribute, \"%s\", report to blender.org", attrdef->m_name);
                                        goto UNDO_AND_ERROR;
                                }
                                // walkthrough
@@ -397,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)
@@ -409,14 +492,14 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                                                }
                                                else if (val < attrdef->m_imin || val > attrdef->m_imax)
                                                {
-                                                       PyErr_SetString(PyExc_ValueError, "item value out of range");
+                                                       PyErr_Format(PyExc_ValueError, "item value out of range for attribute \"%s\"", attrdef->m_name);
                                                        goto UNDO_AND_ERROR;
                                                }
                                                *var = (int)val;
                                        }
                                        else
                                        {
-                                               PyErr_SetString(PyExc_TypeError, "expected an integer");
+                                               PyErr_Format(PyExc_TypeError, "expected an integer for attribute \"%s\"", attrdef->m_name);
                                                goto UNDO_AND_ERROR;
                                        }
                                        break;
@@ -428,7 +511,7 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                                        double val = PyFloat_AsDouble(item);
                                        if (val == -1.0 && PyErr_Occurred())
                                        {
-                                               PyErr_SetString(PyExc_TypeError, "expected a float");
+                                               PyErr_Format(PyExc_TypeError, "expected a float for attribute \"%s\"", attrdef->m_name);
                                                goto UNDO_AND_ERROR;
                                        }
                                        else if (attrdef->m_clamp) 
@@ -440,7 +523,7 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                                        }
                                        else if (val < attrdef->m_fmin || val > attrdef->m_fmax)
                                        {
-                                               PyErr_SetString(PyExc_ValueError, "item value out of range");
+                                               PyErr_Format(PyExc_ValueError, "item value out of range for attribute \"%s\"", attrdef->m_name);
                                                goto UNDO_AND_ERROR;
                                        }
                                        *var = (float)val;
@@ -448,7 +531,7 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                                }
                        default:
                                // should not happen
-                               PyErr_SetString(PyExc_AttributeError, "attribute type check error, report to blender.org");
+                               PyErr_Format(PyExc_AttributeError, "type check error for attribute \"%s\", report to blender.org", attrdef->m_name);
                                goto UNDO_AND_ERROR;
                        }
                }
@@ -457,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)
@@ -464,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
        {
@@ -477,12 +564,12 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                {
                        if (attrdef->m_setFunction == NULL)
                        {
-                               PyErr_SetString(PyExc_AttributeError, "function attribute without function, report to blender.org");
-                               return 1;
+                               PyErr_Format(PyExc_AttributeError, "function attribute without function \"%s\", report to blender.org", attrdef->m_name);
+                               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;
@@ -506,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_SetString(PyExc_AttributeError, "unknown attribute type, report to blender.org");
-                               return 1;
+                               PyErr_Format(PyExc_AttributeError, "unknown type for attribute \"%s\", report to blender.org", attrdef->m_name);
+                               return PY_SET_ATTR_FAIL;
                        }
                        if (bufferSize)
                        {
@@ -525,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))
                                {
@@ -535,7 +625,7 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                                }
                                else
                                {
-                                       PyErr_SetString(PyExc_TypeError, "expected an integer or a bool");
+                                       PyErr_Format(PyExc_TypeError, "expected an integer or a bool for attribute \"%s\"", attrdef->m_name);
                                        goto FREE_AND_ERROR;
                                }
                                break;
@@ -543,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)
@@ -555,14 +645,14 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                                        }
                                        else if (val < attrdef->m_imin || val > attrdef->m_imax)
                                        {
-                                               PyErr_SetString(PyExc_ValueError, "value out of range");
+                                               PyErr_Format(PyExc_ValueError, "value out of range for attribute \"%s\"", attrdef->m_name);
                                                goto FREE_AND_ERROR;
                                        }
                                        *var = (short int)val;
                                }
                                else
                                {
-                                       PyErr_SetString(PyExc_TypeError, "expected an integer");
+                                       PyErr_Format(PyExc_TypeError, "expected an integer for attribute \"%s\"", attrdef->m_name);
                                        goto FREE_AND_ERROR;
                                }
                                break;
@@ -571,16 +661,16 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                        // enum are equivalent to int, just make sure that the field size matches:
                        if (sizeof(int) != attrdef->m_size)
                        {
-                               PyErr_SetString(PyExc_AttributeError, "attribute size check error, report to blender.org");
+                               PyErr_Format(PyExc_AttributeError, "attribute size check error for attribute \"%s\", report to blender.org", attrdef->m_name);
                                goto FREE_AND_ERROR;
                        }
                        // walkthrough
                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)
@@ -590,14 +680,14 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                                        }
                                        else if (val < attrdef->m_imin || val > attrdef->m_imax)
                                        {
-                                               PyErr_SetString(PyExc_ValueError, "value out of range");
+                                               PyErr_Format(PyExc_ValueError, "value out of range for attribute \"%s\"", attrdef->m_name);
                                                goto FREE_AND_ERROR;
                                        }
                                        *var = (int)val;
                                }
                                else
                                {
-                                       PyErr_SetString(PyExc_TypeError, "expected an integer");
+                                       PyErr_Format(PyExc_TypeError, "expected an integer for attribute \"%s\"", attrdef->m_name);
                                        goto FREE_AND_ERROR;
                                }
                                break;
@@ -608,7 +698,7 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                                double val = PyFloat_AsDouble(value);
                                if (val == -1.0 && PyErr_Occurred())
                                {
-                                       PyErr_SetString(PyExc_TypeError, "expected a float");
+                                       PyErr_Format(PyExc_TypeError, "expected a float for attribute \"%s\"", attrdef->m_name);
                                        goto FREE_AND_ERROR;
                                }
                                else if (attrdef->m_clamp)
@@ -620,24 +710,60 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                                }
                                else if (val < attrdef->m_fmin || val > attrdef->m_fmax)
                                {
-                                       PyErr_SetString(PyExc_ValueError, "value out of range");
+                                       PyErr_Format(PyExc_ValueError, "value out of range for attribute \"%s\"", attrdef->m_name);
                                        goto FREE_AND_ERROR;
                                }
                                *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)
                                                {
                                                        // can't increase the length of the string
-                                                       PyErr_SetString(PyExc_ValueError, "string length too short");
+                                                       PyErr_Format(PyExc_ValueError, "string length too short for attribute \"%s\"", attrdef->m_name);
                                                        goto FREE_AND_ERROR;
                                                }
                                                else if (strlen(val) > attrdef->m_imax)
@@ -651,21 +777,21 @@ int PyObjectPlus::py_set_attrdef(void *self, const PyAttributeDef *attrdef, PyOb
                                                }
                                        } else if (strlen(val) < attrdef->m_imin || strlen(val) > attrdef->m_imax)
                                        {
-                                               PyErr_SetString(PyExc_ValueError, "string length out of range");
+                                               PyErr_Format(PyExc_ValueError, "string length out of range for attribute \"%s\"", attrdef->m_name);
                                                goto FREE_AND_ERROR;
                                        }
                                        *var = val;
                                }
                                else
                                {
-                                       PyErr_SetString(PyExc_TypeError, "expected a string");
+                                       PyErr_Format(PyExc_TypeError, "expected a string for attribute \"%s\"", attrdef->m_name);
                                        goto FREE_AND_ERROR;
                                }
                                break;
                        }
                default:
                        // should not happen
-                       PyErr_SetString(PyExc_AttributeError, "unknown attribute type, report to blender.org");
+                       PyErr_Format(PyExc_AttributeError, "unknown type for attribute \"%s\", report to blender.org", attrdef->m_name);
                        goto FREE_AND_ERROR;
                }
        }
@@ -701,29 +827,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
@@ -734,65 +838,144 @@ PyObject *PyObjectPlus::py_repr(void)
        return NULL;
 }
 
-/*------------------------------
- * PyObjectPlus isA            -- the isA functions
-------------------------------*/
-bool PyObjectPlus::isA(PyTypeObject *T)                // if called with a Type, use "typename"
+void PyObjectPlus::ProcessReplica()
+{
+       /* Clear the proxy, will be created again if needed with GetProxy()
+        * otherwise the PyObject will point to the wrong reference */
+       m_proxy= NULL;
+}
+
+/* Sometimes we might want to manually invalidate a BGE type even if
+ * it hasnt been released by the BGE, say for example when an object
+ * is removed from a scene, accessing it may cause problems.
+ * 
+ * In this case the current proxy is made invalid, disowned,
+ * and will raise an error on access. However if python can get access
+ * to this class again it will make a new proxy and work as expected.
+ */
+void PyObjectPlus::InvalidateProxy()           // check typename of each parent
 {
-       int i;
-       PyParentObject  P;
-       PyParentObject *Ps = GetParents();
+       if(m_proxy) { 
+               BGE_PROXY_REF(m_proxy)=NULL;
+               Py_DECREF(m_proxy);
+               m_proxy= NULL;
+       }
+}
 
-       for (P = Ps[i=0]; P != NULL; P = Ps[i++])
-               if (P==T)
-                       return true;
+PyObject *PyObjectPlus::GetProxy_Ext(PyObjectPlus *self, PyTypeObject *tp)
+{
+       if (self->m_proxy==NULL)
+       {
+               self->m_proxy = reinterpret_cast<PyObject *>PyObject_NEW( PyObjectPlus_Proxy, tp);
+               BGE_PROXY_PYOWNS(self->m_proxy) = false;
+       }
+       //PyObject_Print(self->m_proxy, stdout, 0);
+       //printf("ref %d\n", self->m_proxy->ob_refcnt);
+       
+       BGE_PROXY_REF(self->m_proxy) = self; /* Its possible this was set to NULL, so set it back here */
+       Py_INCREF(self->m_proxy); /* we own one, thos ones fore the return */
+       return self->m_proxy;
+}
 
-       return false;
+PyObject *PyObjectPlus::NewProxy_Ext(PyObjectPlus *self, PyTypeObject *tp, bool py_owns)
+{
+       if (self->m_proxy)
+       {
+               if(py_owns)
+               {       /* Free */
+                       BGE_PROXY_REF(self->m_proxy) = NULL;
+                       Py_DECREF(self->m_proxy);
+                       self->m_proxy= NULL;
+               }
+               else {
+                       Py_INCREF(self->m_proxy);
+                       return self->m_proxy;
+               }
+               
+       }
+       
+       GetProxy_Ext(self, tp);
+       if(py_owns) {
+               BGE_PROXY_PYOWNS(self->m_proxy) = py_owns;
+               Py_DECREF(self->m_proxy); /* could avoid thrashing here but for now its ok */
+       }
+       return self->m_proxy;
 }
 
+///////////////////////////////////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////////////////////////////////
+/* deprecation warning management */
 
-bool PyObjectPlus::isA(const char *mytypename)         // check typename of each parent
+bool PyObjectPlus::m_ignore_deprecation_warnings(false);
+void PyObjectPlus::SetDeprecationWarnings(bool ignoreDeprecationWarnings)
 {
-       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;
+       m_ignore_deprecation_warnings = ignoreDeprecationWarnings;
 }
 
-PyObject *PyObjectPlus::Py_isA(PyObject *value)                // Python wrapper for isA
+void PyObjectPlus::ShowDeprecationWarning_func(const char* old_way,const char* new_way)
 {
-       if (PyType_Check(value)) {
-               return PyBool_FromLong(isA((PyTypeObject *)value));
-       } else if (PyString_Check(value)) {
-               return PyBool_FromLong(isA(PyString_AsString(value)));
+       {
+               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)
+               
+               PyObject *getframe, *frame;
+               PyObject *f_lineno, *f_code, *co_filename;
+               
+               getframe = PySys_GetObject((char *)"_getframe"); // borrowed
+               if (getframe) {
+                       frame = PyObject_CallObject(getframe, NULL);
+                       if (frame) {
+                               f_lineno= PyObject_GetAttrString(frame, "f_lineno");
+                               f_code= PyObject_GetAttrString(frame, "f_code");
+                               if (f_lineno && f_code) {
+                                       co_filename= PyObject_GetAttrString(f_code, "co_filename");
+                                       if (co_filename) {
+                                               
+                                               printf("\t%s:%d\n", _PyUnicode_AsString(co_filename), (int)PyLong_AsSsize_t(f_lineno));
+                                               
+                                               Py_DECREF(f_lineno);
+                                               Py_DECREF(f_code);
+                                               Py_DECREF(co_filename);
+                                               Py_DECREF(frame);
+                                               return;
+                                       }
+                               }
+                               
+                               Py_XDECREF(f_lineno);
+                               Py_XDECREF(f_code);
+                               Py_DECREF(frame);
+                       }
+                       
+               }
+               PyErr_Clear();
+               printf("\tERROR - Could not access sys._getframe(0).f_lineno or sys._getframe().f_code.co_filename\n");
        }
-    PyErr_SetString(PyExc_TypeError, "expected a type or a string");
-    return NULL;       
 }
 
-/* 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)
+void PyObjectPlus::ClearDeprecationWarning()
 {
-    if(pydict==NULL) { /* incase calling __dict__ on the parent of this object raised an error */
-       PyErr_Clear();
-       pydict = PyDict_New();
-    }
+       WarnLink *wlink_next;
+       WarnLink *wlink = GetDeprecationWarningLinkFirst();
        
-       PyDict_Update(pydict, tp_dict);
-       return pydict;
+       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