- Got rid of newmodule.c
[python/dscho.git] / Python / traceback.c
blobde918f9a203e6304ceca12bd9c628ab9564b51b3
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, int lasti,
107 int lineno)
109 tracebackobject *tb;
110 if ((next != NULL && !PyTraceBack_Check(next)) ||
111 frame == NULL || !PyFrame_Check(frame)) {
112 PyErr_BadInternalCall();
113 return NULL;
115 tb = PyObject_GC_New(tracebackobject, &PyTraceBack_Type);
116 if (tb != NULL) {
117 Py_XINCREF(next);
118 tb->tb_next = next;
119 Py_XINCREF(frame);
120 tb->tb_frame = frame;
121 tb->tb_lasti = lasti;
122 tb->tb_lineno = lineno;
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,
134 frame, frame->f_lasti, frame->f_lineno);
135 if (tb == NULL)
136 return -1;
137 tstate->curexc_traceback = (PyObject *)tb;
138 Py_XDECREF(oldtb);
139 return 0;
142 static int
143 tb_displayline(PyObject *f, char *filename, int lineno, char *name)
145 int err = 0;
146 FILE *xfp;
147 char linebuf[2000];
148 int i;
149 if (filename == NULL || name == NULL)
150 return -1;
151 #ifdef MPW
152 /* This is needed by MPW's File and Line commands */
153 #define FMT " File \"%.500s\"; line %d # in %.500s\n"
154 #else
155 /* This is needed by Emacs' compile command */
156 #define FMT " File \"%.500s\", line %d, in %.500s\n"
157 #endif
158 xfp = fopen(filename, "r" PY_STDIOTEXTMODE);
159 if (xfp == NULL) {
160 /* Search tail of filename in sys.path before giving up */
161 PyObject *path;
162 char *tail = strrchr(filename, SEP);
163 if (tail == NULL)
164 tail = filename;
165 else
166 tail++;
167 path = PySys_GetObject("path");
168 if (path != NULL && PyList_Check(path)) {
169 int npath = PyList_Size(path);
170 size_t taillen = strlen(tail);
171 char namebuf[MAXPATHLEN+1];
172 for (i = 0; i < npath; i++) {
173 PyObject *v = PyList_GetItem(path, i);
174 if (v == NULL) {
175 PyErr_Clear();
176 break;
178 if (PyString_Check(v)) {
179 size_t len;
180 len = PyString_Size(v);
181 if (len + 1 + taillen >= MAXPATHLEN)
182 continue; /* Too long */
183 strcpy(namebuf, PyString_AsString(v));
184 if (strlen(namebuf) != len)
185 continue; /* v contains '\0' */
186 if (len > 0 && namebuf[len-1] != SEP)
187 namebuf[len++] = SEP;
188 strcpy(namebuf+len, tail);
189 xfp = fopen(namebuf, "r" PY_STDIOTEXTMODE);
190 if (xfp != NULL) {
191 filename = namebuf;
192 break;
198 PyOS_snprintf(linebuf, sizeof(linebuf), FMT, filename, lineno, name);
199 err = PyFile_WriteString(linebuf, f);
200 if (xfp == NULL || err != 0)
201 return err;
202 for (i = 0; i < lineno; i++) {
203 char* pLastChar = &linebuf[sizeof(linebuf)-2];
204 do {
205 *pLastChar = '\0';
206 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, xfp, NULL) == NULL)
207 break;
208 /* fgets read *something*; if it didn't get as
209 far as pLastChar, it must have found a newline
210 or hit the end of the file; if pLastChar is \n,
211 it obviously found a newline; else we haven't
212 yet seen a newline, so must continue */
213 } while (*pLastChar != '\0' && *pLastChar != '\n');
215 if (i == lineno) {
216 char *p = linebuf;
217 while (*p == ' ' || *p == '\t' || *p == '\014')
218 p++;
219 err = PyFile_WriteString(" ", f);
220 if (err == 0) {
221 err = PyFile_WriteString(p, f);
222 if (err == 0 && strchr(p, '\n') == NULL)
223 err = PyFile_WriteString("\n", f);
226 fclose(xfp);
227 return err;
230 static int
231 tb_printinternal(tracebackobject *tb, PyObject *f, int limit)
233 int err = 0;
234 int depth = 0;
235 tracebackobject *tb1 = tb;
236 while (tb1 != NULL) {
237 depth++;
238 tb1 = tb1->tb_next;
240 while (tb != NULL && err == 0) {
241 if (depth <= limit) {
242 if (Py_OptimizeFlag)
243 tb->tb_lineno = PyCode_Addr2Line(
244 tb->tb_frame->f_code, tb->tb_lasti);
245 err = tb_displayline(f,
246 PyString_AsString(
247 tb->tb_frame->f_code->co_filename),
248 tb->tb_lineno,
249 PyString_AsString(tb->tb_frame->f_code->co_name));
251 depth--;
252 tb = tb->tb_next;
253 if (err == 0)
254 err = PyErr_CheckSignals();
256 return err;
260 PyTraceBack_Print(PyObject *v, PyObject *f)
262 int err;
263 PyObject *limitv;
264 int limit = 1000;
265 if (v == NULL)
266 return 0;
267 if (!PyTraceBack_Check(v)) {
268 PyErr_BadInternalCall();
269 return -1;
271 limitv = PySys_GetObject("tracebacklimit");
272 if (limitv && PyInt_Check(limitv)) {
273 limit = PyInt_AsLong(limitv);
274 if (limit <= 0)
275 return 0;
277 err = PyFile_WriteString("Traceback (most recent call last):\n", f);
278 if (!err)
279 err = tb_printinternal((tracebackobject *)v, f, limit);
280 return err;