Blender V5.3
bpy_threads.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
12
13#include <Python.h>
14
15#include "python_compat.hh" /* IWYU pragma: keep. */
16
17#include "../BPY_extern.hh"
18
19namespace blender {
20
22{
23 /* Use `_PyThreadState_UncheckedGet()` instead of `PyThreadState_Get()`, to avoid a fatal error
24 * issued when a thread state is nullptr (the thread state can be nullptr when quitting Blender).
25 *
26 * `PyEval_SaveThread()` will release the GIL, so this thread has to have the GIL to begin with
27 * or badness will ensue. */
28 if (PyThreadState_GetUnchecked() && PyGILState_Check()) {
29 return static_cast<BPy_ThreadStatePtr>(PyEval_SaveThread());
30 }
31 return nullptr;
32}
33
35{
36 if (tstate) {
37 PyEval_RestoreThread(static_cast<PyThreadState *>(tstate));
38 }
39}
40
42{
43 PyThreadState *tstate = PyGILState_GetThisThreadState();
44
45 if (tstate) {
46 PyFrameObject *frame = PyThreadState_GetFrame(tstate);
47
48 printf(frame ? "Python stack trace:\n" : "No Python stack trace available.\n");
49
50 while (frame) {
51 PyCodeObject *frame_co = PyFrame_GetCode(frame);
52 int line = PyFrame_GetLineNumber(frame);
53 const char *filename = PyUnicode_AsUTF8(frame_co->co_filename);
54 const char *funcname = PyUnicode_AsUTF8(frame_co->co_name);
55 printf(" %s:%d %s\n", filename, line, funcname);
56 Py_DECREF(frame_co);
57 PyFrameObject *frame_back = PyFrame_GetBack(frame);
58 Py_DECREF(frame);
59 frame = frame_back;
60 }
61 printf("\n");
62 }
63 else {
64 printf("No Python thread state available.\n");
65 }
66}
67
68} // namespace blender
std::string filename(const std::string_view &str, size_t pos)
BMesh Iterators.
void * BPy_ThreadStatePtr
Definition BPY_extern.hh:40
void BPY_thread_restore(BPy_ThreadStatePtr tstate)
void BPY_thread_backtrace_print()
BPy_ThreadStatePtr BPY_thread_save()
header-only compatibility defines.
#define PyThreadState_GetUnchecked