* Extract a Python traceback from the given exception data. * * The exception error message is returned in xmsg, the traceback in * tbmsg (both as palloc'd strings) and the traceback depth in * tb_depth. * * We release refcounts on all the Python objects in the traceback stack, * but not on e or v. */
| 169 | * but not on e or v. |
| 170 | */ |
| 171 | static void |
| 172 | PLy_traceback(PyObject *e, PyObject *v, PyObject *tb, |
| 173 | char **xmsg, char **tbmsg, int *tb_depth) |
| 174 | { |
| 175 | PyObject *e_type_o; |
| 176 | PyObject *e_module_o; |
| 177 | char *e_type_s = NULL; |
| 178 | char *e_module_s = NULL; |
| 179 | PyObject *vob = NULL; |
| 180 | char *vstr; |
| 181 | StringInfoData xstr; |
| 182 | StringInfoData tbstr; |
| 183 | |
| 184 | /* |
| 185 | * if no exception, return nulls |
| 186 | */ |
| 187 | if (e == NULL) |
| 188 | { |
| 189 | *xmsg = NULL; |
| 190 | *tbmsg = NULL; |
| 191 | *tb_depth = 0; |
| 192 | |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * Format the exception and its value and put it in xmsg. |
| 198 | */ |
| 199 | |
| 200 | e_type_o = PyObject_GetAttrString(e, "__name__"); |
| 201 | e_module_o = PyObject_GetAttrString(e, "__module__"); |
| 202 | if (e_type_o) |
| 203 | e_type_s = PyString_AsString(e_type_o); |
| 204 | if (e_type_s) |
| 205 | e_module_s = PyString_AsString(e_module_o); |
| 206 | |
| 207 | if (v && ((vob = PyObject_Str(v)) != NULL)) |
| 208 | vstr = PyString_AsString(vob); |
| 209 | else |
| 210 | vstr = "unknown"; |
| 211 | |
| 212 | initStringInfo(&xstr); |
| 213 | if (!e_type_s || !e_module_s) |
| 214 | { |
| 215 | if (PyString_Check(e)) |
| 216 | /* deprecated string exceptions */ |
| 217 | appendStringInfoString(&xstr, PyString_AsString(e)); |
| 218 | else |
| 219 | /* shouldn't happen */ |
| 220 | appendStringInfoString(&xstr, "unrecognized exception"); |
| 221 | } |
| 222 | /* mimics behavior of traceback.format_exception_only */ |
| 223 | else if (strcmp(e_module_s, "builtins") == 0 |
| 224 | || strcmp(e_module_s, "__main__") == 0 |
| 225 | || strcmp(e_module_s, "exceptions") == 0) |
| 226 | appendStringInfoString(&xstr, e_type_s); |
| 227 | else |
| 228 | appendStringInfo(&xstr, "%s.%s", e_module_s, e_type_s); |
no test coverage detected