This commit was manufactured by cvs2svn to create tag 'r234c1'.
[python/dscho.git] / Python / traceback.c
blobb8edf139644da6db8fc845202fb598815f1b743d
2 /* Traceback implementation */
4 #include "Python.h"
6 #include "compile.h"
7 #include "frameobject.h"
8 #include "structmember.h"
9 #include "osdefs.h"
11 typedef struct _tracebackobject {
12 PyObject_HEAD
13 struct _tracebackobject *tb_next;
14 PyFrameObject *tb_frame;
15 int tb_lasti;
16 int tb_lineno;
17 } tracebackobject;
19 #define OFF(x) offsetof(tracebackobject, x)
21 static 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 */
29 static PyObject *
30 tb_getattr(tracebackobject *tb, char *name)
32 return PyMember_Get((char *)tb, tb_memberlist, name);
35 static void
36 tb_dealloc(tracebackobject *tb)
38 PyObject_GC_UnTrack(tb);
39 Py_TRASHCAN_SAFE_BEGIN(tb)
40 Py_XDECREF(tb->tb_next);
41 Py_XDECREF(tb->tb_frame);
42 PyObject_GC_Del(tb);
43 Py_TRASHCAN_SAFE_END(tb)
46 static int
47 tb_traverse(tracebackobject *tb, visitproc visit, void *arg)
49 int err = 0;
50 if (tb->tb_next) {
51 err = visit((PyObject *)tb->tb_next, arg);
52 if (err)
53 return err;
55 if (tb->tb_frame)
56 err = visit((PyObject *)tb->tb_frame, arg);
57 return err;
60 static void
61 tb_clear(tracebackobject *tb)
63 Py_XDECREF(tb->tb_next);
64 Py_XDECREF(tb->tb_frame);
65 tb->tb_next = NULL;
66 tb->tb_frame = NULL;
69 PyTypeObject PyTraceBack_Type = {
70 PyObject_HEAD_INIT(&PyType_Type)
72 "traceback",
73 sizeof(tracebackobject),
75 (destructor)tb_dealloc, /*tp_dealloc*/
76 0, /*tp_print*/
77 (getattrfunc)tb_getattr, /*tp_getattr*/
78 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*/
84 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 */
105 static tracebackobject *
106 newtracebackobject(tracebackobject *next, PyFrameObject *frame)
108 tracebackobject *tb;
109 if ((next != NULL && !PyTraceBack_Check(next)) ||
110 frame == NULL || !PyFrame_Check(frame)) {
111 PyErr_BadInternalCall();
112 return NULL;
114 tb = PyObject_GC_New(tracebackobject, &PyTraceBack_Type);
115 if (tb != NULL) {
116 Py_XINCREF(next);
117 tb->tb_next = next;
118 Py_XINCREF(frame);
119 tb->tb_frame = frame;
120 tb->tb_lasti = frame->f_lasti;
121 tb->tb_lineno = PyCode_Addr2Line(frame->f_code,
122 frame->f_lasti);
123 PyObject_GC_Track(tb);
125 return tb;
129 PyTraceBack_Here(PyFrameObject *frame)
131 PyThreadState *tstate = frame->f_tstate;
132 tracebackobject *oldtb = (tracebackobject *) tstate->curexc_traceback;
133 tracebackobject *tb = newtracebackobject(oldtb, frame);
134 if (tb == NULL)
135 return -1;
136 tstate->curexc_traceback = (PyObject *)tb;
137 Py_XDECREF(oldtb);
138 return 0;
141 static int
142 tb_displayline(PyObject *f, char *filename, int lineno, char *name)
144 int err = 0;
145 FILE *xfp;
146 char linebuf[2000];
147 int i;
148 if (filename == NULL || name == NULL)
149 return -1;
150 #ifdef MPW
151 /* This is needed by MPW's File and Line commands */
152 #define FMT " File \"%.500s\"; line %d # in %.500s\n"
153 #else
154 /* This is needed by Emacs' compile command */
155 #define FMT " File \"%.500s\", line %d, in %.500s\n"
156 #endif
157 xfp = fopen(filename, "r" PY_STDIOTEXTMODE);
158 if (xfp == NULL) {
159 /* Search tail of filename in sys.path before giving up */
160 PyObject *path;
161 char *tail = strrchr(filename, SEP);
162 if (tail == NULL)
163 tail = filename;
164 else
165 tail++;
166 path = PySys_GetObject("path");
167 if (path != NULL && PyList_Check(path)) {
168 int npath = PyList_Size(path);
169 size_t taillen = strlen(tail);
170 char namebuf[MAXPATHLEN+1];
171 for (i = 0; i < npath; i++) {
172 PyObject *v = PyList_GetItem(path, i);
173 if (v == NULL) {
174 PyErr_Clear();
175 break;
177 if (PyString_Check(v)) {
178 size_t len;
179 len = PyString_Size(v);
180 if (len + 1 + taillen >= MAXPATHLEN)
181 continue; /* Too long */
182 strcpy(namebuf, PyString_AsString(v));
183 if (strlen(namebuf) != len)
184 continue; /* v contains '\0' */
185 if (len > 0 && namebuf[len-1] != SEP)
186 namebuf[len++] = SEP;
187 strcpy(namebuf+len, tail);
188 xfp = fopen(namebuf, "r" PY_STDIOTEXTMODE);
189 if (xfp != NULL) {
190 filename = namebuf;
191 break;
197 PyOS_snprintf(linebuf, sizeof(linebuf), FMT, filename, lineno, name);
198 err = PyFile_WriteString(linebuf, f);
199 if (xfp == NULL || err != 0)
200 return err;
201 for (i = 0; i < lineno; i++) {
202 char* pLastChar = &linebuf[sizeof(linebuf)-2];
203 do {
204 *pLastChar = '\0';
205 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, xfp, NULL) == NULL)
206 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');
214 if (i == lineno) {
215 char *p = linebuf;
216 while (*p == ' ' || *p == '\t' || *p == '\014')
217 p++;
218 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);
225 fclose(xfp);
226 return err;
229 static int
230 tb_printinternal(tracebackobject *tb, PyObject *f, int limit)
232 int err = 0;
233 int depth = 0;
234 tracebackobject *tb1 = tb;
235 while (tb1 != NULL) {
236 depth++;
237 tb1 = tb1->tb_next;
239 while (tb != NULL && err == 0) {
240 if (depth <= limit) {
241 err = tb_displayline(f,
242 PyString_AsString(
243 tb->tb_frame->f_code->co_filename),
244 tb->tb_lineno,
245 PyString_AsString(tb->tb_frame->f_code->co_name));
247 depth--;
248 tb = tb->tb_next;
249 if (err == 0)
250 err = PyErr_CheckSignals();
252 return err;
256 PyTraceBack_Print(PyObject *v, PyObject *f)
258 int err;
259 PyObject *limitv;
260 int limit = 1000;
261 if (v == NULL)
262 return 0;
263 if (!PyTraceBack_Check(v)) {
264 PyErr_BadInternalCall();
265 return -1;
267 limitv = PySys_GetObject("tracebacklimit");
268 if (limitv && PyInt_Check(limitv)) {
269 limit = PyInt_AsLong(limitv);
270 if (limit <= 0)
271 return 0;
273 err = PyFile_WriteString("Traceback (most recent call last):\n", f);
274 if (!err)
275 err = tb_printinternal((tracebackobject *)v, f, limit);
276 return err;