comparison Python/traceback.c @ 48474:2ee09afee126

implement chained exception tracebacks patch from Antoine Pitrou #3112
author Benjamin Peterson <benjamin@python.org>
date Tue, 15 Jul 2008 15:32:09 +0000
parents 23ab586c427a
children de0bf60f1245
comparison
equal deleted inserted replaced
48473:7d570df5e90a 48474:2ee09afee126
127 Py_XDECREF(oldtb); 127 Py_XDECREF(oldtb);
128 return 0; 128 return 0;
129 } 129 }
130 130
131 int 131 int
132 Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno) 132 Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno, int indent)
133 { 133 {
134 int err = 0; 134 int err = 0;
135 FILE *xfp = NULL; 135 FILE *xfp = NULL;
136 char linebuf[2000]; 136 char linebuf[2000];
137 int i; 137 int i;
138 char namebuf[MAXPATHLEN+1]; 138 char namebuf[MAXPATHLEN+1];
139 139
140 if (filename == NULL) 140 if (filename == NULL)
141 return -1; 141 return -1;
142 /* This is needed by Emacs' compile command */
143 #define FMT " File \"%.500s\", line %d, in %.500s\n"
144 xfp = fopen(filename, "r" PY_STDIOTEXTMODE); 142 xfp = fopen(filename, "r" PY_STDIOTEXTMODE);
145 if (xfp == NULL) { 143 if (xfp == NULL) {
146 /* Search tail of filename in sys.path before giving up */ 144 /* Search tail of filename in sys.path before giving up */
147 PyObject *path; 145 PyObject *path;
148 const char *tail = strrchr(filename, SEP); 146 const char *tail = strrchr(filename, SEP);
201 it obviously found a newline; else we haven't 199 it obviously found a newline; else we haven't
202 yet seen a newline, so must continue */ 200 yet seen a newline, so must continue */
203 } while (*pLastChar != '\0' && *pLastChar != '\n'); 201 } while (*pLastChar != '\0' && *pLastChar != '\n');
204 } 202 }
205 if (i == lineno) { 203 if (i == lineno) {
204 char buf[11];
206 char *p = linebuf; 205 char *p = linebuf;
207 while (*p == ' ' || *p == '\t' || *p == '\014') 206 while (*p == ' ' || *p == '\t' || *p == '\014')
208 p++; 207 p++;
209 err = PyFile_WriteString(p, f); 208
210 if (err == 0 && strchr(p, '\n') == NULL) 209 /* Write some spaces before the line */
211 err = PyFile_WriteString("\n", f); 210 strcpy(buf, " ");
211 assert (strlen(buf) == 10);
212 while (indent > 0) {
213 if(indent < 10)
214 buf[indent] = '\0';
215 err = PyFile_WriteString(buf, f);
216 if (err != 0)
217 break;
218 indent -= 10;
219 }
220
221 if (err == 0)
222 err = PyFile_WriteString(p, f);
223 if (err == 0 && strchr(p, '\n') == NULL)
224 err = PyFile_WriteString("\n", f);
212 } 225 }
213 fclose(xfp); 226 fclose(xfp);
214 return err; 227 return err;
215 } 228 }
216 229
226 #define FMT " File \"%.500s\", line %d, in %.500s\n" 239 #define FMT " File \"%.500s\", line %d, in %.500s\n"
227 PyOS_snprintf(linebuf, sizeof(linebuf), FMT, filename, lineno, name); 240 PyOS_snprintf(linebuf, sizeof(linebuf), FMT, filename, lineno, name);
228 err = PyFile_WriteString(linebuf, f); 241 err = PyFile_WriteString(linebuf, f);
229 if (err != 0) 242 if (err != 0)
230 return err; 243 return err;
231 return Py_DisplaySourceLine(f, filename, lineno); 244 return Py_DisplaySourceLine(f, filename, lineno, 4);
232 } 245 }
233 246
234 static int 247 static int
235 tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit) 248 tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit)
236 { 249 {