Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2 | /* Traceback implementation */ |
| 3 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 5 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6 | #include "code.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 7 | #include "frameobject.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 8 | #include "structmember.h" |
Guido van Rossum | 7169dbb | 1992-02-26 15:17:59 +0000 | [diff] [blame] | 9 | #include "osdefs.h" |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 10 | #include "traceback.h" |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 11 | #ifdef HAVE_FCNTL_H |
| 12 | #include <fcntl.h> |
| 13 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 14 | |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 15 | #define OFF(x) offsetof(PyTracebackObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 16 | |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 17 | /* Method from Parser/tokenizer.c */ |
| 18 | extern char * PyTokenizer_FindEncoding(int); |
| 19 | |
Collin Winter | 3eed765 | 2007-08-14 17:53:54 +0000 | [diff] [blame] | 20 | static PyObject * |
| 21 | tb_dir(PyTracebackObject *self) |
| 22 | { |
| 23 | return Py_BuildValue("[ssss]", "tb_frame", "tb_next", |
| 24 | "tb_lasti", "tb_lineno"); |
| 25 | } |
| 26 | |
| 27 | static PyMethodDef tb_methods[] = { |
| 28 | {"__dir__", (PyCFunction)tb_dir, METH_NOARGS}, |
| 29 | {NULL, NULL, 0, NULL}, |
| 30 | }; |
| 31 | |
Neal Norwitz | 8dfc4a9 | 2007-08-11 06:39:53 +0000 | [diff] [blame] | 32 | static PyMemberDef tb_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 33 | {"tb_next", T_OBJECT, OFF(tb_next), READONLY}, |
| 34 | {"tb_frame", T_OBJECT, OFF(tb_frame), READONLY}, |
| 35 | {"tb_lasti", T_INT, OFF(tb_lasti), READONLY}, |
| 36 | {"tb_lineno", T_INT, OFF(tb_lineno), READONLY}, |
| 37 | {NULL} /* Sentinel */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 40 | static void |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 41 | tb_dealloc(PyTracebackObject *tb) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 42 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 43 | PyObject_GC_UnTrack(tb); |
| 44 | Py_TRASHCAN_SAFE_BEGIN(tb) |
| 45 | Py_XDECREF(tb->tb_next); |
| 46 | Py_XDECREF(tb->tb_frame); |
| 47 | PyObject_GC_Del(tb); |
| 48 | Py_TRASHCAN_SAFE_END(tb) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 51 | static int |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 52 | tb_traverse(PyTracebackObject *tb, visitproc visit, void *arg) |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 53 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 54 | Py_VISIT(tb->tb_next); |
| 55 | Py_VISIT(tb->tb_frame); |
| 56 | return 0; |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static void |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 60 | tb_clear(PyTracebackObject *tb) |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 61 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 62 | Py_CLEAR(tb->tb_next); |
| 63 | Py_CLEAR(tb->tb_frame); |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Tim Peters | d7c3652 | 2001-10-22 19:34:09 +0000 | [diff] [blame] | 66 | PyTypeObject PyTraceBack_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 67 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 68 | "traceback", |
| 69 | sizeof(PyTracebackObject), |
| 70 | 0, |
| 71 | (destructor)tb_dealloc, /*tp_dealloc*/ |
| 72 | 0, /*tp_print*/ |
| 73 | 0, /*tp_getattr*/ |
| 74 | 0, /*tp_setattr*/ |
| 75 | 0, /*tp_reserved*/ |
| 76 | 0, /*tp_repr*/ |
| 77 | 0, /*tp_as_number*/ |
| 78 | 0, /*tp_as_sequence*/ |
| 79 | 0, /*tp_as_mapping*/ |
| 80 | 0, /* tp_hash */ |
| 81 | 0, /* tp_call */ |
| 82 | 0, /* tp_str */ |
| 83 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 84 | 0, /* tp_setattro */ |
| 85 | 0, /* tp_as_buffer */ |
| 86 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 87 | 0, /* tp_doc */ |
| 88 | (traverseproc)tb_traverse, /* tp_traverse */ |
| 89 | (inquiry)tb_clear, /* tp_clear */ |
| 90 | 0, /* tp_richcompare */ |
| 91 | 0, /* tp_weaklistoffset */ |
| 92 | 0, /* tp_iter */ |
| 93 | 0, /* tp_iternext */ |
| 94 | tb_methods, /* tp_methods */ |
| 95 | tb_memberlist, /* tp_members */ |
| 96 | 0, /* tp_getset */ |
| 97 | 0, /* tp_base */ |
| 98 | 0, /* tp_dict */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 101 | static PyTracebackObject * |
| 102 | newtracebackobject(PyTracebackObject *next, PyFrameObject *frame) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 103 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 104 | PyTracebackObject *tb; |
| 105 | if ((next != NULL && !PyTraceBack_Check(next)) || |
| 106 | frame == NULL || !PyFrame_Check(frame)) { |
| 107 | PyErr_BadInternalCall(); |
| 108 | return NULL; |
| 109 | } |
| 110 | tb = PyObject_GC_New(PyTracebackObject, &PyTraceBack_Type); |
| 111 | if (tb != NULL) { |
| 112 | Py_XINCREF(next); |
| 113 | tb->tb_next = next; |
| 114 | Py_XINCREF(frame); |
| 115 | tb->tb_frame = frame; |
| 116 | tb->tb_lasti = frame->f_lasti; |
| 117 | tb->tb_lineno = PyFrame_GetLineNumber(frame); |
| 118 | PyObject_GC_Track(tb); |
| 119 | } |
| 120 | return tb; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 123 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 124 | PyTraceBack_Here(PyFrameObject *frame) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 125 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 126 | PyThreadState *tstate = PyThreadState_GET(); |
| 127 | PyTracebackObject *oldtb = (PyTracebackObject *) tstate->curexc_traceback; |
| 128 | PyTracebackObject *tb = newtracebackobject(oldtb, frame); |
| 129 | if (tb == NULL) |
| 130 | return -1; |
| 131 | tstate->curexc_traceback = (PyObject *)tb; |
| 132 | Py_XDECREF(oldtb); |
| 133 | return 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 136 | static int |
| 137 | _Py_FindSourceFile(const char* filename, char* namebuf, size_t namelen, int open_flags) |
| 138 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 139 | int i; |
| 140 | int fd = -1; |
| 141 | PyObject *v; |
| 142 | Py_ssize_t _npath; |
| 143 | int npath; |
| 144 | size_t taillen; |
| 145 | PyObject *syspath; |
| 146 | const char* path; |
| 147 | const char* tail; |
| 148 | Py_ssize_t len; |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 149 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 150 | /* Search tail of filename in sys.path before giving up */ |
| 151 | tail = strrchr(filename, SEP); |
| 152 | if (tail == NULL) |
| 153 | tail = filename; |
| 154 | else |
| 155 | tail++; |
| 156 | taillen = strlen(tail); |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 157 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 158 | syspath = PySys_GetObject("path"); |
| 159 | if (syspath == NULL || !PyList_Check(syspath)) |
| 160 | return -1; |
| 161 | _npath = PyList_Size(syspath); |
| 162 | npath = Py_SAFE_DOWNCAST(_npath, Py_ssize_t, int); |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 163 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 164 | for (i = 0; i < npath; i++) { |
| 165 | v = PyList_GetItem(syspath, i); |
| 166 | if (v == NULL) { |
| 167 | PyErr_Clear(); |
| 168 | break; |
| 169 | } |
| 170 | if (!PyUnicode_Check(v)) |
| 171 | continue; |
| 172 | path = _PyUnicode_AsStringAndSize(v, &len); |
| 173 | if (len + 1 + (Py_ssize_t)taillen >= (Py_ssize_t)namelen - 1) |
| 174 | continue; /* Too long */ |
| 175 | strcpy(namebuf, path); |
| 176 | if (strlen(namebuf) != len) |
| 177 | continue; /* v contains '\0' */ |
| 178 | if (len > 0 && namebuf[len-1] != SEP) |
| 179 | namebuf[len++] = SEP; |
| 180 | strcpy(namebuf+len, tail); |
| 181 | Py_BEGIN_ALLOW_THREADS |
| 182 | fd = open(namebuf, open_flags); |
| 183 | Py_END_ALLOW_THREADS |
| 184 | if (0 <= fd) { |
| 185 | return fd; |
| 186 | } |
| 187 | } |
| 188 | return -1; |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 191 | int |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 192 | _Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno, int indent) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 193 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 194 | int err = 0; |
| 195 | int fd; |
| 196 | int i; |
| 197 | char *found_encoding; |
| 198 | char *encoding; |
| 199 | PyObject *fob = NULL; |
| 200 | PyObject *lineobj = NULL; |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 201 | #ifdef O_BINARY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 202 | const int open_flags = O_RDONLY | O_BINARY; /* necessary for Windows */ |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 203 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 204 | const int open_flags = O_RDONLY; |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 205 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 206 | char buf[MAXPATHLEN+1]; |
| 207 | Py_UNICODE *u, *p; |
| 208 | Py_ssize_t len; |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 209 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 210 | /* open the file */ |
| 211 | if (filename == NULL) |
| 212 | return 0; |
| 213 | Py_BEGIN_ALLOW_THREADS |
| 214 | fd = open(filename, open_flags); |
| 215 | Py_END_ALLOW_THREADS |
| 216 | if (fd < 0) { |
| 217 | fd = _Py_FindSourceFile(filename, buf, sizeof(buf), open_flags); |
| 218 | if (fd < 0) |
| 219 | return 0; |
| 220 | filename = buf; |
| 221 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 222 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 223 | /* use the right encoding to decode the file as unicode */ |
| 224 | found_encoding = PyTokenizer_FindEncoding(fd); |
| 225 | encoding = (found_encoding != NULL) ? found_encoding : |
| 226 | (char*)PyUnicode_GetDefaultEncoding(); |
| 227 | lseek(fd, 0, 0); /* Reset position */ |
| 228 | fob = PyFile_FromFd(fd, (char*)filename, "r", -1, (char*)encoding, |
| 229 | NULL, NULL, 1); |
| 230 | PyMem_FREE(found_encoding); |
| 231 | if (fob == NULL) { |
| 232 | PyErr_Clear(); |
| 233 | close(fd); |
| 234 | return 0; |
| 235 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 236 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 237 | /* get the line number lineno */ |
| 238 | for (i = 0; i < lineno; i++) { |
| 239 | Py_XDECREF(lineobj); |
| 240 | lineobj = PyFile_GetLine(fob, -1); |
| 241 | if (!lineobj) { |
| 242 | err = -1; |
| 243 | break; |
| 244 | } |
| 245 | } |
| 246 | Py_DECREF(fob); |
| 247 | if (!lineobj || !PyUnicode_Check(lineobj)) { |
| 248 | Py_XDECREF(lineobj); |
| 249 | return err; |
| 250 | } |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 251 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 252 | /* remove the indentation of the line */ |
| 253 | u = PyUnicode_AS_UNICODE(lineobj); |
| 254 | len = PyUnicode_GET_SIZE(lineobj); |
| 255 | for (p=u; *p == ' ' || *p == '\t' || *p == '\014'; p++) |
| 256 | len--; |
| 257 | if (u != p) { |
| 258 | PyObject *truncated; |
| 259 | truncated = PyUnicode_FromUnicode(p, len); |
| 260 | if (truncated) { |
| 261 | Py_DECREF(lineobj); |
| 262 | lineobj = truncated; |
| 263 | } else { |
| 264 | PyErr_Clear(); |
| 265 | } |
| 266 | } |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 267 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 268 | /* Write some spaces before the line */ |
| 269 | strcpy(buf, " "); |
| 270 | assert (strlen(buf) == 10); |
| 271 | while (indent > 0) { |
| 272 | if(indent < 10) |
| 273 | buf[indent] = '\0'; |
| 274 | err = PyFile_WriteString(buf, f); |
| 275 | if (err != 0) |
| 276 | break; |
| 277 | indent -= 10; |
| 278 | } |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 280 | /* finally display the line */ |
| 281 | if (err == 0) |
| 282 | err = PyFile_WriteObject(lineobj, f, Py_PRINT_RAW); |
| 283 | Py_DECREF(lineobj); |
| 284 | if (err == 0) |
| 285 | err = PyFile_WriteString("\n", f); |
| 286 | return err; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 289 | static int |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 290 | tb_displayline(PyObject *f, const char *filename, int lineno, const char *name) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 291 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 292 | int err = 0; |
| 293 | char linebuf[2000]; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 294 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 295 | if (filename == NULL || name == NULL) |
| 296 | return -1; |
| 297 | /* This is needed by Emacs' compile command */ |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 298 | #define FMT " File \"%.500s\", line %d, in %.500s\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | PyOS_snprintf(linebuf, sizeof(linebuf), FMT, filename, lineno, name); |
| 300 | err = PyFile_WriteString(linebuf, f); |
| 301 | if (err != 0) |
| 302 | return err; |
| 303 | return _Py_DisplaySourceLine(f, filename, lineno, 4); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | static int |
| 307 | tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit) |
| 308 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 309 | int err = 0; |
| 310 | long depth = 0; |
| 311 | PyTracebackObject *tb1 = tb; |
| 312 | while (tb1 != NULL) { |
| 313 | depth++; |
| 314 | tb1 = tb1->tb_next; |
| 315 | } |
| 316 | while (tb != NULL && err == 0) { |
| 317 | if (depth <= limit) { |
| 318 | err = tb_displayline(f, |
| 319 | _PyUnicode_AsString( |
| 320 | tb->tb_frame->f_code->co_filename), |
| 321 | tb->tb_lineno, |
| 322 | _PyUnicode_AsString(tb->tb_frame->f_code->co_name)); |
| 323 | } |
| 324 | depth--; |
| 325 | tb = tb->tb_next; |
| 326 | if (err == 0) |
| 327 | err = PyErr_CheckSignals(); |
| 328 | } |
| 329 | return err; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Christian Heimes | 0fbab7f | 2007-12-04 21:55:18 +0000 | [diff] [blame] | 332 | #define PyTraceBack_LIMIT 1000 |
| 333 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 334 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 335 | PyTraceBack_Print(PyObject *v, PyObject *f) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 336 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 337 | int err; |
| 338 | PyObject *limitv; |
| 339 | long limit = PyTraceBack_LIMIT; |
Christian Heimes | 0fbab7f | 2007-12-04 21:55:18 +0000 | [diff] [blame] | 340 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 341 | if (v == NULL) |
| 342 | return 0; |
| 343 | if (!PyTraceBack_Check(v)) { |
| 344 | PyErr_BadInternalCall(); |
| 345 | return -1; |
| 346 | } |
| 347 | limitv = PySys_GetObject("tracebacklimit"); |
| 348 | if (limitv) { |
| 349 | PyObject *exc_type, *exc_value, *exc_tb; |
Christian Heimes | 0fbab7f | 2007-12-04 21:55:18 +0000 | [diff] [blame] | 350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 351 | PyErr_Fetch(&exc_type, &exc_value, &exc_tb); |
| 352 | limit = PyLong_AsLong(limitv); |
| 353 | if (limit == -1 && PyErr_Occurred()) { |
| 354 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) { |
| 355 | limit = PyTraceBack_LIMIT; |
| 356 | } |
| 357 | else { |
| 358 | Py_XDECREF(exc_type); |
| 359 | Py_XDECREF(exc_value); |
| 360 | Py_XDECREF(exc_tb); |
| 361 | return 0; |
| 362 | } |
| 363 | } |
| 364 | else if (limit <= 0) { |
| 365 | limit = PyTraceBack_LIMIT; |
| 366 | } |
| 367 | PyErr_Restore(exc_type, exc_value, exc_tb); |
| 368 | } |
| 369 | err = PyFile_WriteString("Traceback (most recent call last):\n", f); |
| 370 | if (!err) |
| 371 | err = tb_printinternal((PyTracebackObject *)v, f, limit); |
| 372 | return err; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 373 | } |