2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2011-09-20 12:22:19 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup pythonintern
|
2011-11-05 08:21:12 +00:00
|
|
|
*
|
2019-06-21 09:50:23 +10:00
|
|
|
* This file inserts an exit callback into Python's 'atexit' module.
|
2023-05-24 11:21:18 +10:00
|
|
|
* Without this `sys.exit()` can crash because blender is not properly closing
|
2011-11-05 08:21:12 +00:00
|
|
|
* resources.
|
2011-09-20 12:22:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <Python.h>
|
|
|
|
|
|
2024-09-24 12:23:25 +02:00
|
|
|
#include "bpy.hh" /* own include */
|
2024-09-24 15:25:36 +02:00
|
|
|
#include "bpy_capi_utils.hh"
|
2011-09-20 12:22:19 +00:00
|
|
|
|
2023-08-04 23:11:22 +02:00
|
|
|
#include "WM_api.hh"
|
2011-09-20 12:22:19 +00:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
static PyObject *bpy_atexit(PyObject * /*self*/, PyObject * /*args*/, PyObject * /*kw*/)
|
2011-09-20 12:22:19 +00:00
|
|
|
{
|
2023-09-19 15:00:23 +10:00
|
|
|
/* NOTE(@ideasman42): This doesn't have to match Blender shutting down exactly,
|
|
|
|
|
* leaks reported by memory checking tools may be reported but are harmless
|
|
|
|
|
* and don't have to be *fixed* unless doing so is trivial.
|
|
|
|
|
*
|
|
|
|
|
* Just handle the basics:
|
|
|
|
|
* - Free resources avoiding crashes and errors on exit.
|
|
|
|
|
* - Remove Blender's temporary directory.
|
|
|
|
|
*
|
|
|
|
|
* Anything else that prevents `sys.exit(..)` from exiting gracefully should be handled here too.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-07-21 10:59:54 +10:00
|
|
|
bContext *C = BPY_context_get();
|
2023-11-21 09:39:55 -05:00
|
|
|
/* As Python requested the exit, it handles shutting itself down. */
|
2023-09-19 15:38:55 +10:00
|
|
|
const bool do_python_exit = false;
|
2025-05-29 21:21:18 +02:00
|
|
|
/* User actions such as saving the session, preferences, and recent-files
|
2023-09-19 15:00:23 +10:00
|
|
|
* should be skipped because an explicit call to exit is more likely to be used as part of
|
|
|
|
|
* automated processes shouldn't impact the users session in the future. */
|
|
|
|
|
const bool do_user_exit_actions = false;
|
2011-09-20 12:22:19 +00:00
|
|
|
|
2023-09-19 15:38:55 +10:00
|
|
|
WM_exit_ex(C, do_python_exit, do_user_exit_actions);
|
2011-09-20 12:22:19 +00:00
|
|
|
|
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-01 12:06:03 +11:00
|
|
|
#ifdef __GNUC__
|
|
|
|
|
# ifdef __clang__
|
|
|
|
|
# pragma clang diagnostic push
|
|
|
|
|
# pragma clang diagnostic ignored "-Wcast-function-type"
|
|
|
|
|
# else
|
|
|
|
|
# pragma GCC diagnostic push
|
|
|
|
|
# pragma GCC diagnostic ignored "-Wcast-function-type"
|
|
|
|
|
# endif
|
2023-07-21 13:42:35 +10:00
|
|
|
#endif
|
|
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
static PyMethodDef meth_bpy_atexit = {"bpy_atexit", (PyCFunction)bpy_atexit, METH_NOARGS, nullptr};
|
2023-07-21 13:42:35 +10:00
|
|
|
|
2025-04-01 12:06:03 +11:00
|
|
|
#ifdef __GNUC__
|
|
|
|
|
# ifdef __clang__
|
|
|
|
|
# pragma clang diagnostic pop
|
|
|
|
|
# else
|
|
|
|
|
# pragma GCC diagnostic pop
|
|
|
|
|
# endif
|
2023-07-21 13:42:35 +10:00
|
|
|
#endif
|
|
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
static PyObject *func_bpy_atregister = nullptr; /* borrowed reference, `atexit` holds. */
|
2011-09-20 12:22:19 +00:00
|
|
|
|
2011-09-20 15:17:24 +00:00
|
|
|
static void atexit_func_call(const char *func_name, PyObject *atexit_func_arg)
|
2011-09-20 12:22:19 +00:00
|
|
|
{
|
2023-02-09 11:30:25 +11:00
|
|
|
/* NOTE(@ideasman42): no error checking, if any of these fail we'll get a crash
|
2021-07-03 23:08:40 +10:00
|
|
|
* this is intended, but if its problematic it could be changed. */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
PyObject *atexit_mod = PyImport_ImportModuleLevel("atexit", nullptr, nullptr, nullptr, 0);
|
2011-12-26 12:26:11 +00:00
|
|
|
PyObject *atexit_func = PyObject_GetAttrString(atexit_mod, func_name);
|
|
|
|
|
PyObject *args = PyTuple_New(1);
|
2011-09-20 12:22:19 +00:00
|
|
|
PyObject *ret;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-09-20 15:17:24 +00:00
|
|
|
PyTuple_SET_ITEM(args, 0, atexit_func_arg);
|
2012-03-18 07:38:51 +00:00
|
|
|
Py_INCREF(atexit_func_arg); /* only incref so we don't dec'ref along with 'args' */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
ret = PyObject_CallObject(atexit_func, args);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-09-20 12:22:19 +00:00
|
|
|
Py_DECREF(atexit_mod);
|
2011-09-20 15:17:24 +00:00
|
|
|
Py_DECREF(atexit_func);
|
2011-09-20 12:22:19 +00:00
|
|
|
Py_DECREF(args);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-10-13 01:29:08 +00:00
|
|
|
if (ret) {
|
2011-09-20 12:22:19 +00:00
|
|
|
Py_DECREF(ret);
|
|
|
|
|
}
|
|
|
|
|
else { /* should never happen */
|
|
|
|
|
PyErr_Print();
|
|
|
|
|
}
|
2011-09-20 15:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-21 10:59:54 +10:00
|
|
|
void BPY_atexit_register()
|
2011-09-20 15:17:24 +00:00
|
|
|
{
|
|
|
|
|
/* atexit module owns this new function reference */
|
2023-07-21 02:18:59 +02:00
|
|
|
BLI_assert(func_bpy_atregister == nullptr);
|
2011-09-20 12:22:19 +00:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
func_bpy_atregister = (PyObject *)PyCFunction_New(&meth_bpy_atexit, nullptr);
|
2011-09-20 15:17:24 +00:00
|
|
|
atexit_func_call("register", func_bpy_atregister);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-21 10:59:54 +10:00
|
|
|
void BPY_atexit_unregister()
|
2011-09-20 15:17:24 +00:00
|
|
|
{
|
2023-07-21 02:18:59 +02:00
|
|
|
BLI_assert(func_bpy_atregister != nullptr);
|
2011-09-20 17:07:33 +00:00
|
|
|
|
2011-09-20 15:17:24 +00:00
|
|
|
atexit_func_call("unregister", func_bpy_atregister);
|
2023-07-21 02:18:59 +02:00
|
|
|
func_bpy_atregister = nullptr; /* don't really need to set but just in case */
|
2011-09-20 12:22:19 +00:00
|
|
|
}
|