Visibility hidden by default
[babeltrace.git] / src / py-common / py-common.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (c) 2019 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2019 Philippe Proulx <pproulx@efficios.com>
6 * Copyright (c) 2019 Simon Marchi <simon.marchi@efficios.com>
7 */
8
9 #define BT_LOG_OUTPUT_LEVEL log_level
10 #define BT_LOG_TAG "PY-COMMON"
11 #include "logging/log.h"
12
13 #include <stdbool.h>
14
15 #include <Python.h>
16
17 #include "common/assert.h"
18 #include "py-common.h"
19
20 /*
21 * Concatenate strings in list `py_str_list`, returning the result as a
22 * GString. Remove the trailing \n, if the last string ends with a \n.
23 */
24 static
25 GString *py_str_list_to_gstring(PyObject *py_str_list, int log_level)
26 {
27 Py_ssize_t i;
28 GString *gstr;
29
30 gstr = g_string_new(NULL);
31 if (!gstr) {
32 BT_LOGE("Failed to allocate a GString.");
33 goto end;
34 }
35
36 for (i = 0; i < PyList_Size(py_str_list); i++) {
37 PyObject *py_str;
38 const char *str;
39
40 py_str = PyList_GetItem(py_str_list, i);
41 BT_ASSERT(py_str);
42 BT_ASSERT(PyUnicode_CheckExact(py_str));
43
44 /* `str` is owned by `py_str`, not by us */
45 str = PyUnicode_AsUTF8(py_str);
46 if (!str) {
47 if (BT_LOG_ON_ERROR) {
48 BT_LOGE_STR("PyUnicode_AsUTF8() failed:");
49 PyErr_Print();
50 }
51
52 goto error;
53 }
54
55 /* `str` has a trailing newline */
56 g_string_append(gstr, str);
57 }
58
59 if (gstr->len > 0) {
60 /* Remove trailing newline if any */
61 if (gstr->str[gstr->len - 1] == '\n') {
62 g_string_truncate(gstr, gstr->len - 1);
63 }
64 }
65
66 goto end;
67
68 error:
69 g_string_free(gstr, TRUE);
70 gstr = NULL;
71
72 end:
73 return gstr;
74 }
75
76 GString *bt_py_common_format_tb(PyObject *py_exc_tb, int log_level)
77 {
78 PyObject *traceback_module = NULL;
79 PyObject *format_tb_func = NULL;
80 PyObject *exc_str_list = NULL;
81 GString *msg_buf = NULL;
82
83 BT_ASSERT(py_exc_tb);
84
85 /*
86 * Import the standard `traceback` module which contains the
87 * functions to format a traceback.
88 */
89 traceback_module = PyImport_ImportModule("traceback");
90 if (!traceback_module) {
91 BT_LOGE_STR("Failed to import `traceback` module.");
92 goto error;
93 }
94
95 format_tb_func = PyObject_GetAttrString(traceback_module,
96 "format_tb");
97 if (!format_tb_func) {
98 BT_LOGE("Cannot find `format_tb` attribute in `traceback` module.");
99 goto error;
100 }
101
102 if (!PyCallable_Check(format_tb_func)) {
103 BT_LOGE("`traceback.format_tb` attribute is not callable.");
104 goto error;
105 }
106
107 /*
108 * If we are calling format_exception_only, `py_exc_tb` is NULL, so the
109 * effective argument list is of length 2.
110 */
111 exc_str_list = PyObject_CallFunctionObjArgs(
112 format_tb_func, py_exc_tb, NULL);
113 if (!exc_str_list) {
114 if (BT_LOG_ON_ERROR) {
115 BT_LOGE("Failed to call `traceback.format_tb` function:");
116 PyErr_Print();
117 }
118
119 goto error;
120 }
121
122 msg_buf = py_str_list_to_gstring(exc_str_list, log_level);
123 if (!msg_buf) {
124 goto error;
125 }
126
127 error:
128 Py_XDECREF(traceback_module);
129 Py_XDECREF(format_tb_func);
130 Py_XDECREF(exc_str_list);
131
132 return msg_buf;
133 }
134
135 GString *bt_py_common_format_exception(PyObject *py_exc_type,
136 PyObject *py_exc_value, PyObject *py_exc_tb,
137 int log_level, bool chain)
138 {
139 PyObject *traceback_module = NULL;
140 PyObject *format_exception_func = NULL;
141 PyObject *exc_str_list = NULL;
142 GString *msg_buf = NULL;
143 const char *format_exc_func_name;
144
145 /*
146 * Import the standard `traceback` module which contains the
147 * functions to format an exception.
148 */
149 traceback_module = PyImport_ImportModule("traceback");
150 if (!traceback_module) {
151 BT_LOGE_STR("Failed to import `traceback` module.");
152 goto error;
153 }
154
155 /*
156 * `py_exc_tb` can be `NULL`, when we fail to call a Python
157 * function from native code (there is no Python stack at that
158 * point). For example, a function which takes 5 positional
159 * arguments, but 8 were given.
160 */
161 format_exc_func_name = py_exc_tb ? "format_exception" :
162 "format_exception_only";
163 format_exception_func = PyObject_GetAttrString(traceback_module,
164 format_exc_func_name);
165 if (!format_exception_func) {
166 BT_LOGE("Cannot find `%s` attribute in `traceback` module.",
167 format_exc_func_name);
168 goto error;
169 }
170
171 if (!PyCallable_Check(format_exception_func)) {
172 BT_LOGE("`traceback.%s` attribute is not callable.",
173 format_exc_func_name);
174 goto error;
175 }
176
177 /*
178 * If we are calling format_exception_only, `py_exc_tb` is NULL, so the
179 * effective argument list is of length 2.
180 */
181 exc_str_list = PyObject_CallFunctionObjArgs(format_exception_func,
182 py_exc_type, py_exc_value, py_exc_tb, Py_None /* limit */,
183 chain ? Py_True : Py_False /* chain */, NULL);
184 if (!exc_str_list) {
185 if (BT_LOG_ON_ERROR) {
186 BT_LOGE("Failed to call `traceback.%s` function:",
187 format_exc_func_name);
188 PyErr_Print();
189 }
190
191 goto error;
192 }
193
194 msg_buf = py_str_list_to_gstring(exc_str_list, log_level);
195 if (!msg_buf) {
196 goto error;
197 }
198
199 error:
200 Py_XDECREF(exc_str_list);
201 Py_XDECREF(format_exception_func);
202 Py_XDECREF(traceback_module);
203
204 return msg_buf;
205 }
206
207 GString *bt_py_common_format_current_exception(int log_level)
208 {
209 GString *result;
210 PyObject *py_exc_type = NULL;
211 PyObject *py_exc_value = NULL;
212 PyObject *py_exc_tb = NULL;
213
214 BT_ASSERT(PyErr_Occurred());
215 PyErr_Fetch(&py_exc_type, &py_exc_value, &py_exc_tb);
216 BT_ASSERT(py_exc_type);
217
218 /*
219 * Make sure `py_exc_value` is what we expected: an instance of
220 * `py_exc_type`.
221 */
222 PyErr_NormalizeException(&py_exc_type, &py_exc_value, &py_exc_tb);
223
224 result = bt_py_common_format_exception(py_exc_type, py_exc_value,
225 py_exc_tb, log_level, true);
226
227 /*
228 * We can safely call PyErr_Restore() because we always call
229 * PyErr_Fetch(), and having an error indicator is a function's
230 * precondition.
231 *
232 * PyErr_Restore() steals our references.
233 */
234 PyErr_Restore(py_exc_type, py_exc_value, py_exc_tb);
235
236 return result;
237 }
This page took 0.03436 seconds and 4 git commands to generate.