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

@@ -12,6 +12,7 @@
#include <Python.h>
#include <frameobject.h>
#include <optional>
#ifdef WITH_PYTHON_MODULE
# include "pylifecycle.h" /* For `Py_Version`. */
@@ -21,6 +22,7 @@
#include "CLG_log.h"
#include "BLI_path_utils.hh"
#include "BLI_string.h"
#include "BLI_string_utf8.h"
#include "BLI_threads.h"
#include "BLI_utildefines.h"
@@ -850,6 +852,37 @@ bool BPY_context_member_get(bContext *C, const char *member, bContextDataResult
return done;
}
std::optional<std::string> BPY_python_current_file_and_line()
{
/* Early return if Python is not initialized, usually during startup.
* This function shouldn't operate if Python isn't initialized yet.
*
* In most cases this shouldn't be done, make an exception as it's needed for logging. */
if (!Py_IsInitialized()) {
return std::nullopt;
}
PyGILState_STATE gilstate;
const bool use_gil = !PyC_IsInterpreterActive();
std::optional<std::string> result = std::nullopt;
if (use_gil) {
gilstate = PyGILState_Ensure();
}
const char *filename = nullptr;
int lineno = -1;
PyC_FileAndNum_Safe(&filename, &lineno);
if (filename) {
result = std::string(filename) + ":" + std::to_string(lineno);
}
if (use_gil) {
PyGILState_Release(gilstate);
}
return result;
}
#ifdef WITH_PYTHON_MODULE
/* TODO: reloading the module isn't functional at the moment. */