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.
This commit is contained in:
@@ -6017,6 +6017,36 @@ static PyObject *small_dict_get_item_string(PyObject *dict, const char *key_look
|
|||||||
return NULL;
|
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)
|
static PyObject *pyrna_func_call(BPy_FunctionRNA *self, PyObject *args, PyObject *kw)
|
||||||
{
|
{
|
||||||
/* NOTE: both BPy_StructRNA and BPy_PropertyRNA can be used here. */
|
/* 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;
|
kw_arg = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
i++; /* Current argument. */
|
|
||||||
|
|
||||||
if (item == NULL) {
|
if (item == NULL) {
|
||||||
if (flag_parameter & PARM_REQUIRED) {
|
if (flag_parameter & PARM_REQUIRED) {
|
||||||
PyErr_Format(PyExc_TypeError,
|
PyErr_Format(PyExc_TypeError,
|
||||||
@@ -6156,46 +6184,33 @@ static PyObject *pyrna_func_call(BPy_FunctionRNA *self, PyObject *args, PyObject
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
/* PyDict_GetItemString won't raise an error. */
|
/* PyDict_GetItemString won't raise an error. */
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
#ifdef DEBUG_STRING_FREE
|
#ifdef DEBUG_STRING_FREE
|
||||||
if (item) {
|
if (item) {
|
||||||
if (PyUnicode_Check(item)) {
|
if (PyUnicode_Check(item)) {
|
||||||
PyList_APPEND(string_free_ls, PyUnicode_FromString(PyUnicode_AsUTF8(item)));
|
PyList_APPEND(string_free_ls, PyUnicode_FromString(PyUnicode_AsUTF8(item)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
#endif
|
#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
|
/* 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 */
|
* could also write a function to prepend to error messages */
|
||||||
char error_prefix[512];
|
char error_prefix[512];
|
||||||
PyErr_Clear(); /* Re-raise. */
|
|
||||||
|
|
||||||
if (kw_arg == true) {
|
err = pyrna_py_to_prop(&funcptr, parm, iter.data, item, "");
|
||||||
BLI_snprintf(error_prefix,
|
|
||||||
sizeof(error_prefix),
|
if (err != 0) {
|
||||||
"%.200s.%.200s(): error with keyword argument \"%.200s\" - ",
|
PyErr_Clear(); /* Re-raise. */
|
||||||
RNA_struct_identifier(self_ptr->type),
|
pyrna_func_error_prefix(self, parm, kw_arg ? -1 : i, error_prefix, sizeof(error_prefix));
|
||||||
RNA_function_identifier(self_func),
|
pyrna_py_to_prop(&funcptr, parm, iter.data, item, error_prefix);
|
||||||
RNA_property_identifier(parm));
|
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
pyrna_py_to_prop(&funcptr, parm, iter.data, item, error_prefix);
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
i++; /* Current argument. */
|
||||||
}
|
}
|
||||||
|
|
||||||
RNA_parameter_list_end(&iter);
|
RNA_parameter_list_end(&iter);
|
||||||
|
|||||||
Reference in New Issue
Block a user