Mercurial > cpython-lru_cache-weakref
annotate Python/traceback.c @ 597:800cf4086fb3 legacy-trunk
tb_here() can now get the lasti and lineno arguments from the frame.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Tue, 14 Jan 1992 18:44:48 +0000 |
| parents | 06774a94abb5 |
| children | 4e2d239dc64c |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
| 2 Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The | |
| 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 | |
| 29 #include "compile.h" | |
| 30 #include "frameobject.h" | |
| 31 #include "traceback.h" | |
| 32 #include "structmember.h" | |
| 33 | |
| 34 typedef 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 | |
| 44 static 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 | |
| 52 static object * | |
| 53 tb_getattr(tb, name) | |
| 54 tracebackobject *tb; | |
| 55 char *name; | |
| 56 { | |
| 57 return getmember((char *)tb, tb_memberlist, name); | |
| 58 } | |
| 59 | |
| 60 static void | |
| 61 tb_dealloc(tb) | |
| 62 tracebackobject *tb; | |
| 63 { | |
| 64 XDECREF(tb->tb_next); | |
| 65 XDECREF(tb->tb_frame); | |
| 66 DEL(tb); | |
| 67 } | |
| 68 | |
| 69 static 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 | |
| 88 static tracebackobject * | |
| 89 newtracebackobject(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 | |
| 112 static tracebackobject *tb_current = NULL; | |
| 113 | |
| 114 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
|
115 tb_here(frame) |
| 86 | 116 frameobject *frame; |
| 117 { | |
| 118 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
|
119 tb = newtracebackobject(tb_current, frame, frame->f_lasti, frame->f_lineno); |
| 86 | 120 if (tb == NULL) |
| 121 return -1; | |
| 122 XDECREF(tb_current); | |
| 123 tb_current = tb; | |
| 124 return 0; | |
| 125 } | |
| 126 | |
| 127 object * | |
| 128 tb_fetch() | |
| 129 { | |
| 130 object *v; | |
| 131 v = (object *)tb_current; | |
| 132 tb_current = NULL; | |
| 133 return v; | |
| 134 } | |
| 135 | |
| 136 int | |
| 137 tb_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 | |
| 150 static void | |
| 151 tb_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 | |
| 181 static void | |
| 182 tb_printinternal(tb, fp) | |
| 183 tracebackobject *tb; | |
| 184 FILE *fp; | |
| 185 { | |
| 186 while (tb != NULL) { | |
|
285
85bd7dac13f3
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
187 if (intrcheck()) |
|
85bd7dac13f3
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
188 break; |
|
291
06774a94abb5
Don't use printobject() to print a string (filename).
Guido van Rossum <guido@python.org>
parents:
285
diff
changeset
|
189 fprintf(fp, " File \"%s\"", |
|
06774a94abb5
Don't use printobject() to print a string (filename).
Guido van Rossum <guido@python.org>
parents:
285
diff
changeset
|
190 getstringvalue(tb->tb_frame->f_code->co_filename)); |
|
06774a94abb5
Don't use printobject() to print a string (filename).
Guido van Rossum <guido@python.org>
parents:
285
diff
changeset
|
191 #ifdef applec /* MPW */ |
|
06774a94abb5
Don't use printobject() to print a string (filename).
Guido van Rossum <guido@python.org>
parents:
285
diff
changeset
|
192 /* This is needed by MPW's File and Line commands */ |
|
06774a94abb5
Don't use printobject() to print a string (filename).
Guido van Rossum <guido@python.org>
parents:
285
diff
changeset
|
193 fprintf(fp, "; "); |
|
06774a94abb5
Don't use printobject() to print a string (filename).
Guido van Rossum <guido@python.org>
parents:
285
diff
changeset
|
194 #else |
|
06774a94abb5
Don't use printobject() to print a string (filename).
Guido van Rossum <guido@python.org>
parents:
285
diff
changeset
|
195 /* This is needed by Emacs' compile command */ |
|
06774a94abb5
Don't use printobject() to print a string (filename).
Guido van Rossum <guido@python.org>
parents:
285
diff
changeset
|
196 fprintf(fp, ", "); |
|
06774a94abb5
Don't use printobject() to print a string (filename).
Guido van Rossum <guido@python.org>
parents:
285
diff
changeset
|
197 #endif |
|
06774a94abb5
Don't use printobject() to print a string (filename).
Guido van Rossum <guido@python.org>
parents:
285
diff
changeset
|
198 fprintf(fp, "line %d\n", tb->tb_lineno); |
| 86 | 199 tb_displayline(fp, |
| 200 getstringvalue(tb->tb_frame->f_code->co_filename), | |
| 201 tb->tb_lineno); | |
| 202 tb = tb->tb_next; | |
| 203 } | |
| 204 } | |
| 205 | |
| 206 int | |
| 207 tb_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 } |
