@private */
| 88 | |
| 89 | /** @private */ |
| 90 | SRD_PRIV void srd_exception_catch(char **error, const char *format, ...) |
| 91 | { |
| 92 | int i, ret; |
| 93 | va_list args; |
| 94 | PyObject *py_etype, *py_evalue, *py_etraceback; |
| 95 | PyObject *py_mod, *py_func, *py_tracefmt; |
| 96 | char *msg, *etype_name, *evalue_str, *outstr; |
| 97 | const char *etype_name_fallback; |
| 98 | PyGILState_STATE gstate; |
| 99 | GString *s; |
| 100 | char *final_msg; |
| 101 | |
| 102 | py_etype = py_evalue = py_etraceback = py_mod = py_func = NULL; |
| 103 | |
| 104 | va_start(args, format); |
| 105 | msg = g_strdup_vprintf(format, args); |
| 106 | va_end(args); |
| 107 | |
| 108 | gstate = PyGILState_Ensure(); |
| 109 | |
| 110 | PyErr_Fetch(&py_etype, &py_evalue, &py_etraceback); |
| 111 | if (!py_etype) { |
| 112 | /* No current exception, so just print the message. */ |
| 113 | final_msg = g_strjoin(":", msg, "unknown error", NULL); |
| 114 | srd_err("%s.", final_msg); |
| 115 | goto cleanup; |
| 116 | } |
| 117 | PyErr_NormalizeException(&py_etype, &py_evalue, &py_etraceback); |
| 118 | |
| 119 | etype_name = py_get_string_attr(py_etype, "__name__"); |
| 120 | evalue_str = py_stringify(py_evalue); |
| 121 | etype_name_fallback = (etype_name) ? etype_name : "(unknown exception)"; |
| 122 | |
| 123 | if (evalue_str) |
| 124 | final_msg = g_strjoin(":", msg, etype_name_fallback, evalue_str, NULL); |
| 125 | else |
| 126 | final_msg = g_strjoin(":", msg, etype_name_fallback, NULL); |
| 127 | |
| 128 | g_free(evalue_str); |
| 129 | g_free(etype_name); |
| 130 | |
| 131 | srd_err("%s.", final_msg); |
| 132 | |
| 133 | /* If there is no traceback object, we are done. */ |
| 134 | if (!py_etraceback) |
| 135 | goto cleanup; |
| 136 | |
| 137 | py_mod = py_import_by_name("traceback"); |
| 138 | if (!py_mod) |
| 139 | goto cleanup; |
| 140 | |
| 141 | py_func = PyObject_GetAttrString(py_mod, "format_exception"); |
| 142 | if (!py_func || !PyCallable_Check(py_func)) |
| 143 | goto cleanup; |
| 144 | |
| 145 | /* Call into Python to format the stack trace. */ |
| 146 | py_tracefmt = PyObject_CallFunctionObjArgs(py_func, |
| 147 | py_etype, py_evalue, py_etraceback, NULL); |
no test coverage detected