2 * SPDX-License-Identifier: MIT
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>
9 #define BT_LOG_OUTPUT_LEVEL log_level
10 #define BT_LOG_TAG "PY-COMMON"
11 #include "logging/log.h"
17 #include "common/assert.h"
18 #include "py-common.h"
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.
25 GString
*py_str_list_to_gstring(PyObject
*py_str_list
, int log_level
)
30 gstr
= g_string_new(NULL
);
32 BT_LOGE("Failed to allocate a GString.");
36 for (i
= 0; i
< PyList_Size(py_str_list
); i
++) {
40 py_str
= PyList_GetItem(py_str_list
, i
);
42 BT_ASSERT(PyUnicode_CheckExact(py_str
));
44 /* `str` is owned by `py_str`, not by us */
45 str
= PyUnicode_AsUTF8(py_str
);
47 if (BT_LOG_ON_ERROR
) {
48 BT_LOGE_STR("PyUnicode_AsUTF8() failed:");
55 /* `str` has a trailing newline */
56 g_string_append(gstr
, str
);
60 /* Remove trailing newline if any */
61 if (gstr
->str
[gstr
->len
- 1] == '\n') {
62 g_string_truncate(gstr
, gstr
->len
- 1);
69 g_string_free(gstr
, TRUE
);
76 GString
*bt_py_common_format_tb(PyObject
*py_exc_tb
, int log_level
)
78 PyObject
*traceback_module
= NULL
;
79 PyObject
*format_tb_func
= NULL
;
80 PyObject
*exc_str_list
= NULL
;
81 GString
*msg_buf
= NULL
;
86 * Import the standard `traceback` module which contains the
87 * functions to format a traceback.
89 traceback_module
= PyImport_ImportModule("traceback");
90 if (!traceback_module
) {
91 BT_LOGE_STR("Failed to import `traceback` module.");
95 format_tb_func
= PyObject_GetAttrString(traceback_module
,
97 if (!format_tb_func
) {
98 BT_LOGE("Cannot find `format_tb` attribute in `traceback` module.");
102 if (!PyCallable_Check(format_tb_func
)) {
103 BT_LOGE("`traceback.format_tb` attribute is not callable.");
108 * If we are calling format_exception_only, `py_exc_tb` is NULL, so the
109 * effective argument list is of length 2.
111 exc_str_list
= PyObject_CallFunctionObjArgs(
112 format_tb_func
, py_exc_tb
, NULL
);
114 if (BT_LOG_ON_ERROR
) {
115 BT_LOGE("Failed to call `traceback.format_tb` function:");
122 msg_buf
= py_str_list_to_gstring(exc_str_list
, log_level
);
128 Py_XDECREF(traceback_module
);
129 Py_XDECREF(format_tb_func
);
130 Py_XDECREF(exc_str_list
);
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
)
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
;
146 * Import the standard `traceback` module which contains the
147 * functions to format an exception.
149 traceback_module
= PyImport_ImportModule("traceback");
150 if (!traceback_module
) {
151 BT_LOGE_STR("Failed to import `traceback` module.");
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.
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
);
171 if (!PyCallable_Check(format_exception_func
)) {
172 BT_LOGE("`traceback.%s` attribute is not callable.",
173 format_exc_func_name
);
178 * If we are calling format_exception_only, `py_exc_tb` is NULL, so the
179 * effective argument list is of length 2.
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
);
185 if (BT_LOG_ON_ERROR
) {
186 BT_LOGE("Failed to call `traceback.%s` function:",
187 format_exc_func_name
);
194 msg_buf
= py_str_list_to_gstring(exc_str_list
, log_level
);
200 Py_XDECREF(exc_str_list
);
201 Py_XDECREF(format_exception_func
);
202 Py_XDECREF(traceback_module
);
207 GString
*bt_py_common_format_current_exception(int log_level
)
210 PyObject
*py_exc_type
= NULL
;
211 PyObject
*py_exc_value
= NULL
;
212 PyObject
*py_exc_tb
= NULL
;
214 BT_ASSERT(PyErr_Occurred());
215 PyErr_Fetch(&py_exc_type
, &py_exc_value
, &py_exc_tb
);
216 BT_ASSERT(py_exc_type
);
219 * Make sure `py_exc_value` is what we expected: an instance of
222 PyErr_NormalizeException(&py_exc_type
, &py_exc_value
, &py_exc_tb
);
224 result
= bt_py_common_format_exception(py_exc_type
, py_exc_value
,
225 py_exc_tb
, log_level
, true);
228 * We can safely call PyErr_Restore() because we always call
229 * PyErr_Fetch(), and having an error indicator is a function's
232 * PyErr_Restore() steals our references.
234 PyErr_Restore(py_exc_type
, py_exc_value
, py_exc_tb
);
This page took 0.033998 seconds and 4 git commands to generate.