comparison Python/traceback.c @ 5608:79e49cbce2ef legacy-trunk

PyFile_WriteString now returns an error indicator instead of calling PyErr_Clear(). Add checking of those errors.
author Guido van Rossum <guido@python.org>
date Thu, 22 May 1997 22:35:47 +0000
parents b7871ca930ad
children d3696bf08eee
comparison
equal deleted inserted replaced
5607:cbb4e3cf086e 5608:79e49cbce2ef
130 tstate->curexc_traceback = (PyObject *)tb; 130 tstate->curexc_traceback = (PyObject *)tb;
131 Py_XDECREF(oldtb); 131 Py_XDECREF(oldtb);
132 return 0; 132 return 0;
133 } 133 }
134 134
135 static void 135 static int
136 tb_displayline(f, filename, lineno, name) 136 tb_displayline(f, filename, lineno, name)
137 PyObject *f; 137 PyObject *f;
138 char *filename; 138 char *filename;
139 int lineno; 139 int lineno;
140 char *name; 140 char *name;
141 { 141 {
142 int err = 0;
142 FILE *xfp; 143 FILE *xfp;
143 char linebuf[1000]; 144 char linebuf[1000];
144 int i; 145 int i;
146 if (filename == NULL || name == NULL)
147 return -1;
145 #ifdef MPW 148 #ifdef MPW
146 /* This is needed by MPW's File and Line commands */ 149 /* This is needed by MPW's File and Line commands */
147 #define FMT " File \"%.900s\"; line %d # in %s\n" 150 #define FMT " File \"%.900s\"; line %d # in %s\n"
148 #else 151 #else
149 /* This is needed by Emacs' compile command */ 152 /* This is needed by Emacs' compile command */
163 int npath = PyList_Size(path); 166 int npath = PyList_Size(path);
164 int taillen = strlen(tail); 167 int taillen = strlen(tail);
165 char namebuf[MAXPATHLEN+1]; 168 char namebuf[MAXPATHLEN+1];
166 for (i = 0; i < npath; i++) { 169 for (i = 0; i < npath; i++) {
167 PyObject *v = PyList_GetItem(path, i); 170 PyObject *v = PyList_GetItem(path, i);
171 if (v == NULL) {
172 PyErr_Clear();
173 break;
174 }
168 if (PyString_Check(v)) { 175 if (PyString_Check(v)) {
169 int len; 176 int len;
170 len = PyString_Size(v); 177 len = PyString_Size(v);
171 if (len + 1 + taillen >= MAXPATHLEN) 178 if (len + 1 + taillen >= MAXPATHLEN)
172 continue; /* Too long */ 179 continue; /* Too long */
184 } 191 }
185 } 192 }
186 } 193 }
187 } 194 }
188 sprintf(linebuf, FMT, filename, lineno, name); 195 sprintf(linebuf, FMT, filename, lineno, name);
189 PyFile_WriteString(linebuf, f); 196 err = PyFile_WriteString(linebuf, f);
190 if (xfp == NULL) 197 if (xfp == NULL || err != 0)
191 return; 198 return err;
192 for (i = 0; i < lineno; i++) { 199 for (i = 0; i < lineno; i++) {
193 if (fgets(linebuf, sizeof linebuf, xfp) == NULL) 200 if (fgets(linebuf, sizeof linebuf, xfp) == NULL)
194 break; 201 break;
195 } 202 }
196 if (i == lineno) { 203 if (i == lineno) {
197 char *p = linebuf; 204 char *p = linebuf;
198 while (*p == ' ' || *p == '\t' || *p == '\014') 205 while (*p == ' ' || *p == '\t' || *p == '\014')
199 p++; 206 p++;
200 PyFile_WriteString(" ", f); 207 err = PyFile_WriteString(" ", f);
201 PyFile_WriteString(p, f); 208 if (err == 0) {
202 if (strchr(p, '\n') == NULL) 209 err = PyFile_WriteString(p, f);
203 PyFile_WriteString("\n", f); 210 if (err == 0 && strchr(p, '\n') == NULL)
211 err = PyFile_WriteString("\n", f);
212 }
204 } 213 }
205 fclose(xfp); 214 fclose(xfp);
206 } 215 return err;
207 216 }
208 static void 217
218 static int
209 tb_printinternal(tb, f, limit) 219 tb_printinternal(tb, f, limit)
210 tracebackobject *tb; 220 tracebackobject *tb;
211 PyObject *f; 221 PyObject *f;
212 int limit; 222 int limit;
213 { 223 {
224 int err = 0;
214 int depth = 0; 225 int depth = 0;
215 tracebackobject *tb1 = tb; 226 tracebackobject *tb1 = tb;
216 while (tb1 != NULL) { 227 while (tb1 != NULL) {
217 depth++; 228 depth++;
218 tb1 = tb1->tb_next; 229 tb1 = tb1->tb_next;
219 } 230 }
220 while (tb != NULL && !PyOS_InterruptOccurred()) { 231 while (tb != NULL && err == 0) {
221 if (depth <= limit) { 232 if (depth <= limit) {
222 if (Py_OptimizeFlag) 233 if (Py_OptimizeFlag)
223 tb->tb_lineno = PyCode_Addr2Line( 234 tb->tb_lineno = PyCode_Addr2Line(
224 tb->tb_frame->f_code, tb->tb_lasti); 235 tb->tb_frame->f_code, tb->tb_lasti);
225 tb_displayline(f, 236 err = tb_displayline(f,
226 PyString_AsString( 237 PyString_AsString(
227 tb->tb_frame->f_code->co_filename), 238 tb->tb_frame->f_code->co_filename),
228 tb->tb_lineno, 239 tb->tb_lineno,
229 PyString_AsString(tb->tb_frame->f_code->co_name)); 240 PyString_AsString(tb->tb_frame->f_code->co_name));
230 } 241 }
231 depth--; 242 depth--;
232 tb = tb->tb_next; 243 tb = tb->tb_next;
233 } 244 if (err == 0)
245 err = PyErr_CheckSignals();
246 }
247 return err;
234 } 248 }
235 249
236 int 250 int
237 PyTraceBack_Print(v, f) 251 PyTraceBack_Print(v, f)
238 PyObject *v; 252 PyObject *v;
239 PyObject *f; 253 PyObject *f;
240 { 254 {
255 int err;
241 PyObject *limitv; 256 PyObject *limitv;
242 int limit = 1000; 257 int limit = 1000;
243 if (v == NULL) 258 if (v == NULL)
244 return 0; 259 return 0;
245 if (!is_tracebackobject(v)) { 260 if (!is_tracebackobject(v)) {
250 if (limitv && PyInt_Check(limitv)) { 265 if (limitv && PyInt_Check(limitv)) {
251 limit = PyInt_AsLong(limitv); 266 limit = PyInt_AsLong(limitv);
252 if (limit <= 0) 267 if (limit <= 0)
253 return 0; 268 return 0;
254 } 269 }
255 PyFile_WriteString("Traceback (innermost last):\n", f); 270 err = PyFile_WriteString("Traceback (innermost last):\n", f);
256 tb_printinternal((tracebackobject *)v, f, limit); 271 if (!err)
257 return 0; 272 err = tb_printinternal((tracebackobject *)v, f, limit);
258 } 273 return err;
274 }