utility function pyrna_enum_value_from_id for getting the enum from a string and raising an error if its invalid.

This commit is contained in:
2010-02-27 15:28:34 +00:00
parent 3dd3e7321e
commit c76b6fcb06
3 changed files with 23 additions and 14 deletions

View File

@@ -155,6 +155,19 @@ Mathutils_Callback mathutils_rna_matrix_cb = {
(BaseMathSetIndexFunc) NULL (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 #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) 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); PyErr_Format(PyExc_TypeError, "%.200s expected a string. found a %.200s", error_prefix, Py_TYPE(key)->tp_name);
return -1; return -1;
} }
if(pyrna_enum_value_from_id(items, param, &ret, error_prefix) < 0)
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);
return -1; return -1;
}
flag |= ret; flag |= ret;
} }

View File

@@ -84,6 +84,8 @@ PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop);
PyObject *pyrna_enum_bitfield_to_py(struct EnumPropertyItem *items, int value); 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_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 */ /* function for registering types */
PyObject *pyrna_basetype_register(PyObject *self, PyObject *args); PyObject *pyrna_basetype_register(PyObject *self, PyObject *args);
PyObject *pyrna_basetype_unregister(PyObject *self, PyObject *args); PyObject *pyrna_basetype_unregister(PyObject *self, PyObject *args);

View File

@@ -66,19 +66,18 @@ PyObject *pyrna_callback_add(BPy_StructRNA *self, PyObject *args)
void *handle; void *handle;
PyObject *cb_func, *cb_args; PyObject *cb_func, *cb_args;
char *cb_type= NULL; char *cb_event_str= NULL;
int cb_type_enum; 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; return NULL;
if(RNA_struct_is_a(self->ptr.type, &RNA_Region)) { 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");
return NULL;
}
handle= ED_region_draw_cb_activate(((ARegion *)self->ptr.data)->type, cb_region_draw, (void *)args, cb_type_enum); 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_event);
Py_INCREF(args); Py_INCREF(args);
} }
else { else {