Mercurial > cpython-lru_cache-weakref
annotate Python/traceback.c @ 795:0cd197947d81 legacy-trunk
Copyright for 1992 added
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Sun, 05 Apr 1992 14:26:55 +0000 |
| parents | 4e2d239dc64c |
| children | 2c0432fcc270 |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
795
0cd197947d81
Copyright for 1992 added
Guido van Rossum <guido@python.org>
parents:
674
diff
changeset
|
2 Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The |
| 145 | 3 Netherlands. |
| 4 | |
| 5 All Rights Reserved | |
| 6 | |
| 7 Permission to use, copy, modify, and distribute this software and its | |
| 8 documentation for any purpose and without fee is hereby granted, | |
| 9 provided that the above copyright notice appear in all copies and that | |
| 10 both that copyright notice and this permission notice appear in | |
| 11 supporting documentation, and that the names of Stichting Mathematisch | |
| 12 Centrum or CWI not be used in advertising or publicity pertaining to | |
| 13 distribution of the software without specific, written prior permission. | |
| 14 | |
| 15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO | |
| 16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE | |
| 18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
| 19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
| 20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT | |
| 21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
| 22 | |
| 23 ******************************************************************/ | |
| 24 | |
| 86 | 25 /* Traceback implementation */ |
| 26 | |
| 27 #include "allobjects.h" | |
| 28 | |
|
674
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
29 #include "sysmodule.h" |
| 86 | 30 #include "compile.h" |
| 31 #include "frameobject.h" | |
| 32 #include "traceback.h" | |
| 33 #include "structmember.h" | |
|
674
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
34 #include "osdefs.h" |
| 86 | 35 |
| 36 typedef struct _tracebackobject { | |
| 37 OB_HEAD | |
| 38 struct _tracebackobject *tb_next; | |
| 39 frameobject *tb_frame; | |
| 40 int tb_lasti; | |
| 41 int tb_lineno; | |
| 42 } tracebackobject; | |
| 43 | |
| 44 #define OFF(x) offsetof(tracebackobject, x) | |
| 45 | |
| 46 static struct memberlist tb_memberlist[] = { | |
| 47 {"tb_next", T_OBJECT, OFF(tb_next)}, | |
| 48 {"tb_frame", T_OBJECT, OFF(tb_frame)}, | |
| 49 {"tb_lasti", T_INT, OFF(tb_lasti)}, | |
| 50 {"tb_lineno", T_INT, OFF(tb_lineno)}, | |
| 51 {NULL} /* Sentinel */ | |
| 52 }; | |
| 53 | |
| 54 static object * | |
| 55 tb_getattr(tb, name) | |
| 56 tracebackobject *tb; | |
| 57 char *name; | |
| 58 { | |
| 59 return getmember((char *)tb, tb_memberlist, name); | |
| 60 } | |
| 61 | |
| 62 static void | |
| 63 tb_dealloc(tb) | |
| 64 tracebackobject *tb; | |
| 65 { | |
| 66 XDECREF(tb->tb_next); | |
| 67 XDECREF(tb->tb_frame); | |
| 68 DEL(tb); | |
| 69 } | |
| 70 | |
| 71 static typeobject Tracebacktype = { | |
| 72 OB_HEAD_INIT(&Typetype) | |
| 73 0, | |
| 74 "traceback", | |
| 75 sizeof(tracebackobject), | |
| 76 0, | |
| 77 tb_dealloc, /*tp_dealloc*/ | |
| 78 0, /*tp_print*/ | |
| 79 tb_getattr, /*tp_getattr*/ | |
| 80 0, /*tp_setattr*/ | |
| 81 0, /*tp_compare*/ | |
| 82 0, /*tp_repr*/ | |
| 83 0, /*tp_as_number*/ | |
| 84 0, /*tp_as_sequence*/ | |
| 85 0, /*tp_as_mapping*/ | |
| 86 }; | |
| 87 | |
| 88 #define is_tracebackobject(v) ((v)->ob_type == &Tracebacktype) | |
| 89 | |
| 90 static tracebackobject * | |
| 91 newtracebackobject(next, frame, lasti, lineno) | |
| 92 tracebackobject *next; | |
| 93 frameobject *frame; | |
| 94 int lasti, lineno; | |
| 95 { | |
| 96 tracebackobject *tb; | |
| 97 if ((next != NULL && !is_tracebackobject(next)) || | |
| 98 frame == NULL || !is_frameobject(frame)) { | |
| 99 err_badcall(); | |
| 100 return NULL; | |
| 101 } | |
| 102 tb = NEWOBJ(tracebackobject, &Tracebacktype); | |
| 103 if (tb != NULL) { | |
| 104 XINCREF(next); | |
| 105 tb->tb_next = next; | |
| 106 XINCREF(frame); | |
| 107 tb->tb_frame = frame; | |
| 108 tb->tb_lasti = lasti; | |
| 109 tb->tb_lineno = lineno; | |
| 110 } | |
| 111 return tb; | |
| 112 } | |
| 113 | |
| 114 static tracebackobject *tb_current = NULL; | |
| 115 | |
| 116 int | |
|
597
800cf4086fb3
tb_here() can now get the lasti and lineno arguments from the frame.
Guido van Rossum <guido@python.org>
parents:
291
diff
changeset
|
117 tb_here(frame) |
| 86 | 118 frameobject *frame; |
| 119 { | |
| 120 tracebackobject *tb; | |
|
597
800cf4086fb3
tb_here() can now get the lasti and lineno arguments from the frame.
Guido van Rossum <guido@python.org>
parents:
291
diff
changeset
|
121 tb = newtracebackobject(tb_current, frame, frame->f_lasti, frame->f_lineno); |
| 86 | 122 if (tb == NULL) |
| 123 return -1; | |
| 124 XDECREF(tb_current); | |
| 125 tb_current = tb; | |
| 126 return 0; | |
| 127 } | |
| 128 | |
| 129 object * | |
| 130 tb_fetch() | |
| 131 { | |
| 132 object *v; | |
| 133 v = (object *)tb_current; | |
| 134 tb_current = NULL; | |
| 135 return v; | |
| 136 } | |
| 137 | |
| 138 int | |
| 139 tb_store(v) | |
| 140 object *v; | |
| 141 { | |
| 142 if (v != NULL && !is_tracebackobject(v)) { | |
| 143 err_badcall(); | |
| 144 return -1; | |
| 145 } | |
| 146 XDECREF(tb_current); | |
| 147 XINCREF(v); | |
| 148 tb_current = (tracebackobject *)v; | |
| 149 return 0; | |
| 150 } | |
| 151 | |
| 152 static void | |
| 153 tb_displayline(fp, filename, lineno) | |
| 154 FILE *fp; | |
| 155 char *filename; | |
| 156 int lineno; | |
| 157 { | |
| 158 FILE *xfp; | |
|
674
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
159 char linebuf[1000]; |
| 86 | 160 int i; |
| 161 xfp = fopen(filename, "r"); | |
| 162 if (xfp == NULL) { | |
|
674
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
163 /* Search tail of filename in sys.path before giving up */ |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
164 object *path; |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
165 char *tail = strrchr(filename, SEP); |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
166 if (tail == NULL) |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
167 tail = filename; |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
168 else |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
169 tail++; |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
170 path = sysget("path"); |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
171 if (path != NULL && is_listobject(path)) { |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
172 int npath = getlistsize(path); |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
173 char namebuf[MAXPATHLEN+1]; |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
174 for (i = 0; i < npath; i++) { |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
175 object *v = getlistitem(path, i); |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
176 if (is_stringobject(v)) { |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
177 int len; |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
178 strcpy(namebuf, getstringvalue(v)); |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
179 len = getstringsize(v); |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
180 if (len > 0 && namebuf[len-1] != SEP) |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
181 namebuf[len++] = SEP; |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
182 strcpy(namebuf+len, tail); |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
183 xfp = fopen(namebuf, "r"); |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
184 if (xfp != NULL) { |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
185 filename = namebuf; |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
186 break; |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
187 } |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
188 } |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
189 } |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
190 } |
| 86 | 191 } |
|
674
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
192 fprintf(fp, " File \"%s\"", filename); |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
193 #ifdef applec /* MPW */ |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
194 /* This is needed by MPW's File and Line commands */ |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
195 fprintf(fp, "; "); |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
196 #else |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
197 /* This is needed by Emacs' compile command */ |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
198 fprintf(fp, ", "); |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
199 #endif |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
200 fprintf(fp, "line %d\n", lineno); |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
201 if (xfp == NULL) |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
202 return; |
| 86 | 203 for (i = 0; i < lineno; i++) { |
|
674
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
204 if (fgets(linebuf, sizeof linebuf, xfp) == NULL) |
| 86 | 205 break; |
| 206 } | |
| 207 if (i == lineno) { | |
|
674
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
208 char *p = linebuf; |
| 86 | 209 while (*p == ' ' || *p == '\t') |
| 210 p++; | |
| 211 fprintf(fp, " %s", p); | |
| 212 if (strchr(p, '\n') == NULL) | |
| 213 fprintf(fp, "\n"); | |
| 214 } | |
| 215 fclose(xfp); | |
| 216 } | |
| 217 | |
| 218 static void | |
| 219 tb_printinternal(tb, fp) | |
| 220 tracebackobject *tb; | |
| 221 FILE *fp; | |
| 222 { | |
|
674
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
223 while (tb != NULL && !intrcheck()) { |
| 86 | 224 tb_displayline(fp, |
| 225 getstringvalue(tb->tb_frame->f_code->co_filename), | |
| 226 tb->tb_lineno); | |
| 227 tb = tb->tb_next; | |
| 228 } | |
| 229 } | |
| 230 | |
| 231 int | |
| 232 tb_print(v, fp) | |
| 233 object *v; | |
| 234 FILE *fp; | |
| 235 { | |
| 236 if (v == NULL) | |
| 237 return 0; | |
| 238 if (!is_tracebackobject(v)) { | |
| 239 err_badcall(); | |
| 240 return -1; | |
| 241 } | |
| 242 sysset("last_traceback", v); | |
| 243 tb_printinternal((tracebackobject *)v, fp); | |
| 244 return 0; | |
| 245 } |
