comparison Python/traceback.c @ 674:4e2d239dc64c legacy-trunk

Move printing of filename and lineno to tb_displayline. Search sys.path if the filename isn't found. Include osdefs.h.
author Guido van Rossum <guido@python.org>
date Wed, 26 Feb 1992 15:17:59 +0000
parents 800cf4086fb3
children 0cd197947d81
comparison
equal deleted inserted replaced
673:71d496259ad3 674:4e2d239dc64c
24 24
25 /* Traceback implementation */ 25 /* Traceback implementation */
26 26
27 #include "allobjects.h" 27 #include "allobjects.h"
28 28
29 #include "sysmodule.h"
29 #include "compile.h" 30 #include "compile.h"
30 #include "frameobject.h" 31 #include "frameobject.h"
31 #include "traceback.h" 32 #include "traceback.h"
32 #include "structmember.h" 33 #include "structmember.h"
34 #include "osdefs.h"
33 35
34 typedef struct _tracebackobject { 36 typedef struct _tracebackobject {
35 OB_HEAD 37 OB_HEAD
36 struct _tracebackobject *tb_next; 38 struct _tracebackobject *tb_next;
37 frameobject *tb_frame; 39 frameobject *tb_frame;
152 FILE *fp; 154 FILE *fp;
153 char *filename; 155 char *filename;
154 int lineno; 156 int lineno;
155 { 157 {
156 FILE *xfp; 158 FILE *xfp;
157 char buf[1000]; 159 char linebuf[1000];
158 int i; 160 int i;
159 if (filename[0] == '<' && filename[strlen(filename)-1] == '>')
160 return;
161 xfp = fopen(filename, "r"); 161 xfp = fopen(filename, "r");
162 if (xfp == NULL) { 162 if (xfp == NULL) {
163 fprintf(fp, " (cannot open \"%s\")\n", filename); 163 /* Search tail of filename in sys.path before giving up */
164 object *path;
165 char *tail = strrchr(filename, SEP);
166 if (tail == NULL)
167 tail = filename;
168 else
169 tail++;
170 path = sysget("path");
171 if (path != NULL && is_listobject(path)) {
172 int npath = getlistsize(path);
173 char namebuf[MAXPATHLEN+1];
174 for (i = 0; i < npath; i++) {
175 object *v = getlistitem(path, i);
176 if (is_stringobject(v)) {
177 int len;
178 strcpy(namebuf, getstringvalue(v));
179 len = getstringsize(v);
180 if (len > 0 && namebuf[len-1] != SEP)
181 namebuf[len++] = SEP;
182 strcpy(namebuf+len, tail);
183 xfp = fopen(namebuf, "r");
184 if (xfp != NULL) {
185 filename = namebuf;
186 break;
187 }
188 }
189 }
190 }
191 }
192 fprintf(fp, " File \"%s\"", filename);
193 #ifdef applec /* MPW */
194 /* This is needed by MPW's File and Line commands */
195 fprintf(fp, "; ");
196 #else
197 /* This is needed by Emacs' compile command */
198 fprintf(fp, ", ");
199 #endif
200 fprintf(fp, "line %d\n", lineno);
201 if (xfp == NULL)
164 return; 202 return;
165 }
166 for (i = 0; i < lineno; i++) { 203 for (i = 0; i < lineno; i++) {
167 if (fgets(buf, sizeof buf, xfp) == NULL) 204 if (fgets(linebuf, sizeof linebuf, xfp) == NULL)
168 break; 205 break;
169 } 206 }
170 if (i == lineno) { 207 if (i == lineno) {
171 char *p = buf; 208 char *p = linebuf;
172 while (*p == ' ' || *p == '\t') 209 while (*p == ' ' || *p == '\t')
173 p++; 210 p++;
174 fprintf(fp, " %s", p); 211 fprintf(fp, " %s", p);
175 if (strchr(p, '\n') == NULL) 212 if (strchr(p, '\n') == NULL)
176 fprintf(fp, "\n"); 213 fprintf(fp, "\n");
181 static void 218 static void
182 tb_printinternal(tb, fp) 219 tb_printinternal(tb, fp)
183 tracebackobject *tb; 220 tracebackobject *tb;
184 FILE *fp; 221 FILE *fp;
185 { 222 {
186 while (tb != NULL) { 223 while (tb != NULL && !intrcheck()) {
187 if (intrcheck())
188 break;
189 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);
199 tb_displayline(fp, 224 tb_displayline(fp,
200 getstringvalue(tb->tb_frame->f_code->co_filename), 225 getstringvalue(tb->tb_frame->f_code->co_filename),
201 tb->tb_lineno); 226 tb->tb_lineno);
202 tb = tb->tb_next; 227 tb = tb->tb_next;
203 } 228 }