[Python-checkins] bpo-33451: Close pyc files before calling PyEval_EvalCode() (GH-7884)

Miss Islington (bot) webhook-mailer at python.org
Sat Jun 23 23:31:24 EDT 2018


https://github.com/python/cpython/commit/56aaef0ddba6275b8043b58433739a64497f33b4
commit: 56aaef0ddba6275b8043b58433739a64497f33b4
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018-06-23T20:31:21-07:00
summary:

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 ea737751b10fff752aafed0231e8a02b82ba365d)

Co-authored-by: Zackery Spytz <zspytz at gmail.com>

files:
A Misc/NEWS.d/next/Core and Builtins/2018-06-23-15-32-02.bpo-33451.sWN-1l.rst
M Python/pythonrun.c

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 000000000000..202fb38a370c
--- /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 directly executed pyc files before calling ``PyEval_EvalCode()``.
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 26f74c80d032..24c476b20ee4 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, "<stdin>") != 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 *



More information about the Python-checkins mailing list