blob: e74d4426326027795aac771e61f1bb128ad257e3 [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;
146 const char* path;
147 const char* tail;
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000148 const char* filepath;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000149 Py_ssize_t len;
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000150
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000151 filepath = _PyUnicode_AsString(filename);
152 if (filepath == NULL) {
153 PyErr_Clear();
154 return NULL;
155 }
156
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000157 /* Search tail of filename in sys.path before giving up */
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000158 tail = strrchr(filepath, SEP);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000159 if (tail == NULL)
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000160 tail = filepath;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000161 else
162 tail++;
163 taillen = strlen(tail);
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000164
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000165 syspath = PySys_GetObject("path");
166 if (syspath == NULL || !PyList_Check(syspath))
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000167 return NULL;
168 npath = PyList_Size(syspath);
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000169
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000170 for (i = 0; i < npath; i++) {
171 v = PyList_GetItem(syspath, i);
172 if (v == NULL) {
173 PyErr_Clear();
174 break;
175 }
176 if (!PyUnicode_Check(v))
177 continue;
178 path = _PyUnicode_AsStringAndSize(v, &len);
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000179 if (path == NULL) {
180 PyErr_Clear();
181 continue;
182 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000183 if (len + 1 + (Py_ssize_t)taillen >= (Py_ssize_t)namelen - 1)
184 continue; /* Too long */
185 strcpy(namebuf, path);
186 if (strlen(namebuf) != len)
187 continue; /* v contains '\0' */
188 if (len > 0 && namebuf[len-1] != SEP)
189 namebuf[len++] = SEP;
190 strcpy(namebuf+len, tail);
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000191
192 binary = PyObject_CallMethod(io, "open", "ss", namebuf, "rb");
193 if (binary != NULL)
194 return binary;
195 PyErr_Clear();
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000196 }
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000197 return NULL;
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000198}
199
Christian Heimes33fe8092008-04-13 13:53:33 +0000200int
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000201_Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000202{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000203 int err = 0;
204 int fd;
205 int i;
206 char *found_encoding;
207 char *encoding;
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000208 PyObject *io;
209 PyObject *binary;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000210 PyObject *fob = NULL;
211 PyObject *lineobj = NULL;
Antoine Pitrou3d400b72010-10-14 21:17:39 +0000212 PyObject *res;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000213 char buf[MAXPATHLEN+1];
214 Py_UNICODE *u, *p;
215 Py_ssize_t len;
Christian Heimes679db4a2008-01-18 09:56:22 +0000216
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000217 /* open the file */
218 if (filename == NULL)
219 return 0;
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000220
221 io = PyImport_ImportModuleNoBlock("io");
222 if (io == NULL)
223 return -1;
224 binary = PyObject_CallMethod(io, "open", "Os", filename, "rb");
225
226 if (binary == NULL) {
227 binary = _Py_FindSourceFile(filename, buf, sizeof(buf), io);
228 if (binary == NULL) {
229 Py_DECREF(io);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000230 return 0;
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000231 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000232 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000233
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000234 /* use the right encoding to decode the file as unicode */
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000235 fd = PyObject_AsFileDescriptor(binary);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000236 found_encoding = PyTokenizer_FindEncoding(fd);
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000237 encoding = (found_encoding != NULL) ? found_encoding : "utf-8";
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000238 lseek(fd, 0, 0); /* Reset position */
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000239 fob = PyObject_CallMethod(io, "TextIOWrapper", "Os", binary, encoding);
240 Py_DECREF(io);
241 Py_DECREF(binary);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000242 PyMem_FREE(found_encoding);
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000243
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000244 if (fob == NULL) {
245 PyErr_Clear();
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000246 return 0;
247 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000248
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000249 /* get the line number lineno */
250 for (i = 0; i < lineno; i++) {
251 Py_XDECREF(lineobj);
252 lineobj = PyFile_GetLine(fob, -1);
253 if (!lineobj) {
254 err = -1;
255 break;
256 }
257 }
Antoine Pitrou3d400b72010-10-14 21:17:39 +0000258 res = PyObject_CallMethod(fob, "close", "");
259 if (res)
260 Py_DECREF(res);
261 else
262 PyErr_Clear();
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000263 Py_DECREF(fob);
264 if (!lineobj || !PyUnicode_Check(lineobj)) {
265 Py_XDECREF(lineobj);
266 return err;
267 }
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000268
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000269 /* remove the indentation of the line */
270 u = PyUnicode_AS_UNICODE(lineobj);
271 len = PyUnicode_GET_SIZE(lineobj);
272 for (p=u; *p == ' ' || *p == '\t' || *p == '\014'; p++)
273 len--;
274 if (u != p) {
275 PyObject *truncated;
276 truncated = PyUnicode_FromUnicode(p, len);
277 if (truncated) {
278 Py_DECREF(lineobj);
279 lineobj = truncated;
280 } else {
281 PyErr_Clear();
282 }
283 }
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000284
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000285 /* Write some spaces before the line */
286 strcpy(buf, " ");
287 assert (strlen(buf) == 10);
288 while (indent > 0) {
289 if(indent < 10)
290 buf[indent] = '\0';
291 err = PyFile_WriteString(buf, f);
292 if (err != 0)
293 break;
294 indent -= 10;
295 }
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000296
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000297 /* finally display the line */
298 if (err == 0)
299 err = PyFile_WriteObject(lineobj, f, Py_PRINT_RAW);
300 Py_DECREF(lineobj);
301 if (err == 0)
302 err = PyFile_WriteString("\n", f);
303 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000304}
305
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000306static int
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000307tb_displayline(PyObject *f, PyObject *filename, int lineno, PyObject *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000308{
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000309 int err;
310 PyObject *line;
Christian Heimes33fe8092008-04-13 13:53:33 +0000311
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000312 if (filename == NULL || name == NULL)
313 return -1;
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000314 line = PyUnicode_FromFormat(" File \"%U\", line %d, in %U\n",
315 filename, lineno, name);
316 if (line == NULL)
317 return -1;
318 err = PyFile_WriteObject(line, f, Py_PRINT_RAW);
319 Py_DECREF(line);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000320 if (err != 0)
321 return err;
322 return _Py_DisplaySourceLine(f, filename, lineno, 4);
Christian Heimes33fe8092008-04-13 13:53:33 +0000323}
324
325static int
326tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit)
327{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000328 int err = 0;
329 long depth = 0;
330 PyTracebackObject *tb1 = tb;
331 while (tb1 != NULL) {
332 depth++;
333 tb1 = tb1->tb_next;
334 }
335 while (tb != NULL && err == 0) {
336 if (depth <= limit) {
337 err = tb_displayline(f,
Victor Stinner25bb0fd2010-06-17 23:23:37 +0000338 tb->tb_frame->f_code->co_filename,
339 tb->tb_lineno,
340 tb->tb_frame->f_code->co_name);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000341 }
342 depth--;
343 tb = tb->tb_next;
344 if (err == 0)
345 err = PyErr_CheckSignals();
346 }
347 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000348}
349
Christian Heimes0fbab7f2007-12-04 21:55:18 +0000350#define PyTraceBack_LIMIT 1000
351
Guido van Rossum3f5da241990-12-20 15:06:42 +0000352int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000353PyTraceBack_Print(PyObject *v, PyObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000354{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000355 int err;
356 PyObject *limitv;
357 long limit = PyTraceBack_LIMIT;
Christian Heimes0fbab7f2007-12-04 21:55:18 +0000358
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000359 if (v == NULL)
360 return 0;
361 if (!PyTraceBack_Check(v)) {
362 PyErr_BadInternalCall();
363 return -1;
364 }
365 limitv = PySys_GetObject("tracebacklimit");
366 if (limitv) {
367 PyObject *exc_type, *exc_value, *exc_tb;
Christian Heimes0fbab7f2007-12-04 21:55:18 +0000368
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000369 PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
370 limit = PyLong_AsLong(limitv);
371 if (limit == -1 && PyErr_Occurred()) {
372 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
373 limit = PyTraceBack_LIMIT;
374 }
375 else {
376 Py_XDECREF(exc_type);
377 Py_XDECREF(exc_value);
378 Py_XDECREF(exc_tb);
379 return 0;
380 }
381 }
382 else if (limit <= 0) {
383 limit = PyTraceBack_LIMIT;
384 }
385 PyErr_Restore(exc_type, exc_value, exc_tb);
386 }
387 err = PyFile_WriteString("Traceback (most recent call last):\n", f);
388 if (!err)
389 err = tb_printinternal((PyTracebackObject *)v, f, limit);
390 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000391}