diff options
author | Campbell Barton <ideasman42@gmail.com> | 2011-06-03 07:19:22 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2011-06-03 07:19:22 +0400 |
commit | 5332c602c9f6c492e897779accc5e52496017809 (patch) | |
tree | 543c1f87db44da5e8e218484594e6b42487d94ea /source/blender/python/intern | |
parent | 60f5b51484a2ab748fccca85b01b1a45be760912 (diff) |
pre-allocate the array when converting py/rna enums, also fix for memory leak with bad values.
Diffstat (limited to 'source/blender/python/intern')
-rw-r--r-- | source/blender/python/intern/bpy_props.c | 32 |
1 files changed, 24 insertions, 8 deletions
diff --git a/source/blender/python/intern/bpy_props.c b/source/blender/python/intern/bpy_props.c index 352f7408961..99021ab5f18 100644 --- a/source/blender/python/intern/bpy_props.c +++ b/source/blender/python/intern/bpy_props.c @@ -644,7 +644,9 @@ static PyObject *BPy_StringProperty(PyObject *self, PyObject *args, PyObject *kw 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 @@ static EnumPropertyItem *enum_items_from_py(PyObject *seq_fast, PyObject *def, i /* 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 @@ static EnumPropertyItem *enum_items_from_py(PyObject *seq_fast, PyObject *def, i 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 @@ static EnumPropertyItem *enum_items_from_py(PyObject *seq_fast, PyObject *def, i } return items; + +# undef PREALLOC_ENUM } static EnumPropertyItem *bpy_props_enum_itemf(struct bContext *C, PointerRNA *ptr, PropertyRNA *prop, int *free) |