blob: 987013ccf5a3e068e9bef062f803dda923117a1f [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 /* This is needed by Emacs' compile command */
Tim Peters6d20b432001-11-27 20:30:42 +0000151#define FMT " File \"%.500s\", line %d, in %.500s\n"
Jack Jansen7b8c7542002-04-14 20:12:41 +0000152 xfp = fopen(filename, "r" PY_STDIOTEXTMODE);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000153 if (xfp == NULL) {
Guido van Rossum7169dbb1992-02-26 15:17:59 +0000154 /* Search tail of filename in sys.path before giving up */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000155 PyObject *path;
Guido van Rossum7169dbb1992-02-26 15:17:59 +0000156 char *tail = strrchr(filename, SEP);
157 if (tail == NULL)
158 tail = filename;
159 else
160 tail++;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000161 path = PySys_GetObject("path");
162 if (path != NULL && PyList_Check(path)) {
163 int npath = PyList_Size(path);
Guido van Rossum582acec2000-06-28 22:07:35 +0000164 size_t taillen = strlen(tail);
Guido van Rossum7169dbb1992-02-26 15:17:59 +0000165 char namebuf[MAXPATHLEN+1];
166 for (i = 0; i < npath; i++) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000167 PyObject *v = PyList_GetItem(path, i);
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000168 if (v == NULL) {
169 PyErr_Clear();
170 break;
171 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000172 if (PyString_Check(v)) {
Guido van Rossum582acec2000-06-28 22:07:35 +0000173 size_t len;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000174 len = PyString_Size(v);
Guido van Rossumbfd5d7551994-09-29 09:38:04 +0000175 if (len + 1 + taillen >= MAXPATHLEN)
176 continue; /* Too long */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000177 strcpy(namebuf, PyString_AsString(v));
Guido van Rossum582acec2000-06-28 22:07:35 +0000178 if (strlen(namebuf) != len)
Guido van Rossumbfd5d7551994-09-29 09:38:04 +0000179 continue; /* v contains '\0' */
Guido van Rossum7169dbb1992-02-26 15:17:59 +0000180 if (len > 0 && namebuf[len-1] != SEP)
181 namebuf[len++] = SEP;
182 strcpy(namebuf+len, tail);
Jack Jansen7b8c7542002-04-14 20:12:41 +0000183 xfp = fopen(namebuf, "r" PY_STDIOTEXTMODE);
Guido van Rossum7169dbb1992-02-26 15:17:59 +0000184 if (xfp != NULL) {
185 filename = namebuf;
186 break;
187 }
188 }
189 }
190 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000191 }
Jeremy Hylton518ab1c2001-11-28 20:42:20 +0000192 PyOS_snprintf(linebuf, sizeof(linebuf), FMT, filename, lineno, name);
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000193 err = PyFile_WriteString(linebuf, f);
194 if (xfp == NULL || err != 0)
195 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000196 for (i = 0; i < lineno; i++) {
Guido van Rossum3aca6531999-09-18 20:49:39 +0000197 char* pLastChar = &linebuf[sizeof(linebuf)-2];
198 do {
199 *pLastChar = '\0';
Jack Jansen7b8c7542002-04-14 20:12:41 +0000200 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, xfp, NULL) == NULL)
Guido van Rossum3aca6531999-09-18 20:49:39 +0000201 break;
202 /* fgets read *something*; if it didn't get as
203 far as pLastChar, it must have found a newline
204 or hit the end of the file; if pLastChar is \n,
205 it obviously found a newline; else we haven't
206 yet seen a newline, so must continue */
207 } while (*pLastChar != '\0' && *pLastChar != '\n');
Guido van Rossum3f5da241990-12-20 15:06:42 +0000208 }
209 if (i == lineno) {
Guido van Rossum7169dbb1992-02-26 15:17:59 +0000210 char *p = linebuf;
Guido van Rossume78c5d01995-07-07 22:45:41 +0000211 while (*p == ' ' || *p == '\t' || *p == '\014')
Guido van Rossum3f5da241990-12-20 15:06:42 +0000212 p++;
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000213 err = PyFile_WriteString(" ", f);
214 if (err == 0) {
215 err = PyFile_WriteString(p, f);
216 if (err == 0 && strchr(p, '\n') == NULL)
217 err = PyFile_WriteString("\n", f);
218 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000219 }
220 fclose(xfp);
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000221 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000222}
223
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000224static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000225tb_printinternal(tracebackobject *tb, PyObject *f, int limit)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000226{
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000227 int err = 0;
Guido van Rossum67a5fdb1993-12-17 12:09:14 +0000228 int depth = 0;
229 tracebackobject *tb1 = tb;
230 while (tb1 != NULL) {
231 depth++;
232 tb1 = tb1->tb_next;
233 }
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000234 while (tb != NULL && err == 0) {
Guido van Rossumb3f515a1997-01-24 04:02:55 +0000235 if (depth <= limit) {
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000236 err = tb_displayline(f,
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000237 PyString_AsString(
238 tb->tb_frame->f_code->co_filename),
Guido van Rossum13836d91994-08-29 12:09:58 +0000239 tb->tb_lineno,
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000240 PyString_AsString(tb->tb_frame->f_code->co_name));
Guido van Rossumb3f515a1997-01-24 04:02:55 +0000241 }
Guido van Rossum67a5fdb1993-12-17 12:09:14 +0000242 depth--;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000243 tb = tb->tb_next;
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000244 if (err == 0)
245 err = PyErr_CheckSignals();
Guido van Rossum3f5da241990-12-20 15:06:42 +0000246 }
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000247 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000248}
249
250int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000251PyTraceBack_Print(PyObject *v, PyObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000252{
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000253 int err;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000254 PyObject *limitv;
Guido van Rossum67a5fdb1993-12-17 12:09:14 +0000255 int limit = 1000;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000256 if (v == NULL)
257 return 0;
Tim Petersd7c36522001-10-22 19:34:09 +0000258 if (!PyTraceBack_Check(v)) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000259 PyErr_BadInternalCall();
Guido van Rossum3f5da241990-12-20 15:06:42 +0000260 return -1;
261 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000262 limitv = PySys_GetObject("tracebacklimit");
263 if (limitv && PyInt_Check(limitv)) {
264 limit = PyInt_AsLong(limitv);
Guido van Rossum67a5fdb1993-12-17 12:09:14 +0000265 if (limit <= 0)
266 return 0;
267 }
Guido van Rossum6d108872000-03-31 00:39:23 +0000268 err = PyFile_WriteString("Traceback (most recent call last):\n", f);
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000269 if (!err)
270 err = tb_printinternal((tracebackobject *)v, f, limit);
271 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000272}