From 972a697f822b65cc1887c305dd62e54d5b19d3e3 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Wed, 20 Apr 2022 13:17:16 +1000 Subject: PyAPI: improve deprecation warning for bpy.ops context override - Increase the stack level so the reported line number references script authors code (not Blender's wrapper function). - Include the operator name and poll/call usage in the warning. --- source/blender/python/intern/bpy_operator.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/source/blender/python/intern/bpy_operator.c b/source/blender/python/intern/bpy_operator.c index 0cfe6dab2f5..95879b02295 100644 --- a/source/blender/python/intern/bpy_operator.c +++ b/source/blender/python/intern/bpy_operator.c @@ -60,12 +60,17 @@ static wmOperatorType *ot_lookup_from_py_string(PyObject *value, const char *py_ return ot; } -static void op_context_override_deprecated_warning(void) +static void op_context_override_deprecated_warning(const char *action, const char *opname) { - if (PyErr_WarnEx(PyExc_DeprecationWarning, - "Passing in context overrides is deprecated in favor of " - "Context.temp_override(..)", - 1) < 0) { + if (PyErr_WarnFormat( + PyExc_DeprecationWarning, + /* Use stack level 2 as this call is wrapped by `release/scripts/modules/bpy/ops.py`, + * An extra stack level is needed to show the warning in the authors script. */ + 2, + "Passing in context overrides is deprecated in favor of " + "Context.temp_override(..), %s \"%s\"", + action, + opname) < 0) { /* The function has no return value, the exception cannot * be reported to the caller, so just log it. */ PyErr_WriteUnraisable(NULL); @@ -126,7 +131,7 @@ static PyObject *pyop_poll(PyObject *UNUSED(self), PyObject *args) context_dict = NULL; } else if (PyDict_Check(context_dict)) { - op_context_override_deprecated_warning(); + op_context_override_deprecated_warning("polling", opname); } else { PyErr_Format(PyExc_TypeError, @@ -234,7 +239,7 @@ static PyObject *pyop_call(PyObject *UNUSED(self), PyObject *args) context_dict = NULL; } else if (PyDict_Check(context_dict)) { - op_context_override_deprecated_warning(); + op_context_override_deprecated_warning("calling", opname); } else { PyErr_Format(PyExc_TypeError, -- cgit v1.2.3