From e2872c0bfef729bcc2f03a583b100033f13c5ea0 Mon Sep 17 00:00:00 2001 From: Nick Alberelli Date: Tue, 23 Sep 2025 21:15:42 -0400 Subject: [PATCH] Core: de-duplicate bpy.context and context logging Merges the existing `bpy.context` logging with the new `context` logging added in [0]. In most cases `bpy.context` & `context` log the same thing. Where the new `context` logging produces more accurate results. Ref !146407 [0]: 439fe8a1a0d69be6bb9e080e90b89bc6342cac82 --- source/blender/python/BPY_extern_clog.hh | 1 - source/blender/python/intern/bpy.hh | 1 - source/blender/python/intern/bpy_interface.cc | 45 ++++++++++++++----- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/source/blender/python/BPY_extern_clog.hh b/source/blender/python/BPY_extern_clog.hh index e0f02a6dbcf..ce0a51137ef 100644 --- a/source/blender/python/BPY_extern_clog.hh +++ b/source/blender/python/BPY_extern_clog.hh @@ -13,4 +13,3 @@ /* bpy_interface.cc */ extern struct CLG_LogRef *BPY_LOG_RNA; -extern struct CLG_LogRef *BPY_LOG_CONTEXT; diff --git a/source/blender/python/intern/bpy.hh b/source/blender/python/intern/bpy.hh index dac8196c612..db1be8a40de 100644 --- a/source/blender/python/intern/bpy.hh +++ b/source/blender/python/intern/bpy.hh @@ -22,6 +22,5 @@ extern PyObject *bpy_package_py; void BPY_atexit_register(); void BPY_atexit_unregister(); -extern struct CLG_LogRef *BPY_LOG_CONTEXT; extern struct CLG_LogRef *BPY_LOG_RNA; extern struct CLG_LogRef *BPY_LOG_INTERFACE; diff --git a/source/blender/python/intern/bpy_interface.cc b/source/blender/python/intern/bpy_interface.cc index ce70416bfbe..bcbb8a08057 100644 --- a/source/blender/python/intern/bpy_interface.cc +++ b/source/blender/python/intern/bpy_interface.cc @@ -72,10 +72,11 @@ /* Logging types to use anywhere in the Python modules. */ -CLG_LOGREF_DECLARE_GLOBAL(BPY_LOG_CONTEXT, "bpy.context"); CLG_LOGREF_DECLARE_GLOBAL(BPY_LOG_INTERFACE, "bpy.interface"); CLG_LOGREF_DECLARE_GLOBAL(BPY_LOG_RNA, "bpy.rna"); +extern CLG_LogRef *BKE_LOG_CONTEXT; + /* For internal use, when starting and ending Python scripts. */ /* In case a Python script triggers another Python call, @@ -773,6 +774,29 @@ void BPY_modules_load_user(bContext *C) bpy_context_clear(C, &gilstate); } +/** Helper function for logging context member access errors with both CLI and Python support */ +static void bpy_context_log_member_error(const bContext *C, const char *message) +{ + const bool use_logging_info = CLOG_CHECK(BKE_LOG_CONTEXT, CLG_LEVEL_INFO); + const bool use_logging_member = C && CTX_member_logging_get(C); + if (!(use_logging_info || use_logging_member)) { + return; + } + + std::optional python_location = BPY_python_current_file_and_line(); + const char *location = python_location ? python_location->c_str() : "unknown:0"; + + if (use_logging_info) { + CLOG_INFO(BKE_LOG_CONTEXT, "%s: %s", location, message); + } + else if (use_logging_member) { + CLOG_AT_LEVEL_NOCHECK(BKE_LOG_CONTEXT, CLG_LEVEL_INFO, "%s: %s", location, message); + } + else { + BLI_assert_unreachable(); + } +} + bool BPY_context_member_get(bContext *C, const char *member, bContextDataResult *result) { PyGILState_STATE gilstate; @@ -821,10 +845,11 @@ bool BPY_context_member_get(bContext *C, const char *member, bContextDataResult CTX_data_list_add_ptr(result, ptr); } else { - CLOG_INFO(BPY_LOG_CONTEXT, - "'%s' list item not a valid type in sequence type '%s'", - member, - Py_TYPE(item)->tp_name); + /* Log invalid list item type */ + std::string message = std::string("'") + member + + "' list item not a valid type in sequence type '" + + Py_TYPE(list_item)->tp_name + "'"; + bpy_context_log_member_error(C, message.c_str()); } } Py_DECREF(seq_fast); @@ -835,14 +860,10 @@ bool BPY_context_member_get(bContext *C, const char *member, bContextDataResult if (done == false) { if (item) { - CLOG_INFO(BPY_LOG_CONTEXT, "'%s' not a valid type", member); + /* Log invalid member type */ + std::string message = std::string("'") + member + "' not a valid type"; + bpy_context_log_member_error(C, message.c_str()); } - else { - CLOG_INFO(BPY_LOG_CONTEXT, "'%s' not found", member); - } - } - else { - CLOG_DEBUG(BPY_LOG_CONTEXT, "'%s' found", member); } if (use_gil) {