diff options
author | Campbell Barton <ideasman42@gmail.com> | 2009-12-07 05:20:55 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2009-12-07 05:20:55 +0300 |
commit | 764c4c94fa85d135cf7c54e49ccb7647a246fb18 (patch) | |
tree | bd9654841df31c3a789ef4626479ee714b23f98e /source | |
parent | 7f590d3a4636a2c2891dcaf2e0b833582db8f191 (diff) |
use sets rather then tuples for enum/flags so you can use bitfield operators
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/python/intern/bpy_rna.c | 56 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_util.c | 6 |
2 files changed, 36 insertions, 26 deletions
diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c index 9136a166365..f8e87912f1e 100644 --- a/source/blender/python/intern/bpy_rna.c +++ b/source/blender/python/intern/bpy_rna.c @@ -381,23 +381,22 @@ static int pyrna_string_to_enum(PyObject *item, PointerRNA *ptr, PropertyRNA *pr static PyObject *pyrna_enum_to_py(PointerRNA *ptr, PropertyRNA *prop, int val) { - PyObject *ret= NULL; + PyObject *item, *ret= NULL; if(RNA_property_flag(prop) & PROP_ENUM_FLAG) { const char *identifier[RNA_ENUM_BITFLAG_SIZE + 1]; - int index; - if ((index=RNA_property_enum_bitflag_identifiers(BPy_GetContext(), ptr, prop, val, identifier))) { - ret= PyTuple_New(index); - index= 0; + ret= PySet_New(NULL); + + if (RNA_property_enum_bitflag_identifiers(BPy_GetContext(), ptr, prop, val, identifier)) { + int index; - while(identifier[index]) { - PyTuple_SET_ITEM(ret, index, PyUnicode_FromString(identifier[index])); - index++; + for(index=0; identifier[index]; index++) { + item= PyUnicode_FromString(identifier[index]); + PySet_Add(ret, item); + Py_DECREF(item); } - } - else { - ret= PyTuple_New(0); + } } else { @@ -559,7 +558,7 @@ int pyrna_pydict_to_props(PointerRNA *ptr, PyObject *kw, int all_args, const cha static PyObject * pyrna_func_call(PyObject *self, PyObject *args, PyObject *kw); -PyObject *pyrna_func_to_py(BPy_DummyPointerRNA *pyrna, FunctionRNA *func) +static PyObject *pyrna_func_to_py(BPy_DummyPointerRNA *pyrna, FunctionRNA *func) { static PyMethodDef func_meth = {"<generic rna function>", (PyCFunction)pyrna_func_call, METH_VARARGS|METH_KEYWORDS, "python rna function"}; PyObject *self; @@ -584,6 +583,7 @@ PyObject *pyrna_func_to_py(BPy_DummyPointerRNA *pyrna, FunctionRNA *func) } + int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *value, const char *error_prefix) { /* XXX hard limits should be checked here */ @@ -678,28 +678,36 @@ int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyObject *v } case PROP_ENUM: { - int val, i; + int val= 0, tmpval; if (PyUnicode_Check(value)) { if (!pyrna_string_to_enum(value, ptr, prop, &val, error_prefix)) return -1; } - else if (PyTuple_Check(value)) { - /* tuple of enum items, concatenate all values with OR */ - val= 0; - for (i= 0; i < PyTuple_Size(value); i++) { - int tmpval; - - /* PyTuple_GET_ITEM returns a borrowed reference */ - if (!pyrna_string_to_enum(PyTuple_GET_ITEM(value, i), ptr, prop, &tmpval, error_prefix)) - return -1; + else if (PyAnySet_Check(value)) { + if(RNA_property_flag(prop) & PROP_ENUM_FLAG) { + /* set of enum items, concatenate all values with OR */ + + /* set looping */ + Py_ssize_t pos = 0; + PyObject *key; + long hash; - val |= tmpval; + while (_PySet_NextEntry(value, &pos, &key, &hash)) { + if (!pyrna_string_to_enum(key, ptr, prop, &tmpval, error_prefix)) + return -1; + + val |= tmpval; + } + } + else { + PyErr_Format(PyExc_TypeError, "%.200s, %.200s.%.200s is not a bitflag enum type", error_prefix, RNA_struct_identifier(ptr->type), RNA_property_identifier(prop)); + return -1; } } else { char *enum_str= pyrna_enum_as_string(ptr, prop); - PyErr_Format(PyExc_TypeError, "%.200s expected a string enum or a tuple of strings in (%.200s)", error_prefix, enum_str); + PyErr_Format(PyExc_TypeError, "%.200s expected a string enum or a set of strings in (%.200s)", error_prefix, enum_str); MEM_freeN(enum_str); return -1; } diff --git a/source/blender/python/intern/bpy_util.c b/source/blender/python/intern/bpy_util.c index cd53ba9c069..db3798146d3 100644 --- a/source/blender/python/intern/bpy_util.c +++ b/source/blender/python/intern/bpy_util.c @@ -92,11 +92,13 @@ int BPY_flag_from_seq(BPY_flag_def *flagdef, PyObject *seq, int *flag) if(cstring) { fd= flagdef; while(fd->name) { - if (strcmp(cstring, fd->name) == 0) + if (strcmp(cstring, fd->name) == 0) { (*flag) |= fd->flag; + break; + } fd++; } - if (fd==NULL) { /* could not find a match */ + if (fd->name==NULL) { /* could not find a match */ error_val= 1; } } else { |