Welcome to mirror list, hosted at ThFree Co, Russian Federation.

bpy_interface_atexit.c « intern « python « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1ba3ab6a40b4774bbef169bbe7ad83785eb42c42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* SPDX-License-Identifier: GPL-2.0-or-later */

/** \file
 * \ingroup pythonintern
 *
 * This file inserts an exit callback into Python's 'atexit' module.
 * Without this sys.exit() can crash because blender is not properly closing
 * resources.
 */

#include <Python.h>

#include "BLI_utildefines.h"

#include "bpy.h" /* own include */
#include "bpy_capi_utils.h"

#include "WM_api.h"

static PyObject *bpy_atexit(PyObject *UNUSED(self), PyObject *UNUSED(args), PyObject *UNUSED(kw))
{
  /* close down enough of blender at least not to crash */
  struct bContext *C = BPY_context_get();

  WM_exit_ex(C, false);

  Py_RETURN_NONE;
}

static PyMethodDef meth_bpy_atexit = {"bpy_atexit", (PyCFunction)bpy_atexit, METH_NOARGS, NULL};
static PyObject *func_bpy_atregister = NULL; /* borrowed reference, `atexit` holds. */

static void atexit_func_call(const char *func_name, PyObject *atexit_func_arg)
{
  /* NOTE(campbell): no error checking, if any of these fail we'll get a crash
   * this is intended, but if its problematic it could be changed. */

  PyObject *atexit_mod = PyImport_ImportModuleLevel("atexit", NULL, NULL, NULL, 0);
  PyObject *atexit_func = PyObject_GetAttrString(atexit_mod, func_name);
  PyObject *args = PyTuple_New(1);
  PyObject *ret;

  PyTuple_SET_ITEM(args, 0, atexit_func_arg);
  Py_INCREF(atexit_func_arg); /* only incref so we don't dec'ref along with 'args' */

  ret = PyObject_CallObject(atexit_func, args);

  Py_DECREF(atexit_mod);
  Py_DECREF(atexit_func);
  Py_DECREF(args);

  if (ret) {
    Py_DECREF(ret);
  }
  else { /* should never happen */
    PyErr_Print();
  }
}

void BPY_atexit_register(void)
{
  /* atexit module owns this new function reference */
  BLI_assert(func_bpy_atregister == NULL);

  func_bpy_atregister = (PyObject *)PyCFunction_New(&meth_bpy_atexit, NULL);
  atexit_func_call("register", func_bpy_atregister);
}

void BPY_atexit_unregister(void)
{
  BLI_assert(func_bpy_atregister != NULL);

  atexit_func_call("unregister", func_bpy_atregister);
  func_bpy_atregister = NULL; /* don't really need to set but just in case */
}