Fix HAVE_DECL_ISINF/ISNAN test (again).
[python.git] / Python / traceback.c
blob5df7694e66a43ae6d1e745cd366f1446aa7434d8
2 /* Traceback implementation */
4 #include "Python.h"
6 #include "code.h"
7 #include "frameobject.h"
8 #include "structmember.h"
9 #include "osdefs.h"
10 #include "traceback.h"
12 #define OFF(x) offsetof(PyTracebackObject, x)
14 static struct memberlist tb_memberlist[] = {
15 {"tb_next", T_OBJECT, OFF(tb_next)},
16 {"tb_frame", T_OBJECT, OFF(tb_frame)},
17 {"tb_lasti", T_INT, OFF(tb_lasti)},
18 {"tb_lineno", T_INT, OFF(tb_lineno)},
19 {NULL} /* Sentinel */
22 static PyObject *
23 tb_getattr(PyTracebackObject *tb, char *name)
25 return PyMember_Get((char *)tb, tb_memberlist, name);
28 static void
29 tb_dealloc(PyTracebackObject *tb)
31 PyObject_GC_UnTrack(tb);
32 Py_TRASHCAN_SAFE_BEGIN(tb)
33 Py_XDECREF(tb->tb_next);
34 Py_XDECREF(tb->tb_frame);
35 PyObject_GC_Del(tb);
36 Py_TRASHCAN_SAFE_END(tb)
39 static int
40 tb_traverse(PyTracebackObject *tb, visitproc visit, void *arg)
42 Py_VISIT(tb->tb_next);
43 Py_VISIT(tb->tb_frame);
44 return 0;
47 static void
48 tb_clear(PyTracebackObject *tb)
50 Py_CLEAR(tb->tb_next);
51 Py_CLEAR(tb->tb_frame);
54 PyTypeObject PyTraceBack_Type = {
55 PyVarObject_HEAD_INIT(&PyType_Type, 0)
56 "traceback",
57 sizeof(PyTracebackObject),
59 (destructor)tb_dealloc, /*tp_dealloc*/
60 0, /*tp_print*/
61 (getattrfunc)tb_getattr, /*tp_getattr*/
62 0, /*tp_setattr*/
63 0, /*tp_compare*/
64 0, /*tp_repr*/
65 0, /*tp_as_number*/
66 0, /*tp_as_sequence*/
67 0, /*tp_as_mapping*/
68 0, /* tp_hash */
69 0, /* tp_call */
70 0, /* tp_str */
71 0, /* tp_getattro */
72 0, /* tp_setattro */
73 0, /* tp_as_buffer */
74 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
75 0, /* tp_doc */
76 (traverseproc)tb_traverse, /* tp_traverse */
77 (inquiry)tb_clear, /* tp_clear */
78 0, /* tp_richcompare */
79 0, /* tp_weaklistoffset */
80 0, /* tp_iter */
81 0, /* tp_iternext */
82 0, /* tp_methods */
83 0, /* tp_members */
84 0, /* tp_getset */
85 0, /* tp_base */
86 0, /* tp_dict */
89 static PyTracebackObject *
90 newtracebackobject(PyTracebackObject *next, PyFrameObject *frame)
92 PyTracebackObject *tb;
93 if ((next != NULL && !PyTraceBack_Check(next)) ||
94 frame == NULL || !PyFrame_Check(frame)) {
95 PyErr_BadInternalCall();
96 return NULL;
98 tb = PyObject_GC_New(PyTracebackObject, &PyTraceBack_Type);
99 if (tb != NULL) {
100 Py_XINCREF(next);
101 tb->tb_next = next;
102 Py_XINCREF(frame);
103 tb->tb_frame = frame;
104 tb->tb_lasti = frame->f_lasti;
105 tb->tb_lineno = PyCode_Addr2Line(frame->f_code,
106 frame->f_lasti);
107 PyObject_GC_Track(tb);
109 return tb;
113 PyTraceBack_Here(PyFrameObject *frame)
115 PyThreadState *tstate = PyThreadState_GET();
116 PyTracebackObject *oldtb = (PyTracebackObject *) tstate->curexc_traceback;
117 PyTracebackObject *tb = newtracebackobject(oldtb, frame);
118 if (tb == NULL)
119 return -1;
120 tstate->curexc_traceback = (PyObject *)tb;
121 Py_XDECREF(oldtb);
122 return 0;
126 _Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno, int indent)
128 int err = 0;
129 FILE *xfp = NULL;
130 char linebuf[2000];
131 int i;
132 char namebuf[MAXPATHLEN+1];
134 if (filename == NULL)
135 return -1;
136 /* This is needed by Emacs' compile command */
137 #define FMT " File \"%.500s\", line %d, in %.500s\n"
138 xfp = fopen(filename, "r" PY_STDIOTEXTMODE);
139 if (xfp == NULL) {
140 /* Search tail of filename in sys.path before giving up */
141 PyObject *path;
142 const char *tail = strrchr(filename, SEP);
143 if (tail == NULL)
144 tail = filename;
145 else
146 tail++;
147 path = PySys_GetObject("path");
148 if (path != NULL && PyList_Check(path)) {
149 Py_ssize_t _npath = PyList_Size(path);
150 int npath = Py_SAFE_DOWNCAST(_npath, Py_ssize_t, int);
151 size_t taillen = strlen(tail);
152 for (i = 0; i < npath; i++) {
153 PyObject *v = PyList_GetItem(path, i);
154 if (v == NULL) {
155 PyErr_Clear();
156 break;
158 if (PyString_Check(v)) {
159 size_t len;
160 len = PyString_GET_SIZE(v);
161 if (len + 1 + taillen >= MAXPATHLEN)
162 continue; /* Too long */
163 strcpy(namebuf, PyString_AsString(v));
164 if (strlen(namebuf) != len)
165 continue; /* v contains '\0' */
166 if (len > 0 && namebuf[len-1] != SEP)
167 namebuf[len++] = SEP;
168 strcpy(namebuf+len, tail);
169 xfp = fopen(namebuf, "r" PY_STDIOTEXTMODE);
170 if (xfp != NULL) {
171 filename = namebuf;
172 break;
179 if (xfp == NULL)
180 return err;
181 if (err != 0) {
182 fclose(xfp);
183 return err;
186 for (i = 0; i < lineno; i++) {
187 char* pLastChar = &linebuf[sizeof(linebuf)-2];
188 do {
189 *pLastChar = '\0';
190 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, xfp, NULL) == NULL)
191 break;
192 /* fgets read *something*; if it didn't get as
193 far as pLastChar, it must have found a newline
194 or hit the end of the file; if pLastChar is \n,
195 it obviously found a newline; else we haven't
196 yet seen a newline, so must continue */
197 } while (*pLastChar != '\0' && *pLastChar != '\n');
199 if (i == lineno) {
200 char buf[11];
201 char *p = linebuf;
202 while (*p == ' ' || *p == '\t' || *p == '\014')
203 p++;
205 /* Write some spaces before the line */
206 strcpy(buf, " ");
207 assert (strlen(buf) == 10);
208 while (indent > 0) {
209 if(indent < 10)
210 buf[indent] = '\0';
211 err = PyFile_WriteString(buf, f);
212 if (err != 0)
213 break;
214 indent -= 10;
217 if (err == 0)
218 err = PyFile_WriteString(p, f);
219 if (err == 0 && strchr(p, '\n') == NULL)
220 err = PyFile_WriteString("\n", f);
222 fclose(xfp);
223 return err;
226 static int
227 tb_displayline(PyObject *f, const char *filename, int lineno, const char *name)
229 int err = 0;
230 char linebuf[2000];
232 if (filename == NULL || name == NULL)
233 return -1;
234 /* This is needed by Emacs' compile command */
235 #define FMT " File \"%.500s\", line %d, in %.500s\n"
236 PyOS_snprintf(linebuf, sizeof(linebuf), FMT, filename, lineno, name);
237 err = PyFile_WriteString(linebuf, f);
238 if (err != 0)
239 return err;
240 return _Py_DisplaySourceLine(f, filename, lineno, 4);
243 static int
244 tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit)
246 int err = 0;
247 long depth = 0;
248 PyTracebackObject *tb1 = tb;
249 while (tb1 != NULL) {
250 depth++;
251 tb1 = tb1->tb_next;
253 while (tb != NULL && err == 0) {
254 if (depth <= limit) {
255 err = tb_displayline(f,
256 PyString_AsString(
257 tb->tb_frame->f_code->co_filename),
258 tb->tb_lineno,
259 PyString_AsString(tb->tb_frame->f_code->co_name));
261 depth--;
262 tb = tb->tb_next;
263 if (err == 0)
264 err = PyErr_CheckSignals();
266 return err;
270 PyTraceBack_Print(PyObject *v, PyObject *f)
272 int err;
273 PyObject *limitv;
274 long limit = 1000;
275 if (v == NULL)
276 return 0;
277 if (!PyTraceBack_Check(v)) {
278 PyErr_BadInternalCall();
279 return -1;
281 limitv = PySys_GetObject("tracebacklimit");
282 if (limitv && PyInt_Check(limitv)) {
283 limit = PyInt_AsLong(limitv);
284 if (limit <= 0)
285 return 0;
287 err = PyFile_WriteString("Traceback (most recent call last):\n", f);
288 if (!err)
289 err = tb_printinternal((PyTracebackObject *)v, f, limit);
290 return err;