Move to kernel style SPDX license identifiers
[babeltrace.git] / src / bindings / python / bt2 / bt2 / native_bt_bt2_objects.h
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
5 */
6
7 #include "logging/comp-logging.h"
8
9
10 /*
11 * Useful Python objects.
12 */
13
14 static PyObject *py_mod_bt2 = NULL;
15 static PyObject *py_mod_bt2_exc_error_type = NULL;
16 static PyObject *py_mod_bt2_exc_memory_error = NULL;
17 static PyObject *py_mod_bt2_exc_try_again_type = NULL;
18 static PyObject *py_mod_bt2_exc_stop_type = NULL;
19 static PyObject *py_mod_bt2_exc_unknown_object_type = NULL;
20
21 static
22 void bt_bt2_init_from_bt2(void)
23 {
24 /*
25 * This is called once the bt2 package is loaded.
26 *
27 * Those modules and functions are needed while the package is
28 * used. Loading them here is safe because we know the bt2
29 * package is imported, and we know that the user cannot use the
30 * code here without importing bt2 first.
31 */
32 py_mod_bt2 = PyImport_ImportModule("bt2");
33 BT_ASSERT(py_mod_bt2);
34 py_mod_bt2_exc_error_type =
35 PyObject_GetAttrString(py_mod_bt2, "_Error");
36 BT_ASSERT(py_mod_bt2_exc_error_type);
37 py_mod_bt2_exc_memory_error =
38 PyObject_GetAttrString(py_mod_bt2, "_MemoryError");
39 BT_ASSERT(py_mod_bt2_exc_memory_error);
40 py_mod_bt2_exc_try_again_type =
41 PyObject_GetAttrString(py_mod_bt2, "TryAgain");
42 BT_ASSERT(py_mod_bt2_exc_try_again_type);
43 py_mod_bt2_exc_stop_type =
44 PyObject_GetAttrString(py_mod_bt2, "Stop");
45 BT_ASSERT(py_mod_bt2_exc_stop_type);
46 py_mod_bt2_exc_unknown_object_type =
47 PyObject_GetAttrString(py_mod_bt2, "UnknownObject");
48 BT_ASSERT(py_mod_bt2_exc_unknown_object_type);
49 }
50
51 static
52 void bt_bt2_exit_handler(void)
53 {
54 /*
55 * This is an exit handler (set by the bt2 package).
56 *
57 * We only give back the references that we took in
58 * bt_bt2_init_from_bt2() here. The global variables continue
59 * to exist for the code of this file, but they are now borrowed
60 * references. If this code is executed, it means that somehow
61 * the modules are still loaded, so it should be safe to use
62 * them even without a strong reference.
63 *
64 * We cannot do this in the library's destructor because it
65 * gets executed once Python is already finalized.
66 */
67 Py_XDECREF(py_mod_bt2);
68 Py_XDECREF(py_mod_bt2_exc_error_type);
69 Py_XDECREF(py_mod_bt2_exc_try_again_type);
70 Py_XDECREF(py_mod_bt2_exc_stop_type);
71 Py_XDECREF(py_mod_bt2_exc_unknown_object_type);
72 }
This page took 0.030648 seconds and 4 git commands to generate.