blob: bcec4904e20bfd04502a5b25f279562faa9ca3b3 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
2Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
3Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum3f5da241990-12-20 15:06:42 +000025/* Traceback implementation */
26
27#include "allobjects.h"
28
29#include "compile.h"
30#include "frameobject.h"
31#include "traceback.h"
32#include "structmember.h"
33
34typedef struct _tracebackobject {
35 OB_HEAD
36 struct _tracebackobject *tb_next;
37 frameobject *tb_frame;
38 int tb_lasti;
39 int tb_lineno;
40} tracebackobject;
41
42#define OFF(x) offsetof(tracebackobject, x)
43
44static struct memberlist tb_memberlist[] = {
45 {"tb_next", T_OBJECT, OFF(tb_next)},
46 {"tb_frame", T_OBJECT, OFF(tb_frame)},
47 {"tb_lasti", T_INT, OFF(tb_lasti)},
48 {"tb_lineno", T_INT, OFF(tb_lineno)},
49 {NULL} /* Sentinel */
50};
51
52static object *
53tb_getattr(tb, name)
54 tracebackobject *tb;
55 char *name;
56{
57 return getmember((char *)tb, tb_memberlist, name);
58}
59
60static void
61tb_dealloc(tb)
62 tracebackobject *tb;
63{
64 XDECREF(tb->tb_next);
65 XDECREF(tb->tb_frame);
66 DEL(tb);
67}
68
69static typeobject Tracebacktype = {
70 OB_HEAD_INIT(&Typetype)
71 0,
72 "traceback",
73 sizeof(tracebackobject),
74 0,
75 tb_dealloc, /*tp_dealloc*/
76 0, /*tp_print*/
77 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};
85
86#define is_tracebackobject(v) ((v)->ob_type == &Tracebacktype)
87
88static tracebackobject *
89newtracebackobject(next, frame, lasti, lineno)
90 tracebackobject *next;
91 frameobject *frame;
92 int lasti, lineno;
93{
94 tracebackobject *tb;
95 if ((next != NULL && !is_tracebackobject(next)) ||
96 frame == NULL || !is_frameobject(frame)) {
97 err_badcall();
98 return NULL;
99 }
100 tb = NEWOBJ(tracebackobject, &Tracebacktype);
101 if (tb != NULL) {
102 XINCREF(next);
103 tb->tb_next = next;
104 XINCREF(frame);
105 tb->tb_frame = frame;
106 tb->tb_lasti = lasti;
107 tb->tb_lineno = lineno;
108 }
109 return tb;
110}
111
112static tracebackobject *tb_current = NULL;
113
114int
Guido van Rossumc6515d11992-01-14 18:44:48 +0000115tb_here(frame)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000116 frameobject *frame;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000117{
118 tracebackobject *tb;
Guido van Rossumc6515d11992-01-14 18:44:48 +0000119 tb = newtracebackobject(tb_current, frame, frame->f_lasti, frame->f_lineno);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000120 if (tb == NULL)
121 return -1;
122 XDECREF(tb_current);
123 tb_current = tb;
124 return 0;
125}
126
127object *
128tb_fetch()
129{
130 object *v;
131 v = (object *)tb_current;
132 tb_current = NULL;
133 return v;
134}
135
136int
137tb_store(v)
138 object *v;
139{
140 if (v != NULL && !is_tracebackobject(v)) {
141 err_badcall();
142 return -1;
143 }
144 XDECREF(tb_current);
145 XINCREF(v);
146 tb_current = (tracebackobject *)v;
147 return 0;
148}
149
150static void
151tb_displayline(fp, filename, lineno)
152 FILE *fp;
153 char *filename;
154 int lineno;
155{
156 FILE *xfp;
157 char buf[1000];
158 int i;
159 if (filename[0] == '<' && filename[strlen(filename)-1] == '>')
160 return;
161 xfp = fopen(filename, "r");
162 if (xfp == NULL) {
163 fprintf(fp, " (cannot open \"%s\")\n", filename);
164 return;
165 }
166 for (i = 0; i < lineno; i++) {
167 if (fgets(buf, sizeof buf, xfp) == NULL)
168 break;
169 }
170 if (i == lineno) {
171 char *p = buf;
172 while (*p == ' ' || *p == '\t')
173 p++;
174 fprintf(fp, " %s", p);
175 if (strchr(p, '\n') == NULL)
176 fprintf(fp, "\n");
177 }
178 fclose(xfp);
179}
180
181static void
182tb_printinternal(tb, fp)
183 tracebackobject *tb;
184 FILE *fp;
185{
186 while (tb != NULL) {
Guido van Rossumd783a461991-06-07 22:35:42 +0000187 if (intrcheck())
188 break;
Guido van Rossum247ff711991-06-24 22:25:27 +0000189 fprintf(fp, " File \"%s\"",
190 getstringvalue(tb->tb_frame->f_code->co_filename));
191#ifdef applec /* MPW */
192 /* This is needed by MPW's File and Line commands */
193 fprintf(fp, "; ");
194#else
195 /* This is needed by Emacs' compile command */
196 fprintf(fp, ", ");
197#endif
198 fprintf(fp, "line %d\n", tb->tb_lineno);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000199 tb_displayline(fp,
200 getstringvalue(tb->tb_frame->f_code->co_filename),
201 tb->tb_lineno);
202 tb = tb->tb_next;
203 }
204}
205
206int
207tb_print(v, fp)
208 object *v;
209 FILE *fp;
210{
211 if (v == NULL)
212 return 0;
213 if (!is_tracebackobject(v)) {
214 err_badcall();
215 return -1;
216 }
217 sysset("last_traceback", v);
218 tb_printinternal((tracebackobject *)v, fp);
219 return 0;
220}