blob: ab10cfd161897c1b52deff7cf890c993ad876c3d [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Traceback implementation */
3
Guido van Rossum65bf9f21997-04-29 18:33:38 +00004#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00005
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00007#include "frameobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00008#include "structmember.h"
Guido van Rossum7169dbb1992-02-26 15:17:59 +00009#include "osdefs.h"
Nicholas Bastina7604bf2004-03-21 18:37:23 +000010#include "traceback.h"
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +000011#ifdef HAVE_FCNTL_H
12#include <fcntl.h>
13#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +000014
Nicholas Bastina7604bf2004-03-21 18:37:23 +000015#define OFF(x) offsetof(PyTracebackObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +000016
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +000017/* Method from Parser/tokenizer.c */
18extern char * PyTokenizer_FindEncoding(int);
19
Collin Winter3eed7652007-08-14 17:53:54 +000020static PyObject *
21tb_dir(PyTracebackObject *self)
22{
23 return Py_BuildValue("[ssss]", "tb_frame", "tb_next",
24 "tb_lasti", "tb_lineno");
25}
26
27static PyMethodDef tb_methods[] = {
28 {"__dir__", (PyCFunction)tb_dir, METH_NOARGS},
29 {NULL, NULL, 0, NULL},
30};
31
Neal Norwitz8dfc4a92007-08-11 06:39:53 +000032static PyMemberDef tb_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000033 {"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 Rossum3f5da241990-12-20 15:06:42 +000038};
39
Guido van Rossum3f5da241990-12-20 15:06:42 +000040static void
Nicholas Bastina7604bf2004-03-21 18:37:23 +000041tb_dealloc(PyTracebackObject *tb)
Guido van Rossum3f5da241990-12-20 15:06:42 +000042{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 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 Rossum3f5da241990-12-20 15:06:42 +000049}
50
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000051static int
Nicholas Bastina7604bf2004-03-21 18:37:23 +000052tb_traverse(PyTracebackObject *tb, visitproc visit, void *arg)
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 Py_VISIT(tb->tb_next);
55 Py_VISIT(tb->tb_frame);
56 return 0;
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000057}
58
59static void
Nicholas Bastina7604bf2004-03-21 18:37:23 +000060tb_clear(PyTracebackObject *tb)
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000061{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 Py_CLEAR(tb->tb_next);
63 Py_CLEAR(tb->tb_frame);
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000064}
65
Tim Petersd7c36522001-10-22 19:34:09 +000066PyTypeObject PyTraceBack_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 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 Rossum3f5da241990-12-20 15:06:42 +000099};
100
Nicholas Bastina7604bf2004-03-21 18:37:23 +0000101static PyTracebackObject *
102newtracebackobject(PyTracebackObject *next, PyFrameObject *frame)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000103{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 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 Rossum3f5da241990-12-20 15:06:42 +0000121}
122
Guido van Rossum3f5da241990-12-20 15:06:42 +0000123int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000124PyTraceBack_Here(PyFrameObject *frame)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 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 Rossum3f5da241990-12-20 15:06:42 +0000134}
135
Victor Stinner0fe25a42010-06-17 23:08:50 +0000136static PyObject *
137_Py_FindSourceFile(PyObject *filename, char* namebuf, size_t namelen, PyObject *io)
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000138{
Victor Stinner0fe25a42010-06-17 23:08:50 +0000139 Py_ssize_t i;
140 PyObject *binary;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 PyObject *v;
Victor Stinner0fe25a42010-06-17 23:08:50 +0000142 Py_ssize_t npath;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 size_t taillen;
144 PyObject *syspath;
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000145 PyObject *path;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 const char* tail;
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000147 PyObject *filebytes;
Victor Stinner0fe25a42010-06-17 23:08:50 +0000148 const char* filepath;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 Py_ssize_t len;
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000150 PyObject* result;
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000151
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000152 filebytes = PyUnicode_EncodeFSDefault(filename);
153 if (filebytes == NULL) {
Victor Stinner0fe25a42010-06-17 23:08:50 +0000154 PyErr_Clear();
155 return NULL;
156 }
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000157 filepath = PyBytes_AS_STRING(filebytes);
Victor Stinner0fe25a42010-06-17 23:08:50 +0000158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 /* Search tail of filename in sys.path before giving up */
Victor Stinner0fe25a42010-06-17 23:08:50 +0000160 tail = strrchr(filepath, SEP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161 if (tail == NULL)
Victor Stinner0fe25a42010-06-17 23:08:50 +0000162 tail = filepath;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 else
164 tail++;
165 taillen = strlen(tail);
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 syspath = PySys_GetObject("path");
168 if (syspath == NULL || !PyList_Check(syspath))
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000169 goto error;
Victor Stinner0fe25a42010-06-17 23:08:50 +0000170 npath = PyList_Size(syspath);
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 for (i = 0; i < npath; i++) {
173 v = PyList_GetItem(syspath, i);
174 if (v == NULL) {
175 PyErr_Clear();
176 break;
177 }
178 if (!PyUnicode_Check(v))
179 continue;
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000180 path = PyUnicode_EncodeFSDefault(v);
Victor Stinner0fe25a42010-06-17 23:08:50 +0000181 if (path == NULL) {
182 PyErr_Clear();
183 continue;
184 }
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000185 len = PyBytes_GET_SIZE(path);
186 if (len + 1 + (Py_ssize_t)taillen >= (Py_ssize_t)namelen - 1) {
187 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 continue; /* Too long */
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000189 }
190 strcpy(namebuf, PyBytes_AS_STRING(path));
191 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 if (strlen(namebuf) != len)
193 continue; /* v contains '\0' */
194 if (len > 0 && namebuf[len-1] != SEP)
195 namebuf[len++] = SEP;
196 strcpy(namebuf+len, tail);
Victor Stinner0fe25a42010-06-17 23:08:50 +0000197
198 binary = PyObject_CallMethod(io, "open", "ss", namebuf, "rb");
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000199 if (binary != NULL) {
200 result = binary;
201 goto finally;
202 }
Victor Stinner0fe25a42010-06-17 23:08:50 +0000203 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 }
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000205 goto error;
206
207error:
208 result = NULL;
209finally:
210 Py_DECREF(filebytes);
211 return result;
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000212}
213
Christian Heimes33fe8092008-04-13 13:53:33 +0000214int
Victor Stinner0fe25a42010-06-17 23:08:50 +0000215_Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 int err = 0;
218 int fd;
219 int i;
220 char *found_encoding;
221 char *encoding;
Victor Stinner0fe25a42010-06-17 23:08:50 +0000222 PyObject *io;
223 PyObject *binary;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 PyObject *fob = NULL;
225 PyObject *lineobj = NULL;
Antoine Pitroub86680e2010-10-14 21:15:17 +0000226 PyObject *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 char buf[MAXPATHLEN+1];
228 Py_UNICODE *u, *p;
229 Py_ssize_t len;
Christian Heimes679db4a2008-01-18 09:56:22 +0000230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 /* open the file */
232 if (filename == NULL)
233 return 0;
Victor Stinner0fe25a42010-06-17 23:08:50 +0000234
235 io = PyImport_ImportModuleNoBlock("io");
236 if (io == NULL)
237 return -1;
238 binary = PyObject_CallMethod(io, "open", "Os", filename, "rb");
239
240 if (binary == NULL) {
241 binary = _Py_FindSourceFile(filename, buf, sizeof(buf), io);
242 if (binary == NULL) {
243 Py_DECREF(io);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 return 0;
Victor Stinner0fe25a42010-06-17 23:08:50 +0000245 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 /* use the right encoding to decode the file as unicode */
Victor Stinner0fe25a42010-06-17 23:08:50 +0000249 fd = PyObject_AsFileDescriptor(binary);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 found_encoding = PyTokenizer_FindEncoding(fd);
Victor Stinner0fe25a42010-06-17 23:08:50 +0000251 encoding = (found_encoding != NULL) ? found_encoding : "utf-8";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 lseek(fd, 0, 0); /* Reset position */
Victor Stinner0fe25a42010-06-17 23:08:50 +0000253 fob = PyObject_CallMethod(io, "TextIOWrapper", "Os", binary, encoding);
254 Py_DECREF(io);
255 Py_DECREF(binary);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 PyMem_FREE(found_encoding);
Victor Stinner0fe25a42010-06-17 23:08:50 +0000257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 if (fob == NULL) {
259 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 return 0;
261 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 /* get the line number lineno */
264 for (i = 0; i < lineno; i++) {
265 Py_XDECREF(lineobj);
266 lineobj = PyFile_GetLine(fob, -1);
267 if (!lineobj) {
268 err = -1;
269 break;
270 }
271 }
Antoine Pitroub86680e2010-10-14 21:15:17 +0000272 res = PyObject_CallMethod(fob, "close", "");
273 if (res)
274 Py_DECREF(res);
275 else
276 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 Py_DECREF(fob);
278 if (!lineobj || !PyUnicode_Check(lineobj)) {
279 Py_XDECREF(lineobj);
280 return err;
281 }
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 /* remove the indentation of the line */
284 u = PyUnicode_AS_UNICODE(lineobj);
285 len = PyUnicode_GET_SIZE(lineobj);
286 for (p=u; *p == ' ' || *p == '\t' || *p == '\014'; p++)
287 len--;
288 if (u != p) {
289 PyObject *truncated;
290 truncated = PyUnicode_FromUnicode(p, len);
291 if (truncated) {
292 Py_DECREF(lineobj);
293 lineobj = truncated;
294 } else {
295 PyErr_Clear();
296 }
297 }
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 /* Write some spaces before the line */
300 strcpy(buf, " ");
301 assert (strlen(buf) == 10);
302 while (indent > 0) {
303 if(indent < 10)
304 buf[indent] = '\0';
305 err = PyFile_WriteString(buf, f);
306 if (err != 0)
307 break;
308 indent -= 10;
309 }
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 /* finally display the line */
312 if (err == 0)
313 err = PyFile_WriteObject(lineobj, f, Py_PRINT_RAW);
314 Py_DECREF(lineobj);
315 if (err == 0)
316 err = PyFile_WriteString("\n", f);
317 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000318}
319
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000320static int
Victor Stinner0fe25a42010-06-17 23:08:50 +0000321tb_displayline(PyObject *f, PyObject *filename, int lineno, PyObject *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000322{
Victor Stinner0fe25a42010-06-17 23:08:50 +0000323 int err;
324 PyObject *line;
Christian Heimes33fe8092008-04-13 13:53:33 +0000325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 if (filename == NULL || name == NULL)
327 return -1;
Victor Stinner0fe25a42010-06-17 23:08:50 +0000328 line = PyUnicode_FromFormat(" File \"%U\", line %d, in %U\n",
329 filename, lineno, name);
330 if (line == NULL)
331 return -1;
332 err = PyFile_WriteObject(line, f, Py_PRINT_RAW);
333 Py_DECREF(line);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 if (err != 0)
335 return err;
336 return _Py_DisplaySourceLine(f, filename, lineno, 4);
Christian Heimes33fe8092008-04-13 13:53:33 +0000337}
338
339static int
340tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit)
341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 int err = 0;
343 long depth = 0;
344 PyTracebackObject *tb1 = tb;
345 while (tb1 != NULL) {
346 depth++;
347 tb1 = tb1->tb_next;
348 }
349 while (tb != NULL && err == 0) {
350 if (depth <= limit) {
351 err = tb_displayline(f,
Victor Stinner0fe25a42010-06-17 23:08:50 +0000352 tb->tb_frame->f_code->co_filename,
353 tb->tb_lineno,
354 tb->tb_frame->f_code->co_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 }
356 depth--;
357 tb = tb->tb_next;
358 if (err == 0)
359 err = PyErr_CheckSignals();
360 }
361 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000362}
363
Christian Heimes0fbab7f2007-12-04 21:55:18 +0000364#define PyTraceBack_LIMIT 1000
365
Guido van Rossum3f5da241990-12-20 15:06:42 +0000366int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000367PyTraceBack_Print(PyObject *v, PyObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000368{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 int err;
370 PyObject *limitv;
371 long limit = PyTraceBack_LIMIT;
Christian Heimes0fbab7f2007-12-04 21:55:18 +0000372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 if (v == NULL)
374 return 0;
375 if (!PyTraceBack_Check(v)) {
376 PyErr_BadInternalCall();
377 return -1;
378 }
379 limitv = PySys_GetObject("tracebacklimit");
380 if (limitv) {
381 PyObject *exc_type, *exc_value, *exc_tb;
Christian Heimes0fbab7f2007-12-04 21:55:18 +0000382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
384 limit = PyLong_AsLong(limitv);
385 if (limit == -1 && PyErr_Occurred()) {
386 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
387 limit = PyTraceBack_LIMIT;
388 }
389 else {
390 Py_XDECREF(exc_type);
391 Py_XDECREF(exc_value);
392 Py_XDECREF(exc_tb);
393 return 0;
394 }
395 }
396 else if (limit <= 0) {
397 limit = PyTraceBack_LIMIT;
398 }
399 PyErr_Restore(exc_type, exc_value, exc_tb);
400 }
401 err = PyFile_WriteString("Traceback (most recent call last):\n", f);
402 if (!err)
403 err = tb_printinternal((PyTracebackObject *)v, f, limit);
404 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000405}