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