From d06b03f80da5ce7a5c833f25be783df3b4bc4c00 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 30 Jul 2021 16:04:00 +1000 Subject: PyAPI: include the property name & type in registration errors This gives useful context in errors, also remove newline endings from exceptions. --- source/blender/python/intern/bpy_props.h | 5 ++++- source/blender/python/intern/bpy_rna.c | 27 +++++++++++++++++++-------- 2 files changed, 23 insertions(+), 9 deletions(-) (limited to 'source/blender/python') diff --git a/source/blender/python/intern/bpy_props.h b/source/blender/python/intern/bpy_props.h index bc7f0cfe416..e6bbd6f708d 100644 --- a/source/blender/python/intern/bpy_props.h +++ b/source/blender/python/intern/bpy_props.h @@ -33,7 +33,10 @@ StructRNA *pointer_type_from_py(PyObject *value, const char *error_prefix); typedef struct { PyObject_HEAD - /* This isn't GC tracked, it's a function from `bpy.props` so it's not going away. */ + /** + * Internally a #PyCFunctionObject type. + * \note This isn't GC tracked, it's a function from `bpy.props` so it's not going away. + */ void *fn; PyObject *kw; } BPy_PropDeferred; diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c index 759630b73ac..dff96f74d62 100644 --- a/source/blender/python/intern/bpy_rna.c +++ b/source/blender/python/intern/bpy_rna.c @@ -8040,14 +8040,21 @@ static int deferred_register_prop(StructRNA *srna, PyObject *key, PyObject *item PyObject *py_kw = ((BPy_PropDeferred *)item)->kw; PyObject *py_srna_cobject, *py_ret; + /* Show the function name in errors to help give context. */ + BLI_assert(PyCFunction_CheckExact(py_func)); + PyMethodDef *py_func_method_def = ((PyCFunctionObject *)py_func)->m_ml; + const char *func_name = py_func_method_def->ml_name; + PyObject *args_fake; + const char *key_str = PyUnicode_AsUTF8(key); - if (*PyUnicode_AsUTF8(key) == '_') { + if (*key_str == '_') { PyErr_Format(PyExc_ValueError, "bpy_struct \"%.200s\" registration error: " - "%.200s could not register because the property starts with an '_'\n", + "'%.200s' %.200s could not register because it starts with an '_'", RNA_struct_identifier(srna), - PyUnicode_AsUTF8(key)); + key_str, + func_name); return -1; } py_srna_cobject = PyCapsule_New(srna, NULL, NULL); @@ -8066,8 +8073,12 @@ static int deferred_register_prop(StructRNA *srna, PyObject *key, PyObject *item *(PyCFunctionWithKeywords)PyCFunction_GET_FUNCTION(py_func) == BPy_CollectionProperty) && RNA_struct_idprops_contains_datablock(type_srna)) { PyErr_Format(PyExc_ValueError, - "bpy_struct \"%.200s\" doesn't support datablock properties\n", - RNA_struct_identifier(srna)); + "bpy_struct \"%.200s\" registration error: " + "'%.200s' %.200s could not register because " + "this type doesn't support data-block properties", + RNA_struct_identifier(srna), + key_str, + func_name); return -1; } } @@ -8085,12 +8096,12 @@ static int deferred_register_prop(StructRNA *srna, PyObject *key, PyObject *item Py_DECREF(args_fake); /* Free's py_srna_cobject too. */ - // PyC_LineSpit(); PyErr_Format(PyExc_ValueError, "bpy_struct \"%.200s\" registration error: " - "%.200s could not register\n", + "'%.200s' %.200s could not register (see previous error)", RNA_struct_identifier(srna), - PyUnicode_AsUTF8(key)); + key_str, + func_name); return -1; } -- cgit v1.2.3