=ID Property Bugfix=
[blender-staging.git] / source / blender / python / api2_2x / IDProp.c
index 3097f51e83940424c396e5ccd2489ff3d7e8dcdf..3358b06d30b4e4e1737672901f67ce7ddb4ed9c2 100644 (file)
@@ -49,7 +49,7 @@ extern PyTypeObject IDGroup_Iter_Type;
 
 PyObject *IDGroup_repr( BPy_IDProperty *self )
 {
-       return Py_BuildValue( "s", "(ID Property)" );
+       return PyString_FromString( "(ID Property)" );
 }
 
 extern PyTypeObject IDGroup_Type;
@@ -58,11 +58,11 @@ PyObject *BPy_IDGroup_WrapData( ID *id, IDProperty *prop )
 {
        switch ( prop->type ) {
                case IDP_STRING:
-                       return Py_BuildValue( "s", prop->data.pointer );
+                       return PyString_FromString( prop->data.pointer );
                case IDP_INT:
-                       return Py_BuildValue( "i", prop->data.val );
+                       return PyInt_FromLong( (long)prop->data.val );
                case IDP_FLOAT:
-                       return Py_BuildValue( "f", *(float*)(&prop->data.val) );
+                       return PyFloat_FromDouble( (double)(*(float*)(&prop->data.val)) );
                case IDP_GROUP:
                        /*blegh*/
                        {
@@ -140,7 +140,7 @@ int BPy_IDGroup_SetData(BPy_IDProperty *self, IDProperty *prop, PyObject *value)
 
 PyObject *BPy_IDGroup_GetName(BPy_IDProperty *self, void *bleh)
 {
-       return Py_BuildValue("s", self->prop->name);
+       return PyString_FromString(self->prop->name);
 }
 
 int BPy_IDGroup_SetName(BPy_IDProperty *self, PyObject *value, void *bleh)
@@ -159,7 +159,7 @@ int BPy_IDGroup_SetName(BPy_IDProperty *self, PyObject *value, void *bleh)
 
 PyObject *BPy_IDGroup_GetType(BPy_IDProperty *self)
 {
-       return Py_BuildValue("i", self->prop->type);
+       return PyInt_FromLong((long)self->prop->type);
 }
 
 static PyGetSetDef BPy_IDGroup_getseters[] = {
@@ -332,13 +332,13 @@ PyObject *BPy_IDGroup_MapDataToPy(IDProperty *prop)
 {
        switch (prop->type) {
                case IDP_STRING:
-                       return Py_BuildValue("s", prop->data.pointer);
+                       return PyString_FromString(prop->data.pointer);
                        break;
                case IDP_FLOAT:
-                       return Py_BuildValue("f", *((float*)&prop->data.val));
+                       return PyFloat_FromDouble(*((float*)&prop->data.val));
                        break;
                case IDP_INT:
-                       return Py_BuildValue("i", prop->data.val);
+                       return PyInt_FromLong( (long)prop->data.val );
                        break;
                case IDP_ARRAY:
                {
@@ -351,8 +351,11 @@ PyObject *BPy_IDGroup_MapDataToPy(IDProperty *prop)
                        
                        for (i=0; i<prop->len; i++) {
                                if (prop->subtype == IDP_FLOAT)
-                                       PyList_SetItem(seq, i, Py_BuildValue("f", ((float*)prop->data.pointer)[i]));
-                               else PyList_SetItem(seq, i, Py_BuildValue("i",   ((int*)prop->data.pointer)[i]));
+                                               PyList_SetItem(seq, i,
+                                               PyFloat_FromDouble(((float*)prop->data.pointer)[i]));
+                               
+                               else    PyList_SetItem(seq, i,
+                                               PyInt_FromLong(((int*)prop->data.pointer)[i]));
                        }
                        return seq;
                }
@@ -381,16 +384,15 @@ PyObject *BPy_IDGroup_MapDataToPy(IDProperty *prop)
                                           "eek!! a property exists with a bad type code!!!" );
 }
 
-PyObject *BPy_IDGroup_Pop(BPy_IDProperty *self, PyObject *vars)
+PyObject *BPy_IDGroup_Pop(BPy_IDProperty *self, PyObject *value)
 {
        IDProperty *loop;
        PyObject *pyform;
-       char *name;
-       int ok = PyArg_ParseTuple(vars, "s", &name);
+       char *name = PyString_AsString(value);
        
-       if (!ok) {
+       if (!name) {
                return EXPP_ReturnPyObjError( PyExc_TypeError,
-                  "pop expected at least 1 arguments, got 0" );
+                  "pop expected at least 1 argument, got 0" );
        }
        
        for (loop=self->prop->data.group.first; loop; loop=loop->next) {
@@ -429,17 +431,34 @@ PyObject *BPy_IDGroup_IterItems(BPy_IDProperty *self)
 
 PyObject *BPy_IDGroup_GetKeys(BPy_IDProperty *self)
 {
-       PyObject *seq = PyList_New(self->prop->len), *st;
+       PyObject *seq = PyList_New(self->prop->len);
        IDProperty *loop;
-       int i;
+       int i, j;
 
        if (!seq) 
                return EXPP_ReturnPyObjError( PyExc_RuntimeError,
                   "PyList_New() failed" );
                   
-       for (i=0, loop=self->prop->data.group.first; loop; loop=loop->next, i++) {
-               st = Py_BuildValue("s", loop->name);
-               PyList_SetItem(seq, i, st);
+       for (i=0, loop=self->prop->data.group.first; loop; loop=loop->next, i++)
+               PyList_SetItem(seq, i, PyString_FromString(loop->name));
+       
+       if (i != self->prop->len) {
+               printf("ID Property Error found and corrected in BPy_IDGroup_GetKeys!\n");
+               
+               /*fill rest of list with valid references to None*/
+               for (j=i; j<self->prop->len; j++) {
+                       Py_INCREF(Py_None);
+                       PyList_SetItem(seq, j, Py_None);
+               }
+               
+               /*set correct group length*/
+               self->prop->len = i;
+               
+               /*free the old list*/
+               Py_DECREF(seq);
+               
+               /*call self again*/
+               return BPy_IDGroup_GetKeys(self);               
        }
        
        return seq;
@@ -449,7 +468,7 @@ PyObject *BPy_IDGroup_GetValues(BPy_IDProperty *self)
 {
        PyObject *seq = PyList_New(self->prop->len);
        IDProperty *loop;
-       int i;
+       int i, j;
 
        if (!seq) 
                return EXPP_ReturnPyObjError( PyExc_RuntimeError,
@@ -458,16 +477,35 @@ PyObject *BPy_IDGroup_GetValues(BPy_IDProperty *self)
        for (i=0, loop=self->prop->data.group.first; loop; loop=loop->next, i++) {
                PyList_SetItem(seq, i, BPy_IDGroup_WrapData(self->id, loop));
        }
-       
+
+       if (i != self->prop->len) {
+               printf("ID Property Error found and corrected in BPy_IDGroup_GetValues!\n");
+               
+               /*fill rest of list with valid references to None*/
+               for (j=i; j<self->prop->len; j++) {
+                       Py_INCREF(Py_None);
+                       PyList_SetItem(seq, j, Py_None);
+               }
+               
+               /*set correct group length*/
+               self->prop->len = i;
+               
+               /*free the old list*/
+               Py_DECREF(seq);
+               
+               /*call self again*/
+               return BPy_IDGroup_GetValues(self);             
+       }
+               
        return seq;
 }
 
-PyObject *BPy_IDGroup_HasKey(BPy_IDProperty *self, PyObject *vars)
+PyObject *BPy_IDGroup_HasKey(BPy_IDProperty *self, PyObject *value)
 {
        IDProperty *loop;
-       char *name;
+       char *name = PyString_AsString(value);
        
-       if (!PyArg_ParseTuple(vars, "s", &name))
+       if (!name)
                return EXPP_ReturnPyObjError( PyExc_TypeError,
                   "expected a string");
                   
@@ -506,7 +544,7 @@ PyObject *BPy_IDGroup_ConvertToPy(BPy_IDProperty *self)
 }
 
 static struct PyMethodDef BPy_IDGroup_methods[] = {
-       {"pop", (PyCFunction)BPy_IDGroup_Pop, METH_VARARGS,
+       {"pop", (PyCFunction)BPy_IDGroup_Pop, METH_O,
                "pop an item from the group; raises KeyError if the item doesn't exist."},
        {"iteritems", (PyCFunction)BPy_IDGroup_IterItems, METH_NOARGS,
                "iterate through the items in the dict; behaves like dictionary method iteritems."},
@@ -514,7 +552,7 @@ static struct PyMethodDef BPy_IDGroup_methods[] = {
                "get the keys associated with this group as a list of strings."},
        {"values", (PyCFunction)BPy_IDGroup_GetValues, METH_NOARGS,
                "get the values associated with this group."},
-       {"has_key", (PyCFunction)BPy_IDGroup_HasKey, METH_VARARGS,
+       {"has_key", (PyCFunction)BPy_IDGroup_HasKey, METH_O,
                "returns true if the group contains a key, false if not."},
        {"update", (PyCFunction)BPy_IDGroup_Update, METH_VARARGS,
                "updates the values in the group with the values of another or a dict."},
@@ -612,18 +650,18 @@ PyObject *BPy_Wrap_IDProperty(ID *id, IDProperty *prop, IDProperty *parent)
 
 PyObject *IDArray_repr(BPy_IDArray *self)
 {
-       return Py_BuildValue("s", "(ID Array)");
+       return PyString_FromString("(ID Array)");
 }
 
 
 PyObject *BPy_IDArray_GetType(BPy_IDArray *self)
 {
-       return Py_BuildValue("i", self->prop->subtype);
+       return PyInt_FromLong( (long)self->prop->subtype );
 }
 
 PyObject *BPy_IDArray_GetLen(BPy_IDArray *self)
 {
-       return Py_BuildValue("i", self->prop->len);
+       return PyInt_FromLong( (long)self->prop->len );
 }
 
 static PyGetSetDef BPy_IDArray_getseters[] = {
@@ -651,10 +689,10 @@ PyObject *BPy_IDArray_GetItem(BPy_IDArray *self, int index)
 
        switch (self->prop->subtype) {
                case IDP_FLOAT:
-                       return Py_BuildValue("f", ((float*)self->prop->data.pointer)[index]);
+                       return PyFloat_FromDouble( (double)(((float*)self->prop->data.pointer)[index]));
                        break;
                case IDP_INT:
-                       return Py_BuildValue("i", ((int*)self->prop->data.pointer)[index]);
+                       return PyInt_FromLong( (long)((int*)self->prop->data.pointer)[index] );
                        break;
        }
                return EXPP_ReturnPyObjError( PyExc_RuntimeError,
@@ -803,20 +841,25 @@ PyObject *IDGroup_Iter_iterself(PyObject *self)
 
 PyObject *IDGroup_Iter_repr(BPy_IDGroup_Iter *self)
 {
-       return Py_BuildValue("s", "(ID Property Group)");
+       return PyString_FromString("(ID Property Group)");
 }
 
 PyObject *BPy_Group_Iter_Next(BPy_IDGroup_Iter *self)
 {
        IDProperty *cur=NULL;
+       PyObject *tmpval;
+       PyObject *ret;
 
        if (self->cur) {
                cur = self->cur;
                self->cur = self->cur->next;
                if (self->mode == IDPROP_ITER_ITEMS) {
-                       return Py_BuildValue("[s, O]", cur->name, BPy_IDGroup_WrapData(self->group->id, cur));
+                       tmpval = BPy_IDGroup_WrapData(self->group->id, cur);
+                       ret = Py_BuildValue("[s, O]", cur->name, tmpval);
+                       Py_DECREF(tmpval);
+                       return ret;
                } else {
-                       return Py_BuildValue("s", cur->name);
+                       return PyString_FromString(cur->name);
                }
        } else {
                return EXPP_ReturnPyObjError( PyExc_StopIteration,