Blender V5.3
bpy_interface_atexit.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 "bpy.hh" /* own include */
16#include "bpy_capi_utils.hh"
17
19
20#include "WM_api.hh"
21
22#ifdef _WIN32
23# include <cstdio>
24
25# include "BLI_winstuff.hh"
26#else
27# include <cstdlib>
28#endif
29
30namespace blender {
31
32static PyObject *bpy_atexit(PyObject * /*self*/, PyObject * /*args*/, PyObject * /*kw*/)
33{
34 /* NOTE(@ideasman42): This doesn't have to match Blender shutting down exactly,
35 * leaks reported by memory checking tools may be reported but are harmless
36 * and don't have to be *fixed* unless doing so is trivial.
37 *
38 * Just handle the basics:
39 * - Free resources avoiding crashes and errors on exit.
40 * - Remove Blender's temporary directory.
41 *
42 * Anything else that prevents `sys.exit(..)` from exiting gracefully should be handled here too.
43 */
44
46 /* As Python requested the exit, it handles shutting itself down. */
47 const bool do_python_exit = false;
48 /* User actions such as saving the session, preferences, and recent-files
49 * should be skipped because an explicit call to exit is more likely to be used as part of
50 * automated processes shouldn't impact the users session in the future. */
51 const bool do_user_exit_actions = false;
52
53 WM_exit_ex(C, do_python_exit, do_user_exit_actions);
54
55#if !defined(WITH_PYTHON_MODULE) && defined(_WIN32) && defined(_M_ARM64)
56 /* Force immediate exit without e.g. heap cleanup that may deadlock on Windows ARM.
57 * In general, using exit() is unsafe in multithreaded applications and not recommended
58 * to be used at all. But tests use it, and there's nothing stopping user Python
59 * code from using it either.
60 *
61 * For scripts that want to exit Blender, the quit operator `bpy.ops.wm.quit_blender()`
62 * should be used instead. See pull request #155169 for details. */
63 std::optional<int> exit_code = PyC_ExceptionSystemExitCode();
64 BLI_assert(exit_code.has_value());
65 if (exit_code.has_value()) {
66 /* Flush Python & C streams as terminating skips the flushing otherwise
67 * performed by `Py_FinalizeEx` & the CRT at exit, see: #161830. */
69 fflush(stdout);
70 fflush(stderr);
71# ifdef _WIN32
72 TerminateProcess(GetCurrentProcess(), exit_code.value_or(0));
73# else
74 std::_Exit(exit_code.value_or(0));
75# endif
76 }
77#endif
78
79 Py_RETURN_NONE;
80}
81
82#ifdef __GNUC__
83# ifdef __clang__
84# pragma clang diagnostic push
85# pragma clang diagnostic ignored "-Wcast-function-type"
86# else
87# pragma GCC diagnostic push
88# pragma GCC diagnostic ignored "-Wcast-function-type"
89# endif
90#endif
91
92static PyMethodDef meth_bpy_atexit = {
93 "bpy_atexit", reinterpret_cast<PyCFunction>(bpy_atexit), METH_NOARGS, nullptr};
94
95#ifdef __GNUC__
96# ifdef __clang__
97# pragma clang diagnostic pop
98# else
99# pragma GCC diagnostic pop
100# endif
101#endif
102
104static PyObject *func_bpy_atregister = nullptr;
105
106static void atexit_func_call(const char *func_name, PyObject *atexit_func_arg)
107{
108 /* NOTE(@ideasman42): no error checking, if any of these fail we'll get a crash
109 * this is intended, but if its problematic it could be changed. */
110
111 PyObject *atexit_mod = PyImport_ImportModuleLevel("atexit", nullptr, nullptr, nullptr, 0);
112 PyObject *atexit_func = PyObject_GetAttrString(atexit_mod, func_name);
113 PyObject *args = PyTuple_New(1);
114 PyObject *ret;
115
116 PyTuple_SET_ITEM(args, 0, atexit_func_arg);
117 Py_INCREF(atexit_func_arg); /* only incref so we don't dec'ref along with 'args' */
118
119 ret = PyObject_CallObject(atexit_func, args);
120
121 Py_DECREF(atexit_mod);
122 Py_DECREF(atexit_func);
123 Py_DECREF(args);
124
125 if (ret) {
126 Py_DECREF(ret);
127 }
128 else { /* should never happen */
129 PyErr_Print();
130 }
131}
132
134{
136
137 func_bpy_atregister = static_cast<PyObject *>(PyCFunction_New(&meth_bpy_atexit, nullptr));
139}
140
142{
144
146 Py_CLEAR(func_bpy_atregister);
147}
148
149} // namespace blender
Compatibility-like things for windows.
#define C
Definition RandGen.cpp:29
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static PyMethodDef meth_bpy_atexit
void WM_exit_ex(bContext *C, const bool do_python_exit, const bool do_user_exit_actions)
static PyObject * bpy_atexit(PyObject *, PyObject *, PyObject *)
struct bContext * BPY_context_get()
std::optional< int > PyC_ExceptionSystemExitCode()
void BPY_atexit_register()
void PyC_StdFilesFlush()
static PyObject * func_bpy_atregister
void BPY_atexit_unregister()
static void atexit_func_call(const char *func_name, PyObject *atexit_func_arg)