Commit ea737751 authored by Zackery Spytz's avatar Zackery Spytz Committed by Nick Coghlan

bpo-33451: Close pyc files before calling PyEval_EvalCode() (GH-7884)

Directly executed pyc files were being kept open longer than necessary.
parent 0ba9a0b7
Close directly executed pyc files before calling ``PyEval_EvalCode()``.
...@@ -418,7 +418,6 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, ...@@ -418,7 +418,6 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
goto done; goto done;
} }
v = run_pyc_file(pyc_fp, filename, d, d, flags); v = run_pyc_file(pyc_fp, filename, d, d, flags);
fclose(pyc_fp);
} else { } else {
/* When running from stdin, leave __main__.__loader__ alone */ /* When running from stdin, leave __main__.__loader__ alone */
if (strcmp(filename, "<stdin>") != 0 && if (strcmp(filename, "<stdin>") != 0 &&
...@@ -1051,28 +1050,32 @@ run_pyc_file(FILE *fp, const char *filename, PyObject *globals, ...@@ -1051,28 +1050,32 @@ run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
if (!PyErr_Occurred()) if (!PyErr_Occurred())
PyErr_SetString(PyExc_RuntimeError, PyErr_SetString(PyExc_RuntimeError,
"Bad magic number in .pyc file"); "Bad magic number in .pyc file");
return NULL; goto error;
} }
/* Skip the rest of the header. */ /* Skip the rest of the header. */
(void) PyMarshal_ReadLongFromFile(fp); (void) PyMarshal_ReadLongFromFile(fp);
(void) PyMarshal_ReadLongFromFile(fp); (void) PyMarshal_ReadLongFromFile(fp);
(void) PyMarshal_ReadLongFromFile(fp); (void) PyMarshal_ReadLongFromFile(fp);
if (PyErr_Occurred()) if (PyErr_Occurred()) {
return NULL; goto error;
}
v = PyMarshal_ReadLastObjectFromFile(fp); v = PyMarshal_ReadLastObjectFromFile(fp);
if (v == NULL || !PyCode_Check(v)) { if (v == NULL || !PyCode_Check(v)) {
Py_XDECREF(v); Py_XDECREF(v);
PyErr_SetString(PyExc_RuntimeError, PyErr_SetString(PyExc_RuntimeError,
"Bad code object in .pyc file"); "Bad code object in .pyc file");
return NULL; goto error;
} }
fclose(fp);
co = (PyCodeObject *)v; co = (PyCodeObject *)v;
v = PyEval_EvalCode((PyObject*)co, globals, locals); v = PyEval_EvalCode((PyObject*)co, globals, locals);
if (v && flags) if (v && flags)
flags->cf_flags |= (co->co_flags & PyCF_MASK); flags->cf_flags |= (co->co_flags & PyCF_MASK);
Py_DECREF(co); Py_DECREF(co);
return v; return v;
error:
fclose(fp);
return NULL;
} }
PyObject * PyObject *
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment