Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
bpo-33451: Close pyc files before calling PyEval_EvalCode() (GH-7884)
Directly executed pyc files were being kept open longer than necessary.
(cherry picked from commit ea73775)

Co-authored-by: Zackery Spytz <zspytz@gmail.com>
  • Loading branch information
ZackerySpytz authored and miss-islington committed Jun 24, 2018
commit 3da1734e9e0f98b4af21b16724e680ff86822e4d
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Close directly executed pyc files before calling ``PyEval_EvalCode()``.
15 changes: 9 additions & 6 deletions Python/pythonrun.c
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,6 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
goto done;
}
v = run_pyc_file(pyc_fp, filename, d, d, flags);
fclose(pyc_fp);
} else {
/* When running from stdin, leave __main__.__loader__ alone */
if (strcmp(filename, "<stdin>") != 0 &&
Expand Down Expand Up @@ -1041,27 +1040,31 @@ run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
if (!PyErr_Occurred())
PyErr_SetString(PyExc_RuntimeError,
"Bad magic number in .pyc file");
return NULL;
goto error;
}
/* Skip mtime and size */
(void) PyMarshal_ReadLongFromFile(fp);
(void) PyMarshal_ReadLongFromFile(fp);
if (PyErr_Occurred())
return NULL;

if (PyErr_Occurred()) {
goto error;
}
v = PyMarshal_ReadLastObjectFromFile(fp);
if (v == NULL || !PyCode_Check(v)) {
Py_XDECREF(v);
PyErr_SetString(PyExc_RuntimeError,
"Bad code object in .pyc file");
return NULL;
goto error;
}
fclose(fp);
co = (PyCodeObject *)v;
v = PyEval_EvalCode((PyObject*)co, globals, locals);
if (v && flags)
flags->cf_flags |= (co->co_flags & PyCF_MASK);
Py_DECREF(co);
return v;
error:
fclose(fp);
return NULL;
}

PyObject *
Expand Down