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>
|
|
|
|
|
|
2013-01-07 05:26:12 +00:00
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
|
|
2012-09-15 01:52:28 +00:00
|
|
|
#include "bpy.h" /* own include */
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "bpy_capi_utils.h"
|
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
|
|
|
{
|
|
|
|
|
/* close down enough of blender at least not to crash */
|
2023-07-21 10:59:54 +10:00
|
|
|
bContext *C = BPY_context_get();
|
2011-09-20 12:22:19 +00:00
|
|
|
|
2023-05-30 13:24:18 +10:00
|
|
|
WM_exit_ex(C, false, false);
|
2011-09-20 12:22:19 +00:00
|
|
|
|
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-21 13:42:35 +10:00
|
|
|
#if (defined(__GNUC__) && !defined(__clang__))
|
|
|
|
|
# pragma GCC diagnostic push
|
|
|
|
|
# pragma GCC diagnostic ignored "-Wcast-function-type"
|
|
|
|
|
#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
|
|
|
|
|
|
|
|
#if (defined(__GNUC__) && !defined(__clang__))
|
|
|
|
|
# pragma GCC diagnostic pop
|
|
|
|
|
#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
|
|
|
}
|