Skip to content

Commit 728c6e1

Browse files
committed
Handle exception in PyRun_SimpleFile and PyRun_AnyFile
These functions can fail with MemoryError
1 parent 95f6071 commit 728c6e1

1 file changed

Lines changed: 28 additions & 7 deletions

File tree

Modules/_testcapi/run.c

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,14 @@ run_simplefile(PyObject *mod, PyObject *args)
125125
int fd = fileno(fp);
126126

127127
int res = PyRun_SimpleFile(fp, filename);
128-
assert(!PyErr_Occurred());
129128

130129
assert(_Py_IsValidFD(fd));
131130
fclose(fp);
132131

132+
if (res == -1 && PyErr_Occurred()) {
133+
return NULL;
134+
}
135+
assert(!PyErr_Occurred());
133136
return PyLong_FromLong(res);
134137
}
135138

@@ -195,12 +198,15 @@ run_simplefileex(PyObject *mod, PyObject *args)
195198
int fd = fileno(fp);
196199

197200
int res = PyRun_SimpleFileEx(fp, filename, closeit);
198-
assert(!PyErr_Occurred());
199201

200202
if (close_fd(fp, fd, closeit) < 0) {
201203
return NULL;
202204
}
203205

206+
if (res == -1 && PyErr_Occurred()) {
207+
return NULL;
208+
}
209+
assert(!PyErr_Occurred());
204210
return PyLong_FromLong(res);
205211
}
206212

@@ -232,12 +238,15 @@ run_simplefileexflags(PyObject *mod, PyObject *args)
232238
}
233239

234240
int res = PyRun_SimpleFileExFlags(fp, filename, closeit, pflags);
235-
assert(!PyErr_Occurred());
236241

237242
if (close_fd(fp, fd, closeit) < 0) {
238243
return NULL;
239244
}
240245

246+
if (res == -1 && PyErr_Occurred()) {
247+
return NULL;
248+
}
249+
assert(!PyErr_Occurred());
241250
return PyLong_FromLong(res);
242251
}
243252

@@ -259,11 +268,14 @@ run_anyfile(PyObject *mod, PyObject *args)
259268
int fd = fileno(fp);
260269

261270
int res = PyRun_AnyFile(fp, filename);
262-
assert(!PyErr_Occurred());
263271

264272
assert(_Py_IsValidFD(fd));
265273
fclose(fp);
266274

275+
if (res == -1 && PyErr_Occurred()) {
276+
return NULL;
277+
}
278+
assert(!PyErr_Occurred());
267279
return PyLong_FromLong(res);
268280
}
269281

@@ -294,11 +306,14 @@ run_anyfileflags(PyObject *mod, PyObject *args)
294306
}
295307

296308
int res = PyRun_AnyFileFlags(fp, filename, pflags);
297-
assert(!PyErr_Occurred());
298309

299310
assert(_Py_IsValidFD(fd));
300311
fclose(fp);
301312

313+
if (res == -1 && PyErr_Occurred()) {
314+
return NULL;
315+
}
316+
assert(!PyErr_Occurred());
302317
return PyLong_FromLong(res);
303318
}
304319

@@ -322,12 +337,15 @@ run_anyfileex(PyObject *mod, PyObject *args)
322337
int fd = fileno(fp);
323338

324339
int res = PyRun_AnyFileEx(fp, filename, closeit);
325-
assert(!PyErr_Occurred());
326340

327341
if (close_fd(fp, fd, closeit) < 0) {
328342
return NULL;
329343
}
330344

345+
if (res == -1 && PyErr_Occurred()) {
346+
return NULL;
347+
}
348+
assert(!PyErr_Occurred());
331349
return PyLong_FromLong(res);
332350
}
333351

@@ -359,12 +377,15 @@ run_anyfileexflags(PyObject *mod, PyObject *args)
359377
}
360378

361379
int res = PyRun_AnyFileExFlags(fp, filename, closeit, pflags);
362-
assert(!PyErr_Occurred());
363380

364381
if (close_fd(fp, fd, closeit) < 0) {
365382
return NULL;
366383
}
367384

385+
if (res == -1 && PyErr_Occurred()) {
386+
return NULL;
387+
}
388+
assert(!PyErr_Occurred());
368389
return PyLong_FromLong(res);
369390
}
370391

0 commit comments

Comments
 (0)