[Bf-blender-cvs] [27ea0862426] blender-v2.90-release: Fix T80694: Crash reloading scripts from the Python console

Campbell Barton noreply at git.blender.org
Mon Sep 21 09:50:35 CEST 2020


Commit: 27ea0862426d910badc6f81ce13d29a6ef3cc5f0
Author: Campbell Barton
Date:   Mon Sep 14 17:50:01 2020 +1000
Branches: blender-v2.90-release
https://developer.blender.org/rB27ea0862426d910badc6f81ce13d29a6ef3cc5f0

Fix T80694: Crash reloading scripts from the Python console

Running `bpy.ops.script.reload()` from Python was crashing
since the operator being called was it's self freed.

Change the reload operator to defer execution - as supporting
re-registration during execution is quite involved for a corner-case.

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

M	release/scripts/modules/bpy/utils/__init__.py
M	source/blender/editors/space_script/script_edit.c
M	source/blender/makesrna/intern/rna_wm_api.c

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

diff --git a/release/scripts/modules/bpy/utils/__init__.py b/release/scripts/modules/bpy/utils/__init__.py
index 8a67a598ccd..4e0220ee1cc 100644
--- a/release/scripts/modules/bpy/utils/__init__.py
+++ b/release/scripts/modules/bpy/utils/__init__.py
@@ -283,6 +283,8 @@ def load_scripts(reload_scripts=False, refresh_scripts=False):
     del _initialize
 
     if reload_scripts:
+        _bpy.context.window_manager.tag_script_reload()
+
         import gc
         print("gc.collect() -> %d" % gc.collect())
 
diff --git a/source/blender/editors/space_script/script_edit.c b/source/blender/editors/space_script/script_edit.c
index e9ed1cec228..0c5cadf74e7 100644
--- a/source/blender/editors/space_script/script_edit.c
+++ b/source/blender/editors/space_script/script_edit.c
@@ -115,15 +115,32 @@ static int script_reload_exec(bContext *C, wmOperator *op)
     return OPERATOR_CANCELLED;
   }
 
-  WM_script_tag_reload();
-
-  /* TODO, this crashes on netrender and keying sets, need to look into why
-   * disable for now unless running in debug mode */
-  WM_cursor_wait(1);
-  BPY_execute_string(
-      C, (const char *[]){"bpy", NULL}, "bpy.utils.load_scripts(reload_scripts=True)");
-  WM_cursor_wait(0);
-  WM_event_add_notifier(C, NC_WINDOW, NULL);
+  /* TODO(campbell): this crashes on netrender and keying sets, need to look into why
+   * disable for now unless running in debug mode. */
+
+  /* It would be nice if we could detect when this is called from the Python
+   * only postponing in that case, for now always do it. */
+  if (true) {
+    /* Postpone when called from Python so this can be called from an operator
+     * that might be re-registered, crashing Blender when we try to read from the
+     * freed operator type which, see T80694. */
+    BPY_execute_string(C,
+                       (const char *[]){"bpy", NULL},
+                       "def fn():\n"
+                       "    bpy.utils.load_scripts(reload_scripts=True)\n"
+                       "    return None\n"
+                       "bpy.app.timers.register(fn)");
+  }
+  else {
+    WM_cursor_wait(true);
+    BPY_execute_string(
+        C, (const char *[]){"bpy", NULL}, "bpy.utils.load_scripts(reload_scripts=True)");
+    WM_cursor_wait(false);
+  }
+
+  /* Note that #WM_script_tag_reload is called from `bpy.utils.load_scripts`,
+   * any additional updates required by this operator should go there. */
+
   return OPERATOR_FINISHED;
 #else
   UNUSED_VARS(C, op);
diff --git a/source/blender/makesrna/intern/rna_wm_api.c b/source/blender/makesrna/intern/rna_wm_api.c
index ee7ff472c12..8d7a3cddbc5 100644
--- a/source/blender/makesrna/intern/rna_wm_api.c
+++ b/source/blender/makesrna/intern/rna_wm_api.c
@@ -558,6 +558,12 @@ static void rna_WindowManager_print_undo_steps(wmWindowManager *wm)
   BKE_undosys_print(wm->undo_stack);
 }
 
+static void rna_WindowManager_tag_script_reload(void)
+{
+  WM_script_tag_reload();
+  WM_main_add_notifier(NC_WINDOW, NULL);
+}
+
 static PointerRNA rna_WindoManager_operator_properties_last(const char *idname)
 {
   wmOperatorType *ot = WM_operatortype_find(idname, true);
@@ -913,6 +919,12 @@ void RNA_api_wm(StructRNA *srna)
 
   RNA_def_function(srna, "print_undo_steps", "rna_WindowManager_print_undo_steps");
 
+  /* Used by (#SCRIPT_OT_reload). */
+  func = RNA_def_function(srna, "tag_script_reload", "rna_WindowManager_tag_script_reload");
+  RNA_def_function_ui_description(
+      func, "Tag for refreshing the interface after scripts have been reloaded");
+  RNA_def_function_flag(func, FUNC_NO_SELF);
+
   parm = RNA_def_property(srna, "is_interface_locked", PROP_BOOLEAN, PROP_NONE);
   RNA_def_property_ui_text(
       parm,



More information about the Bf-blender-cvs mailing list