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