From 0de9c81c604e9e6257e8be27a0a9830aac8c44e0 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Sat, 23 Jun 2018 15:34:49 -0600 Subject: [PATCH 1/2] bpo-33451: Close .pyc files before calling PyEval_EvalCode() .pyc files were kept open longer than necessary. --- .../2018-06-23-15-32-02.bpo-33451.sWN-1l.rst | 1 + Python/pythonrun.c | 15 +++++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2018-06-23-15-32-02.bpo-33451.sWN-1l.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-06-23-15-32-02.bpo-33451.sWN-1l.rst b/Misc/NEWS.d/next/Core and Builtins/2018-06-23-15-32-02.bpo-33451.sWN-1l.rst new file mode 100644 index 00000000000000..e879738bb63424 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2018-06-23-15-32-02.bpo-33451.sWN-1l.rst @@ -0,0 +1 @@ +Close .pyc files before calling PyEval_EvalCode(). diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 3d40c79bc1bec1..5cf7c33c93ae67 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -418,7 +418,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, "") != 0 && @@ -1051,28 +1050,32 @@ 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 the rest of the header. */ (void) PyMarshal_ReadLongFromFile(fp); (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 * From 13f4c40565a9e1fcf7f5f5cfc148095ce9bae4f0 Mon Sep 17 00:00:00 2001 From: Nick Coghlan Date: Sun, 24 Jun 2018 12:29:42 +1000 Subject: [PATCH 2/2] Clarify What's New entry --- .../Core and Builtins/2018-06-23-15-32-02.bpo-33451.sWN-1l.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-06-23-15-32-02.bpo-33451.sWN-1l.rst b/Misc/NEWS.d/next/Core and Builtins/2018-06-23-15-32-02.bpo-33451.sWN-1l.rst index e879738bb63424..202fb38a370c9b 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2018-06-23-15-32-02.bpo-33451.sWN-1l.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2018-06-23-15-32-02.bpo-33451.sWN-1l.rst @@ -1 +1 @@ -Close .pyc files before calling PyEval_EvalCode(). +Close directly executed pyc files before calling ``PyEval_EvalCode()``.