summaryrefslogtreecommitdiffstats
path: root/Python/pythonrun.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2000-08-27 19:21:52 (GMT)
committerGuido van Rossum <guido@python.org>2000-08-27 19:21:52 (GMT)
commit0df002c45b0bf2bf23bb8c0a395e86f907a94d81 (patch)
tree2812bdefa81aca791ce27544f424271e5f857c67 /Python/pythonrun.c
parent2f15b25da2060ab723e0bb82a8f4f713d547b2b8 (diff)
downloadcpython-0df002c45b0bf2bf23bb8c0a395e86f907a94d81.zip
cpython-0df002c45b0bf2bf23bb8c0a395e86f907a94d81.tar.gz
cpython-0df002c45b0bf2bf23bb8c0a395e86f907a94d81.tar.bz2
Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
PyRun_FileEx(). These are the same as their non-Ex counterparts but have an extra argument, a flag telling them to close the file when done. Then this is used by Py_Main() and execfile() to close the file after it is parsed but before it is executed. Adding APIs seems strange given the feature freeze but it's the only way I see to close the bug report without incompatible changes. [ Bug #110616 ] source file stays open after parsing is done (PR#209)
Diffstat (limited to 'Python/pythonrun.c')
-rw-r--r--Python/pythonrun.c40
1 files changed, 33 insertions, 7 deletions
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index e951ccd..56b3ba8 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -450,12 +450,22 @@ initsite(void)
int
PyRun_AnyFile(FILE *fp, char *filename)
{
+ return PyRun_AnyFileEx(fp, filename, 0);
+}
+
+int
+PyRun_AnyFileEx(FILE *fp, char *filename, int closeit)
+{
if (filename == NULL)
filename = "???";
- if (Py_FdIsInteractive(fp, filename))
- return PyRun_InteractiveLoop(fp, filename);
+ if (Py_FdIsInteractive(fp, filename)) {
+ int err = PyRun_InteractiveLoop(fp, filename);
+ if (closeit)
+ fclose(fp);
+ return err;
+ }
else
- return PyRun_SimpleFile(fp, filename);
+ return PyRun_SimpleFileEx(fp, filename, closeit);
}
int
@@ -542,6 +552,12 @@ PyRun_InteractiveOne(FILE *fp, char *filename)
int
PyRun_SimpleFile(FILE *fp, char *filename)
{
+ return PyRun_SimpleFileEx(fp, filename, 0);
+}
+
+int
+PyRun_SimpleFileEx(FILE *fp, char *filename, int closeit)
+{
PyObject *m, *d, *v;
char *ext;
@@ -558,7 +574,8 @@ PyRun_SimpleFile(FILE *fp, char *filename)
#endif /* macintosh */
) {
/* Try to run a pyc file. First, re-open in binary */
- /* Don't close, done in main: fclose(fp); */
+ if (closeit)
+ fclose(fp);
if( (fp = fopen(filename, "rb")) == NULL ) {
fprintf(stderr, "python: Can't reopen .pyc file\n");
return -1;
@@ -568,7 +585,7 @@ PyRun_SimpleFile(FILE *fp, char *filename)
Py_OptimizeFlag = 1;
v = run_pyc_file(fp, filename, d, d);
} else {
- v = PyRun_File(fp, filename, Py_file_input, d, d);
+ v = PyRun_FileEx(fp, filename, Py_file_input, d, d, closeit);
}
if (v == NULL) {
PyErr_Print();
@@ -845,8 +862,17 @@ PyObject *
PyRun_File(FILE *fp, char *filename, int start, PyObject *globals,
PyObject *locals)
{
- return run_err_node(PyParser_SimpleParseFile(fp, filename, start),
- filename, globals, locals);
+ PyRun_FileEx(fp, filename, start, globals, locals, 0);
+}
+
+PyObject *
+PyRun_FileEx(FILE *fp, char *filename, int start, PyObject *globals,
+ PyObject *locals, int closeit)
+{
+ node *n = PyParser_SimpleParseFile(fp, filename, start);
+ if (closeit)
+ fclose(fp);
+ return run_err_node(n, filename, globals, locals);
}
static PyObject *