blob: f40cfb4a2365abbcb8dc1b71b981b50d03c6461c [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
6#include "compile.h"
7#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"
Guido van Rossum3f5da241990-12-20 15:06:42 +000011
Nicholas Bastina7604bf2004-03-21 18:37:23 +000012#define OFF(x) offsetof(PyTracebackObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +000013
14static struct memberlist tb_memberlist[] = {
15 {"tb_next", T_OBJECT, OFF(tb_next)},
16 {"tb_frame", T_OBJECT, OFF(tb_frame)},
17 {"tb_lasti", T_INT, OFF(tb_lasti)},
18 {"tb_lineno", T_INT, OFF(tb_lineno)},
19 {NULL} /* Sentinel */
20};
21
Guido van Rossum65bf9f21997-04-29 18:33:38 +000022static PyObject *
Nicholas Bastina7604bf2004-03-21 18:37:23 +000023tb_getattr(PyTracebackObject *tb, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +000024{
Guido van Rossum65bf9f21997-04-29 18:33:38 +000025 return PyMember_Get((char *)tb, tb_memberlist, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +000026}
27
28static void
Nicholas Bastina7604bf2004-03-21 18:37:23 +000029tb_dealloc(PyTracebackObject *tb)
Guido van Rossum3f5da241990-12-20 15:06:42 +000030{
Neil Schemenauer83584052002-03-29 03:07:29 +000031 PyObject_GC_UnTrack(tb);
Guido van Rossumd724b232000-03-13 16:01:29 +000032 Py_TRASHCAN_SAFE_BEGIN(tb)
Guido van Rossum65bf9f21997-04-29 18:33:38 +000033 Py_XDECREF(tb->tb_next);
34 Py_XDECREF(tb->tb_frame);
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000035 PyObject_GC_Del(tb);
Guido van Rossumd724b232000-03-13 16:01:29 +000036 Py_TRASHCAN_SAFE_END(tb)
Guido van Rossum3f5da241990-12-20 15:06:42 +000037}
38
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000039static int
Nicholas Bastina7604bf2004-03-21 18:37:23 +000040tb_traverse(PyTracebackObject *tb, visitproc visit, void *arg)
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000041{
42 int err = 0;
43 if (tb->tb_next) {
44 err = visit((PyObject *)tb->tb_next, arg);
45 if (err)
46 return err;
47 }
48 if (tb->tb_frame)
49 err = visit((PyObject *)tb->tb_frame, arg);
50 return err;
51}
52
53static void
Nicholas Bastina7604bf2004-03-21 18:37:23 +000054tb_clear(PyTracebackObject *tb)
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000055{
56 Py_XDECREF(tb->tb_next);
57 Py_XDECREF(tb->tb_frame);
58 tb->tb_next = NULL;
59 tb->tb_frame = NULL;
60}
61
Tim Petersd7c36522001-10-22 19:34:09 +000062PyTypeObject PyTraceBack_Type = {
Guido van Rossum65bf9f21997-04-29 18:33:38 +000063 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum3f5da241990-12-20 15:06:42 +000064 0,
65 "traceback",
Nicholas Bastina7604bf2004-03-21 18:37:23 +000066 sizeof(PyTracebackObject),
Guido van Rossum3f5da241990-12-20 15:06:42 +000067 0,
Guido van Rossum13836d91994-08-29 12:09:58 +000068 (destructor)tb_dealloc, /*tp_dealloc*/
Guido van Rossum3f5da241990-12-20 15:06:42 +000069 0, /*tp_print*/
Guido van Rossum13836d91994-08-29 12:09:58 +000070 (getattrfunc)tb_getattr, /*tp_getattr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +000071 0, /*tp_setattr*/
72 0, /*tp_compare*/
73 0, /*tp_repr*/
74 0, /*tp_as_number*/
75 0, /*tp_as_sequence*/
76 0, /*tp_as_mapping*/
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000077 0, /* tp_hash */
78 0, /* tp_call */
79 0, /* tp_str */
80 0, /* tp_getattro */
81 0, /* tp_setattro */
82 0, /* tp_as_buffer */
83 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
84 0, /* tp_doc */
85 (traverseproc)tb_traverse, /* tp_traverse */
86 (inquiry)tb_clear, /* tp_clear */
87 0, /* tp_richcompare */
88 0, /* tp_weaklistoffset */
89 0, /* tp_iter */
90 0, /* tp_iternext */
91 0, /* tp_methods */
92 0, /* tp_members */
93 0, /* tp_getset */
94 0, /* tp_base */
95 0, /* tp_dict */
Guido van Rossum3f5da241990-12-20 15:06:42 +000096};
97
Nicholas Bastina7604bf2004-03-21 18:37:23 +000098static PyTracebackObject *
99newtracebackobject(PyTracebackObject *next, PyFrameObject *frame)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000100{
Nicholas Bastina7604bf2004-03-21 18:37:23 +0000101 PyTracebackObject *tb;
Tim Petersd7c36522001-10-22 19:34:09 +0000102 if ((next != NULL && !PyTraceBack_Check(next)) ||
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000103 frame == NULL || !PyFrame_Check(frame)) {
104 PyErr_BadInternalCall();
Guido van Rossum3f5da241990-12-20 15:06:42 +0000105 return NULL;
106 }
Nicholas Bastina7604bf2004-03-21 18:37:23 +0000107 tb = PyObject_GC_New(PyTracebackObject, &PyTraceBack_Type);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000108 if (tb != NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000109 Py_XINCREF(next);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000110 tb->tb_next = next;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000111 Py_XINCREF(frame);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000112 tb->tb_frame = frame;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000113 tb->tb_lasti = frame->f_lasti;
114 tb->tb_lineno = PyCode_Addr2Line(frame->f_code,
115 frame->f_lasti);
Neil Schemenauer83584052002-03-29 03:07:29 +0000116 PyObject_GC_Track(tb);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000117 }
118 return tb;
119}
120
Guido van Rossum3f5da241990-12-20 15:06:42 +0000121int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000122PyTraceBack_Here(PyFrameObject *frame)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000123{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000124 PyThreadState *tstate = frame->f_tstate;
Nicholas Bastina7604bf2004-03-21 18:37:23 +0000125 PyTracebackObject *oldtb = (PyTracebackObject *) tstate->curexc_traceback;
126 PyTracebackObject *tb = newtracebackobject(oldtb, frame);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000127 if (tb == NULL)
128 return -1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000129 tstate->curexc_traceback = (PyObject *)tb;
130 Py_XDECREF(oldtb);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000131 return 0;
132}
133
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000134static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000135tb_displayline(PyObject *f, char *filename, int lineno, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000136{
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000137 int err = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000138 FILE *xfp;
Tim Peters6d20b432001-11-27 20:30:42 +0000139 char linebuf[2000];
Guido van Rossum3f5da241990-12-20 15:06:42 +0000140 int i;
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000141 if (filename == NULL || name == NULL)
142 return -1;
Guido van Rossum13836d91994-08-29 12:09:58 +0000143 /* This is needed by Emacs' compile command */
Tim Peters6d20b432001-11-27 20:30:42 +0000144#define FMT " File \"%.500s\", line %d, in %.500s\n"
Jack Jansen7b8c7542002-04-14 20:12:41 +0000145 xfp = fopen(filename, "r" PY_STDIOTEXTMODE);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000146 if (xfp == NULL) {
Guido van Rossum7169dbb1992-02-26 15:17:59 +0000147 /* Search tail of filename in sys.path before giving up */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000148 PyObject *path;
Guido van Rossum7169dbb1992-02-26 15:17:59 +0000149 char *tail = strrchr(filename, SEP);
150 if (tail == NULL)
151 tail = filename;
152 else
153 tail++;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000154 path = PySys_GetObject("path");
155 if (path != NULL && PyList_Check(path)) {
156 int npath = PyList_Size(path);
Guido van Rossum582acec2000-06-28 22:07:35 +0000157 size_t taillen = strlen(tail);
Guido van Rossum7169dbb1992-02-26 15:17:59 +0000158 char namebuf[MAXPATHLEN+1];
159 for (i = 0; i < npath; i++) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000160 PyObject *v = PyList_GetItem(path, i);
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000161 if (v == NULL) {
162 PyErr_Clear();
163 break;
164 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000165 if (PyString_Check(v)) {
Guido van Rossum582acec2000-06-28 22:07:35 +0000166 size_t len;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000167 len = PyString_Size(v);
Guido van Rossumbfd5d7551994-09-29 09:38:04 +0000168 if (len + 1 + taillen >= MAXPATHLEN)
169 continue; /* Too long */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000170 strcpy(namebuf, PyString_AsString(v));
Guido van Rossum582acec2000-06-28 22:07:35 +0000171 if (strlen(namebuf) != len)
Guido van Rossumbfd5d7551994-09-29 09:38:04 +0000172 continue; /* v contains '\0' */
Guido van Rossum7169dbb1992-02-26 15:17:59 +0000173 if (len > 0 && namebuf[len-1] != SEP)
174 namebuf[len++] = SEP;
175 strcpy(namebuf+len, tail);
Jack Jansen7b8c7542002-04-14 20:12:41 +0000176 xfp = fopen(namebuf, "r" PY_STDIOTEXTMODE);
Guido van Rossum7169dbb1992-02-26 15:17:59 +0000177 if (xfp != NULL) {
178 filename = namebuf;
179 break;
180 }
181 }
182 }
183 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000184 }
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000185 PyOS_snprintf(linebuf, sizeof(linebuf), FMT, filename, lineno, name);
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000186 err = PyFile_WriteString(linebuf, f);
187 if (xfp == NULL || err != 0)
188 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000189 for (i = 0; i < lineno; i++) {
Guido van Rossum3aca6531999-09-18 20:49:39 +0000190 char* pLastChar = &linebuf[sizeof(linebuf)-2];
191 do {
192 *pLastChar = '\0';
Jack Jansen7b8c7542002-04-14 20:12:41 +0000193 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, xfp, NULL) == NULL)
Guido van Rossum3aca6531999-09-18 20:49:39 +0000194 break;
195 /* fgets read *something*; if it didn't get as
196 far as pLastChar, it must have found a newline
197 or hit the end of the file; if pLastChar is \n,
198 it obviously found a newline; else we haven't
199 yet seen a newline, so must continue */
200 } while (*pLastChar != '\0' && *pLastChar != '\n');
Guido van Rossum3f5da241990-12-20 15:06:42 +0000201 }
202 if (i == lineno) {
Guido van Rossum7169dbb1992-02-26 15:17:59 +0000203 char *p = linebuf;
Guido van Rossume78c5d01995-07-07 22:45:41 +0000204 while (*p == ' ' || *p == '\t' || *p == '\014')
Guido van Rossum3f5da241990-12-20 15:06:42 +0000205 p++;
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000206 err = PyFile_WriteString(" ", f);
207 if (err == 0) {
208 err = PyFile_WriteString(p, f);
209 if (err == 0 && strchr(p, '\n') == NULL)
210 err = PyFile_WriteString("\n", f);
211 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000212 }
213 fclose(xfp);
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000214 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000215}
216
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000217static int
Nicholas Bastina7604bf2004-03-21 18:37:23 +0000218tb_printinternal(PyTracebackObject *tb, PyObject *f, int limit)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000219{
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000220 int err = 0;
Guido van Rossum67a5fdb1993-12-17 12:09:14 +0000221 int depth = 0;
Nicholas Bastina7604bf2004-03-21 18:37:23 +0000222 PyTracebackObject *tb1 = tb;
Guido van Rossum67a5fdb1993-12-17 12:09:14 +0000223 while (tb1 != NULL) {
224 depth++;
225 tb1 = tb1->tb_next;
226 }
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000227 while (tb != NULL && err == 0) {
Guido van Rossumb3f515a1997-01-24 04:02:55 +0000228 if (depth <= limit) {
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000229 err = tb_displayline(f,
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000230 PyString_AsString(
231 tb->tb_frame->f_code->co_filename),
Guido van Rossum13836d91994-08-29 12:09:58 +0000232 tb->tb_lineno,
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000233 PyString_AsString(tb->tb_frame->f_code->co_name));
Guido van Rossumb3f515a1997-01-24 04:02:55 +0000234 }
Guido van Rossum67a5fdb1993-12-17 12:09:14 +0000235 depth--;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000236 tb = tb->tb_next;
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000237 if (err == 0)
238 err = PyErr_CheckSignals();
Guido van Rossum3f5da241990-12-20 15:06:42 +0000239 }
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000240 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000241}
242
243int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000244PyTraceBack_Print(PyObject *v, PyObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000245{
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000246 int err;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000247 PyObject *limitv;
Guido van Rossum67a5fdb1993-12-17 12:09:14 +0000248 int limit = 1000;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000249 if (v == NULL)
250 return 0;
Tim Petersd7c36522001-10-22 19:34:09 +0000251 if (!PyTraceBack_Check(v)) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000252 PyErr_BadInternalCall();
Guido van Rossum3f5da241990-12-20 15:06:42 +0000253 return -1;
254 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000255 limitv = PySys_GetObject("tracebacklimit");
256 if (limitv && PyInt_Check(limitv)) {
257 limit = PyInt_AsLong(limitv);
Guido van Rossum67a5fdb1993-12-17 12:09:14 +0000258 if (limit <= 0)
259 return 0;
260 }
Guido van Rossum6d108872000-03-31 00:39:23 +0000261 err = PyFile_WriteString("Traceback (most recent call last):\n", f);
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000262 if (!err)
Nicholas Bastina7604bf2004-03-21 18:37:23 +0000263 err = tb_printinternal((PyTracebackObject *)v, f, limit);
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000264 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000265}