diff options
author | Campbell Barton <ideasman42@gmail.com> | 2021-09-01 09:30:47 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2021-09-01 09:33:42 +0300 |
commit | 1730829592478f584d15f7874fa7e9cade1eb169 (patch) | |
tree | f7fbc703403a1f8675af9f7ae9966e7ae44d4f0a /source/blender/python/intern | |
parent | 5352b335983279a479abf22abe3a2d3d457597b4 (diff) |
Cleanup: move RNA utility functions into a generic module
Avoid having to include bpy_rna.h for enum utility functions,
recently added to idprop_py_ui_api.c.
Diffstat (limited to 'source/blender/python/intern')
-rw-r--r-- | source/blender/python/intern/bpy_capi_utils.c | 18 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_capi_utils.h | 2 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_gizmo_wrap.c | 2 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_msgbus.c | 1 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_operator.c | 1 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_props.c | 1 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_rna.c | 188 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_rna.h | 36 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_rna_anim.c | 1 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_rna_callback.c | 1 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_rna_id_collection.c | 1 |
11 files changed, 9 insertions, 243 deletions
diff --git a/source/blender/python/intern/bpy_capi_utils.c b/source/blender/python/intern/bpy_capi_utils.c index b2670161d92..8e9d8c02c03 100644 --- a/source/blender/python/intern/bpy_capi_utils.c +++ b/source/blender/python/intern/bpy_capi_utils.c @@ -38,24 +38,6 @@ #include "../generic/py_capi_utils.h" -char *BPy_enum_as_string(const EnumPropertyItem *item) -{ - DynStr *dynstr = BLI_dynstr_new(); - - /* We can't compare with the first element in the array - * since it may be a category (without an identifier). */ - for (bool is_first = true; item->identifier; item++) { - if (item->identifier[0]) { - BLI_dynstr_appendf(dynstr, is_first ? "'%s'" : ", '%s'", item->identifier); - is_first = false; - } - } - - char *cstring = BLI_dynstr_get_cstring(dynstr); - BLI_dynstr_free(dynstr); - return cstring; -} - short BPy_reports_to_error(ReportList *reports, PyObject *exception, const bool clear) { char *report_str; diff --git a/source/blender/python/intern/bpy_capi_utils.h b/source/blender/python/intern/bpy_capi_utils.h index d7ee1eedabb..80e92d918ac 100644 --- a/source/blender/python/intern/bpy_capi_utils.h +++ b/source/blender/python/intern/bpy_capi_utils.h @@ -31,8 +31,6 @@ extern "C" { struct EnumPropertyItem; struct ReportList; -char *BPy_enum_as_string(const struct EnumPropertyItem *item); - /* error reporting */ short BPy_reports_to_error(struct ReportList *reports, PyObject *exception, const bool clear); void BPy_reports_write_stdout(const struct ReportList *reports, const char *header); diff --git a/source/blender/python/intern/bpy_gizmo_wrap.c b/source/blender/python/intern/bpy_gizmo_wrap.c index a05ec6b7000..8d9b9e86589 100644 --- a/source/blender/python/intern/bpy_gizmo_wrap.c +++ b/source/blender/python/intern/bpy_gizmo_wrap.c @@ -41,6 +41,8 @@ #include "bpy_intern_string.h" #include "bpy_rna.h" +#include "../generic/py_capi_rna.h" + /* we may want to add, but not now */ /* -------------------------------------------------------------------- */ diff --git a/source/blender/python/intern/bpy_msgbus.c b/source/blender/python/intern/bpy_msgbus.c index 04220cf3105..0aff1249923 100644 --- a/source/blender/python/intern/bpy_msgbus.c +++ b/source/blender/python/intern/bpy_msgbus.c @@ -21,6 +21,7 @@ #include <Python.h> +#include "../generic/py_capi_rna.h" #include "../generic/py_capi_utils.h" #include "../generic/python_utildefines.h" #include "../mathutils/mathutils.h" diff --git a/source/blender/python/intern/bpy_operator.c b/source/blender/python/intern/bpy_operator.c index d5afb2334f0..a08b375ee61 100644 --- a/source/blender/python/intern/bpy_operator.c +++ b/source/blender/python/intern/bpy_operator.c @@ -32,6 +32,7 @@ #include "BLI_listbase.h" #include "BLI_utildefines.h" +#include "../generic/py_capi_rna.h" #include "../generic/py_capi_utils.h" #include "../generic/python_utildefines.h" #include "BPY_extern.h" diff --git a/source/blender/python/intern/bpy_props.c b/source/blender/python/intern/bpy_props.c index f89a4809587..ac624d365fa 100644 --- a/source/blender/python/intern/bpy_props.c +++ b/source/blender/python/intern/bpy_props.c @@ -46,6 +46,7 @@ #include "DNA_ID.h" /* MAX_IDPROP_NAME */ +#include "../generic/py_capi_rna.h" #include "../generic/py_capi_utils.h" /* -------------------------------------------------------------------- */ diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c index 19b2a7d1377..42db49b7688 100644 --- a/source/blender/python/intern/bpy_rna.c +++ b/source/blender/python/intern/bpy_rna.c @@ -74,6 +74,7 @@ #include "../generic/idprop_py_api.h" /* For IDprop lookups. */ #include "../generic/idprop_py_ui_api.h" +#include "../generic/py_capi_rna.h" #include "../generic/py_capi_utils.h" #include "../generic/python_utildefines.h" @@ -782,66 +783,6 @@ PyObject *pyrna_math_object_from_array(PointerRNA *ptr, PropertyRNA *prop) return ret; } -/** - * Same as #RNA_enum_value_from_id, but raises an exception. - */ -int pyrna_enum_value_from_id(const EnumPropertyItem *item, - const char *identifier, - int *r_value, - const char *error_prefix) -{ - if (RNA_enum_value_from_id(item, identifier, r_value) == 0) { - const char *enum_str = BPy_enum_as_string(item); - PyErr_Format( - PyExc_ValueError, "%s: '%.200s' not found in (%s)", error_prefix, identifier, enum_str); - MEM_freeN((void *)enum_str); - return -1; - } - - return 0; -} - -/** - * Use with #PyArg_ParseTuple's `O&` formatting. - */ -int pyrna_enum_value_parse_string(PyObject *o, void *p) -{ - const char *identifier = PyUnicode_AsUTF8(o); - if (identifier == NULL) { - PyErr_Format(PyExc_TypeError, "expected a string enum, not %.200s", Py_TYPE(o)->tp_name); - return 0; - } - struct BPy_EnumProperty_Parse *parse_data = p; - if (pyrna_enum_value_from_id( - parse_data->items, identifier, &parse_data->value, "enum identifier") == -1) { - return 0; - } - - parse_data->value_orig = o; - parse_data->is_set = true; - return 1; -} - -/** - * Use with #PyArg_ParseTuple's `O&` formatting. - */ -int pyrna_enum_bitfield_parse_set(PyObject *o, void *p) -{ - if (!PySet_Check(o)) { - PyErr_Format(PyExc_TypeError, "expected a set, not %.200s", Py_TYPE(o)->tp_name); - return 0; - } - - struct BPy_EnumProperty_Parse *parse_data = p; - if (pyrna_set_to_enum_bitfield( - parse_data->items, o, &parse_data->value, "enum identifier set") == -1) { - return 0; - } - parse_data->value_orig = o; - parse_data->is_set = true; - return 1; -} - /* NOTE(campbell): Regarding comparison `__cmp__`: * checking the 'ptr->data' matches works in almost all cases, * however there are a few RNA properties that are fake sub-structs and @@ -1312,115 +1253,6 @@ static int pyrna_string_to_enum( return 0; } -/** - * Takes a set of strings and map it to and array of booleans. - * - * Useful when the values aren't flags. - * - * \param type_convert_sign: Maps signed to unsigned range, - * needed when we want to use the full range of a signed short/char. - */ -BLI_bitmap *pyrna_set_to_enum_bitmap(const EnumPropertyItem *items, - PyObject *value, - int type_size, - bool type_convert_sign, - int bitmap_size, - const char *error_prefix) -{ - /* Set looping. */ - Py_ssize_t pos = 0; - Py_ssize_t hash = 0; - PyObject *key; - - BLI_bitmap *bitmap = BLI_BITMAP_NEW(bitmap_size, __func__); - - while (_PySet_NextEntry(value, &pos, &key, &hash)) { - const char *param = PyUnicode_AsUTF8(key); - if (param == NULL) { - PyErr_Format(PyExc_TypeError, - "%.200s expected a string, not %.200s", - error_prefix, - Py_TYPE(key)->tp_name); - goto error; - } - - int ret; - if (pyrna_enum_value_from_id(items, param, &ret, error_prefix) == -1) { - goto error; - } - - int index = ret; - - if (type_convert_sign) { - if (type_size == 2) { - union { - signed short as_signed; - ushort as_unsigned; - } ret_convert; - ret_convert.as_signed = (signed short)ret; - index = (int)ret_convert.as_unsigned; - } - else if (type_size == 1) { - union { - signed char as_signed; - uchar as_unsigned; - } ret_convert; - ret_convert.as_signed = (signed char)ret; - index = (int)ret_convert.as_unsigned; - } - else { - BLI_assert_unreachable(); - } - } - BLI_assert(index < bitmap_size); - BLI_BITMAP_ENABLE(bitmap, index); - } - - return bitmap; - -error: - MEM_freeN(bitmap); - return NULL; -} - -/* 'value' _must_ be a set type, error check before calling. */ -int pyrna_set_to_enum_bitfield(const EnumPropertyItem *items, - PyObject *value, - int *r_value, - const char *error_prefix) -{ - /* Set of enum items, concatenate all values with OR. */ - int ret, flag = 0; - - /* Set looping. */ - Py_ssize_t pos = 0; - Py_ssize_t hash = 0; - PyObject *key; - - *r_value = 0; - - while (_PySet_NextEntry(value, &pos, &key, &hash)) { - const char *param = PyUnicode_AsUTF8(key); - - if (param == NULL) { - PyErr_Format(PyExc_TypeError, - "%.200s expected a string, not %.200s", - error_prefix, - Py_TYPE(key)->tp_name); - return -1; - } - - if (pyrna_enum_value_from_id(items, param, &ret, error_prefix) == -1) { - return -1; - } - - flag |= ret; - } - - *r_value = flag; - return 0; -} - static int pyrna_prop_to_enum_bitfield( PointerRNA *ptr, PropertyRNA *prop, PyObject *value, int *r_value, const char *error_prefix) { @@ -1465,24 +1297,6 @@ static int pyrna_prop_to_enum_bitfield( return ret; } -PyObject *pyrna_enum_bitfield_to_py(const EnumPropertyItem *items, int value) -{ - PyObject *ret = PySet_New(NULL); - const char *identifier[RNA_ENUM_BITFLAG_SIZE + 1]; - - if (RNA_enum_bitflag_identifiers(items, value, identifier)) { - PyObject *item; - int index; - for (index = 0; identifier[index]; index++) { - item = PyUnicode_FromString(identifier[index]); - PySet_Add(ret, item); - Py_DECREF(item); - } - } - - return ret; -} - static PyObject *pyrna_enum_to_py(PointerRNA *ptr, PropertyRNA *prop, int val) { PyObject *item, *ret = NULL; diff --git a/source/blender/python/intern/bpy_rna.h b/source/blender/python/intern/bpy_rna.h index fd468bed470..f6a05a86311 100644 --- a/source/blender/python/intern/bpy_rna.h +++ b/source/blender/python/intern/bpy_rna.h @@ -202,42 +202,6 @@ int pyrna_pydict_to_props(PointerRNA *ptr, const char *error_prefix); PyObject *pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop); -uint *pyrna_set_to_enum_bitmap(const struct EnumPropertyItem *items, - PyObject *value, - int type_size, - bool type_convert_sign, - int bitmap_size, - const char *error_prefix); -PyObject *pyrna_enum_bitfield_to_py(const struct EnumPropertyItem *items, int value); -int pyrna_set_to_enum_bitfield(const struct EnumPropertyItem *items, - PyObject *value, - int *r_value, - const char *error_prefix); - -/** - * Data for #pyrna_enum_value_parse_string & #pyrna_enum_bitfield_parse_set parsing utilities. - * Use with #PyArg_ParseTuple's `O&` formatting. - */ -struct BPy_EnumProperty_Parse { - const EnumPropertyItem *items; - /** - * Set when the value was successfully parsed. - * Useful if the input ever needs to be included in an error message. - * (if the value is not supported under certain conditions). - */ - PyObject *value_orig; - - int value; - bool is_set; -}; -int pyrna_enum_value_parse_string(PyObject *o, void *p); -int pyrna_enum_bitfield_parse_set(PyObject *o, void *p); - -int pyrna_enum_value_from_id(const EnumPropertyItem *item, - const char *identifier, - int *value, - const char *error_prefix); - int pyrna_deferred_register_class(struct StructRNA *srna, PyTypeObject *py_class); void pyrna_struct_type_extend_capi(struct StructRNA *srna, diff --git a/source/blender/python/intern/bpy_rna_anim.c b/source/blender/python/intern/bpy_rna_anim.c index a716f4ab9e5..4063bb702bb 100644 --- a/source/blender/python/intern/bpy_rna_anim.c +++ b/source/blender/python/intern/bpy_rna_anim.c @@ -54,6 +54,7 @@ #include "bpy_rna.h" #include "bpy_rna_anim.h" +#include "../generic/py_capi_rna.h" #include "../generic/python_utildefines.h" #include "DEG_depsgraph_build.h" diff --git a/source/blender/python/intern/bpy_rna_callback.c b/source/blender/python/intern/bpy_rna_callback.c index adc0f425b52..c19d9ba47b1 100644 --- a/source/blender/python/intern/bpy_rna_callback.c +++ b/source/blender/python/intern/bpy_rna_callback.c @@ -23,6 +23,7 @@ #include <Python.h> +#include "../generic/py_capi_rna.h" #include "../generic/python_utildefines.h" #include "DNA_space_types.h" diff --git a/source/blender/python/intern/bpy_rna_id_collection.c b/source/blender/python/intern/bpy_rna_id_collection.c index 7bdb0e30410..4f3480ddc13 100644 --- a/source/blender/python/intern/bpy_rna_id_collection.c +++ b/source/blender/python/intern/bpy_rna_id_collection.c @@ -45,6 +45,7 @@ #include "bpy_capi_utils.h" #include "bpy_rna_id_collection.h" +#include "../generic/py_capi_rna.h" #include "../generic/py_capi_utils.h" #include "../generic/python_utildefines.h" |