remove python2.x support
[blender.git] / source / blender / python / intern / bpy_rna.c
index e13f02c5529ffe9f245b331a153b9d91d924944b..6c959b9f42c3eeea44f0e289ebe42f079a3c0225 100644 (file)
@@ -23,7 +23,6 @@
  */
 
 #include "bpy_rna.h"
-#include "bpy_compat.h"
 #include "bpy_util.h"
 //#include "blendef.h"
 #include "BLI_dynstr.h"
 #include "RNA_define.h" /* for defining our own rna */
 
 #include "MEM_guardedalloc.h"
+#include "BKE_utildefines.h"
 #include "BKE_context.h"
 #include "BKE_global.h" /* evil G.* */
 #include "BKE_report.h"
 
+/* only for keyframing */
+#include "DNA_scene_types.h"
+#include "ED_keyframing.h"
+
 #define USE_MATHUTILS
 
 #ifdef USE_MATHUTILS
@@ -89,13 +93,14 @@ static int mathutils_rna_vector_set_index(BPy_PropertyRNA *self, int subtype, fl
 }
 
 Mathutils_Callback mathutils_rna_array_cb = {
-       mathutils_rna_generic_check,
-       mathutils_rna_vector_get,
-       mathutils_rna_vector_set,
-       mathutils_rna_vector_get_index,
-       mathutils_rna_vector_set_index
+       (BaseMathCheckFunc)             mathutils_rna_generic_check,
+       (BaseMathGetFunc)               mathutils_rna_vector_get,
+       (BaseMathSetFunc)               mathutils_rna_vector_set,
+       (BaseMathGetIndexFunc)  mathutils_rna_vector_get_index,
+       (BaseMathSetIndexFunc)  mathutils_rna_vector_set_index
 };
 
+
 /* bpyrna matrix callbacks */
 static int mathutils_rna_matrix_cb_index= -1; /* index for our callbacks */
 
@@ -118,11 +123,11 @@ static int mathutils_rna_matrix_set(BPy_PropertyRNA *self, int subtype, float *m
 }
 
 Mathutils_Callback mathutils_rna_matrix_cb = {
-       mathutils_rna_generic_check,
-       mathutils_rna_matrix_get,
-       mathutils_rna_matrix_set,
-       NULL,
-       NULL
+       (BaseMathCheckFunc)             mathutils_rna_generic_check,
+       (BaseMathGetFunc)               mathutils_rna_matrix_get,
+       (BaseMathSetFunc)               mathutils_rna_matrix_set,
+       (BaseMathGetIndexFunc)  NULL,
+       (BaseMathSetIndexFunc)  NULL
 };
 
 #endif
@@ -165,14 +170,14 @@ static PyObject *pyrna_struct_repr( BPy_StructRNA * self )
        char *name;
 
        /* print name if available */
-       name= RNA_struct_name_get_alloc(&self->ptr, NULL, 0);
+       name= RNA_struct_name_get_alloc(&self->ptr, NULL, FALSE);
        if(name) {
-               pyob= PyUnicode_FromFormat( "[BPy_StructRNA \"%s\" -> \"%s\"]", RNA_struct_identifier(self->ptr.type), name);
+               pyob= PyUnicode_FromFormat( "[BPy_StructRNA \"%.200s\" -> \"%.200s\"]", RNA_struct_identifier(self->ptr.type), name);
                MEM_freeN(name);
                return pyob;
        }
 
-       return PyUnicode_FromFormat( "[BPy_StructRNA \"%s\"]", RNA_struct_identifier(self->ptr.type));
+       return PyUnicode_FromFormat( "[BPy_StructRNA \"%.200s\"]", RNA_struct_identifier(self->ptr.type));
 }
 
 static PyObject *pyrna_prop_repr( BPy_PropertyRNA * self )
@@ -184,16 +189,16 @@ static PyObject *pyrna_prop_repr( BPy_PropertyRNA * self )
        /* if a pointer, try to print name of pointer target too */
        if(RNA_property_type(self->prop) == PROP_POINTER) {
                ptr= RNA_property_pointer_get(&self->ptr, self->prop);
-               name= RNA_struct_name_get_alloc(&ptr, NULL, 0);
+               name= RNA_struct_name_get_alloc(&ptr, NULL, FALSE);
 
                if(name) {
-                       pyob= PyUnicode_FromFormat( "[BPy_PropertyRNA \"%s\" -> \"%s\" -> \"%s\" ]", RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop), name);
+                       pyob= PyUnicode_FromFormat( "[BPy_PropertyRNA \"%.200s\" -> \"%.200s\" -> \"%.200s\" ]", RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop), name);
                        MEM_freeN(name);
                        return pyob;
                }
        }
 
-       return PyUnicode_FromFormat( "[BPy_PropertyRNA \"%s\" -> \"%s\"]", RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop));
+       return PyUnicode_FromFormat( "[BPy_PropertyRNA \"%.200s\" -> \"%.200s\"]", RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop));
 }
 
 static long pyrna_struct_hash( BPy_StructRNA * self )
@@ -217,10 +222,22 @@ static void pyrna_struct_dealloc( BPy_StructRNA * self )
 
 static char *pyrna_enum_as_string(PointerRNA *ptr, PropertyRNA *prop)
 {
-       const EnumPropertyItem *item;
+       EnumPropertyItem *item;
+       char *result;
+       int free= FALSE;
+       
+       RNA_property_enum_items(BPy_GetContext(), ptr, prop, &item, NULL, &free);
+       if(item) {
+               result= (char*)BPy_enum_as_string(item);
+       }
+       else {
+               result= "";
+       }
+       
+       if(free)
+               MEM_freeN(item);
        
-       RNA_property_enum_items(ptr, prop, &item, NULL);
-       return (char*)BPy_enum_as_string((EnumPropertyItem*)item);
+       return result;
 }
 
 PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop)
@@ -240,31 +257,31 @@ PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop)
                        switch(RNA_property_subtype(prop)) {
                        case PROP_VECTOR:
                                if(len>=2 && len <= 4) {
-                                       PyObject *vec_cb= newVectorObject_cb(ret, len, mathutils_rna_array_cb_index, 0);
+                                       PyObject *vec_cb= newVectorObject_cb(ret, len, mathutils_rna_array_cb_index, FALSE);
                                        Py_DECREF(ret); /* the vector owns now */
                                        ret= vec_cb; /* return the vector instead */
                                }
                                break;
                        case PROP_MATRIX:
                                if(len==16) {
-                                       PyObject *mat_cb= newMatrixObject_cb(ret, 4,4, mathutils_rna_matrix_cb_index, 0);
+                                       PyObject *mat_cb= newMatrixObject_cb(ret, 4,4, mathutils_rna_matrix_cb_index, FALSE);
                                        Py_DECREF(ret); /* the matrix owns now */
                                        ret= mat_cb; /* return the matrix instead */
                                }
                                else if (len==9) {
-                                       PyObject *mat_cb= newMatrixObject_cb(ret, 3,3, mathutils_rna_matrix_cb_index, 0);
+                                       PyObject *mat_cb= newMatrixObject_cb(ret, 3,3, mathutils_rna_matrix_cb_index, FALSE);
                                        Py_DECREF(ret); /* the matrix owns now */
                                        ret= mat_cb; /* return the matrix instead */
                                }
                                break;
                        case PROP_ROTATION:
                                if(len==3) { /* euler */
-                                       PyObject *eul_cb= newEulerObject_cb(ret, mathutils_rna_array_cb_index, 0);
+                                       PyObject *eul_cb= newEulerObject_cb(ret, mathutils_rna_array_cb_index, FALSE);
                                        Py_DECREF(ret); /* the matrix owns now */
                                        ret= eul_cb; /* return the matrix instead */
                                }
                                else if (len==4) {
-                                       PyObject *quat_cb= newQuaternionObject_cb(ret, mathutils_rna_array_cb_index, 0);
+                                       PyObject *quat_cb= newQuaternionObject_cb(ret, mathutils_rna_array_cb_index, FALSE);
                                        Py_DECREF(ret); /* the matrix owns now */
                                        ret= quat_cb; /* return the matrix instead */
                                }
@@ -303,11 +320,32 @@ PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop)
                const char *identifier;
                int val = RNA_property_enum_get(ptr, prop);
                
-               if (RNA_property_enum_identifier(ptr, prop, val, &identifier)) {
+               if (RNA_property_enum_identifier(BPy_GetContext(), ptr, prop, val, &identifier)) {
                        ret = PyUnicode_FromString( identifier );
                } else {
-                       PyErr_Format(PyExc_AttributeError, "RNA Error: Current value \"%d\" matches no enum", val);
-                       ret = NULL;
+                       EnumPropertyItem *item;
+                       int free= FALSE;
+
+                       /* don't throw error here, can't trust blender 100% to give the
+                        * right values, python code should not generate error for that */
+                       RNA_property_enum_items(BPy_GetContext(), ptr, prop, &item, NULL, &free);
+                       if(item && item->identifier) {
+                               ret = PyUnicode_FromString( item->identifier );
+                       }
+                       else {
+                               /* prefer not fail silently incase of api errors, maybe disable it later */
+                               char error_str[128];
+                               sprintf(error_str, "RNA Warning: Current value \"%d\" matches no enum", val);
+                               PyErr_Warn(PyExc_RuntimeWarning, error_str);
+
+                               ret = PyUnicode_FromString( "" );
+                       }
+
+                       if(free)
+                               MEM_freeN(item);
+
+                       /*PyErr_Format(PyExc_AttributeError, "RNA Error: Current value \"%d\" matches no enum", val);
+                       ret = NULL;*/
                }
 
                break;
@@ -328,7 +366,7 @@ PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop)
                ret = pyrna_prop_CreatePyObject(ptr, prop);
                break;
        default:
-               PyErr_Format(PyExc_AttributeError, "RNA Error: unknown type \"%d\" (pyrna_prop_to_py)", type);
+               PyErr_Format(PyExc_TypeError, "RNA Error: unknown type \"%d\" (pyrna_prop_to_py)", type);
                ret = NULL;
                break;
        }
@@ -336,9 +374,9 @@ PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop)
        return ret;
 }
 
-/* This function is only used by operators right now
- * Its used for taking keyword args and filling in property values */
-int pyrna_pydict_to_props(PointerRNA *ptr, PyObject *kw, const char *error_prefix)
+/* This function is used by operators and converting dicts into collections.
+ * Its takes keyword args and fills them with property values */
+int pyrna_pydict_to_props(PointerRNA *ptr, PyObject *kw, int all_args, const char *error_prefix)
 {
        int error_val = 0;
        int totkw;
@@ -353,25 +391,26 @@ int pyrna_pydict_to_props(PointerRNA *ptr, PyObject *kw, const char *error_prefi
                if (strcmp(arg_name, "rna_type")==0) continue;
 
                if (kw==NULL) {
-                       PyErr_Format( PyExc_AttributeError, "%s: no keywords, expected \"%s\"", error_prefix, arg_name ? arg_name : "<UNKNOWN>");
+                       PyErr_Format( PyExc_TypeError, "%.200s: no keywords, expected \"%.200s\"", error_prefix, arg_name ? arg_name : "<UNKNOWN>");
                        error_val= -1;
                        break;
                }
 
-               item= PyDict_GetItemString(kw, arg_name);
+               item= PyDict_GetItemString(kw, arg_name); /* wont set an error */
 
                if (item == NULL) {
-                       PyErr_Format( PyExc_AttributeError, "%s: keyword \"%s\" missing", error_prefix, arg_name ? arg_name : "<UNKNOWN>");
-                       error_val = -1; /* pyrna_py_to_prop sets the error */
-                       break;
-               }
-
-               if (pyrna_py_to_prop(ptr, prop, NULL, item)) {
-                       error_val= -1;
-                       break;
+                       if(all_args) {
+                               PyErr_Format( PyExc_TypeError, "%.200s: keyword \"%.200s\" missing", error_prefix, arg_name ? arg_name : "<UNKNOWN>");
+                               error_val = -1; /* pyrna_py_to_prop sets the error */
+                               break;
+                       }
+               } else {
+                       if (pyrna_py_to_prop(ptr, prop, NULL, item, error_prefix)) {
+                               error_val= -1;
+                               break;
+                       }
+                       totkw--;
                }
-
-               totkw--;
        }
        RNA_STRUCT_END;
 
@@ -385,7 +424,7 @@ int pyrna_pydict_to_props(PointerRNA *ptr, PyObject *kw, const char *error_prefi
                        arg_name= NULL;
                }
 
-               PyErr_Format( PyExc_AttributeError, "%s: keyword \"%s\" unrecognized", error_prefix, arg_name ? arg_name : "<UNKNOWN>");
+               PyErr_Format( PyExc_TypeError, "%.200s: keyword \"%.200s\" unrecognized", error_prefix, arg_name ? arg_name : "<UNKNOWN>");
                error_val = -1;
        }
 
@@ -397,9 +436,16 @@ static PyObject * pyrna_func_call(PyObject * self, PyObject *args, PyObject *kw)
 PyObject *pyrna_func_to_py(BPy_StructRNA *pyrna, FunctionRNA *func)
 {
        static PyMethodDef func_meth = {"<generic rna function>", (PyCFunction)pyrna_func_call, METH_VARARGS|METH_KEYWORDS, "python rna function"};
-       PyObject *self= PyTuple_New(2);
+       PyObject *self;
        PyObject *ret;
+       
+       if(func==NULL) {
+               PyErr_Format( PyExc_RuntimeError, "%.200s: type attempted to get NULL function", RNA_struct_identifier(pyrna->ptr.type));
+               return NULL;
+       }
 
+       self= PyTuple_New(2);
+       
        PyTuple_SET_ITEM(self, 0, (PyObject *)pyrna);
        Py_INCREF(pyrna);
 
@@ -412,7 +458,7 @@ PyObject *pyrna_func_to_py(BPy_StructRNA *pyrna, FunctionRNA *func)
 }
 
 
-int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *value)
+int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *value, const char *error_prefix)
 {
        /* XXX hard limits should be checked here */
        int type = RNA_property_type(prop);
@@ -437,13 +483,13 @@ int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *v
                        py_len= (int)PySequence_Length(value);
                }
                else {
-                       PyErr_Format(PyExc_TypeError, "RNA array assignment expected a sequence instead of %s instance.", Py_TYPE(value)->tp_name);
+                       PyErr_Format(PyExc_TypeError, "%.200s RNA array assignment expected a sequence instead of %.200s instance.", error_prefix, Py_TYPE(value)->tp_name);
                        return -1;
                }
                /* done getting the length */
                
                if (py_len != len) {
-                       PyErr_Format(PyExc_AttributeError, "python sequence length %d did not match the RNA array length %d.", py_len, len);
+                       PyErr_Format(PyExc_TypeError, "%.200s python sequence length %d did not match the RNA array length %d.", error_prefix, py_len, len);
                        return -1;
                }
                
@@ -453,7 +499,7 @@ int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *v
                {
                        int *param_arr;
                        if(data)        param_arr= (int*)data;
-                       else            param_arr= MEM_mallocN(sizeof(char) * len, "pyrna bool array");
+                       else            param_arr= MEM_mallocN(sizeof(int) * len, "pyrna bool array");
 
                        
                        /* collect the variables before assigning, incase one of them is incorrect */
@@ -465,7 +511,7 @@ int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *v
                                if (param_arr[i] < 0) {
                                        if(data==NULL)
                                                MEM_freeN(param_arr);
-                                       PyErr_SetString(PyExc_AttributeError, "one or more of the values in the sequence is not a boolean");
+                                       PyErr_Format(PyExc_AttributeError, "%.200s one or more of the values in the sequence is not a boolean", error_prefix);
                                        return -1;
                                }
                        }
@@ -493,7 +539,7 @@ int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *v
                        if (PyErr_Occurred()) {
                                if(data==NULL)
                                        MEM_freeN(param_arr);
-                               PyErr_SetString(PyExc_AttributeError, "one or more of the values in the sequence could not be used as an int");
+                               PyErr_Format(PyExc_AttributeError, "%.200s one or more of the values in the sequence could not be used as an int", error_prefix);
                                return -1;
                        }
                        if(data==NULL) {
@@ -527,7 +573,7 @@ int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *v
                        if (PyErr_Occurred()) {
                                if(data==NULL)
                                        MEM_freeN(param_arr);
-                               PyErr_SetString(PyExc_AttributeError, "one or more of the values in the sequence could not be used as a float");
+                               PyErr_Format(PyExc_AttributeError, "%.200s one or more of the values in the sequence could not be used as a float", error_prefix);
                                return -1;
                        }
                        if(data==NULL) {
@@ -547,7 +593,7 @@ int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *v
                        int param = PyObject_IsTrue( value );
                        
                        if( param < 0 ) {
-                               PyErr_SetString(PyExc_TypeError, "expected True/False or 0/1");
+                               PyErr_Format(PyExc_TypeError, "%.200s expected True/False or 0/1", error_prefix);
                                return -1;
                        } else {
                                if(data)        *((int*)data)= param;
@@ -559,7 +605,7 @@ int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *v
                {
                        int param = PyLong_AsSsize_t(value);
                        if (PyErr_Occurred()) {
-                               PyErr_SetString(PyExc_TypeError, "expected an int type");
+                               PyErr_Format(PyExc_TypeError, "%.200s expected an int type", error_prefix);
                                return -1;
                        } else {
                                if(data)        *((int*)data)= param;
@@ -571,7 +617,7 @@ int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *v
                {
                        float param = PyFloat_AsDouble(value);
                        if (PyErr_Occurred()) {
-                               PyErr_SetString(PyExc_TypeError, "expected a float type");
+                               PyErr_Format(PyExc_TypeError, "%.200s expected a float type", error_prefix);
                                return -1;
                        } else {
                                if(data)        *((float*)data)= param;
@@ -584,7 +630,7 @@ int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *v
                        char *param = _PyUnicode_AsString(value);
                        
                        if (param==NULL) {
-                               PyErr_SetString(PyExc_TypeError, "expected a string type");
+                               PyErr_Format(PyExc_TypeError, "%.200s expected a string type", error_prefix);
                                return -1;
                        } else {
                                if(data)        *((char**)data)= param;
@@ -598,17 +644,17 @@ int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *v
                        
                        if (param==NULL) {
                                char *enum_str= pyrna_enum_as_string(ptr, prop);
-                               PyErr_Format(PyExc_TypeError, "expected a string enum type in (%s)", enum_str);
+                               PyErr_Format(PyExc_TypeError, "%.200s expected a string enum type in (%.200s)", error_prefix, enum_str);
                                MEM_freeN(enum_str);
                                return -1;
                        } else {
                                int val;
-                               if (RNA_property_enum_value(ptr, prop, param, &val)) {
+                               if (RNA_property_enum_value(BPy_GetContext(), ptr, prop, param, &val)) {
                                        if(data)        *((int*)data)= val;
                                        else            RNA_property_enum_set(ptr, prop, val);
                                } else {
                                        char *enum_str= pyrna_enum_as_string(ptr, prop);
-                                       PyErr_Format(PyExc_AttributeError, "enum \"%s\" not found in (%s)", param, enum_str);
+                                       PyErr_Format(PyExc_TypeError, "%.200s enum \"%.200s\" not found in (%.200s)", error_prefix, param, enum_str);
                                        MEM_freeN(enum_str);
                                        return -1;
                                }
@@ -623,11 +669,11 @@ int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *v
                        if(!BPy_StructRNA_Check(value) && value != Py_None) {
                                PointerRNA tmp;
                                RNA_pointer_create(NULL, ptype, NULL, &tmp);
-                               PyErr_Format(PyExc_TypeError, "expected a %s type", RNA_struct_identifier(tmp.type));
+                               PyErr_Format(PyExc_TypeError, "%.200s expected a %.200s type", error_prefix, RNA_struct_identifier(tmp.type));
                                return -1;
                        } else {
                                BPy_StructRNA *param= (BPy_StructRNA*)value;
-                               int raise_error= 0;
+                               int raise_error= FALSE;
                                if(data) {
                                        int flag = RNA_property_flag(prop);
 
@@ -644,7 +690,7 @@ int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *v
                                                *((void**)data)= param->ptr.data;
                                        }
                                        else {
-                                               raise_error= 1;
+                                               raise_error= TRUE;
                                        }
                                }
                                else {
@@ -660,7 +706,7 @@ int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *v
                                        else {
                                                PointerRNA tmp;
                                                RNA_pointer_create(NULL, ptype, NULL, &tmp);
-                                               PyErr_Format(PyExc_TypeError, "expected a %s type", RNA_struct_identifier(tmp.type));
+                                               PyErr_Format(PyExc_TypeError, "%.200s expected a %.200s type", error_prefix, RNA_struct_identifier(tmp.type));
                                                return -1;
                                        }
                                }
@@ -668,7 +714,7 @@ int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *v
                                if(raise_error) {
                                        PointerRNA tmp;
                                        RNA_pointer_create(NULL, ptype, NULL, &tmp);
-                                       PyErr_Format(PyExc_TypeError, "expected a %s type", RNA_struct_identifier(tmp.type));
+                                       PyErr_Format(PyExc_TypeError, "%.200s expected a %.200s type", error_prefix, RNA_struct_identifier(tmp.type));
                                        return -1;
                                }
                        }
@@ -686,7 +732,7 @@ int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *v
                        
                        /* convert a sequence of dict's into a collection */
                        if(!PySequence_Check(value)) {
-                               PyErr_SetString(PyExc_TypeError, "expected a sequence of dicts for an RNA collection");
+                               PyErr_Format(PyExc_TypeError, "%.200s expected a sequence of dicts for an RNA collection", error_prefix);
                                return -1;
                        }
                        
@@ -694,7 +740,7 @@ int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *v
                        for(i=0; i<seq_len; i++) {
                                item= PySequence_GetItem(value, i);
                                if(item==NULL || PyDict_Check(item)==0) {
-                                       PyErr_SetString(PyExc_TypeError, "expected a sequence of dicts for an RNA collection");
+                                       PyErr_Format(PyExc_TypeError, "%.200s expected a sequence of dicts for an RNA collection", error_prefix);
                                        Py_XDECREF(item);
                                        return -1;
                                }
@@ -707,7 +753,7 @@ int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *v
                                else
                                        RNA_property_collection_add(ptr, prop, &itemptr);
 
-                               if(pyrna_pydict_to_props(&itemptr, item, "Converting a python list to an RNA collection")==-1) {
+                               if(pyrna_pydict_to_props(&itemptr, item, 1, "Converting a python list to an RNA collection")==-1) {
                                        Py_DECREF(item);
                                        return -1;
                                }
@@ -717,7 +763,7 @@ int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *v
                        break;
                }
                default:
-                       PyErr_SetString(PyExc_AttributeError, "unknown property type (pyrna_py_to_prop)");
+                       PyErr_Format(PyExc_AttributeError, "%.200s unknown property type (pyrna_py_to_prop)", error_prefix);
                        return -1;
                        break;
                }
@@ -820,108 +866,244 @@ static Py_ssize_t pyrna_prop_len( BPy_PropertyRNA * self )
        return len;
 }
 
-static PyObject *pyrna_prop_subscript( BPy_PropertyRNA * self, PyObject *key )
+/* internal use only */
+static PyObject *prop_subscript_collection_int(BPy_PropertyRNA * self, int keynum)
 {
-       PyObject *ret;
        PointerRNA newptr;
-       int keynum = 0;
-       char *keyname = NULL;
-       
+
+       if(keynum < 0) keynum += RNA_property_collection_length(&self->ptr, self->prop);
+
+       if(RNA_property_collection_lookup_int(&self->ptr, self->prop, keynum, &newptr))
+               return pyrna_struct_CreatePyObject(&newptr);
+
+       PyErr_Format(PyExc_IndexError, "index %d out of range", keynum);
+       return NULL;
+}
+static PyObject *prop_subscript_array_int(BPy_PropertyRNA * self, int keynum)
+{
+       int len= RNA_property_array_length(self->prop);
+
+       if(keynum < 0) keynum += len;
+
+       if(keynum >= 0 && keynum < len)
+               return pyrna_prop_to_py_index(&self->ptr, self->prop, keynum);
+
+       PyErr_Format(PyExc_IndexError, "index %d out of range", keynum);
+       return NULL;
+}
+
+static PyObject *prop_subscript_collection_str(BPy_PropertyRNA * self, char *keyname)
+{
+       PointerRNA newptr;
+       if(RNA_property_collection_lookup_string(&self->ptr, self->prop, keyname, &newptr))
+               return pyrna_struct_CreatePyObject(&newptr);
+
+       PyErr_Format(PyExc_KeyError, "key \"%.200s\" not found", keyname);
+       return NULL;
+}
+/* static PyObject *prop_subscript_array_str(BPy_PropertyRNA * self, char *keyname) */
+
+static PyObject *prop_subscript_collection_slice(BPy_PropertyRNA * self, int start, int stop)
+{
+       PointerRNA newptr;
+       PyObject *list = PyList_New(stop - start);
+       int count;
+
+       start = MIN2(start,stop); /* values are clamped from  */
+
+       for(count = start; count < stop; count++) {
+               if(RNA_property_collection_lookup_int(&self->ptr, self->prop, count - start, &newptr)) {
+                       PyList_SetItem(list, count - start, pyrna_struct_CreatePyObject(&newptr));
+               }
+               else {
+                       Py_DECREF(list);
+
+                       PyErr_SetString(PyExc_RuntimeError, "error getting an rna struct from a collection");
+                       return NULL;
+               }
+       }
+
+       return list;
+}
+static PyObject *prop_subscript_array_slice(BPy_PropertyRNA * self, int start, int stop)
+{
+       PyObject *list = PyList_New(stop - start);
+       int count;
+
+       start = MIN2(start,stop); /* values are clamped from PySlice_GetIndicesEx */
+
+       for(count = start; count < stop; count++)
+               PyList_SetItem(list, count - start, pyrna_prop_to_py_index(&self->ptr, self->prop, count));
+
+       return list;
+}
+
+static PyObject *prop_subscript_collection(BPy_PropertyRNA * self, PyObject *key)
+{
        if (PyUnicode_Check(key)) {
-               keyname = _PyUnicode_AsString(key);
-       } else if (PyLong_Check(key)) {
-               keynum = PyLong_AsSsize_t(key);
-       } else {
-               PyErr_SetString(PyExc_AttributeError, "invalid key, key must be a string or an int");
+               return prop_subscript_collection_str(self, _PyUnicode_AsString(key));
+       }
+       else if (PyIndex_Check(key)) {
+               Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
+               if (i == -1 && PyErr_Occurred())
+                       return NULL;
+
+               return prop_subscript_collection_int(self, i);
+       }
+       else if (PySlice_Check(key)) {
+               int len= RNA_property_collection_length(&self->ptr, self->prop);
+               Py_ssize_t start, stop, step, slicelength;
+
+               if (PySlice_GetIndicesEx((PySliceObject*)key, len, &start, &stop, &step, &slicelength) < 0)
+                       return NULL;
+
+               if (slicelength <= 0) {
+                       return PyList_New(0);
+               }
+               else if (step == 1) {
+                       return prop_subscript_collection_slice(self, start, stop);
+               }
+               else {
+                       PyErr_SetString(PyExc_TypeError, "slice steps not supported with rna");
+                       return NULL;
+               }
+       }
+       else {
+               PyErr_Format(PyExc_TypeError, "invalid rna key, key must be a string or an int instead of %.200s instance.", Py_TYPE(key)->tp_name);
                return NULL;
        }
-       
-       if (RNA_property_type(self->prop) == PROP_COLLECTION) {
-               int ok;
-               if (keyname)    ok = RNA_property_collection_lookup_string(&self->ptr, self->prop, keyname, &newptr);
-               else                    ok = RNA_property_collection_lookup_int(&self->ptr, self->prop, keynum, &newptr);
-               
-               if (ok) {
-                       ret = pyrna_struct_CreatePyObject(&newptr);
-               } else {
-                       PyErr_SetString(PyExc_AttributeError, "out of range");
-                       ret = NULL;
+}
+
+static PyObject *prop_subscript_array(BPy_PropertyRNA * self, PyObject *key)
+{
+       /*if (PyUnicode_Check(key)) {
+               return prop_subscript_array_str(self, _PyUnicode_AsString(key));
+       } else*/
+       if (PyIndex_Check(key)) {
+               Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
+               if (i == -1 && PyErr_Occurred())
+                       return NULL;
+               return prop_subscript_array_int(self, PyLong_AsSsize_t(key));
+       }
+       else if (PySlice_Check(key)) {
+               int len= RNA_property_array_length(self->prop);
+               Py_ssize_t start, stop, step, slicelength;
+
+               if (PySlice_GetIndicesEx((PySliceObject*)key, len, &start, &stop, &step, &slicelength) < 0)
+                       return NULL;
+
+               if (slicelength <= 0) {
+                       return PyList_New(0);
                }
-               
-       } else if (keyname) {
-               PyErr_SetString(PyExc_AttributeError, "string keys are only supported for collections");
-               ret = NULL;
-       } else {
-               int len = RNA_property_array_length(self->prop);
-               
-               if (len==0) { /* not an array*/
-                       PyErr_Format(PyExc_AttributeError, "not an array or collection %d", keynum);
-                       ret = NULL;
+               else if (step == 1) {
+                       return prop_subscript_array_slice(self, start, stop);
                }
-               
-               if (keynum >= len){
-                       PyErr_SetString(PyExc_AttributeError, "index out of range");
-                       ret = NULL;
-               } else { /* not an array*/
-                       ret = pyrna_prop_to_py_index(&self->ptr, self->prop, keynum);
+               else {
+                       PyErr_SetString(PyExc_TypeError, "slice steps not supported with rna");
+                       return NULL;
                }
        }
-       
-       return ret;
+       else {
+               PyErr_SetString(PyExc_AttributeError, "invalid key, key must be an int");
+               return NULL;
+       }
+}
+
+static PyObject *pyrna_prop_subscript( BPy_PropertyRNA * self, PyObject *key )
+{
+       if (RNA_property_type(self->prop) == PROP_COLLECTION) {
+               return prop_subscript_collection(self, key);
+       } else if (RNA_property_array_length(self->prop)) { /* arrays are currently fixed length, zero length means its not an array */
+               return prop_subscript_array(self, key);
+       } else {
+               PyErr_SetString(PyExc_TypeError, "rna type is not an array or a collection");
+               return NULL;
+       }
+
+}
+
+static int prop_subscript_ass_array_slice(BPy_PropertyRNA * self, int begin, int end, PyObject *value)
+{
+       int count;
+
+       /* values are clamped from */
+       begin = MIN2(begin,end);
+
+       for(count = begin; count < end; count++) {
+               if(pyrna_py_to_prop_index(&self->ptr, self->prop, count - begin, value) == -1) {
+                       /* TODO - this is wrong since some values have been assigned... will need to fix that */
+                       return -1; /* pyrna_struct_CreatePyObject should set the error */
+               }
+       }
+
+       return 0;
 }
 
+static int prop_subscript_ass_array_int(BPy_PropertyRNA * self, int keynum, PyObject *value)
+{
+
+       int len= RNA_property_array_length(self->prop);
+
+       if(keynum < 0) keynum += len;
 
-static int pyrna_prop_assign_subscript( BPy_PropertyRNA * self, PyObject *key, PyObject *value )
+       if(keynum >= 0 && keynum < len)
+               return pyrna_py_to_prop_index(&self->ptr, self->prop, keynum, value);
+
+       PyErr_SetString(PyExc_IndexError, "out of range");
+       return -1;
+}
+
+static int pyrna_prop_ass_subscript( BPy_PropertyRNA * self, PyObject *key, PyObject *value )
 {
-       int ret = 0;
-       int keynum = 0;
-       char *keyname = NULL;
+       /* char *keyname = NULL; */ /* not supported yet */
        
        if (!RNA_property_editable(&self->ptr, self->prop)) {
-               PyErr_Format( PyExc_AttributeError, "PropertyRNA - attribute \"%s\" from \"%s\" is read-only", RNA_property_identifier(self->prop), RNA_struct_identifier(self->ptr.type) );
+               PyErr_Format( PyExc_AttributeError, "PropertyRNA - attribute \"%.200s\" from \"%.200s\" is read-only", RNA_property_identifier(self->prop), RNA_struct_identifier(self->ptr.type) );
                return -1;
        }
        
-       if (PyUnicode_Check(key)) {
-               keyname = _PyUnicode_AsString(key);
-       } else if (PyLong_Check(key)) {
-               keynum = PyLong_AsSsize_t(key);
-       } else {
-               PyErr_SetString(PyExc_AttributeError, "PropertyRNA - invalid key, key must be a string or an int");
+       /* maybe one day we can support this... */
+       if (RNA_property_type(self->prop) == PROP_COLLECTION) {
+               PyErr_Format( PyExc_AttributeError, "PropertyRNA - attribute \"%.200s\" from \"%.200s\" is a collection, assignment not supported", RNA_property_identifier(self->prop), RNA_struct_identifier(self->ptr.type) );
                return -1;
        }
-       
-       if (RNA_property_type(self->prop) == PROP_COLLECTION) {
-               PyErr_SetString(PyExc_AttributeError, "PropertyRNA - assignment is not supported for collections (yet)");
-               ret = -1;
-       } else if (keyname) {
-               PyErr_SetString(PyExc_AttributeError, "PropertyRNA - string keys are only supported for collections");
-               ret = -1;
-       } else {
-               int len = RNA_property_array_length(self->prop);
-               
-               if (len==0) { /* not an array*/
-                       PyErr_Format(PyExc_AttributeError, "PropertyRNA - not an array or collection %d", keynum);
-                       ret = -1;
+
+       if (PyIndex_Check(key)) {
+               Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
+               if (i == -1 && PyErr_Occurred())
+                       return -1;
+
+               return prop_subscript_ass_array_int(self, i, value);
+       }
+       else if (PySlice_Check(key)) {
+               int len= RNA_property_array_length(self->prop);
+               Py_ssize_t start, stop, step, slicelength;
+
+               if (PySlice_GetIndicesEx((PySliceObject*)key, len, &start, &stop, &step, &slicelength) < 0)
+                       return -1;
+
+               if (slicelength <= 0) {
+                       return 0;
                }
-               
-               if (keynum >= len){
-                       PyErr_SetString(PyExc_AttributeError, "PropertyRNA - index out of range");
-                       ret = -1;
-               } else {
-                       ret = pyrna_py_to_prop_index(&self->ptr, self->prop, keynum, value);
+               else if (step == 1) {
+                       return prop_subscript_ass_array_slice(self, start, stop, value);
+               }
+               else {
+                       PyErr_SetString(PyExc_TypeError, "slice steps not supported with rna");
+                       return -1;
                }
        }
-       
-       return ret;
+       else {
+               PyErr_SetString(PyExc_AttributeError, "invalid key, key must be an int");
+               return -1;
+       }
 }
 
 
-
 static PyMappingMethods pyrna_prop_as_mapping = {
        ( lenfunc ) pyrna_prop_len,     /* mp_length */
        ( binaryfunc ) pyrna_prop_subscript,    /* mp_subscript */
-       ( objobjargproc ) pyrna_prop_assign_subscript,  /* mp_ass_subscript */
+       ( objobjargproc ) pyrna_prop_ass_subscript,     /* mp_ass_subscript */
 };
 
 static int pyrna_prop_contains(BPy_PropertyRNA * self, PyObject *value)
@@ -957,6 +1139,25 @@ static PySequenceMethods pyrna_prop_as_sequence = {
        (objobjproc)pyrna_prop_contains,        /* sq_contains */
 };
 
+
+static PyObject *pyrna_struct_keyframe_insert(BPy_StructRNA * self, PyObject *args)
+{
+       char *path;
+       int index= 0;
+       float cfra = CTX_data_scene(BPy_GetContext())->r.cfra;
+
+       if(!RNA_struct_is_ID(self->ptr.type)) {
+               PyErr_SetString( PyExc_TypeError, "StructRNA - keyframe_insert only for ID type");
+               return NULL;
+       }
+
+       if (!PyArg_ParseTuple(args, "s|if:keyframe_insert", &path, &index, &cfra))
+               return NULL;
+
+       return PyBool_FromLong( insert_keyframe((ID *)self->ptr.data, NULL, NULL, path, index, cfra, 0));
+}
+
+
 static PyObject *pyrna_struct_dir(BPy_StructRNA * self)
 {
        PyObject *ret, *dict;
@@ -1098,7 +1299,7 @@ static PyObject *pyrna_struct_getattro( BPy_StructRNA * self, PyObject *pyname )
                BLI_freelistN(&newlb);
        }
        else {
-               PyErr_Format( PyExc_AttributeError, "StructRNA - Attribute \"%s\" not found", name);
+               PyErr_Format( PyExc_AttributeError, "StructRNA - Attribute \"%.200s\" not found", name);
                ret = NULL;
        }
        
@@ -1116,18 +1317,18 @@ static int pyrna_struct_setattro( BPy_StructRNA * self, PyObject *pyname, PyObje
                        return 0;
                }
                else {
-                       PyErr_Format( PyExc_AttributeError, "StructRNA - Attribute \"%s\" not found", name);
+                       PyErr_Format( PyExc_AttributeError, "StructRNA - Attribute \"%.200s\" not found", name);
                        return -1;
                }
        }               
        
        if (!RNA_property_editable(&self->ptr, prop)) {
-               PyErr_Format( PyExc_AttributeError, "StructRNA - Attribute \"%s\" from \"%s\" is read-only", RNA_property_identifier(prop), RNA_struct_identifier(self->ptr.type) );
+               PyErr_Format( PyExc_AttributeError, "StructRNA - Attribute \"%.200s\" from \"%.200s\" is read-only", RNA_property_identifier(prop), RNA_struct_identifier(self->ptr.type) );
                return -1;
        }
                
        /* pyrna_py_to_prop sets its own exceptions */
-       return pyrna_py_to_prop(&self->ptr, prop, NULL, value);
+       return pyrna_py_to_prop(&self->ptr, prop, NULL, value, "StructRNA - Attribute (setattr):");
 }
 
 static PyObject *pyrna_prop_keys(BPy_PropertyRNA *self)
@@ -1225,7 +1426,25 @@ static PyObject *pyrna_prop_values(BPy_PropertyRNA *self)
        return ret;
 }
 
-#if (PY_VERSION_HEX >= 0x03000000) /* foreach needs py3 */
+static PyObject *pyrna_prop_get(BPy_PropertyRNA *self, PyObject *args)
+{
+       PointerRNA newptr;
+       
+       char *key;
+       PyObject* def = Py_None;
+
+       if (!PyArg_ParseTuple(args, "s|O:get", &key, &def))
+               return NULL;
+       
+       if(RNA_property_collection_lookup_string(&self->ptr, self->prop, key, &newptr))
+               return pyrna_struct_CreatePyObject(&newptr);
+       
+       Py_INCREF(def);
+       return def;
+}
+
+
+
 static void foreach_attr_type( BPy_PropertyRNA *self, char *attr,
                                                                        /* values to assign */
                                                                        RawPropertyType *raw_type, int *attr_tot, int *attr_signed )
@@ -1233,13 +1452,13 @@ static void foreach_attr_type(  BPy_PropertyRNA *self, char *attr,
        PropertyRNA *prop;
        *raw_type= -1;
        *attr_tot= 0;
-       *attr_signed= 0;
+       *attr_signed= FALSE;
 
        RNA_PROP_BEGIN(&self->ptr, itemptr, self->prop) {
                prop = RNA_struct_find_property(&itemptr, attr);
                *raw_type= RNA_property_raw_type(prop);
                *attr_tot = RNA_property_array_length(prop);
-               *attr_signed= (RNA_property_subtype(prop)==PROP_UNSIGNED) ? 0:1;
+               *attr_signed= (RNA_property_subtype(prop)==PROP_UNSIGNED) ? FALSE:TRUE;
                break;
        }
        RNA_PROP_END;
@@ -1252,10 +1471,12 @@ static int foreach_parse_args(
                /*values to assign */
                char **attr, PyObject **seq, int *tot, int *size, RawPropertyType *raw_type, int *attr_tot, int *attr_signed)
 {
+#if 0
        int array_tot;
        int target_tot;
+#endif
 
-       *size= *raw_type= *attr_tot= *attr_signed= 0;
+       *size= *raw_type= *attr_tot= *attr_signed= FALSE;
 
        if(!PyArg_ParseTuple(args, "sO", attr, seq) || (!PySequence_Check(*seq) && PyObject_CheckBuffer(*seq))) {
                PyErr_SetString( PyExc_TypeError, "foreach_get(attr, sequence) expects a string and a sequence" );
@@ -1335,12 +1556,12 @@ static PyObject *foreach_getset(BPy_PropertyRNA *self, PyObject *args, int set)
 
 
        if(set) { /* get the array from python */
-               buffer_is_compat = 0;
+               buffer_is_compat = FALSE;
                if(PyObject_CheckBuffer(seq)) {
                        Py_buffer buf;
                        PyObject_GetBuffer(seq, &buf, PyBUF_SIMPLE | PyBUF_FORMAT);
 
-                       /* check if the buffer matches, TODO - signed/unsigned types */
+                       /* check if the buffer matches */
 
                        buffer_is_compat = foreach_compat_buffer(raw_type, attr_signed, buf.format);
 
@@ -1382,7 +1603,7 @@ static PyObject *foreach_getset(BPy_PropertyRNA *self, PyObject *args, int set)
                }
        }
        else {
-               buffer_is_compat = 0;
+               buffer_is_compat = FALSE;
                if(PyObject_CheckBuffer(seq)) {
                        Py_buffer buf;
                        PyObject_GetBuffer(seq, &buf, PyBUF_SIMPLE | PyBUF_FORMAT);
@@ -1458,7 +1679,6 @@ static  PyObject *pyrna_prop_foreach_set(BPy_PropertyRNA *self, PyObject *args)
 {
        return foreach_getset(self, args, 1);
 }
-#endif /* #if (PY_VERSION_HEX >= 0x03000000) */
 
 /* A bit of a kludge, make a list out of a collection or array,
  * then return the lists iter function, not especially fast but convenient for now */
@@ -1494,6 +1714,10 @@ PyObject *pyrna_prop_iter(BPy_PropertyRNA *self)
 }
 
 static struct PyMethodDef pyrna_struct_methods[] = {
+
+       /* maybe this become and ID function */
+       {"keyframe_insert", (PyCFunction)pyrna_struct_keyframe_insert, METH_VARARGS, NULL},
+
        {"__dir__", (PyCFunction)pyrna_struct_dir, METH_NOARGS, NULL},
        {NULL, NULL, 0, NULL}
 };
@@ -1502,12 +1726,13 @@ static struct PyMethodDef pyrna_prop_methods[] = {
        {"keys", (PyCFunction)pyrna_prop_keys, METH_NOARGS, NULL},
        {"items", (PyCFunction)pyrna_prop_items, METH_NOARGS,NULL},
        {"values", (PyCFunction)pyrna_prop_values, METH_NOARGS, NULL},
+       
+       {"get", (PyCFunction)pyrna_prop_get, METH_VARARGS, NULL},
 
-#if (PY_VERSION_HEX >= 0x03000000)
        /* array accessor function */
        {"foreach_get", (PyCFunction)pyrna_prop_foreach_get, METH_VARARGS, NULL},
        {"foreach_set", (PyCFunction)pyrna_prop_foreach_set, METH_VARARGS, NULL},
-#endif
+
        {NULL, NULL, 0, NULL}
 };
 
@@ -1574,7 +1799,7 @@ PyObject *pyrna_param_to_py(PointerRNA *ptr, PropertyRNA *prop, void *data)
                                PyTuple_SET_ITEM(ret, a, PyFloat_FromDouble( ((float*)data)[a] ));
                        break;
                default:
-                       PyErr_Format(PyExc_AttributeError, "RNA Error: unknown array type \"%d\" (pyrna_param_to_py)", type);
+                       PyErr_Format(PyExc_TypeError, "RNA Error: unknown array type \"%d\" (pyrna_param_to_py)", type);
                        ret = NULL;
                        break;
                }
@@ -1601,11 +1826,17 @@ PyObject *pyrna_param_to_py(PointerRNA *ptr, PropertyRNA *prop, void *data)
                        const char *identifier;
                        int val = *(int*)data;
                        
-                       if (RNA_property_enum_identifier(ptr, prop, val, &identifier)) {
+                       if (RNA_property_enum_identifier(BPy_GetContext(), ptr, prop, val, &identifier)) {
                                ret = PyUnicode_FromString( identifier );
                        } else {
-                               PyErr_Format(PyExc_AttributeError, "RNA Error: Current value \"%d\" matches no enum", val);
-                               ret = NULL;
+                               /* prefer not fail silently incase of api errors, maybe disable it later */
+                               char error_str[128];
+                               sprintf(error_str, "RNA Warning: Current value \"%d\" matches no enum", val);
+                               PyErr_Warn(PyExc_RuntimeWarning, error_str);
+                               
+                               ret = PyUnicode_FromString( "" );
+                               /*PyErr_Format(PyExc_AttributeError, "RNA Error: Current value \"%d\" matches no enum", val);
+                               ret = NULL;*/
                        }
 
                        break;
@@ -1621,8 +1852,12 @@ PyObject *pyrna_param_to_py(PointerRNA *ptr, PropertyRNA *prop, void *data)
                                newptr= *(PointerRNA*)data;
                        }
                        else {
-                               /* XXX this is missing the ID part! */
-                               RNA_pointer_create(NULL, type, *(void**)data, &newptr);
+                               if(RNA_struct_is_ID(type)) {
+                                       RNA_id_pointer_create(*(void**)data, &newptr);
+                               } else {
+                                       /* XXX this is missing the ID part! */
+                                       RNA_pointer_create(NULL, type, *(void**)data, &newptr);
+                               }
                        }
 
                        if (newptr.data) {
@@ -1650,7 +1885,7 @@ PyObject *pyrna_param_to_py(PointerRNA *ptr, PropertyRNA *prop, void *data)
                        break;
                }
                default:
-                       PyErr_Format(PyExc_AttributeError, "RNA Error: unknown type \"%d\" (pyrna_param_to_py)", type);
+                       PyErr_Format(PyExc_TypeError, "RNA Error: unknown type \"%d\" (pyrna_param_to_py)", type);
                        ret = NULL;
                        break;
                }
@@ -1665,25 +1900,42 @@ static PyObject * pyrna_func_call(PyObject * self, PyObject *args, PyObject *kw)
        FunctionRNA *self_func=  PyCObject_AsVoidPtr(PyTuple_GET_ITEM(self, 1));
 
        PointerRNA funcptr;
-       ParameterList *parms;
+       ParameterList parms;
        ParameterIterator iter;
        PropertyRNA *pret, *parm;
        PyObject *ret, *item;
-       int i, tlen, flag, err= 0;
-       const char *tid, *fid, *pid;
+       int i, args_len, parms_len, flag, err= 0, kw_tot= 0, kw_arg;
+       const char *parm_id;
        void *retdata= NULL;
 
+       /* Should never happen but it does in rare cases */
+       if(self_ptr==NULL) {
+               PyErr_SetString(PyExc_RuntimeError, "rna functions internal rna pointer is NULL, this is a bug. aborting");
+               return NULL;
+       }
+       
+       if(self_func==NULL) {
+               PyErr_Format(PyExc_RuntimeError, "%.200s.<unknown>(): rna function internal function is NULL, this is a bug. aborting", RNA_struct_identifier(self_ptr->type));
+               return NULL;
+       }
+       
        /* setup */
        RNA_pointer_create(NULL, &RNA_Function, self_func, &funcptr);
 
        pret= RNA_function_return(self_func);
-       tlen= PyTuple_GET_SIZE(args);
+       args_len= PyTuple_GET_SIZE(args);
+
+       RNA_parameter_list_create(&parms, self_ptr, self_func);
+       RNA_parameter_list_begin(&parms, &iter);
+       parms_len = RNA_parameter_list_size(&parms);
 
-       parms= RNA_parameter_list_create(self_ptr, self_func);
-       RNA_parameter_list_begin(parms, &iter);
+       if(args_len + (kw ? PyDict_Size(kw):0) > parms_len) {
+               PyErr_Format(PyExc_TypeError, "%.200s.%.200s(): takes at most %d arguments, got %d", RNA_struct_identifier(self_ptr->type), RNA_function_identifier(self_func), parms_len, args_len);
+               err= -1;
+       }
 
        /* parse function parameters */
-       for (i= 0; iter.valid; RNA_parameter_list_next(&iter)) {
+       for (i= 0; iter.valid && err==0; RNA_parameter_list_next(&iter)) {
                parm= iter.parm;
 
                if (parm==pret) {
@@ -1691,34 +1943,118 @@ static PyObject * pyrna_func_call(PyObject * self, PyObject *args, PyObject *kw)
                        continue;
                }
 
-               pid= RNA_property_identifier(parm);
+               parm_id= RNA_property_identifier(parm);
                flag= RNA_property_flag(parm);
                item= NULL;
 
-               if ((i < tlen) && (flag & PROP_REQUIRED)) {
+               if ((i < args_len) && (flag & PROP_REQUIRED)) {
                        item= PyTuple_GET_ITEM(args, i);
                        i++;
+
+                       kw_arg= FALSE;
+               }
+               else if (kw != NULL) {
+                       item= PyDict_GetItemString(kw, parm_id);  /* borrow ref */
+                       if(item)
+                               kw_tot++; /* make sure invalid keywords are not given */
+
+                       kw_arg= TRUE;
                }
-               else if (kw != NULL)
-                       item= PyDict_GetItemString(kw, pid);  /* borrow ref */
 
                if (item==NULL) {
                        if(flag & PROP_REQUIRED) {
-                               tid= RNA_struct_identifier(self_ptr->type);
-                               fid= RNA_function_identifier(self_func);
-
-                               PyErr_Format(PyExc_AttributeError, "%s.%s(): required parameter \"%s\" not specified", tid, fid, pid);
+                               PyErr_Format(PyExc_TypeError, "%.200s.%.200s(): required parameter \"%.200s\" not specified", RNA_struct_identifier(self_ptr->type), RNA_function_identifier(self_func), parm_id);
                                err= -1;
                                break;
                        }
-                       else
+                       else /* PyDict_GetItemString wont raise an error */
                                continue;
                }
 
-               err= pyrna_py_to_prop(&funcptr, parm, iter.data, item);
+               err= pyrna_py_to_prop(&funcptr, parm, iter.data, item, "");
+
+               if(err!=0) {
+                       /* the error generated isnt that useful, so generate it again with a useful prefix
+                        * could also write a function to prepend to error messages */
+                       char error_prefix[512];
+                       PyErr_Clear(); /* re-raise */
+
+                       if(kw_arg==TRUE)
+                               snprintf(error_prefix, sizeof(error_prefix), "%s.%s(): error with keyword argument \"%s\" - ", RNA_struct_identifier(self_ptr->type), RNA_function_identifier(self_func), parm_id);
+                       else
+                               snprintf(error_prefix, sizeof(error_prefix), "%s.%s(): error with argument %d, \"%s\" - ", RNA_struct_identifier(self_ptr->type), RNA_function_identifier(self_func), i, parm_id);
+
+                       pyrna_py_to_prop(&funcptr, parm, iter.data, item, error_prefix);
 
-               if(err!=0)
                        break;
+               }
+       }
+
+
+       /* Check if we gave args that dont exist in the function
+        * printing the error is slow but it should only happen when developing.
+        * the if below is quick, checking if it passed less keyword args then we gave */
+       if(kw && (PyDict_Size(kw) > kw_tot)) {
+               PyObject *key, *value;
+               Py_ssize_t pos = 0;
+
+               DynStr *bad_args= BLI_dynstr_new();
+               DynStr *good_args= BLI_dynstr_new();
+
+               char *arg_name, *bad_args_str, *good_args_str;
+               int found= FALSE, first= TRUE;
+
+               while (PyDict_Next(kw, &pos, &key, &value)) {
+
+                       arg_name= _PyUnicode_AsString(key);
+                       found= FALSE;
+
+                       if(arg_name==NULL) { /* unlikely the argname is not a string but ignore if it is*/
+                               PyErr_Clear();
+                       }
+                       else {
+                               /* Search for arg_name */
+                               RNA_parameter_list_begin(&parms, &iter);
+                               for(; iter.valid; RNA_parameter_list_next(&iter)) {
+                                       parm= iter.parm;
+                                       if (strcmp(arg_name, RNA_property_identifier(parm))==0) {
+                                               found= TRUE;
+                                               break;
+                                       }
+                               }
+
+                               RNA_parameter_list_end(&iter);
+
+                               if(found==FALSE) {
+                                       BLI_dynstr_appendf(bad_args, first ? "%s" : ", %s", arg_name);
+                                       first= FALSE;
+                               }
+                       }
+               }
+
+               /* list good args */
+               first= TRUE;
+
+               RNA_parameter_list_begin(&parms, &iter);
+               for(; iter.valid; RNA_parameter_list_next(&iter)) {
+                       parm= iter.parm;
+                       BLI_dynstr_appendf(good_args, first ? "%s" : ", %s", RNA_property_identifier(parm));
+                       first= FALSE;
+               }
+               RNA_parameter_list_end(&iter);
+
+
+               bad_args_str= BLI_dynstr_get_cstring(bad_args);
+               good_args_str= BLI_dynstr_get_cstring(good_args);
+
+               PyErr_Format(PyExc_TypeError, "%.200s.%.200s(): was called with invalid keyword arguments(s) (%s), expected (%s)", RNA_struct_identifier(self_ptr->type), RNA_function_identifier(self_func), bad_args_str, good_args_str);
+
+               BLI_dynstr_free(bad_args);
+               BLI_dynstr_free(good_args);
+               MEM_freeN(bad_args_str);
+               MEM_freeN(good_args_str);
+
+               err= -1;
        }
 
        ret= NULL;
@@ -1728,20 +2064,26 @@ static PyObject * pyrna_func_call(PyObject * self, PyObject *args, PyObject *kw)
                bContext *C= BPy_GetContext();
 
                BKE_reports_init(&reports, RPT_STORE);
-               RNA_function_call(C, &reports, self_ptr, self_func, parms);
+               RNA_function_call(C, &reports, self_ptr, self_func, &parms);
 
                err= (BPy_reports_to_error(&reports))? -1: 0;
                BKE_reports_clear(&reports);
 
                /* return value */
-               if(err==0)
-                       if(pret)
+               if(err==0) {
+                       if(pret) {
                                ret= pyrna_param_to_py(&funcptr, pret, retdata);
+
+                               /* possible there is an error in conversion */
+                               if(ret==NULL)
+                                       err= -1;
+                       }
+               }
        }
 
        /* cleanup */
        RNA_parameter_list_end(&iter);
-       RNA_parameter_list_free(parms);
+       RNA_parameter_list_free(&parms);
 
        if (ret)
                return ret;
@@ -1754,13 +2096,7 @@ static PyObject * pyrna_func_call(PyObject * self, PyObject *args, PyObject *kw)
 
 /*-----------------------BPy_StructRNA method def------------------------------*/
 PyTypeObject pyrna_struct_Type = {
-#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
        "StructRNA",                    /* tp_name */
        sizeof( BPy_StructRNA ),        /* tp_basicsize */
        0,                      /* tp_itemsize */
@@ -1839,14 +2175,7 @@ PyTypeObject pyrna_struct_Type = {
 
 /*-----------------------BPy_PropertyRNA method def------------------------------*/
 PyTypeObject pyrna_prop_Type = {
-#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
-       
        "PropertyRNA",          /* tp_name */
        sizeof( BPy_PropertyRNA ),                      /* tp_basicsize */
        0,                      /* tp_itemsize */
@@ -1946,73 +2275,84 @@ static void pyrna_subtype_set_rna(PyObject *newclass, StructRNA *srna)
        /* done with rna instance */
 }
 
-PyObject* pyrna_struct_Subtype(PointerRNA *ptr)
+static struct PyMethodDef pyrna_struct_subtype_methods[] = {
+       {"FloatProperty", (PyCFunction)BPy_FloatProperty, METH_VARARGS|METH_KEYWORDS, ""},
+       {"IntProperty", (PyCFunction)BPy_IntProperty, METH_VARARGS|METH_KEYWORDS, ""},
+       {"BoolProperty", (PyCFunction)BPy_BoolProperty, METH_VARARGS|METH_KEYWORDS, ""},
+       {"StringProperty", (PyCFunction)BPy_StringProperty, METH_VARARGS|METH_KEYWORDS, ""},
+       {NULL, NULL, 0, NULL}
+};
+
+
+PyObject* pyrna_srna_Subtype(StructRNA *srna)
 {
        PyObject *newclass = NULL;
-       PropertyRNA *nameprop;
 
-       if (ptr->type==NULL) {
+       if (srna == NULL) {
                newclass= NULL; /* Nothing to do */
-       } else if ((newclass= RNA_struct_py_type_get(ptr->data))) {
+       } else if ((newclass= RNA_struct_py_type_get(srna))) {
                Py_INCREF(newclass);
-       } else if ((nameprop = RNA_struct_name_property(ptr->type))) {
-               /* for now, return the base RNA type rather then a real module */
-               
-               /* Assume RNA_struct_py_type_get(ptr->data) was alredy checked */
-               
+       } else {
                /* subclass equivelents
                - class myClass(myBase):
                        some='value' # or ...
-               - myClass = type(name='myClass', bases=(myBase,), dict={'some':'value'})
+               - myClass = type(name='myClass', bases=(myBase,), dict={'__module__':'bpy.types'})
                */
-               char name[256], *nameptr;
-               const char *descr= RNA_struct_ui_description(ptr->type);
 
-               PyObject *args = PyTuple_New(3);
-               PyObject *bases = PyTuple_New(1);
-               PyObject *dict = PyDict_New();
-               PyObject *item;
-               
-               
-               nameptr= RNA_property_string_get_alloc(ptr, nameprop, name, sizeof(name));
-               
-               // arg 1
-               //PyTuple_SET_ITEM(args, 0, PyUnicode_FromString(tp_name));
-               PyTuple_SET_ITEM(args, 0, PyUnicode_FromString(nameptr));
-               
-               // arg 2
-               PyTuple_SET_ITEM(bases, 0, (PyObject *)&pyrna_struct_Type);
-               Py_INCREF(&pyrna_struct_Type);
+               /* Assume RNA_struct_py_type_get(srna) was alredy checked */
+               StructRNA *base;
+
+               PyObject *py_base= NULL;
 
-               PyTuple_SET_ITEM(args, 1, bases);
+               const char *idname= RNA_struct_identifier(srna);
+               const char *descr= RNA_struct_ui_description(srna);
+
+               if(!descr) descr= "(no docs)";
                
-               // arg 3 - add an instance of the rna 
-               if(descr) {
-                       item= PyUnicode_FromString(descr);
-                       PyDict_SetItemString(dict, "__doc__", item);
-                       Py_DECREF(item);
+               /* get the base type */
+               base= RNA_struct_base(srna);
+               if(base && base != srna) {
+                       /*/printf("debug subtype %s %p\n", RNA_struct_identifier(srna), srna); */
+                       py_base= pyrna_srna_Subtype(base);
                }
                
-               PyTuple_SET_ITEM(args, 2, dict); // fill with useful subclass things!
+               if(py_base==NULL) {
+                       py_base= (PyObject *)&pyrna_struct_Type;
+                       Py_INCREF(py_base);
+               }
                
-               if (PyErr_Occurred()) {
+               newclass = PyObject_CallFunction(       (PyObject*)&PyType_Type, "s(N){ssss}", idname, py_base, "__module__","bpy.types", "__doc__",descr);
+
+               if (newclass) {
+                       pyrna_subtype_set_rna(newclass, srna);
+                       // PyObSpit("NewStructRNA Type: ", (PyObject *)newclass);
+
+                       /* attach functions into the class
+                        * so you can do... bpy.types.Scene.SomeFunction()
+                        */
+                       {
+                               PyMethodDef *ml;
+                               for(ml= pyrna_struct_subtype_methods; ml->ml_name; ml++){
+                                       PyObject_SetAttrString(newclass, ml->ml_name, PyCFunction_New(ml, newclass));
+                               }
+                       }
+
+               }
+               else {
+                       /* this should not happen */
                        PyErr_Print();
                        PyErr_Clear();
                }
-               
-               newclass = PyObject_CallObject((PyObject *)&PyType_Type, args);
-               Py_DECREF(args);
-
-               if (newclass)
-                       pyrna_subtype_set_rna(newclass, ptr->data);
-               
-               if (name != nameptr)
-                       MEM_freeN(nameptr);
        }
        
        return newclass;
 }
 
+PyObject* pyrna_struct_Subtype(PointerRNA *ptr)
+{
+       return pyrna_srna_Subtype((ptr->type == &RNA_Struct) ? ptr->data : ptr->type);
+}
+
 /*-----------------------CreatePyObject---------------------------------*/
 PyObject *pyrna_struct_CreatePyObject( PointerRNA *ptr )
 {
@@ -2021,8 +2361,7 @@ PyObject *pyrna_struct_CreatePyObject( PointerRNA *ptr )
        if (ptr->data==NULL && ptr->type==NULL) { /* Operator RNA has NULL data */
                Py_RETURN_NONE;
        }
-       
-       if (ptr->type == &RNA_Struct) { /* always return a python subtype from rna struct types */
+       else {
                PyTypeObject *tp = (PyTypeObject *)pyrna_struct_Subtype(ptr);
                
                if (tp) {
@@ -2033,17 +2372,17 @@ PyObject *pyrna_struct_CreatePyObject( PointerRNA *ptr )
                        pyrna = ( BPy_StructRNA * ) PyObject_NEW( BPy_StructRNA, &pyrna_struct_Type );
                }
        }
-       else {
-               pyrna = ( BPy_StructRNA * ) PyObject_NEW( BPy_StructRNA, &pyrna_struct_Type );
-       }
-       
+
        if( !pyrna ) {
                PyErr_SetString( PyExc_MemoryError, "couldn't create BPy_StructRNA object" );
                return NULL;
        }
        
        pyrna->ptr= *ptr;
-       pyrna->freeptr= 0;
+       pyrna->freeptr= FALSE;
+       
+       // PyObSpit("NewStructRNA: ", (PyObject *)pyrna);
+       
        return ( PyObject * ) pyrna;
 }
 
@@ -2119,12 +2458,12 @@ static PyObject *pyrna_basetype_getattro( BPy_BaseTypeRNA * self, PyObject *pyna
        if (RNA_property_collection_lookup_string(&self->ptr, self->prop, _PyUnicode_AsString(pyname), &newptr)) {
                ret= pyrna_struct_Subtype(&newptr);
                if (ret==NULL) {
-                       PyErr_Format(PyExc_SystemError, "bpy.types.%s subtype could not be generated, this is a bug!", _PyUnicode_AsString(pyname));
+                       PyErr_Format(PyExc_SystemError, "bpy.types.%.200s subtype could not be generated, this is a bug!", _PyUnicode_AsString(pyname));
                }
                return ret;
        }
        else { /* Override the error */
-               PyErr_Format(PyExc_AttributeError, "bpy.types.%s not a valid RNA_Struct", _PyUnicode_AsString(pyname));
+               PyErr_Format(PyExc_AttributeError, "bpy.types.%.200s RNA_Struct does not exist", _PyUnicode_AsString(pyname));
                return NULL;
        }
 }
@@ -2187,28 +2526,19 @@ static struct PyMethodDef props_methods[] = {
        {NULL, NULL, 0, NULL}
 };
 
-#if PY_VERSION_HEX >= 0x03000000
 static struct PyModuleDef props_module = {
        PyModuleDef_HEAD_INIT,
-       "bpyprops",
+       "bpy.props",
        "",
        -1,/* multiple "initialization" just copies the module dict. */
        props_methods,
        NULL, NULL, NULL, NULL
 };
-#endif
 
 PyObject *BPY_rna_props( void )
 {
-       PyObject *submodule, *mod;
-#if PY_VERSION_HEX >= 0x03000000
+       PyObject *submodule;
        submodule= PyModule_Create(&props_module);
-#else /* Py2.x */
-       submodule= Py_InitModule3( "bpy.props", props_methods, "" );
-#endif
-       
-       mod = PyModule_New("props");
-       PyModule_AddObject( submodule, "props", mod );
        
        /* INCREF since its its assumed that all these functions return the
         * module with a new ref like PyDict_New, since they are passed to
@@ -2220,6 +2550,45 @@ PyObject *BPY_rna_props( void )
 
 /* Orphan functions, not sure where they should go */
 
+/* get the srna for methods attached to types */
+static StructRNA *srna_from_self(PyObject *self)
+{
+       BPy_StructRNA *py_srna;
+
+       if(self==NULL) {
+               PyErr_SetString(PyExc_SystemError, "internal error, self was NULL, should never happen.");
+               return NULL;
+       }
+
+       if (PyCObject_Check(self)) {
+               return PyCObject_AsVoidPtr(self);
+       }
+
+       py_srna= (BPy_StructRNA *)PyObject_GetAttrString(self, "__rna__");
+
+       if(py_srna==NULL) {
+               PyErr_SetString(PyExc_SystemError, "internal error, self had no __rna__ attribute, should never happen.");
+               return NULL;
+       }
+
+       if(!BPy_StructRNA_Check(py_srna)) {
+               PyErr_SetString(PyExc_SystemError, "internal error, self's __rna__ attribute isnt a StructRNA type, should never happen.");
+               return NULL;
+       }
+
+       if((py_srna->ptr.data && py_srna->ptr.type == &RNA_Struct) == 0) {
+               PyErr_SetString(PyExc_SystemError, "internal error, self's __rna__ attribute wasnt an RNA_Struct, should never happen.");
+               return NULL;
+       }
+
+       if(!RNA_struct_is_ID(py_srna->ptr.data)) {
+               PyErr_SetString(PyExc_TypeError, "only ID types support python defined properties");
+               return NULL;
+       }
+
+       return py_srna->ptr.data;
+}
+
 /* Function that sets RNA, NOTE - self is NULL when called from python, but being abused from C so we can pass the srna allong
  * This isnt incorrect since its a python object - but be careful */
 PyObject *BPy_FloatProperty(PyObject *self, PyObject *args, PyObject *kw)
@@ -2227,7 +2596,9 @@ PyObject *BPy_FloatProperty(PyObject *self, PyObject *args, PyObject *kw)
        static char *kwlist[] = {"attr", "name", "description", "min", "max", "soft_min", "soft_max", "default", NULL};
        char *id, *name="", *description="";
        float min=FLT_MIN, max=FLT_MAX, soft_min=FLT_MIN, soft_max=FLT_MAX, def=0.0f;
-       
+       PropertyRNA *prop;
+       StructRNA *srna;
+
        if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssfffff:FloatProperty", kwlist, &id, &name, &description, &min, &max, &soft_min, &soft_max, &def))
                return NULL;
        
@@ -2236,17 +2607,21 @@ PyObject *BPy_FloatProperty(PyObject *self, PyObject *args, PyObject *kw)
                return NULL;
        }
        
-       if (self && PyCObject_Check(self)) {
-               StructRNA *srna = PyCObject_AsVoidPtr(self);
-               RNA_def_float(srna, id, def, min, max, name, description, soft_min, soft_max);
-               Py_RETURN_NONE;
-       } else {
+       if(((self && (PyCObject_Check(self))) || (self && BPy_StructRNA_Check(self))) == 0) {
                PyObject *ret = PyTuple_New(2);
                PyTuple_SET_ITEM(ret, 0, PyCObject_FromVoidPtr((void *)BPy_FloatProperty, NULL));
                PyTuple_SET_ITEM(ret, 1, kw);
                Py_INCREF(kw);
                return ret;
        }
+
+       srna= srna_from_self(self);
+       if(srna==NULL)
+               return NULL;
+
+       prop= RNA_def_float(srna, id, def, min, max, name, description, soft_min, soft_max);
+       RNA_def_property_duplicate_pointers(prop);
+       Py_RETURN_NONE;
 }
 
 PyObject *BPy_IntProperty(PyObject *self, PyObject *args, PyObject *kw)
@@ -2254,7 +2629,9 @@ PyObject *BPy_IntProperty(PyObject *self, PyObject *args, PyObject *kw)
        static char *kwlist[] = {"attr", "name", "description", "min", "max", "soft_min", "soft_max", "default", NULL};
        char *id, *name="", *description="";
        int min=INT_MIN, max=INT_MAX, soft_min=INT_MIN, soft_max=INT_MAX, def=0;
-       
+       PropertyRNA *prop;
+       StructRNA *srna;
+
        if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssiiiii:IntProperty", kwlist, &id, &name, &description, &min, &max, &soft_min, &soft_max, &def))
                return NULL;
        
@@ -2263,17 +2640,21 @@ PyObject *BPy_IntProperty(PyObject *self, PyObject *args, PyObject *kw)
                return NULL;
        }
        
-       if (self && PyCObject_Check(self)) {
-               StructRNA *srna = PyCObject_AsVoidPtr(self);
-               RNA_def_int(srna, id, def, min, max, name, description, soft_min, soft_max);
-               Py_RETURN_NONE;
-       } else {
+       if(((self && (PyCObject_Check(self))) || (self && BPy_StructRNA_Check(self))) == 0) {
                PyObject *ret = PyTuple_New(2);
                PyTuple_SET_ITEM(ret, 0, PyCObject_FromVoidPtr((void *)BPy_IntProperty, NULL));
                PyTuple_SET_ITEM(ret, 1, kw);
                Py_INCREF(kw);
                return ret;
        }
+
+       srna= srna_from_self(self);
+       if(srna==NULL)
+               return NULL;
+
+       prop= RNA_def_int(srna, id, def, min, max, name, description, soft_min, soft_max);
+       RNA_def_property_duplicate_pointers(prop);
+       Py_RETURN_NONE;
 }
 
 PyObject *BPy_BoolProperty(PyObject *self, PyObject *args, PyObject *kw)
@@ -2281,7 +2662,9 @@ PyObject *BPy_BoolProperty(PyObject *self, PyObject *args, PyObject *kw)
        static char *kwlist[] = {"attr", "name", "description", "default", NULL};
        char *id, *name="", *description="";
        int def=0;
-       
+       PropertyRNA *prop;
+       StructRNA *srna;
+
        if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssi:BoolProperty", kwlist, &id, &name, &description, &def))
                return NULL;
        
@@ -2289,18 +2672,22 @@ PyObject *BPy_BoolProperty(PyObject *self, PyObject *args, PyObject *kw)
                PyErr_SetString(PyExc_ValueError, "all args must be keywors"); // TODO - py3 can enforce this.
                return NULL;
        }
-       
-       if (self && PyCObject_Check(self)) {
-               StructRNA *srna = PyCObject_AsVoidPtr(self);
-               RNA_def_boolean(srna, id, def, name, description);
-               Py_RETURN_NONE;
-       } else {
+
+       if(((self && (PyCObject_Check(self))) || (self && BPy_StructRNA_Check(self))) == 0) {
                PyObject *ret = PyTuple_New(2);
                PyTuple_SET_ITEM(ret, 0, PyCObject_FromVoidPtr((void *)BPy_BoolProperty, NULL));
                PyTuple_SET_ITEM(ret, 1, kw);
                Py_INCREF(kw);
                return ret;
        }
+
+       srna= srna_from_self(self);
+       if(srna==NULL)
+               return NULL;
+
+       prop= RNA_def_boolean(srna, id, def, name, description);
+       RNA_def_property_duplicate_pointers(prop);
+       Py_RETURN_NONE;
 }
 
 PyObject *BPy_StringProperty(PyObject *self, PyObject *args, PyObject *kw)
@@ -2308,7 +2695,9 @@ PyObject *BPy_StringProperty(PyObject *self, PyObject *args, PyObject *kw)
        static char *kwlist[] = {"attr", "name", "description", "maxlen", "default", NULL};
        char *id, *name="", *description="", *def="";
        int maxlen=0;
-       
+       PropertyRNA *prop;
+       StructRNA *srna;
+
        if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssis:StringProperty", kwlist, &id, &name, &description, &maxlen, &def))
                return NULL;
        
@@ -2316,18 +2705,22 @@ PyObject *BPy_StringProperty(PyObject *self, PyObject *args, PyObject *kw)
                PyErr_SetString(PyExc_ValueError, "all args must be keywors"); // TODO - py3 can enforce this.
                return NULL;
        }
-       
-       if (self && PyCObject_Check(self)) {
-               StructRNA *srna = PyCObject_AsVoidPtr(self);
-               RNA_def_string(srna, id, def, maxlen, name, description);
-               Py_RETURN_NONE;
-       } else {
+
+       if(((self && (PyCObject_Check(self))) || (self && BPy_StructRNA_Check(self))) == 0) {
                PyObject *ret = PyTuple_New(2);
                PyTuple_SET_ITEM(ret, 0, PyCObject_FromVoidPtr((void *)BPy_StringProperty, NULL));
                PyTuple_SET_ITEM(ret, 1, kw);
                Py_INCREF(kw);
                return ret;
        }
+
+       srna= srna_from_self(self);
+       if(srna==NULL)
+               return NULL;
+
+       prop= RNA_def_string(srna, id, def, maxlen, name, description);
+       RNA_def_property_duplicate_pointers(prop);
+       Py_RETURN_NONE;
 }
 
 /*-------------------- Type Registration ------------------------*/
@@ -2366,7 +2759,7 @@ static int bpy_class_validate(PointerRNA *dummyptr, void *py_data, int *have_fun
        if (base_class) {
                if (!PyObject_IsSubclass(py_class, base_class)) {
                        PyObject *name= PyObject_GetAttrString(base_class, "__name__");
-                       PyErr_Format( PyExc_AttributeError, "expected %s subclass of class \"%s\"", class_type, name ? _PyUnicode_AsString(name):"<UNKNOWN>");
+                       PyErr_Format( PyExc_TypeError, "expected %.200s subclass of class \"%.200s\"", class_type, name ? _PyUnicode_AsString(name):"<UNKNOWN>");
                        Py_XDECREF(name);
                        return -1;
                }
@@ -2389,7 +2782,7 @@ static int bpy_class_validate(PointerRNA *dummyptr, void *py_data, int *have_fun
 
                if (item==NULL) {
                        if ((flag & FUNC_REGISTER_OPTIONAL)==0) {
-                               PyErr_Format( PyExc_AttributeError, "expected %s class to have an \"%s\" attribute", class_type, RNA_function_identifier(func));
+                               PyErr_Format( PyExc_AttributeError, "expected %.200s class to have an \"%.200s\" attribute", class_type, RNA_function_identifier(func));
                                return -1;
                        }
 
@@ -2404,7 +2797,7 @@ static int bpy_class_validate(PointerRNA *dummyptr, void *py_data, int *have_fun
                                fitem= item; /* py 3.x */
 
                        if (PyFunction_Check(fitem)==0) {
-                               PyErr_Format( PyExc_AttributeError, "expected %s class \"%s\" attribute to be a function", class_type, RNA_function_identifier(func));
+                               PyErr_Format( PyExc_TypeError, "expected %.200s class \"%.200s\" attribute to be a function", class_type, RNA_function_identifier(func));
                                return -1;
                        }
 
@@ -2416,7 +2809,7 @@ static int bpy_class_validate(PointerRNA *dummyptr, void *py_data, int *have_fun
                                Py_DECREF(py_arg_count);
 
                                if (arg_count != func_arg_count) {
-                                       PyErr_Format( PyExc_AttributeError, "expected %s class \"%s\" function to have %d args", class_type, RNA_function_identifier(func), func_arg_count);
+                                       PyErr_Format( PyExc_AttributeError, "expected %.200s class \"%.200s\" function to have %d args", class_type, RNA_function_identifier(func), func_arg_count);
                                        return -1;
                                }
                        }
@@ -2442,13 +2835,13 @@ static int bpy_class_validate(PointerRNA *dummyptr, void *py_data, int *have_fun
                                if(item) {
                                        Py_DECREF(item); /* no need to keep a ref, the class owns it */
 
-                                       if(pyrna_py_to_prop(dummyptr, prop, NULL, item) != 0)
+                                       if(pyrna_py_to_prop(dummyptr, prop, NULL, item, "validating class error:") != 0)
                                                return -1;
                                }
                        }
 
                        if (item==NULL && (flag & PROP_REGISTER_OPTIONAL)==0) {
-                               PyErr_Format( PyExc_AttributeError, "expected %s class to have an \"%s\" attribute", class_type, identifier);
+                               PyErr_Format( PyExc_AttributeError, "expected %.200s class to have an \"%.200s\" attribute", class_type, identifier);
                                return -1;
                        }
 
@@ -2457,7 +2850,7 @@ static int bpy_class_validate(PointerRNA *dummyptr, void *py_data, int *have_fun
                else {
                        Py_DECREF(item); /* no need to keep a ref, the class owns it */
 
-                       if(pyrna_py_to_prop(dummyptr, prop, NULL, item) != 0)
+                       if(pyrna_py_to_prop(dummyptr, prop, NULL, item, "validating class error:") != 0)
                                return -1;
                }
        }
@@ -2477,9 +2870,10 @@ static int bpy_class_call(PointerRNA *ptr, FunctionRNA *func, ParameterList *par
        void *retdata= NULL;
        int err= 0, i, flag;
 
-       PyGILState_STATE gilstate = PyGILState_Ensure();
+       PyGILState_STATE gilstate;
 
-       BPY_update_modules(); // XXX - the RNA pointers can change so update before running, would like a nicer solution for this.
+       bContext *C= BPy_GetContext(); // XXX - NEEDS FIXING, QUITE BAD.
+       bpy_context_set(C, &gilstate);
 
        py_class= RNA_struct_py_type_get(ptr->type);
        
@@ -2532,26 +2926,30 @@ static int bpy_class_call(PointerRNA *ptr, FunctionRNA *func, ParameterList *par
                }
                else {
                        Py_DECREF(py_class_instance);
-                       PyErr_Format(PyExc_AttributeError, "could not find function %s in %s to execute callback.", RNA_function_identifier(func), RNA_struct_identifier(ptr->type));
+                       PyErr_Format(PyExc_TypeError, "could not find function %.200s in %.200s to execute callback.", RNA_function_identifier(func), RNA_struct_identifier(ptr->type));
                        err= -1;
                }
        }
        else {
-               PyErr_Format(PyExc_AttributeError, "could not create instance of %s to call callback function %s.", RNA_struct_identifier(ptr->type), RNA_function_identifier(func));
+               PyErr_Format(PyExc_RuntimeError, "could not create instance of %.200s to call callback function %.200s.", RNA_struct_identifier(ptr->type), RNA_function_identifier(func));
                err= -1;
        }
 
        if (ret == NULL) { /* covers py_class_instance failing too */
-               PyErr_Print(); /* XXX use reporting api? */
                err= -1;
        }
        else {
                if(retdata)
-                       err= pyrna_py_to_prop(&funcptr, pret, retdata, ret);
+                       err= pyrna_py_to_prop(&funcptr, pret, retdata, ret, "calling class function:");
                Py_DECREF(ret);
        }
 
-       PyGILState_Release(gilstate);
+       if(err != 0) {
+               PyErr_Print();
+               PyErr_Clear();
+       }
+
+       bpy_context_clear(C, &gilstate);
        
        return err;
 }