fix for py/rna mesh.materials[:] where empty materials exist, would raise a runtime...
authorCampbell Barton <ideasman42@gmail.com>
Wed, 5 Jan 2011 14:49:08 +0000 (14:49 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Wed, 5 Jan 2011 14:49:08 +0000 (14:49 +0000)
problem was there was no way to tell the difference between getting an empty item from a collection or the item not being found.

source/blender/makesrna/intern/makesrna.c
source/blender/makesrna/intern/rna_access.c
source/blender/makesrna/intern/rna_internal.h
source/blender/makesrna/intern/rna_internal_types.h
source/blender/makesrna/intern/rna_pose.c
source/blender/makesrna/intern/rna_rna.c
source/blender/makesrna/intern/rna_scene.c
source/blender/python/intern/bpy_rna.c

index 6fbaa76..9be61cd 100644 (file)
@@ -965,16 +965,16 @@ static char *rna_def_property_lookup_int_func(FILE *f, StructRNA *srna, Property
 
        func= rna_alloc_function_name(srna->identifier, prop->identifier, "lookup_int");
 
-       fprintf(f, "PointerRNA %s(PointerRNA *ptr, int index)\n", func);
+       fprintf(f, "int %s(PointerRNA *ptr, int index, PointerRNA *r_ptr)\n", func);
        fprintf(f, "{\n");
 
        if(manualfunc) {
-               fprintf(f, "\n  return %s(ptr, index);\n", manualfunc);
+               fprintf(f, "\n  return %s(ptr, index, r_ptr);\n", manualfunc);
                fprintf(f, "}\n\n");
                return func;
        }
 
-       fprintf(f, "    PointerRNA r_ptr;\n");
+       fprintf(f, "    int found= FALSE;\n");
        fprintf(f, "    CollectionPropertyIterator iter;\n\n");
 
        fprintf(f, "    %s_%s_begin(&iter, ptr);\n\n", srna->identifier, prop->identifier);
@@ -998,6 +998,7 @@ static char *rna_def_property_lookup_int_func(FILE *f, StructRNA *srna, Property
                fprintf(f, "            }\n");
                fprintf(f, "            else {\n");
                fprintf(f, "                    internal->ptr += internal->itemsize*index;\n");
+               fprintf(f, "                    found= TRUE;\n");
                fprintf(f, "            }\n");
        }
        else if(strcmp(nextfunc, "rna_iterator_listbase_next") == 0) {
@@ -1013,14 +1014,15 @@ static char *rna_def_property_lookup_int_func(FILE *f, StructRNA *srna, Property
                fprintf(f, "                    while(index-- > 0 && internal->link)\n");
                fprintf(f, "                            internal->link= internal->link->next;\n");
                fprintf(f, "            }\n");
+               fprintf(f, "            found= (index == -1);\n");
        }
 
        fprintf(f, "    }\n\n");
 
-       fprintf(f, "    r_ptr = %s_%s_get(&iter);\n", srna->identifier, prop->identifier);
+       fprintf(f, "    if(found) *r_ptr = %s_%s_get(&iter);\n", srna->identifier, prop->identifier);
        fprintf(f, "    %s_%s_end(&iter);\n\n", srna->identifier, prop->identifier);
 
-       fprintf(f, "    return r_ptr;\n");
+       fprintf(f, "    return found;\n");
 
 #if 0
        rna_print_data_get(f, dp);
index 23b9692..ce80bb6 100644 (file)
@@ -2302,8 +2302,7 @@ int RNA_property_collection_lookup_int(PointerRNA *ptr, PropertyRNA *prop, int k
 
        if(cprop->lookupint) {
                /* we have a callback defined, use it */
-               *r_ptr= cprop->lookupint(ptr, key);
-               return (r_ptr->data != NULL);
+               return cprop->lookupint(ptr, key, r_ptr);
        }
        else {
                /* no callback defined, just iterate and find the nth item */
@@ -2332,8 +2331,7 @@ int RNA_property_collection_lookup_string(PointerRNA *ptr, PropertyRNA *prop, co
 
        if(cprop->lookupstring) {
                /* we have a callback defined, use it */
-               *r_ptr= cprop->lookupstring(ptr, key);
-               return (r_ptr->data != NULL);
+               return cprop->lookupstring(ptr, key, r_ptr);
        }
        else {
                /* no callback defined, compare with name properties if they exist */
index 90c5a55..39f12c5 100644 (file)
@@ -301,7 +301,7 @@ void rna_builtin_properties_begin(struct CollectionPropertyIterator *iter, struc
 void rna_builtin_properties_next(struct CollectionPropertyIterator *iter);
 PointerRNA rna_builtin_properties_get(struct CollectionPropertyIterator *iter);
 PointerRNA rna_builtin_type_get(struct PointerRNA *ptr);
-PointerRNA rna_builtin_properties_lookup_string(PointerRNA *ptr, const char *key);
+int rna_builtin_properties_lookup_string(PointerRNA *ptr, const char *key, PointerRNA *r_ptr);
 
 /* Iterators */
 
index 216533a..ee484c2 100644 (file)
@@ -93,8 +93,8 @@ typedef void (*PropCollectionNextFunc)(struct CollectionPropertyIterator *iter);
 typedef void (*PropCollectionEndFunc)(struct CollectionPropertyIterator *iter);
 typedef PointerRNA (*PropCollectionGetFunc)(struct CollectionPropertyIterator *iter);
 typedef int (*PropCollectionLengthFunc)(struct PointerRNA *ptr);
-typedef PointerRNA (*PropCollectionLookupIntFunc)(struct PointerRNA *ptr, int key);
-typedef PointerRNA (*PropCollectionLookupStringFunc)(struct PointerRNA *ptr, const char *key);
+typedef int (*PropCollectionLookupIntFunc)(struct PointerRNA *ptr, int key, struct PointerRNA *r_ptr);
+typedef int (*PropCollectionLookupStringFunc)(struct PointerRNA *ptr, const char *key, struct PointerRNA *r_ptr);
 
 /* Container - generic abstracted container of RNA properties */
 typedef struct ContainerRNA {
index 51a44c0..7b1a46d 100644 (file)
@@ -547,13 +547,17 @@ static int rna_PoseChannel_rotation_4d_editable(PointerRNA *ptr, int index)
 }
 
 /* not essential, but much faster then the default lookup function */
-PointerRNA rna_PoseBones_lookup_string(PointerRNA *ptr, const char *key)
+int rna_PoseBones_lookup_string(PointerRNA *ptr, const char *key, PointerRNA *r_ptr)
 {
-       PointerRNA rptr;
        bPose *pose= (bPose*)ptr->data;
        bPoseChannel *pchan= get_pose_channel(pose, key);
-       RNA_pointer_create(ptr->id.data, &RNA_PoseBone, pchan, &rptr);
-       return rptr;
+       if(pchan) {
+               RNA_pointer_create(ptr->id.data, &RNA_PoseBone, pchan, r_ptr);
+               return TRUE;
+       }
+       else {
+               return FALSE;
+       }
 }
 
 static void rna_PoseChannel_matrix_basis_get(PointerRNA *ptr, float *values)
index 9e04132..9158f0c 100644 (file)
@@ -300,7 +300,7 @@ PointerRNA rna_builtin_properties_get(CollectionPropertyIterator *iter)
        return rna_Struct_properties_get(iter);
 }
 
-PointerRNA rna_builtin_properties_lookup_string(PointerRNA *ptr, const char *key)
+int rna_builtin_properties_lookup_string(PointerRNA *ptr, const char *key, PointerRNA *r_ptr)
 {
        StructRNA *srna;
        PropertyRNA *prop;
@@ -315,7 +315,9 @@ PointerRNA rna_builtin_properties_lookup_string(PointerRNA *ptr, const char *key
                        if(prop) {
                                propptr.type= &RNA_Property;
                                propptr.data= prop;
-                               return propptr;
+
+                               *r_ptr= propptr;
+                               return TRUE;
                        }
                }
 
@@ -323,7 +325,9 @@ PointerRNA rna_builtin_properties_lookup_string(PointerRNA *ptr, const char *key
                        if(!(prop->flag & PROP_BUILTIN) && strcmp(prop->identifier, key)==0) {
                                propptr.type= &RNA_Property;
                                propptr.data= prop;
-                               return propptr;
+
+                               *r_ptr= propptr;
+                               return TRUE;
                        }
                }
        } while((srna=srna->base));
@@ -342,13 +346,15 @@ PointerRNA rna_builtin_properties_lookup_string(PointerRNA *ptr, const char *key
                                if(strcmp(idp->name, key) == 0) {
                                        propptr.type= &RNA_Property;
                                        propptr.data= idp;
-                                       return propptr;
+
+                                       *r_ptr= propptr;
+                                       return TRUE;
                                }
                        }
                }
        }
 #endif
-       return propptr;
+       return FALSE;
 }
 
 PointerRNA rna_builtin_type_get(PointerRNA *ptr)
@@ -842,34 +848,29 @@ static int rna_BlenderRNA_structs_length(PointerRNA *ptr)
 {
        return BLI_countlist(&((BlenderRNA*)ptr->data)->structs);
 }
-static PointerRNA rna_BlenderRNA_structs_lookup_int(PointerRNA *ptr, int index)
+static int rna_BlenderRNA_structs_lookup_int(PointerRNA *ptr, int index, PointerRNA *r_ptr)
 {
        StructRNA *srna= BLI_findlink(&((BlenderRNA*)ptr->data)->structs, index);
 
        if(srna) {
-               PointerRNA r_ptr;
-               RNA_pointer_create(NULL, &RNA_Struct, srna, &r_ptr);
-               return r_ptr;
+               RNA_pointer_create(NULL, &RNA_Struct, srna, r_ptr);
+               return TRUE;
        }
        else {
-               return PointerRNA_NULL;
+               return FALSE;
        }
 }
-static PointerRNA rna_BlenderRNA_structs_lookup_string(PointerRNA *ptr, const char *key)
+static int rna_BlenderRNA_structs_lookup_string(PointerRNA *ptr, const char *key, PointerRNA *r_ptr)
 {
        StructRNA *srna= ((BlenderRNA*)ptr->data)->structs.first;
-       for(; srna; srna=srna->cont.next)
-               if(key[0] == srna->identifier[0] && strcmp(key, srna->identifier)==0)
-                       break;
-
-       if(srna) {
-               PointerRNA r_ptr;
-               RNA_pointer_create(NULL, &RNA_Struct, srna, &r_ptr);
-               return r_ptr;
-       }
-       else {
-               return PointerRNA_NULL;
+       for(; srna; srna=srna->cont.next) {
+               if(key[0] == srna->identifier[0] && strcmp(key, srna->identifier)==0) {
+                       RNA_pointer_create(NULL, &RNA_Struct, srna, r_ptr);
+                       return TRUE;
+               }
        }
+
+       return FALSE;
 }
 
 
index 43b78dc..68345ef 100644 (file)
@@ -182,18 +182,19 @@ EnumPropertyItem image_type_items[] = {
 
 #include "RE_pipeline.h"
 
-static PointerRNA rna_Scene_object_bases_lookup_string(PointerRNA *ptr, const char *key)
+static int rna_Scene_object_bases_lookup_string(PointerRNA *ptr, const char *key, PointerRNA *r_ptr)
 {
        Scene *scene= (Scene*)ptr->data;
        Base *base;
 
        for(base= scene->base.first; base; base= base->next) {
                if(strncmp(base->object->id.name+2, key, sizeof(base->object->id.name)-2)==0) {
-                       return rna_pointer_inherit_refine(ptr, &RNA_ObjectBase, base);
+                       *r_ptr= rna_pointer_inherit_refine(ptr, &RNA_ObjectBase, base);
+                       return TRUE;
                }
        }
 
-       return PointerRNA_NULL;
+       return FALSE;
 }
 
 static PointerRNA rna_Scene_objects_get(CollectionPropertyIterator *iter)
index 123091c..fb6bfe8 100644 (file)
@@ -1422,8 +1422,9 @@ static PyObject *pyrna_prop_collection_subscript_int(BPy_PropertyRNA *self, Py_s
                if(RNA_property_collection_lookup_int(&self->ptr, self->prop, keynum, &newptr)) {
                        return pyrna_struct_CreatePyObject(&newptr);
                }
-               else { /* fail's if ptr.data == NULL, valid for mesh.materials */
-                       Py_RETURN_NONE;
+               else {
+                       PyErr_SetString(PyExc_RuntimeError, "error getting an rna struct from a collection");
+                       return NULL;
                }
        }
        PyErr_Format(PyExc_IndexError, "bpy_prop_collection[index]: index %d out of range, size %d", keynum, len);