traceback.c 6.18 KB
Newer Older
1

Guido van Rossum's avatar
Guido van Rossum committed
2 3
/* Traceback implementation */

Guido van Rossum's avatar
Guido van Rossum committed
4
#include "Python.h"
Guido van Rossum's avatar
Guido van Rossum committed
5 6 7 8

#include "compile.h"
#include "frameobject.h"
#include "structmember.h"
9
#include "osdefs.h"
10
#include "traceback.h"
Guido van Rossum's avatar
Guido van Rossum committed
11

12
#define OFF(x) offsetof(PyTracebackObject, x)
Guido van Rossum's avatar
Guido van Rossum committed
13 14 15 16 17 18 19 20 21

static struct memberlist tb_memberlist[] = {
	{"tb_next",	T_OBJECT,	OFF(tb_next)},
	{"tb_frame",	T_OBJECT,	OFF(tb_frame)},
	{"tb_lasti",	T_INT,		OFF(tb_lasti)},
	{"tb_lineno",	T_INT,		OFF(tb_lineno)},
	{NULL}	/* Sentinel */
};

Guido van Rossum's avatar
Guido van Rossum committed
22
static PyObject *
23
tb_getattr(PyTracebackObject *tb, char *name)
Guido van Rossum's avatar
Guido van Rossum committed
24
{
Guido van Rossum's avatar
Guido van Rossum committed
25
	return PyMember_Get((char *)tb, tb_memberlist, name);
Guido van Rossum's avatar
Guido van Rossum committed
26 27 28
}

static void
29
tb_dealloc(PyTracebackObject *tb)
Guido van Rossum's avatar
Guido van Rossum committed
30
{
31
	PyObject_GC_UnTrack(tb);
32
	Py_TRASHCAN_SAFE_BEGIN(tb)
Guido van Rossum's avatar
Guido van Rossum committed
33 34
	Py_XDECREF(tb->tb_next);
	Py_XDECREF(tb->tb_frame);
35
	PyObject_GC_Del(tb);
36
	Py_TRASHCAN_SAFE_END(tb)
Guido van Rossum's avatar
Guido van Rossum committed
37 38
}

39
static int
40
tb_traverse(PyTracebackObject *tb, visitproc visit, void *arg)
41 42 43 44 45 46 47 48 49 50 51 52 53
{
	int err = 0;
	if (tb->tb_next) {
		err = visit((PyObject *)tb->tb_next, arg);
		if (err)
			return err;
	}
	if (tb->tb_frame) 
		err = visit((PyObject *)tb->tb_frame, arg);
	return err;
}

static void
54
tb_clear(PyTracebackObject *tb)
55 56 57 58 59 60 61
{
	Py_XDECREF(tb->tb_next);
	Py_XDECREF(tb->tb_frame);
	tb->tb_next = NULL;
	tb->tb_frame = NULL;
}

62
PyTypeObject PyTraceBack_Type = {
Guido van Rossum's avatar
Guido van Rossum committed
63
	PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum's avatar
Guido van Rossum committed
64 65
	0,
	"traceback",
66
	sizeof(PyTracebackObject),
Guido van Rossum's avatar
Guido van Rossum committed
67
	0,
68
	(destructor)tb_dealloc, /*tp_dealloc*/
Guido van Rossum's avatar
Guido van Rossum committed
69
	0,		/*tp_print*/
70
	(getattrfunc)tb_getattr, /*tp_getattr*/
Guido van Rossum's avatar
Guido van Rossum committed
71 72 73 74 75 76
	0,		/*tp_setattr*/
	0,		/*tp_compare*/
	0,		/*tp_repr*/
	0,		/*tp_as_number*/
	0,		/*tp_as_sequence*/
	0,		/*tp_as_mapping*/
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
	0,		/* tp_hash */
	0,		/* tp_call */
	0,		/* tp_str */
	0,		/* tp_getattro */
	0,		/* tp_setattro */
	0,					/* tp_as_buffer */
	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
	0,             				/* tp_doc */
 	(traverseproc)tb_traverse,		/* tp_traverse */
	(inquiry)tb_clear,			/* tp_clear */
	0,					/* tp_richcompare */
	0,					/* tp_weaklistoffset */
	0,					/* tp_iter */
	0,					/* tp_iternext */
	0,					/* tp_methods */
	0,			/* tp_members */
	0,			/* tp_getset */
	0,					/* tp_base */
	0,					/* tp_dict */
Guido van Rossum's avatar
Guido van Rossum committed
96 97
};

98 99
static PyTracebackObject *
newtracebackobject(PyTracebackObject *next, PyFrameObject *frame)
Guido van Rossum's avatar
Guido van Rossum committed
100
{
101
	PyTracebackObject *tb;
102
	if ((next != NULL && !PyTraceBack_Check(next)) ||
Guido van Rossum's avatar
Guido van Rossum committed
103 104
			frame == NULL || !PyFrame_Check(frame)) {
		PyErr_BadInternalCall();
Guido van Rossum's avatar
Guido van Rossum committed
105 106
		return NULL;
	}
107
	tb = PyObject_GC_New(PyTracebackObject, &PyTraceBack_Type);
Guido van Rossum's avatar
Guido van Rossum committed
108
	if (tb != NULL) {
Guido van Rossum's avatar
Guido van Rossum committed
109
		Py_XINCREF(next);
Guido van Rossum's avatar
Guido van Rossum committed
110
		tb->tb_next = next;
Guido van Rossum's avatar
Guido van Rossum committed
111
		Py_XINCREF(frame);
Guido van Rossum's avatar
Guido van Rossum committed
112
		tb->tb_frame = frame;
Michael W. Hudson's avatar
Michael W. Hudson committed
113 114 115
		tb->tb_lasti = frame->f_lasti;
		tb->tb_lineno = PyCode_Addr2Line(frame->f_code, 
						 frame->f_lasti);
116
		PyObject_GC_Track(tb);
Guido van Rossum's avatar
Guido van Rossum committed
117 118 119 120 121
	}
	return tb;
}

int
122
PyTraceBack_Here(PyFrameObject *frame)
Guido van Rossum's avatar
Guido van Rossum committed
123
{
124
	PyThreadState *tstate = frame->f_tstate;
125 126
	PyTracebackObject *oldtb = (PyTracebackObject *) tstate->curexc_traceback;
	PyTracebackObject *tb = newtracebackobject(oldtb, frame);
Guido van Rossum's avatar
Guido van Rossum committed
127 128
	if (tb == NULL)
		return -1;
129 130
	tstate->curexc_traceback = (PyObject *)tb;
	Py_XDECREF(oldtb);
Guido van Rossum's avatar
Guido van Rossum committed
131 132 133
	return 0;
}

134
static int
135
tb_displayline(PyObject *f, char *filename, int lineno, char *name)
Guido van Rossum's avatar
Guido van Rossum committed
136
{
137
	int err = 0;
Guido van Rossum's avatar
Guido van Rossum committed
138
	FILE *xfp;
139
	char linebuf[2000];
Guido van Rossum's avatar
Guido van Rossum committed
140
	int i;
141 142
	if (filename == NULL || name == NULL)
		return -1;
143
	/* This is needed by Emacs' compile command */
144
#define FMT "  File \"%.500s\", line %d, in %.500s\n"
145
	xfp = fopen(filename, "r" PY_STDIOTEXTMODE);
Guido van Rossum's avatar
Guido van Rossum committed
146
	if (xfp == NULL) {
147
		/* Search tail of filename in sys.path before giving up */
Guido van Rossum's avatar
Guido van Rossum committed
148
		PyObject *path;
149 150 151 152 153
		char *tail = strrchr(filename, SEP);
		if (tail == NULL)
			tail = filename;
		else
			tail++;
Guido van Rossum's avatar
Guido van Rossum committed
154 155 156
		path = PySys_GetObject("path");
		if (path != NULL && PyList_Check(path)) {
			int npath = PyList_Size(path);
157
			size_t taillen = strlen(tail);
158 159
			char namebuf[MAXPATHLEN+1];
			for (i = 0; i < npath; i++) {
Guido van Rossum's avatar
Guido van Rossum committed
160
				PyObject *v = PyList_GetItem(path, i);
161 162 163 164
				if (v == NULL) {
					PyErr_Clear();
					break;
				}
Guido van Rossum's avatar
Guido van Rossum committed
165
				if (PyString_Check(v)) {
166
					size_t len;
Guido van Rossum's avatar
Guido van Rossum committed
167
					len = PyString_Size(v);
168 169
					if (len + 1 + taillen >= MAXPATHLEN)
						continue; /* Too long */
Guido van Rossum's avatar
Guido van Rossum committed
170
					strcpy(namebuf, PyString_AsString(v));
171
					if (strlen(namebuf) != len)
172
						continue; /* v contains '\0' */
173 174 175
					if (len > 0 && namebuf[len-1] != SEP)
						namebuf[len++] = SEP;
					strcpy(namebuf+len, tail);
176
					xfp = fopen(namebuf, "r" PY_STDIOTEXTMODE);
177 178 179 180 181 182 183
					if (xfp != NULL) {
						filename = namebuf;
						break;
					}
				}
			}
		}
Guido van Rossum's avatar
Guido van Rossum committed
184
	}
185
	PyOS_snprintf(linebuf, sizeof(linebuf), FMT, filename, lineno, name);
186 187 188
	err = PyFile_WriteString(linebuf, f);
	if (xfp == NULL || err != 0)
		return err;
Guido van Rossum's avatar
Guido van Rossum committed
189
	for (i = 0; i < lineno; i++) {
190 191 192
		char* pLastChar = &linebuf[sizeof(linebuf)-2];
		do {
			*pLastChar = '\0';
193
			if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, xfp, NULL) == NULL)
194 195 196 197 198 199 200
				break;
			/* fgets read *something*; if it didn't get as
			   far as pLastChar, it must have found a newline
			   or hit the end of the file;	if pLastChar is \n,
			   it obviously found a newline; else we haven't
			   yet seen a newline, so must continue */
		} while (*pLastChar != '\0' && *pLastChar != '\n');
Guido van Rossum's avatar
Guido van Rossum committed
201 202
	}
	if (i == lineno) {
203
		char *p = linebuf;
204
		while (*p == ' ' || *p == '\t' || *p == '\014')
Guido van Rossum's avatar
Guido van Rossum committed
205
			p++;
206 207 208 209 210 211
		err = PyFile_WriteString("    ", f);
		if (err == 0) {
			err = PyFile_WriteString(p, f);
			if (err == 0 && strchr(p, '\n') == NULL)
				err = PyFile_WriteString("\n", f);
		}
Guido van Rossum's avatar
Guido van Rossum committed
212 213
	}
	fclose(xfp);
214
	return err;
Guido van Rossum's avatar
Guido van Rossum committed
215 216
}

217
static int
218
tb_printinternal(PyTracebackObject *tb, PyObject *f, int limit)
Guido van Rossum's avatar
Guido van Rossum committed
219
{
220
	int err = 0;
221
	int depth = 0;
222
	PyTracebackObject *tb1 = tb;
223 224 225 226
	while (tb1 != NULL) {
		depth++;
		tb1 = tb1->tb_next;
	}
227
	while (tb != NULL && err == 0) {
228
		if (depth <= limit) {
229
			err = tb_displayline(f,
Guido van Rossum's avatar
Guido van Rossum committed
230 231
			    PyString_AsString(
				    tb->tb_frame->f_code->co_filename),
232
			    tb->tb_lineno,
Guido van Rossum's avatar
Guido van Rossum committed
233
			    PyString_AsString(tb->tb_frame->f_code->co_name));
234
		}
235
		depth--;
Guido van Rossum's avatar
Guido van Rossum committed
236
		tb = tb->tb_next;
237 238
		if (err == 0)
			err = PyErr_CheckSignals();
Guido van Rossum's avatar
Guido van Rossum committed
239
	}
240
	return err;
Guido van Rossum's avatar
Guido van Rossum committed
241 242 243
}

int
244
PyTraceBack_Print(PyObject *v, PyObject *f)
Guido van Rossum's avatar
Guido van Rossum committed
245
{
246
	int err;
Guido van Rossum's avatar
Guido van Rossum committed
247
	PyObject *limitv;
248
	int limit = 1000;
Guido van Rossum's avatar
Guido van Rossum committed
249 250
	if (v == NULL)
		return 0;
251
	if (!PyTraceBack_Check(v)) {
Guido van Rossum's avatar
Guido van Rossum committed
252
		PyErr_BadInternalCall();
Guido van Rossum's avatar
Guido van Rossum committed
253 254
		return -1;
	}
Guido van Rossum's avatar
Guido van Rossum committed
255 256 257
	limitv = PySys_GetObject("tracebacklimit");
	if (limitv && PyInt_Check(limitv)) {
		limit = PyInt_AsLong(limitv);
258 259 260
		if (limit <= 0)
			return 0;
	}
261
	err = PyFile_WriteString("Traceback (most recent call last):\n", f);
262
	if (!err)
263
		err = tb_printinternal((PyTracebackObject *)v, f, limit);
264
	return err;
Guido van Rossum's avatar
Guido van Rossum committed
265
}