dac2550ccef1a3cccf28ea42383e821dfc17fac9
[babeltrace.git] / src / bindings / python / bt2 / bt2 / native_bt_bt2_objects.h
1 /*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25 #include "logging/comp-logging.h"
26
27
28 /*
29 * Useful Python objects.
30 */
31
32 static PyObject *py_mod_bt2 = NULL;
33 static PyObject *py_mod_bt2_exc_error_type = NULL;
34 static PyObject *py_mod_bt2_exc_memory_error = NULL;
35 static PyObject *py_mod_bt2_exc_try_again_type = NULL;
36 static PyObject *py_mod_bt2_exc_stop_type = NULL;
37 static PyObject *py_mod_bt2_exc_unknown_object_type = NULL;
38
39 static
40 void bt_bt2_init_from_bt2(void)
41 {
42 /*
43 * This is called once the bt2 package is loaded.
44 *
45 * Those modules and functions are needed while the package is
46 * used. Loading them here is safe because we know the bt2
47 * package is imported, and we know that the user cannot use the
48 * code here without importing bt2 first.
49 */
50 py_mod_bt2 = PyImport_ImportModule("bt2");
51 BT_ASSERT(py_mod_bt2);
52 py_mod_bt2_exc_error_type =
53 PyObject_GetAttrString(py_mod_bt2, "_Error");
54 BT_ASSERT(py_mod_bt2_exc_error_type);
55 py_mod_bt2_exc_memory_error =
56 PyObject_GetAttrString(py_mod_bt2, "_MemoryError");
57 BT_ASSERT(py_mod_bt2_exc_memory_error);
58 py_mod_bt2_exc_try_again_type =
59 PyObject_GetAttrString(py_mod_bt2, "TryAgain");
60 BT_ASSERT(py_mod_bt2_exc_try_again_type);
61 py_mod_bt2_exc_stop_type =
62 PyObject_GetAttrString(py_mod_bt2, "Stop");
63 BT_ASSERT(py_mod_bt2_exc_stop_type);
64 py_mod_bt2_exc_unknown_object_type =
65 PyObject_GetAttrString(py_mod_bt2, "UnknownObject");
66 BT_ASSERT(py_mod_bt2_exc_unknown_object_type);
67 }
68
69 static
70 void bt_bt2_exit_handler(void)
71 {
72 /*
73 * This is an exit handler (set by the bt2 package).
74 *
75 * We only give back the references that we took in
76 * bt_bt2_init_from_bt2() here. The global variables continue
77 * to exist for the code of this file, but they are now borrowed
78 * references. If this code is executed, it means that somehow
79 * the modules are still loaded, so it should be safe to use
80 * them even without a strong reference.
81 *
82 * We cannot do this in the library's destructor because it
83 * gets executed once Python is already finalized.
84 */
85 Py_XDECREF(py_mod_bt2);
86 Py_XDECREF(py_mod_bt2_exc_error_type);
87 Py_XDECREF(py_mod_bt2_exc_try_again_type);
88 Py_XDECREF(py_mod_bt2_exc_stop_type);
89 Py_XDECREF(py_mod_bt2_exc_unknown_object_type);
90 }
This page took 0.032229 seconds and 3 git commands to generate.