Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7e8d26d

Browse files
committed
PyFile_WriteString now returns an error indicator instead of calling
PyErr_Clear(). Add checking of those errors.
1 parent 78a1ed3 commit 7e8d26d

2 files changed

Lines changed: 32 additions & 16 deletions

File tree

Python/marshal.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,11 +354,11 @@ r_long64(p)
354354
#else
355355
if (r_long(p) != 0) {
356356
PyObject *f = PySys_GetObject("stderr");
357-
PyErr_Clear();
358357
if (f != NULL)
359-
PyFile_WriteString(
358+
(void) PyFile_WriteString(
360359
"Warning: un-marshal 64-bit int in 32-bit mode\n",
361360
f);
361+
PyErr_Clear();
362362
}
363363
#endif
364364
return x;

Python/traceback.c

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,19 @@ PyTraceBack_Here(frame)
132132
return 0;
133133
}
134134

135-
static void
135+
static int
136136
tb_displayline(f, filename, lineno, name)
137137
PyObject *f;
138138
char *filename;
139139
int lineno;
140140
char *name;
141141
{
142+
int err = 0;
142143
FILE *xfp;
143144
char linebuf[1000];
144145
int i;
146+
if (filename == NULL || name == NULL)
147+
return -1;
145148
#ifdef MPW
146149
/* This is needed by MPW's File and Line commands */
147150
#define FMT " File \"%.900s\"; line %d # in %s\n"
@@ -165,6 +168,10 @@ tb_displayline(f, filename, lineno, name)
165168
char namebuf[MAXPATHLEN+1];
166169
for (i = 0; i < npath; i++) {
167170
PyObject *v = PyList_GetItem(path, i);
171+
if (v == NULL) {
172+
PyErr_Clear();
173+
break;
174+
}
168175
if (PyString_Check(v)) {
169176
int len;
170177
len = PyString_Size(v);
@@ -186,9 +193,9 @@ tb_displayline(f, filename, lineno, name)
186193
}
187194
}
188195
sprintf(linebuf, FMT, filename, lineno, name);
189-
PyFile_WriteString(linebuf, f);
190-
if (xfp == NULL)
191-
return;
196+
err = PyFile_WriteString(linebuf, f);
197+
if (xfp == NULL || err != 0)
198+
return err;
192199
for (i = 0; i < lineno; i++) {
193200
if (fgets(linebuf, sizeof linebuf, xfp) == NULL)
194201
break;
@@ -197,47 +204,55 @@ tb_displayline(f, filename, lineno, name)
197204
char *p = linebuf;
198205
while (*p == ' ' || *p == '\t' || *p == '\014')
199206
p++;
200-
PyFile_WriteString(" ", f);
201-
PyFile_WriteString(p, f);
202-
if (strchr(p, '\n') == NULL)
203-
PyFile_WriteString("\n", f);
207+
err = PyFile_WriteString(" ", f);
208+
if (err == 0) {
209+
err = PyFile_WriteString(p, f);
210+
if (err == 0 && strchr(p, '\n') == NULL)
211+
err = PyFile_WriteString("\n", f);
212+
}
204213
}
205214
fclose(xfp);
215+
return err;
206216
}
207217

208-
static void
218+
static int
209219
tb_printinternal(tb, f, limit)
210220
tracebackobject *tb;
211221
PyObject *f;
212222
int limit;
213223
{
224+
int err = 0;
214225
int depth = 0;
215226
tracebackobject *tb1 = tb;
216227
while (tb1 != NULL) {
217228
depth++;
218229
tb1 = tb1->tb_next;
219230
}
220-
while (tb != NULL && !PyOS_InterruptOccurred()) {
231+
while (tb != NULL && err == 0) {
221232
if (depth <= limit) {
222233
if (Py_OptimizeFlag)
223234
tb->tb_lineno = PyCode_Addr2Line(
224235
tb->tb_frame->f_code, tb->tb_lasti);
225-
tb_displayline(f,
236+
err = tb_displayline(f,
226237
PyString_AsString(
227238
tb->tb_frame->f_code->co_filename),
228239
tb->tb_lineno,
229240
PyString_AsString(tb->tb_frame->f_code->co_name));
230241
}
231242
depth--;
232243
tb = tb->tb_next;
244+
if (err == 0)
245+
err = PyErr_CheckSignals();
233246
}
247+
return err;
234248
}
235249

236250
int
237251
PyTraceBack_Print(v, f)
238252
PyObject *v;
239253
PyObject *f;
240254
{
255+
int err;
241256
PyObject *limitv;
242257
int limit = 1000;
243258
if (v == NULL)
@@ -252,7 +267,8 @@ PyTraceBack_Print(v, f)
252267
if (limit <= 0)
253268
return 0;
254269
}
255-
PyFile_WriteString("Traceback (innermost last):\n", f);
256-
tb_printinternal((tracebackobject *)v, f, limit);
257-
return 0;
270+
err = PyFile_WriteString("Traceback (innermost last):\n", f);
271+
if (!err)
272+
err = tb_printinternal((tracebackobject *)v, f, limit);
273+
return err;
258274
}

0 commit comments

Comments
 (0)