PyAPI: support logging for Context.temp_override

Adds comprehensive logging system for temp_override context manager to
help developers debug "context is incorrect" operator poll failures.
The logging tracks all context member access during temp_override
sessions and provides detailed summaries to identify context
availability issues.

Features:
- Command-line logging: `./blender --log-level trace --log "context" `
- Python programmatic control: `temp_override_handle.logging_set(True)`
- C-level API: CTX_temp_override_logging_set/get() functions
- Tracks individual member access
- Uses CLOG logging infrastructure

The logging helps identify which context members are accessed during
temp_override sessions and whether they return valid data, making it
easier to debug operator poll functions that fail with context errors.

Ref !144810
This commit is contained in:
Nick Alberelli
2025-09-19 06:48:07 +00:00
committed by Campbell Barton
parent 20bea06f4a
commit 439fe8a1a0
5 changed files with 275 additions and 14 deletions

View File

@@ -23,6 +23,7 @@
#include "bpy_rna_context.hh"
#include "../generic/py_capi_utils.hh"
#include "../generic/python_compat.hh" /* IWYU pragma: keep. */
#include "RNA_access.hh"
@@ -106,6 +107,14 @@ static bool wm_check_region_exists(const bScreen *screen,
return false;
}
/**
* Helper function to configure context logging with extensible options.
*/
static void bpy_rna_context_logging_set(bContext *C, bool enable)
{
CTX_member_logging_set(C, enable);
}
/** \} */
/* -------------------------------------------------------------------- */
@@ -121,6 +130,9 @@ struct ContextStore {
bool area_is_set;
ARegion *region;
bool region_is_set;
/** User's desired logging state for this temp_override instance (can be changed at runtime). */
bool use_logging;
};
struct BPyContextTempOverride {
@@ -289,6 +301,11 @@ static PyObject *bpy_rna_context_temp_override_enter(BPyContextTempOverride *sel
bContext *C = self->context;
Main *bmain = CTX_data_main(C);
/* Enable logging for this temporary override context if the user has requested it. */
if (self->ctx_temp.use_logging) {
bpy_rna_context_logging_set(C, true);
}
/* It's crucial to call #CTX_py_state_pop if this function fails with an error. */
CTX_py_state_push(C, &self->py_state, self->py_state_context_dict);
@@ -349,7 +366,7 @@ static PyObject *bpy_rna_context_temp_override_enter(BPyContextTempOverride *sel
CTX_wm_region_set(C, self->ctx_temp.region);
}
Py_RETURN_NONE;
return Py_NewRef(self);
}
static PyObject *bpy_rna_context_temp_override_exit(BPyContextTempOverride *self,
@@ -502,11 +519,33 @@ static PyObject *bpy_rna_context_temp_override_exit(BPyContextTempOverride *self
if (context_dict_test && (context_dict_test != self->py_state_context_dict)) {
Py_DECREF(context_dict_test);
}
/* Restore logging state based on the user's preference stored in ctx_init.use_logging. */
bpy_rna_context_logging_set(C, self->ctx_init.use_logging);
CTX_py_state_pop(C, &self->py_state);
Py_RETURN_NONE;
}
static PyObject *bpy_rna_context_temp_override_logging_set(BPyContextTempOverride *self,
PyObject *args,
PyObject *kwds)
{
bool enable = true;
static const char *kwlist[] = {"", nullptr};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&", (char **)kwlist, PyC_ParseBool, &enable)) {
return nullptr;
}
self->ctx_temp.use_logging = enable;
bpy_rna_context_logging_set(self->context, enable);
Py_RETURN_NONE;
}
#ifdef __GNUC__
# ifdef __clang__
# pragma clang diagnostic push
@@ -520,6 +559,9 @@ static PyObject *bpy_rna_context_temp_override_exit(BPyContextTempOverride *self
static PyMethodDef bpy_rna_context_temp_override_methods[] = {
{"__enter__", (PyCFunction)bpy_rna_context_temp_override_enter, METH_NOARGS},
{"__exit__", (PyCFunction)bpy_rna_context_temp_override_exit, METH_VARARGS},
{"logging_set",
(PyCFunction)bpy_rna_context_temp_override_logging_set,
METH_VARARGS | METH_KEYWORDS},
{nullptr},
};