blob: b8edf139644da6db8fc845202fb598815f1b743d [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"
Guido van Rossum3f5da241990-12-20 15:06:42 +000010
11typedef struct _tracebackobject {
Guido van Rossum65bf9f21997-04-29 18:33:38 +000012 PyObject_HEAD
Guido van Rossum3f5da241990-12-20 15:06:42 +000013 struct _tracebackobject *tb_next;
Guido van Rossum65bf9f21997-04-29 18:33:38 +000014 PyFrameObject *tb_frame;
Guido van Rossum3f5da241990-12-20 15:06:42 +000015 int tb_lasti;
16 int tb_lineno;
17} tracebackobject;
18
19#define OFF(x) offsetof(tracebackobject, x)
20
21static struct memberlist tb_memberlist[] = {
22 {"tb_next", T_OBJECT, OFF(tb_next)},
23 {"tb_frame", T_OBJECT, OFF(tb_frame)},
24 {"tb_lasti", T_INT, OFF(tb_lasti)},
25 {"tb_lineno", T_INT, OFF(tb_lineno)},
26 {NULL} /* Sentinel */
27};
28
Guido van Rossum65bf9f21997-04-29 18:33:38 +000029static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000030tb_getattr(tracebackobject *tb, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +000031{
Guido van Rossum65bf9f21997-04-29 18:33:38 +000032 return PyMember_Get((char *)tb, tb_memberlist, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +000033}
34
35static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000036tb_dealloc(tracebackobject *tb)
Guido van Rossum3f5da241990-12-20 15:06:42 +000037{
Neil Schemenauer83584052002-03-29 03:07:29 +000038 PyObject_GC_UnTrack(tb);
Guido van Rossumd724b232000-03-13 16:01:29 +000039 Py_TRASHCAN_SAFE_BEGIN(tb)
Guido van Rossum65bf9f21997-04-29 18:33:38 +000040 Py_XDECREF(tb->tb_next);
41 Py_XDECREF(tb->tb_frame);
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000042 PyObject_GC_Del(tb);
Guido van Rossumd724b232000-03-13 16:01:29 +000043 Py_TRASHCAN_SAFE_END(tb)
Guido van Rossum3f5da241990-12-20 15:06:42 +000044}
45
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000046static int
47tb_traverse(tracebackobject *tb, visitproc visit, void *arg)
48{
49 int err = 0;
50 if (tb->tb_next) {
51 err = visit((PyObject *)tb->tb_next, arg);
52 if (err)
53 return err;
54 }
55 if (tb->tb_frame)
56 err = visit((PyObject *)tb->tb_frame, arg);
57 return err;
58}
59
60static void
61tb_clear(tracebackobject *tb)
62{
63 Py_XDECREF(tb->tb_next);
64 Py_XDECREF(tb->tb_frame);
65 tb->tb_next = NULL;
66 tb->tb_frame = NULL;
67}
68
Tim Petersd7c36522001-10-22 19:34:09 +000069PyTypeObject PyTraceBack_Type = {
Guido van Rossum65bf9f21997-04-29 18:33:38 +000070 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum3f5da241990-12-20 15:06:42 +000071 0,
72 "traceback",
73 sizeof(tracebackobject),
74 0,
Guido van Rossum13836d91994-08-29 12:09:58 +000075 (destructor)tb_dealloc, /*tp_dealloc*/
Guido van Rossum3f5da241990-12-20 15:06:42 +000076 0, /*tp_print*/
Guido van Rossum13836d91994-08-29 12:09:58 +000077 (getattrfunc)tb_getattr, /*tp_getattr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +000078 0, /*tp_setattr*/
79 0, /*tp_compare*/
80 0, /*tp_repr*/
81 0, /*tp_as_number*/
82 0, /*tp_as_sequence*/
83 0, /*tp_as_mapping*/
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000084 0, /* tp_hash */
85 0, /* tp_call */
86 0, /* tp_str */
87 0, /* tp_getattro */
88 0, /* tp_setattro */
89 0, /* tp_as_buffer */
90 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
91 0, /* tp_doc */
92 (traverseproc)tb_traverse, /* tp_traverse */
93 (inquiry)tb_clear, /* tp_clear */
94 0, /* tp_richcompare */
95 0, /* tp_weaklistoffset */
96 0, /* tp_iter */
97 0, /* tp_iternext */
98 0, /* tp_methods */
99 0, /* tp_members */
100 0, /* tp_getset */
101 0, /* tp_base */
102 0, /* tp_dict */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000103};
104
Guido van Rossum3f5da241990-12-20 15:06:42 +0000105static tracebackobject *
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000106newtracebackobject(tracebackobject *next, PyFrameObject *frame)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000107{
108 tracebackobject *tb;
Tim Petersd7c36522001-10-22 19:34:09 +0000109 if ((next != NULL && !PyTraceBack_Check(next)) ||
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000110 frame == NULL || !PyFrame_Check(frame)) {
111 PyErr_BadInternalCall();
Guido van Rossum3f5da241990-12-20 15:06:42 +0000112 return NULL;
113 }
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +0000114 tb = PyObject_GC_New(tracebackobject, &PyTraceBack_Type);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000115 if (tb != NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000116 Py_XINCREF(next);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000117 tb->tb_next = next;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000118 Py_XINCREF(frame);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000119 tb->tb_frame = frame;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000120 tb->tb_lasti = frame->f_lasti;
121 tb->tb_lineno = PyCode_Addr2Line(frame->f_code,
122 frame->f_lasti);
Neil Schemenauer83584052002-03-29 03:07:29 +0000123 PyObject_GC_Track(tb);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000124 }
125 return tb;
126}
127
Guido van Rossum3f5da241990-12-20 15:06:42 +0000128int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000129PyTraceBack_Here(PyFrameObject *frame)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000130{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000131 PyThreadState *tstate = frame->f_tstate;
132 tracebackobject *oldtb = (tracebackobject *) tstate->curexc_traceback;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000133 tracebackobject *tb = newtracebackobject(oldtb, frame);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000134 if (tb == NULL)
135 return -1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000136 tstate->curexc_traceback = (PyObject *)tb;
137 Py_XDECREF(oldtb);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000138 return 0;
139}
140
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000141static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000142tb_displayline(PyObject *f, char *filename, int lineno, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000143{
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000144 int err = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000145 FILE *xfp;
Tim Peters6d20b432001-11-27 20:30:42 +0000146 char linebuf[2000];
Guido van Rossum3f5da241990-12-20 15:06:42 +0000147 int i;
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000148 if (filename == NULL || name == NULL)
149 return -1;
Guido van Rossum13836d91994-08-29 12:09:58 +0000150#ifdef MPW
151 /* This is needed by MPW's File and Line commands */
Tim Peters6d20b432001-11-27 20:30:42 +0000152#define FMT " File \"%.500s\"; line %d # in %.500s\n"
Guido van Rossum13836d91994-08-29 12:09:58 +0000153#else
154 /* This is needed by Emacs' compile command */
Tim Peters6d20b432001-11-27 20:30:42 +0000155#define FMT " File \"%.500s\", line %d, in %.500s\n"
Guido van Rossum13836d91994-08-29 12:09:58 +0000156#endif
Jack Jansen7b8c7542002-04-14 20:12:41 +0000157 xfp = fopen(filename, "r" PY_STDIOTEXTMODE);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000158 if (xfp == NULL) {
Guido van Rossum7169dbb1992-02-26 15:17:59 +0000159 /* Search tail of filename in sys.path before giving up */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000160 PyObject *path;
Guido van Rossum7169dbb1992-02-26 15:17:59 +0000161 char *tail = strrchr(filename, SEP);
162 if (tail == NULL)
163 tail = filename;
164 else
165 tail++;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000166 path = PySys_GetObject("path");
167 if (path != NULL && PyList_Check(path)) {
168 int npath = PyList_Size(path);
Guido van Rossum582acec2000-06-28 22:07:35 +0000169 size_t taillen = strlen(tail);
Guido van Rossum7169dbb1992-02-26 15:17:59 +0000170 char namebuf[MAXPATHLEN+1];
171 for (i = 0; i < npath; i++) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000172 PyObject *v = PyList_GetItem(path, i);
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000173 if (v == NULL) {
174 PyErr_Clear();
175 break;
176 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000177 if (PyString_Check(v)) {
Guido van Rossum582acec2000-06-28 22:07:35 +0000178 size_t len;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000179 len = PyString_Size(v);
Guido van Rossumbfd5d7551994-09-29 09:38:04 +0000180 if (len + 1 + taillen >= MAXPATHLEN)
181 continue; /* Too long */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000182 strcpy(namebuf, PyString_AsString(v));
Guido van Rossum582acec2000-06-28 22:07:35 +0000183 if (strlen(namebuf) != len)
Guido van Rossumbfd5d7551994-09-29 09:38:04 +0000184 continue; /* v contains '\0' */
Guido van Rossum7169dbb1992-02-26 15:17:59 +0000185 if (len > 0 && namebuf[len-1] != SEP)
186 namebuf[len++] = SEP;
187 strcpy(namebuf+len, tail);
Jack Jansen7b8c7542002-04-14 20:12:41 +0000188 xfp = fopen(namebuf, "r" PY_STDIOTEXTMODE);
Guido van Rossum7169dbb1992-02-26 15:17:59 +0000189 if (xfp != NULL) {
190 filename = namebuf;
191 break;
192 }
193 }
194 }
195 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000196 }
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000197 PyOS_snprintf(linebuf, sizeof(linebuf), FMT, filename, lineno, name);
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000198 err = PyFile_WriteString(linebuf, f);
199 if (xfp == NULL || err != 0)
200 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000201 for (i = 0; i < lineno; i++) {
Guido van Rossum3aca6531999-09-18 20:49:39 +0000202 char* pLastChar = &linebuf[sizeof(linebuf)-2];
203 do {
204 *pLastChar = '\0';
Jack Jansen7b8c7542002-04-14 20:12:41 +0000205 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, xfp, NULL) == NULL)
Guido van Rossum3aca6531999-09-18 20:49:39 +0000206 break;
207 /* fgets read *something*; if it didn't get as
208 far as pLastChar, it must have found a newline
209 or hit the end of the file; if pLastChar is \n,
210 it obviously found a newline; else we haven't
211 yet seen a newline, so must continue */
212 } while (*pLastChar != '\0' && *pLastChar != '\n');
Guido van Rossum3f5da241990-12-20 15:06:42 +0000213 }
214 if (i == lineno) {
Guido van Rossum7169dbb1992-02-26 15:17:59 +0000215 char *p = linebuf;
Guido van Rossume78c5d01995-07-07 22:45:41 +0000216 while (*p == ' ' || *p == '\t' || *p == '\014')
Guido van Rossum3f5da241990-12-20 15:06:42 +0000217 p++;
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000218 err = PyFile_WriteString(" ", f);
219 if (err == 0) {
220 err = PyFile_WriteString(p, f);
221 if (err == 0 && strchr(p, '\n') == NULL)
222 err = PyFile_WriteString("\n", f);
223 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000224 }
225 fclose(xfp);
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000226 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000227}
228
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000229static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000230tb_printinternal(tracebackobject *tb, PyObject *f, int limit)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000231{
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000232 int err = 0;
Guido van Rossum67a5fdb1993-12-17 12:09:14 +0000233 int depth = 0;
234 tracebackobject *tb1 = tb;
235 while (tb1 != NULL) {
236 depth++;
237 tb1 = tb1->tb_next;
238 }
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000239 while (tb != NULL && err == 0) {
Guido van Rossumb3f515a1997-01-24 04:02:55 +0000240 if (depth <= limit) {
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000241 err = tb_displayline(f,
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000242 PyString_AsString(
243 tb->tb_frame->f_code->co_filename),
Guido van Rossum13836d91994-08-29 12:09:58 +0000244 tb->tb_lineno,
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000245 PyString_AsString(tb->tb_frame->f_code->co_name));
Guido van Rossumb3f515a1997-01-24 04:02:55 +0000246 }
Guido van Rossum67a5fdb1993-12-17 12:09:14 +0000247 depth--;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000248 tb = tb->tb_next;
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000249 if (err == 0)
250 err = PyErr_CheckSignals();
Guido van Rossum3f5da241990-12-20 15:06:42 +0000251 }
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000252 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000253}
254
255int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000256PyTraceBack_Print(PyObject *v, PyObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000257{
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000258 int err;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000259 PyObject *limitv;
Guido van Rossum67a5fdb1993-12-17 12:09:14 +0000260 int limit = 1000;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000261 if (v == NULL)
262 return 0;
Tim Petersd7c36522001-10-22 19:34:09 +0000263 if (!PyTraceBack_Check(v)) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000264 PyErr_BadInternalCall();
Guido van Rossum3f5da241990-12-20 15:06:42 +0000265 return -1;
266 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000267 limitv = PySys_GetObject("tracebacklimit");
268 if (limitv && PyInt_Check(limitv)) {
269 limit = PyInt_AsLong(limitv);
Guido van Rossum67a5fdb1993-12-17 12:09:14 +0000270 if (limit <= 0)
271 return 0;
272 }
Guido van Rossum6d108872000-03-31 00:39:23 +0000273 err = PyFile_WriteString("Traceback (most recent call last):\n", f);
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000274 if (!err)
275 err = tb_printinternal((tracebackobject *)v, f, limit);
276 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000277}