[Bf-blender-cvs] [e9c4325515a] blender-v2.90-release: Python: include Python stack trace in the crash log

Daniel Bailey noreply at git.blender.org
Thu Aug 6 07:47:43 CEST 2020


Commit: e9c4325515aed9cb3a35183d4093cda2b6bffd9f
Author: Daniel Bailey
Date:   Thu Aug 6 15:34:55 2020 +1000
Branches: blender-v2.90-release
https://developer.blender.org/rBe9c4325515aed9cb3a35183d4093cda2b6bffd9f

Python: include Python stack trace in the crash log

This helps Python developers troubleshoot errors when
Python causes a crash.

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

M	source/blender/python/BPY_extern.h
M	source/blender/python/generic/py_capi_utils.c
M	source/blender/python/generic/py_capi_utils.h
M	source/blender/python/intern/bpy_interface.c
M	source/creator/creator_signals.c

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

diff --git a/source/blender/python/BPY_extern.h b/source/blender/python/BPY_extern.h
index 5c6e0b0a308..5773f146dcc 100644
--- a/source/blender/python/BPY_extern.h
+++ b/source/blender/python/BPY_extern.h
@@ -55,6 +55,7 @@ void BPY_python_start(int argc, const char **argv);
 void BPY_python_end(void);
 void BPY_python_reset(struct bContext *C);
 void BPY_python_use_system_env(void);
+void BPY_python_backtrace(FILE *file);
 
 /* global interpreter lock */
 
diff --git a/source/blender/python/generic/py_capi_utils.c b/source/blender/python/generic/py_capi_utils.c
index 9c84a4bb824..0a4f3b5bebd 100644
--- a/source/blender/python/generic/py_capi_utils.c
+++ b/source/blender/python/generic/py_capi_utils.c
@@ -360,6 +360,20 @@ void PyC_StackSpit(void)
   }
 }
 
+void PyC_StackPrint(FILE *fp)
+{
+  PyThreadState *tstate = PyGILState_GetThisThreadState();
+  if (tstate != NULL && tstate->frame != NULL) {
+    PyFrameObject *frame = tstate->frame;
+    do {
+      const int line = PyCode_Addr2Line(frame->f_code, frame->f_lasti);
+      const char *filename = _PyUnicode_AsString(frame->f_code->co_filename);
+      const char *funcname = _PyUnicode_AsString(frame->f_code->co_name);
+      fprintf(fp, "  File \"%s\", line %d in %s\n", filename, line, funcname);
+    } while ((frame = frame->f_back));
+  }
+}
+
 void PyC_FileAndNum(const char **r_filename, int *r_lineno)
 {
   PyFrameObject *frame;
diff --git a/source/blender/python/generic/py_capi_utils.h b/source/blender/python/generic/py_capi_utils.h
index e8b2e8ff502..7950ee2c968 100644
--- a/source/blender/python/generic/py_capi_utils.h
+++ b/source/blender/python/generic/py_capi_utils.h
@@ -28,6 +28,7 @@ void PyC_ObSpit(const char *name, PyObject *var);
 void PyC_ObSpitStr(char *result, size_t result_len, PyObject *var);
 void PyC_LineSpit(void);
 void PyC_StackSpit(void);
+void PyC_StackPrint(FILE *fp);
 PyObject *PyC_ExceptionBuffer(void);
 PyObject *PyC_ExceptionBuffer_Simple(void);
 PyObject *PyC_Object_GetAttrStringArgs(PyObject *o, Py_ssize_t n, ...);
diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c
index a880d2cd285..dfddbc047f9 100644
--- a/source/blender/python/intern/bpy_interface.c
+++ b/source/blender/python/intern/bpy_interface.c
@@ -436,6 +436,12 @@ static void python_script_error_jump_text(struct Text *text)
   }
 }
 
+void BPY_python_backtrace(FILE *fp)
+{
+  fputs("\n# Python backtrace\n", fp);
+  PyC_StackPrint(fp);
+}
+
 /* super annoying, undo _PyModule_Clear(), bug [#23871] */
 #define PYMODULE_CLEAR_WORKAROUND
 
diff --git a/source/creator/creator_signals.c b/source/creator/creator_signals.c
index dbf947a86fd..ad0b7b2547d 100644
--- a/source/creator/creator_signals.c
+++ b/source/creator/creator_signals.c
@@ -59,6 +59,10 @@
 
 #  include <signal.h>
 
+#  ifdef WITH_PYTHON
+#    include "BPY_extern.h" /* BPY_python_backtrace */
+#  endif
+
 #  include "creator_intern.h" /* own include */
 
 // #define USE_WRITE_CRASH_BLEND
@@ -174,6 +178,11 @@ static void sig_handle_crash(int signum)
 
     sig_handle_crash_backtrace(fp);
 
+#  ifdef WITH_PYTHON
+    /* Generate python back-trace if Python is currently active. */
+    BPY_python_backtrace(fp);
+#  endif
+
     fclose(fp);
   }



More information about the Bf-blender-cvs mailing list