diff options
author | Campbell Barton <ideasman42@gmail.com> | 2010-02-27 18:28:34 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2010-02-27 18:28:34 +0300 |
commit | c76b6fcb06c45f0e018a6c8a892f380099a2371a (patch) | |
tree | 5dd51522c47dcdfeede000fe1d7a9c95844bdbca /source/blender | |
parent | 3dd3e7321eee426112859bfc2427749c2f6113e6 (diff) |
utility function pyrna_enum_value_from_id for getting the enum from a string and raising an error if its invalid.
Diffstat (limited to 'source/blender')
-rw-r--r-- | source/blender/python/intern/bpy_rna.c | 20 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_rna.h | 2 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_rna_callback.c | 13 |
3 files changed, 22 insertions, 13 deletions
diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c index 6f36a7e12e2..2b31f06f51b 100644 --- a/source/blender/python/intern/bpy_rna.c +++ b/source/blender/python/intern/bpy_rna.c @@ -155,6 +155,19 @@ Mathutils_Callback mathutils_rna_matrix_cb = { (BaseMathSetIndexFunc) NULL }; +/* same as RNA_enum_value_from_id but raises an exception */ +int pyrna_enum_value_from_id(EnumPropertyItem *item, const char *identifier, int *value, const char *error_prefix) +{ + if(RNA_enum_value_from_id(item, identifier, value) == 0) { + char *enum_str= BPy_enum_as_string(item); + PyErr_Format(PyExc_TypeError, "%s: '%.200s' not found in (%s)", error_prefix, identifier, enum_str); + MEM_freeN(enum_str); + return -1; + } + + return 0; +} + #define PROP_ALL_VECTOR_SUBTYPES PROP_TRANSLATION: case PROP_DIRECTION: case PROP_VELOCITY: case PROP_ACCELERATION: case PROP_XYZ: case PROP_XYZ|PROP_UNIT_LENGTH PyObject *pyrna_math_object_from_array(PointerRNA *ptr, PropertyRNA *prop) @@ -478,13 +491,8 @@ int pyrna_set_to_enum_bitfield(EnumPropertyItem *items, PyObject *value, int *r_ PyErr_Format(PyExc_TypeError, "%.200s expected a string. found a %.200s", error_prefix, Py_TYPE(key)->tp_name); return -1; } - - if(RNA_enum_value_from_id(items, param, &ret) == 0) { - char *enum_str= BPy_enum_as_string(items); - PyErr_Format(PyExc_TypeError, "%.200s \"%.200s\" not found in (%.200s)", error_prefix, param, enum_str); - MEM_freeN(enum_str); + if(pyrna_enum_value_from_id(items, param, &ret, error_prefix) < 0) return -1; - } flag |= ret; } diff --git a/source/blender/python/intern/bpy_rna.h b/source/blender/python/intern/bpy_rna.h index 77f6ec00d48..770e88e1a1d 100644 --- a/source/blender/python/intern/bpy_rna.h +++ b/source/blender/python/intern/bpy_rna.h @@ -84,6 +84,8 @@ PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop); PyObject *pyrna_enum_bitfield_to_py(struct EnumPropertyItem *items, int value); int pyrna_set_to_enum_bitfield(EnumPropertyItem *items, PyObject *value, int *r_value, const char *error_prefix); +int pyrna_enum_value_from_id(EnumPropertyItem *item, const char *identifier, int *value, const char *error_prefix); + /* function for registering types */ PyObject *pyrna_basetype_register(PyObject *self, PyObject *args); PyObject *pyrna_basetype_unregister(PyObject *self, PyObject *args); diff --git a/source/blender/python/intern/bpy_rna_callback.c b/source/blender/python/intern/bpy_rna_callback.c index dda3671ebeb..ee0c5cb143b 100644 --- a/source/blender/python/intern/bpy_rna_callback.c +++ b/source/blender/python/intern/bpy_rna_callback.c @@ -66,19 +66,18 @@ PyObject *pyrna_callback_add(BPy_StructRNA *self, PyObject *args) void *handle; PyObject *cb_func, *cb_args; - char *cb_type= NULL; - int cb_type_enum; + char *cb_event_str= NULL; + int cb_event; - if (!PyArg_ParseTuple(args, "OO|s:callback_add", &cb_func, &cb_args, &cb_type)) + if (!PyArg_ParseTuple(args, "OO|s:bpy_struct.callback_add", &cb_func, &cb_args, &cb_event_str)) return NULL; if(RNA_struct_is_a(self->ptr.type, &RNA_Region)) { - if(RNA_enum_value_from_id(region_draw_mode_items, cb_type, &cb_type_enum)==0) { - PyErr_SetString(PyExc_ValueError, "callbcak_add(): enum invalid type"); + + if(pyrna_enum_value_from_id(region_draw_mode_items, cb_event_str, &cb_event, "bpy_struct.callback_add()") < 0) return NULL; - } - handle= ED_region_draw_cb_activate(((ARegion *)self->ptr.data)->type, cb_region_draw, (void *)args, cb_type_enum); + handle= ED_region_draw_cb_activate(((ARegion *)self->ptr.data)->type, cb_region_draw, (void *)args, cb_event); Py_INCREF(args); } else { |