[Bf-blender-cvs] [5531697f6d4] master: Cleanup: remove duplicate context variable (__py_context)

Campbell Barton noreply at git.blender.org
Thu Oct 15 10:01:49 CEST 2020


Commit: 5531697f6d4a27137dd91996ee80d452b7147a02
Author: Campbell Barton
Date:   Thu Oct 15 18:12:03 2020 +1100
Branches: master
https://developer.blender.org/rB5531697f6d4a27137dd91996ee80d452b7147a02

Cleanup: remove duplicate context variable (__py_context)

The context was stored both in __py_context & bpy_context_module.

This avoids duplicate functions to update them too.

===================================================================

M	source/blender/blenkernel/intern/blendfile.c
M	source/blender/python/BPY_extern.h
M	source/blender/python/BPY_extern_python.h
M	source/blender/python/intern/bpy.c
M	source/blender/python/intern/bpy.h
M	source/blender/python/intern/bpy_capi_utils.c
M	source/blender/python/intern/bpy_capi_utils.h
M	source/blender/python/intern/bpy_interface.c
M	source/blender/python/intern/bpy_operator.c
M	source/blender/python/intern/bpy_rna.c
M	source/blender/windowmanager/intern/wm_init_exit.c

===================================================================

diff --git a/source/blender/blenkernel/intern/blendfile.c b/source/blender/blenkernel/intern/blendfile.c
index 9dabbb021b6..567773507cf 100644
--- a/source/blender/blenkernel/intern/blendfile.c
+++ b/source/blender/blenkernel/intern/blendfile.c
@@ -330,7 +330,9 @@ static void setup_app_data(bContext *C,
 
 #ifdef WITH_PYTHON
   /* let python know about new main */
-  BPY_context_update(C);
+  if (CTX_py_init_get(C)) {
+    BPY_context_update(C);
+  }
 #endif
 
   /* FIXME: this version patching should really be part of the file-reading code,
diff --git a/source/blender/python/BPY_extern.h b/source/blender/python/BPY_extern.h
index 6ed403bb21a..46b4dbc96d7 100644
--- a/source/blender/python/BPY_extern.h
+++ b/source/blender/python/BPY_extern.h
@@ -68,8 +68,7 @@ void BPY_thread_restore(BPy_ThreadStatePtr tstate);
   (void)0
 
 void BPY_text_free_code(struct Text *text);
-void BPY_modules_update(
-    struct bContext *C); /* XXX - annoying, need this for pointers that get out of date */
+void BPY_modules_update(void);
 void BPY_modules_load_user(struct bContext *C);
 
 void BPY_app_handlers_reset(const short do_all);
diff --git a/source/blender/python/BPY_extern_python.h b/source/blender/python/BPY_extern_python.h
index 70b5cd679b9..c321fd93379 100644
--- a/source/blender/python/BPY_extern_python.h
+++ b/source/blender/python/BPY_extern_python.h
@@ -32,7 +32,7 @@ extern "C" {
 #include <stdio.h>
 
 /* bpy_interface.c */
-void BPY_python_start(int argc, const char **argv);
+void BPY_python_start(struct bContext *C, int argc, const char **argv);
 void BPY_python_end(void);
 void BPY_python_reset(struct bContext *C);
 void BPY_python_use_system_env(void);
diff --git a/source/blender/python/intern/bpy.c b/source/blender/python/intern/bpy.c
index 75314e4e2ea..804a28d0ebc 100644
--- a/source/blender/python/intern/bpy.c
+++ b/source/blender/python/intern/bpy.c
@@ -349,7 +349,7 @@ static PyObject *bpy_import_test(const char *modname)
 /******************************************************************************
  * Description: Creates the bpy module and adds it to sys.modules for importing
  ******************************************************************************/
-void BPy_init_modules(void)
+void BPy_init_modules(struct bContext *C)
 {
   PointerRNA ctx_ptr;
   PyObject *mod;
@@ -400,8 +400,7 @@ void BPy_init_modules(void)
   PyModule_AddObject(mod, "_utils_previews", BPY_utils_previews_module());
   PyModule_AddObject(mod, "msgbus", BPY_msgbus_module());
 
-  /* bpy context */
-  RNA_pointer_create(NULL, &RNA_Context, (void *)BPy_GetContext(), &ctx_ptr);
+  RNA_pointer_create(NULL, &RNA_Context, C, &ctx_ptr);
   bpy_context_module = (BPy_StructRNA *)pyrna_struct_CreatePyObject(&ctx_ptr);
   /* odd that this is needed, 1 ref on creation and another for the module
    * but without we get a crash on exit */
diff --git a/source/blender/python/intern/bpy.h b/source/blender/python/intern/bpy.h
index 744bf903443..e2fe84f71c7 100644
--- a/source/blender/python/intern/bpy.h
+++ b/source/blender/python/intern/bpy.h
@@ -24,7 +24,9 @@
 extern "C" {
 #endif
 
-void BPy_init_modules(void);
+struct bContext;
+
+void BPy_init_modules(struct bContext *C);
 extern PyObject *bpy_package_py;
 
 /* bpy_interface_atexit.c */
diff --git a/source/blender/python/intern/bpy_capi_utils.c b/source/blender/python/intern/bpy_capi_utils.c
index 8eb44e918d7..57dc4c0a679 100644
--- a/source/blender/python/intern/bpy_capi_utils.c
+++ b/source/blender/python/intern/bpy_capi_utils.c
@@ -38,16 +38,6 @@
 
 #include "../generic/py_capi_utils.h"
 
-static bContext *__py_context = NULL;
-bContext *BPy_GetContext(void)
-{
-  return __py_context;
-}
-void BPy_SetContext(bContext *C)
-{
-  __py_context = C;
-}
-
 char *BPy_enum_as_string(const EnumPropertyItem *item)
 {
   DynStr *dynstr = BLI_dynstr_new();
diff --git a/source/blender/python/intern/bpy_capi_utils.h b/source/blender/python/intern/bpy_capi_utils.h
index 861b23190a2..55f8a291410 100644
--- a/source/blender/python/intern/bpy_capi_utils.h
+++ b/source/blender/python/intern/bpy_capi_utils.h
@@ -48,9 +48,7 @@ bool BPy_errors_to_report_ex(struct ReportList *reports,
 bool BPy_errors_to_report_brief_with_prefix(struct ReportList *reports, const char *error_prefix);
 bool BPy_errors_to_report(struct ReportList *reports);
 
-/* TODO - find a better solution! */
-struct bContext *BPy_GetContext(void);
-void BPy_SetContext(struct bContext *C);
+struct bContext *BPY_context_get(void);
 
 extern void bpy_context_set(struct bContext *C, PyGILState_STATE *gilstate);
 extern void bpy_context_clear(struct bContext *C, const PyGILState_STATE *gilstate);
diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c
index f64332d94cc..468abd7b575 100644
--- a/source/blender/python/intern/bpy_interface.c
+++ b/source/blender/python/intern/bpy_interface.c
@@ -111,8 +111,8 @@ void BPY_context_update(bContext *C)
     return;
   }
 
-  BPy_SetContext(C);
-  BPY_modules_update(C); /* can give really bad results if this isn't here */
+  BPY_context_set(C);
+  BPY_modules_update(); /* can give really bad results if this isn't here */
 }
 
 void bpy_context_set(bContext *C, PyGILState_STATE *gilstate)
@@ -155,7 +155,7 @@ void bpy_context_clear(bContext *UNUSED(C), const PyGILState_STATE *gilstate)
     /* XXX - Calling classes currently wont store the context :\,
      * cant set NULL because of this. but this is very flakey still. */
 #if 0
-    BPy_SetContext(NULL);
+    BPY_context_set(NULL);
 #endif
 
 #ifdef TIME_PY_RUN
@@ -224,7 +224,10 @@ void BPY_text_free_code(Text *text)
   }
 }
 
-void BPY_modules_update(bContext *C)
+/**
+ * Needed so the #Main pointer in `bpy.data` doesn't become out of date.
+ */
+void BPY_modules_update(void)
 {
 #if 0 /* slow, this runs all the time poll, draw etc 100's of time a sec. */
   PyObject *mod = PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0);
@@ -234,14 +237,16 @@ void BPY_modules_update(bContext *C)
 
   /* refreshes the main struct */
   BPY_update_rna_module();
-  if (bpy_context_module) {
-    bpy_context_module->ptr.data = (void *)C;
-  }
+}
+
+bContext *BPy_GetContext(void)
+{
+  return bpy_context_module->ptr.data;
 }
 
 void BPY_context_set(bContext *C)
 {
-  BPy_SetContext(C);
+  bpy_context_module->ptr.data = (void *)C;
 }
 
 #ifdef WITH_FLUID
@@ -295,7 +300,7 @@ static struct _inittab bpy_internal_modules[] = {
 };
 
 /* call BPY_context_set first */
-void BPY_python_start(int argc, const char **argv)
+void BPY_python_start(bContext *C, int argc, const char **argv)
 {
 #ifndef WITH_PYTHON_MODULE
   PyThreadState *py_tstate = NULL;
@@ -387,7 +392,7 @@ void BPY_python_start(int argc, const char **argv)
 #endif
 
   /* bpy.* and lets us import it */
-  BPy_init_modules();
+  BPy_init_modules(C);
 
   pyrna_alloc_types();
 
diff --git a/source/blender/python/intern/bpy_operator.c b/source/blender/python/intern/bpy_operator.c
index 727654e1ac5..f263fd549da 100644
--- a/source/blender/python/intern/bpy_operator.c
+++ b/source/blender/python/intern/bpy_operator.c
@@ -339,7 +339,7 @@ static PyObject *pyop_call(PyObject *UNUSED(self), PyObject *args)
    * is freed by clear_globals(), further access will crash blender.
    * Setting context is not needed in this case, only calling because this
    * function corrects bpy.data (internal Main pointer) */
-  BPY_modules_update(C);
+  BPY_modules_update();
 
   /* return operator_ret as a bpy enum */
   return pyrna_enum_bitfield_to_py(rna_enum_operator_return_items, operator_ret);
diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c
index 71471474ec0..0ff7893a6e9 100644
--- a/source/blender/python/intern/bpy_rna.c
+++ b/source/blender/python/intern/bpy_rna.c
@@ -80,6 +80,11 @@
 #define USE_MATHUTILS
 #define USE_STRING_COERCE
 
+/* Unfortunately Python needs to hold a global reference to the context.
+ * If we remove this is means `bpy.context` won't be usable from some parts of the code:
+ * `bpy.app.handler` callbacks for example.
+ * Even though this is arguably "correct", it's going to cause problems for existing scripts,
+ * so accept having this for the time being. */
 BPy_StructRNA *bpy_context_module = NULL; /* for fast access */
 
 static PyObject *pyrna_struct_Subtype(PointerRNA *ptr);
diff --git a/source/blender/windowmanager/intern/wm_init_exit.c b/source/blender/windowmanager/intern/wm_init_exit.c
index 5a22e990218..4ec3e1297d2 100644
--- a/source/blender/windowmanager/intern/wm_init_exit.c
+++ b/source/blender/windowmanager/intern/wm_init_exit.c
@@ -334,9 +334,7 @@ void WM_init(bContext *C, int argc, const char **argv)
    * Will try fix when the crash can be repeated. - campbell. */
 
 #ifdef WITH_PYTHON
-  BPY_context_set(C); /* necessary evil */
-  BPY_python_start(argc, argv);
-
+  BPY_python_start(C, argc, argv);
   BPY_python_reset(C);
 #else
   (void)argc; /* unused */



More information about the Bf-blender-cvs mailing list