[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [37113] trunk/blender/source/blender/ python/intern/bpy_props.c: pre-allocate the array when converting py/ rna enums, also fix for memory leak with bad values.

Campbell Barton ideasman42 at gmail.com
Fri Jun 3 05:19:24 CEST 2011


Revision: 37113
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=37113
Author:   campbellbarton
Date:     2011-06-03 03:19:22 +0000 (Fri, 03 Jun 2011)
Log Message:
-----------
pre-allocate the array when converting py/rna enums, also fix for memory leak with bad values.

Modified Paths:
--------------
    trunk/blender/source/blender/python/intern/bpy_props.c

Modified: trunk/blender/source/blender/python/intern/bpy_props.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_props.c	2011-06-03 00:11:54 UTC (rev 37112)
+++ trunk/blender/source/blender/python/intern/bpy_props.c	2011-06-03 03:19:22 UTC (rev 37113)
@@ -644,7 +644,9 @@
 
 static EnumPropertyItem *enum_items_from_py(PyObject *seq_fast, PyObject *def, int *defvalue, const short is_enum_flag)
 {
-	EnumPropertyItem *items= NULL;
+#	define PREALLOC_ENUM
+
+	EnumPropertyItem *items;
 	PyObject *item;
 	int seq_len, i, totitem= 0;
 	short def_used= 0;
@@ -681,18 +683,23 @@
 	/* blank value */
 	*defvalue= 0;
 
+#ifdef PREALLOC_ENUM
+	items= MEM_callocN(sizeof(EnumPropertyItem) * (seq_len + 1), "RNA_enum_items_reserve");
+#endif
+
 	for(i=0; i<seq_len; i++) {
 		EnumPropertyItem tmp= {0, "", 0, "", ""};
 
 		item= PySequence_Fast_GET_ITEM(seq_fast, i);
-		if(PyTuple_Check(item)==0) {
-			PyErr_SetString(PyExc_TypeError, "EnumProperty(...): expected a sequence of tuples for the enum items");
-			if(items) MEM_freeN(items);
-			return NULL;
-		}
 
+		/* this also checks for a tuple */
 		if(!PyArg_ParseTuple(item, "sss", &tmp.identifier, &tmp.name, &tmp.description)) {
-			PyErr_SetString(PyExc_TypeError, "EnumProperty(...): expected an identifier, name and description in the tuple");
+#ifdef PREALLOC_ENUM
+			MEM_freeN(items);
+#else
+			if(items) MEM_freeN(items);
+#endif
+			PyErr_SetString(PyExc_TypeError, "EnumProperty(...): expected an tuple containing (identifier, name description)");
 			return NULL;
 		}
 
@@ -712,11 +719,18 @@
 				def_used++; /* only ever 1 */
 			}
 		}
-
+#ifdef PREALLOC_ENUM
+		items[i]= tmp;
+#else
 		RNA_enum_item_add(&items, &totitem, &tmp);
+#endif
 	}
 
+#ifdef PREALLOC_ENUM
+	/* do nohing */
+#else
 	RNA_enum_item_end(&items, &totitem);
+#endif
 
 	if(is_enum_flag) {
 		/* strict check that all set members were used */
@@ -741,6 +755,8 @@
 	}
 
 	return items;
+
+#	undef PREALLOC_ENUM
 }
 
 static EnumPropertyItem *bpy_props_enum_itemf(struct bContext *C, PointerRNA *ptr, PropertyRNA *prop, int *free)




More information about the Bf-blender-cvs mailing list