From e3a18a890d794538f82ccce3d56c595b33f1a4d9 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 15 Feb 2022 12:51:31 +1100 Subject: Cleanup: minor changes to Python argument parsing loop - Increment the argument index at the end of the loop. Otherwise using the index after incrementing required subtracting 1. - Move error prefix creation into a function: `pyrna_func_error_prefix` so it's possible to create an error prefix without duplicate code. This simplifies further changes for argument parsing from D14047. --- source/blender/python/intern/bpy_rna.c | 73 ++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 29 deletions(-) (limited to 'source') diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c index 3654dbbcf29..2e0dfc515d1 100644 --- a/source/blender/python/intern/bpy_rna.c +++ b/source/blender/python/intern/bpy_rna.c @@ -6017,6 +6017,36 @@ static PyObject *small_dict_get_item_string(PyObject *dict, const char *key_look return NULL; } +/** + * \param parm_index: The argument index or -1 for keyword arguments. + */ +static void pyrna_func_error_prefix(BPy_FunctionRNA *self, + PropertyRNA *parm, + const int parm_index, + char *error, + const size_t error_size) +{ + PointerRNA *self_ptr = &self->ptr; + FunctionRNA *self_func = self->func; + if (parm_index == -1) { + BLI_snprintf(error, + error_size, + "%.200s.%.200s(): error with keyword argument \"%.200s\" - ", + RNA_struct_identifier(self_ptr->type), + RNA_function_identifier(self_func), + RNA_property_identifier(parm)); + } + else { + BLI_snprintf(error, + error_size, + "%.200s.%.200s(): error with argument %d, \"%.200s\" - ", + RNA_struct_identifier(self_ptr->type), + RNA_function_identifier(self_func), + parm_index + 1, + RNA_property_identifier(parm)); + } +} + static PyObject *pyrna_func_call(BPy_FunctionRNA *self, PyObject *args, PyObject *kw) { /* NOTE: both BPy_StructRNA and BPy_PropertyRNA can be used here. */ @@ -6143,8 +6173,6 @@ static PyObject *pyrna_func_call(BPy_FunctionRNA *self, PyObject *args, PyObject kw_arg = true; } - i++; /* Current argument. */ - if (item == NULL) { if (flag_parameter & PARM_REQUIRED) { PyErr_Format(PyExc_TypeError, @@ -6156,46 +6184,33 @@ static PyObject *pyrna_func_call(BPy_FunctionRNA *self, PyObject *args, PyObject break; } /* PyDict_GetItemString won't raise an error. */ - continue; } + else { #ifdef DEBUG_STRING_FREE - if (item) { - if (PyUnicode_Check(item)) { - PyList_APPEND(string_free_ls, PyUnicode_FromString(PyUnicode_AsUTF8(item))); + if (item) { + if (PyUnicode_Check(item)) { + PyList_APPEND(string_free_ls, PyUnicode_FromString(PyUnicode_AsUTF8(item))); + } } - } #endif - err = pyrna_py_to_prop(&funcptr, parm, iter.data, item, ""); - if (err != 0) { /* the error generated isn't that useful, so generate it again with a useful prefix * could also write a function to prepend to error messages */ char error_prefix[512]; - PyErr_Clear(); /* Re-raise. */ - if (kw_arg == true) { - BLI_snprintf(error_prefix, - sizeof(error_prefix), - "%.200s.%.200s(): error with keyword argument \"%.200s\" - ", - RNA_struct_identifier(self_ptr->type), - RNA_function_identifier(self_func), - RNA_property_identifier(parm)); - } - else { - BLI_snprintf(error_prefix, - sizeof(error_prefix), - "%.200s.%.200s(): error with argument %d, \"%.200s\" - ", - RNA_struct_identifier(self_ptr->type), - RNA_function_identifier(self_func), - i, - RNA_property_identifier(parm)); - } + err = pyrna_py_to_prop(&funcptr, parm, iter.data, item, ""); - pyrna_py_to_prop(&funcptr, parm, iter.data, item, error_prefix); + if (err != 0) { + PyErr_Clear(); /* Re-raise. */ + pyrna_func_error_prefix(self, parm, kw_arg ? -1 : i, error_prefix, sizeof(error_prefix)); + pyrna_py_to_prop(&funcptr, parm, iter.data, item, error_prefix); - break; + break; + } } + + i++; /* Current argument. */ } RNA_parameter_list_end(&iter); -- cgit v1.2.3