blob: 2f1c213fa31a601aa68f7752828efe7397e63d26 [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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 = PyCode_Addr2Line(frame->f_code,
118 frame->f_lasti);
119 PyObject_GC_Track(tb);
120 }
121 return tb;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000122}
123
Guido van Rossum3f5da241990-12-20 15:06:42 +0000124int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000125PyTraceBack_Here(PyFrameObject *frame)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000126{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000127 PyThreadState *tstate = PyThreadState_GET();
128 PyTracebackObject *oldtb = (PyTracebackObject *) tstate->curexc_traceback;
129 PyTracebackObject *tb = newtracebackobject(oldtb, frame);
130 if (tb == NULL)
131 return -1;
132 tstate->curexc_traceback = (PyObject *)tb;
133 Py_XDECREF(oldtb);
134 return 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000135}
136
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000137static PyObject *
138_Py_FindSourceFile(PyObject *filename, char* namebuf, size_t namelen, PyObject *io)
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000139{
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000140 Py_ssize_t i;
141 PyObject *binary;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000142 PyObject *v;
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000143 Py_ssize_t npath;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000144 size_t taillen;
145 PyObject *syspath;
Victor Stinner15244f72010-10-19 01:22:07 +0000146 PyObject *path;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000147 const char* tail;
Victor Stinner15244f72010-10-19 01:22:07 +0000148 PyObject *filebytes;
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000149 const char* filepath;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000150 Py_ssize_t len;
Victor Stinner15244f72010-10-19 01:22:07 +0000151 PyObject* result;
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000152
Victor Stinner15244f72010-10-19 01:22:07 +0000153 filebytes = PyUnicode_AsEncodedObject(filename,
154 Py_FileSystemDefaultEncoding, "surrogateescape");
155 if (filebytes == NULL) {
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000156 PyErr_Clear();
157 return NULL;
158 }
Victor Stinner15244f72010-10-19 01:22:07 +0000159 filepath = PyBytes_AS_STRING(filebytes);
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000160
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000161 /* Search tail of filename in sys.path before giving up */
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000162 tail = strrchr(filepath, SEP);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000163 if (tail == NULL)
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000164 tail = filepath;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000165 else
166 tail++;
167 taillen = strlen(tail);
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000168
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000169 syspath = PySys_GetObject("path");
170 if (syspath == NULL || !PyList_Check(syspath))
Victor Stinner15244f72010-10-19 01:22:07 +0000171 goto error;
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000172 npath = PyList_Size(syspath);
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000173
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000174 for (i = 0; i < npath; i++) {
175 v = PyList_GetItem(syspath, i);
176 if (v == NULL) {
177 PyErr_Clear();
178 break;
179 }
180 if (!PyUnicode_Check(v))
181 continue;
Victor Stinner15244f72010-10-19 01:22:07 +0000182
183 path = PyUnicode_AsEncodedObject(v, Py_FileSystemDefaultEncoding,
184 "surrogateescape");
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000185 if (path == NULL) {
186 PyErr_Clear();
187 continue;
188 }
Victor Stinner15244f72010-10-19 01:22:07 +0000189 len = PyBytes_GET_SIZE(path);
190 if (len + 1 + (Py_ssize_t)taillen >= (Py_ssize_t)namelen - 1) {
191 Py_DECREF(path);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000192 continue; /* Too long */
Victor Stinner15244f72010-10-19 01:22:07 +0000193 }
194 strcpy(namebuf, PyBytes_AS_STRING(path));
195 Py_DECREF(path);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000196 if (strlen(namebuf) != len)
197 continue; /* v contains '\0' */
198 if (len > 0 && namebuf[len-1] != SEP)
199 namebuf[len++] = SEP;
200 strcpy(namebuf+len, tail);
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000201
202 binary = PyObject_CallMethod(io, "open", "ss", namebuf, "rb");
Victor Stinner15244f72010-10-19 01:22:07 +0000203 if (binary != NULL) {
204 result = binary;
205 goto finally;
206 }
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000207 PyErr_Clear();
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000208 }
Victor Stinner15244f72010-10-19 01:22:07 +0000209 goto error;
210
211error:
212 result = NULL;
213finally:
214 Py_DECREF(filebytes);
215 return result;
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000216}
217
Christian Heimes33fe8092008-04-13 13:53:33 +0000218int
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000219_Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000220{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000221 int err = 0;
222 int fd;
223 int i;
224 char *found_encoding;
225 char *encoding;
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000226 PyObject *io;
227 PyObject *binary;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000228 PyObject *fob = NULL;
229 PyObject *lineobj = NULL;
Antoine Pitrou3d400b72010-10-14 21:17:39 +0000230 PyObject *res;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000231 char buf[MAXPATHLEN+1];
232 Py_UNICODE *u, *p;
233 Py_ssize_t len;
Christian Heimes679db4a2008-01-18 09:56:22 +0000234
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000235 /* open the file */
236 if (filename == NULL)
237 return 0;
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000238
239 io = PyImport_ImportModuleNoBlock("io");
240 if (io == NULL)
241 return -1;
242 binary = PyObject_CallMethod(io, "open", "Os", filename, "rb");
243
244 if (binary == NULL) {
245 binary = _Py_FindSourceFile(filename, buf, sizeof(buf), io);
246 if (binary == NULL) {
247 Py_DECREF(io);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000248 return 0;
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000249 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000250 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000251
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000252 /* use the right encoding to decode the file as unicode */
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000253 fd = PyObject_AsFileDescriptor(binary);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000254 found_encoding = PyTokenizer_FindEncoding(fd);
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000255 encoding = (found_encoding != NULL) ? found_encoding : "utf-8";
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000256 lseek(fd, 0, 0); /* Reset position */
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000257 fob = PyObject_CallMethod(io, "TextIOWrapper", "Os", binary, encoding);
258 Py_DECREF(io);
259 Py_DECREF(binary);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000260 PyMem_FREE(found_encoding);
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000261
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000262 if (fob == NULL) {
263 PyErr_Clear();
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000264 return 0;
265 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000266
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000267 /* get the line number lineno */
268 for (i = 0; i < lineno; i++) {
269 Py_XDECREF(lineobj);
270 lineobj = PyFile_GetLine(fob, -1);
271 if (!lineobj) {
272 err = -1;
273 break;
274 }
275 }
Antoine Pitrou3d400b72010-10-14 21:17:39 +0000276 res = PyObject_CallMethod(fob, "close", "");
277 if (res)
278 Py_DECREF(res);
279 else
280 PyErr_Clear();
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000281 Py_DECREF(fob);
282 if (!lineobj || !PyUnicode_Check(lineobj)) {
283 Py_XDECREF(lineobj);
284 return err;
285 }
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000286
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000287 /* remove the indentation of the line */
288 u = PyUnicode_AS_UNICODE(lineobj);
289 len = PyUnicode_GET_SIZE(lineobj);
290 for (p=u; *p == ' ' || *p == '\t' || *p == '\014'; p++)
291 len--;
292 if (u != p) {
293 PyObject *truncated;
294 truncated = PyUnicode_FromUnicode(p, len);
295 if (truncated) {
296 Py_DECREF(lineobj);
297 lineobj = truncated;
298 } else {
299 PyErr_Clear();
300 }
301 }
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000302
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000303 /* Write some spaces before the line */
304 strcpy(buf, " ");
305 assert (strlen(buf) == 10);
306 while (indent > 0) {
307 if(indent < 10)
308 buf[indent] = '\0';
309 err = PyFile_WriteString(buf, f);
310 if (err != 0)
311 break;
312 indent -= 10;
313 }
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000314
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000315 /* finally display the line */
316 if (err == 0)
317 err = PyFile_WriteObject(lineobj, f, Py_PRINT_RAW);
318 Py_DECREF(lineobj);
319 if (err == 0)
320 err = PyFile_WriteString("\n", f);
321 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000322}
323
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000324static int
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000325tb_displayline(PyObject *f, PyObject *filename, int lineno, PyObject *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000326{
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000327 int err;
328 PyObject *line;
Christian Heimes33fe8092008-04-13 13:53:33 +0000329
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000330 if (filename == NULL || name == NULL)
331 return -1;
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000332 line = PyUnicode_FromFormat(" File \"%U\", line %d, in %U\n",
333 filename, lineno, name);
334 if (line == NULL)
335 return -1;
336 err = PyFile_WriteObject(line, f, Py_PRINT_RAW);
337 Py_DECREF(line);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000338 if (err != 0)
339 return err;
340 return _Py_DisplaySourceLine(f, filename, lineno, 4);
Christian Heimes33fe8092008-04-13 13:53:33 +0000341}
342
343static int
344tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit)
345{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000346 int err = 0;
347 long depth = 0;
348 PyTracebackObject *tb1 = tb;
349 while (tb1 != NULL) {
350 depth++;
351 tb1 = tb1->tb_next;
352 }
353 while (tb != NULL && err == 0) {
354 if (depth <= limit) {
355 err = tb_displayline(f,
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000356 tb->tb_frame->f_code->co_filename,
357 tb->tb_lineno,
358 tb->tb_frame->f_code->co_name);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000359 }
360 depth--;
361 tb = tb->tb_next;
362 if (err == 0)
363 err = PyErr_CheckSignals();
364 }
365 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000366}
367
Christian Heimes0fbab7f2007-12-04 21:55:18 +0000368#define PyTraceBack_LIMIT 1000
369
Guido van Rossum3f5da241990-12-20 15:06:42 +0000370int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000371PyTraceBack_Print(PyObject *v, PyObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000372{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000373 int err;
374 PyObject *limitv;
375 long limit = PyTraceBack_LIMIT;
Christian Heimes0fbab7f2007-12-04 21:55:18 +0000376
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000377 if (v == NULL)
378 return 0;
379 if (!PyTraceBack_Check(v)) {
380 PyErr_BadInternalCall();
381 return -1;
382 }
383 limitv = PySys_GetObject("tracebacklimit");
384 if (limitv) {
385 PyObject *exc_type, *exc_value, *exc_tb;
Christian Heimes0fbab7f2007-12-04 21:55:18 +0000386
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000387 PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
388 limit = PyLong_AsLong(limitv);
389 if (limit == -1 && PyErr_Occurred()) {
390 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
391 limit = PyTraceBack_LIMIT;
392 }
393 else {
394 Py_XDECREF(exc_type);
395 Py_XDECREF(exc_value);
396 Py_XDECREF(exc_tb);
397 return 0;
398 }
399 }
400 else if (limit <= 0) {
401 limit = PyTraceBack_LIMIT;
402 }
403 PyErr_Restore(exc_type, exc_value, exc_tb);
404 }
405 err = PyFile_WriteString("Traceback (most recent call last):\n", f);
406 if (!err)
407 err = tb_printinternal((PyTracebackObject *)v, f, limit);
408 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000409}