blob: c17dbe2d6e5aed5839efd4f5b679c17815854280 [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{
Guido van Rossumd724b232000-03-13 16:01:29 +000038 Py_TRASHCAN_SAFE_BEGIN(tb)
Guido van Rossum65bf9f21997-04-29 18:33:38 +000039 Py_XDECREF(tb->tb_next);
40 Py_XDECREF(tb->tb_frame);
Guido van Rossumb18618d2000-05-03 23:44:39 +000041 PyObject_DEL(tb);
Guido van Rossumd724b232000-03-13 16:01:29 +000042 Py_TRASHCAN_SAFE_END(tb)
Guido van Rossum3f5da241990-12-20 15:06:42 +000043}
44
Guido van Rossum9d78d8d1995-09-18 21:29:36 +000045#define Tracebacktype PyTraceBack_Type
46#define is_tracebackobject PyTraceBack_Check
Guido van Rossum681d79a1995-07-18 14:51:37 +000047
Guido van Rossum65bf9f21997-04-29 18:33:38 +000048PyTypeObject Tracebacktype = {
49 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum3f5da241990-12-20 15:06:42 +000050 0,
51 "traceback",
52 sizeof(tracebackobject),
53 0,
Guido van Rossum13836d91994-08-29 12:09:58 +000054 (destructor)tb_dealloc, /*tp_dealloc*/
Guido van Rossum3f5da241990-12-20 15:06:42 +000055 0, /*tp_print*/
Guido van Rossum13836d91994-08-29 12:09:58 +000056 (getattrfunc)tb_getattr, /*tp_getattr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +000057 0, /*tp_setattr*/
58 0, /*tp_compare*/
59 0, /*tp_repr*/
60 0, /*tp_as_number*/
61 0, /*tp_as_sequence*/
62 0, /*tp_as_mapping*/
63};
64
Guido van Rossum3f5da241990-12-20 15:06:42 +000065static tracebackobject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000066newtracebackobject(tracebackobject *next, PyFrameObject *frame, int lasti,
67 int lineno)
Guido van Rossum3f5da241990-12-20 15:06:42 +000068{
69 tracebackobject *tb;
70 if ((next != NULL && !is_tracebackobject(next)) ||
Guido van Rossum65bf9f21997-04-29 18:33:38 +000071 frame == NULL || !PyFrame_Check(frame)) {
72 PyErr_BadInternalCall();
Guido van Rossum3f5da241990-12-20 15:06:42 +000073 return NULL;
74 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +000075 tb = PyObject_NEW(tracebackobject, &Tracebacktype);
Guido van Rossum3f5da241990-12-20 15:06:42 +000076 if (tb != NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +000077 Py_XINCREF(next);
Guido van Rossum3f5da241990-12-20 15:06:42 +000078 tb->tb_next = next;
Guido van Rossum65bf9f21997-04-29 18:33:38 +000079 Py_XINCREF(frame);
Guido van Rossum3f5da241990-12-20 15:06:42 +000080 tb->tb_frame = frame;
81 tb->tb_lasti = lasti;
82 tb->tb_lineno = lineno;
83 }
84 return tb;
85}
86
Guido van Rossum3f5da241990-12-20 15:06:42 +000087int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000088PyTraceBack_Here(PyFrameObject *frame)
Guido van Rossum3f5da241990-12-20 15:06:42 +000089{
Guido van Rossuma027efa1997-05-05 20:56:21 +000090 PyThreadState *tstate = frame->f_tstate;
91 tracebackobject *oldtb = (tracebackobject *) tstate->curexc_traceback;
92 tracebackobject *tb = newtracebackobject(oldtb,
93 frame, frame->f_lasti, frame->f_lineno);
Guido van Rossum3f5da241990-12-20 15:06:42 +000094 if (tb == NULL)
95 return -1;
Guido van Rossuma027efa1997-05-05 20:56:21 +000096 tstate->curexc_traceback = (PyObject *)tb;
97 Py_XDECREF(oldtb);
Guido van Rossum3f5da241990-12-20 15:06:42 +000098 return 0;
99}
100
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000101static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000102tb_displayline(PyObject *f, char *filename, int lineno, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000103{
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000104 int err = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000105 FILE *xfp;
Guido van Rossum7169dbb1992-02-26 15:17:59 +0000106 char linebuf[1000];
Guido van Rossum3f5da241990-12-20 15:06:42 +0000107 int i;
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000108 if (filename == NULL || name == NULL)
109 return -1;
Guido van Rossum13836d91994-08-29 12:09:58 +0000110#ifdef MPW
111 /* This is needed by MPW's File and Line commands */
112#define FMT " File \"%.900s\"; line %d # in %s\n"
113#else
114 /* This is needed by Emacs' compile command */
115#define FMT " File \"%.900s\", line %d, in %s\n"
116#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000117 xfp = fopen(filename, "r");
118 if (xfp == NULL) {
Guido van Rossum7169dbb1992-02-26 15:17:59 +0000119 /* Search tail of filename in sys.path before giving up */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000120 PyObject *path;
Guido van Rossum7169dbb1992-02-26 15:17:59 +0000121 char *tail = strrchr(filename, SEP);
122 if (tail == NULL)
123 tail = filename;
124 else
125 tail++;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000126 path = PySys_GetObject("path");
127 if (path != NULL && PyList_Check(path)) {
128 int npath = PyList_Size(path);
Guido van Rossum582acec2000-06-28 22:07:35 +0000129 size_t taillen = strlen(tail);
Guido van Rossum7169dbb1992-02-26 15:17:59 +0000130 char namebuf[MAXPATHLEN+1];
131 for (i = 0; i < npath; i++) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000132 PyObject *v = PyList_GetItem(path, i);
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000133 if (v == NULL) {
134 PyErr_Clear();
135 break;
136 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000137 if (PyString_Check(v)) {
Guido van Rossum582acec2000-06-28 22:07:35 +0000138 size_t len;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000139 len = PyString_Size(v);
Guido van Rossumbfd5d7551994-09-29 09:38:04 +0000140 if (len + 1 + taillen >= MAXPATHLEN)
141 continue; /* Too long */
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000142 strcpy(namebuf, PyString_AsString(v));
Guido van Rossum582acec2000-06-28 22:07:35 +0000143 if (strlen(namebuf) != len)
Guido van Rossumbfd5d7551994-09-29 09:38:04 +0000144 continue; /* v contains '\0' */
Guido van Rossum7169dbb1992-02-26 15:17:59 +0000145 if (len > 0 && namebuf[len-1] != SEP)
146 namebuf[len++] = SEP;
147 strcpy(namebuf+len, tail);
148 xfp = fopen(namebuf, "r");
149 if (xfp != NULL) {
150 filename = namebuf;
151 break;
152 }
153 }
154 }
155 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000156 }
Guido van Rossum13836d91994-08-29 12:09:58 +0000157 sprintf(linebuf, FMT, filename, lineno, name);
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000158 err = PyFile_WriteString(linebuf, f);
159 if (xfp == NULL || err != 0)
160 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000161 for (i = 0; i < lineno; i++) {
Guido van Rossum3aca6531999-09-18 20:49:39 +0000162 char* pLastChar = &linebuf[sizeof(linebuf)-2];
163 do {
164 *pLastChar = '\0';
165 if (fgets(linebuf, sizeof linebuf, xfp) == NULL)
166 break;
167 /* fgets read *something*; if it didn't get as
168 far as pLastChar, it must have found a newline
169 or hit the end of the file; if pLastChar is \n,
170 it obviously found a newline; else we haven't
171 yet seen a newline, so must continue */
172 } while (*pLastChar != '\0' && *pLastChar != '\n');
Guido van Rossum3f5da241990-12-20 15:06:42 +0000173 }
174 if (i == lineno) {
Guido van Rossum7169dbb1992-02-26 15:17:59 +0000175 char *p = linebuf;
Guido van Rossume78c5d01995-07-07 22:45:41 +0000176 while (*p == ' ' || *p == '\t' || *p == '\014')
Guido van Rossum3f5da241990-12-20 15:06:42 +0000177 p++;
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000178 err = PyFile_WriteString(" ", f);
179 if (err == 0) {
180 err = PyFile_WriteString(p, f);
181 if (err == 0 && strchr(p, '\n') == NULL)
182 err = PyFile_WriteString("\n", f);
183 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000184 }
185 fclose(xfp);
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000186 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000187}
188
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000189static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000190tb_printinternal(tracebackobject *tb, PyObject *f, int limit)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000191{
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000192 int err = 0;
Guido van Rossum67a5fdb1993-12-17 12:09:14 +0000193 int depth = 0;
194 tracebackobject *tb1 = tb;
195 while (tb1 != NULL) {
196 depth++;
197 tb1 = tb1->tb_next;
198 }
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000199 while (tb != NULL && err == 0) {
Guido van Rossumb3f515a1997-01-24 04:02:55 +0000200 if (depth <= limit) {
Guido van Rossum73237c51997-05-05 20:53:25 +0000201 if (Py_OptimizeFlag)
202 tb->tb_lineno = PyCode_Addr2Line(
203 tb->tb_frame->f_code, tb->tb_lasti);
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000204 err = tb_displayline(f,
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000205 PyString_AsString(
206 tb->tb_frame->f_code->co_filename),
Guido van Rossum13836d91994-08-29 12:09:58 +0000207 tb->tb_lineno,
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000208 PyString_AsString(tb->tb_frame->f_code->co_name));
Guido van Rossumb3f515a1997-01-24 04:02:55 +0000209 }
Guido van Rossum67a5fdb1993-12-17 12:09:14 +0000210 depth--;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000211 tb = tb->tb_next;
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000212 if (err == 0)
213 err = PyErr_CheckSignals();
Guido van Rossum3f5da241990-12-20 15:06:42 +0000214 }
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000215 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000216}
217
218int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000219PyTraceBack_Print(PyObject *v, PyObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000220{
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000221 int err;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000222 PyObject *limitv;
Guido van Rossum67a5fdb1993-12-17 12:09:14 +0000223 int limit = 1000;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000224 if (v == NULL)
225 return 0;
226 if (!is_tracebackobject(v)) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000227 PyErr_BadInternalCall();
Guido van Rossum3f5da241990-12-20 15:06:42 +0000228 return -1;
229 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000230 limitv = PySys_GetObject("tracebacklimit");
231 if (limitv && PyInt_Check(limitv)) {
232 limit = PyInt_AsLong(limitv);
Guido van Rossum67a5fdb1993-12-17 12:09:14 +0000233 if (limit <= 0)
234 return 0;
235 }
Guido van Rossum6d108872000-03-31 00:39:23 +0000236 err = PyFile_WriteString("Traceback (most recent call last):\n", f);
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000237 if (!err)
238 err = tb_printinternal((tracebackobject *)v, f, limit);
239 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000240}