]> granicus.if.org Git - python/commitdiff
Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
authorGuido van Rossum <guido@python.org>
Sun, 27 Aug 2000 19:21:52 +0000 (19:21 +0000)
committerGuido van Rossum <guido@python.org>
Sun, 27 Aug 2000 19:21:52 +0000 (19:21 +0000)
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)

Include/pythonrun.h
Modules/main.c
Python/bltinmodule.c
Python/pythonrun.c

index 05287021aa757c0a8277231ed04219b655593484..bbf2903a397978ae0439e170797319ec7212a744 100644 (file)
@@ -29,9 +29,11 @@ DL_IMPORT(PyThreadState *) Py_NewInterpreter(void);
 DL_IMPORT(void) Py_EndInterpreter(PyThreadState *);
 
 DL_IMPORT(int) PyRun_AnyFile(FILE *, char *);
+DL_IMPORT(int) PyRun_AnyFileEx(FILE *, char *, int);
 
 DL_IMPORT(int) PyRun_SimpleString(char *);
 DL_IMPORT(int) PyRun_SimpleFile(FILE *, char *);
+DL_IMPORT(int) PyRun_SimpleFileEx(FILE *, char *, int);
 DL_IMPORT(int) PyRun_InteractiveOne(FILE *, char *);
 DL_IMPORT(int) PyRun_InteractiveLoop(FILE *, char *);
 
@@ -40,6 +42,8 @@ DL_IMPORT(struct _node *) PyParser_SimpleParseFile(FILE *, char *, int);
 
 DL_IMPORT(PyObject *) PyRun_String(char *, int, PyObject *, PyObject *);
 DL_IMPORT(PyObject *) PyRun_File(FILE *, char *, int, PyObject *, PyObject *);
+DL_IMPORT(PyObject *) PyRun_FileEx(FILE *, char *, int,
+                                  PyObject *, PyObject *, int);
 
 DL_IMPORT(PyObject *) Py_CompileString(char *, char *, int);
 
index 34e99b9de6b37e99ec3800e48a34787a5736d989..9afe80b3b5642ee24ef9c1da735450fe216c2065 100644 (file)
@@ -268,11 +268,10 @@ Py_Main(int argc, char **argv)
                                }
                        }
                }
-               sts = PyRun_AnyFile(
+               sts = PyRun_AnyFileEx(
                        fp,
-                       filename == NULL ? "<stdin>" : filename) != 0;
-               if (filename != NULL)
-                       fclose(fp);
+                       filename == NULL ? "<stdin>" : filename,
+                       filename != NULL) != 0;
        }
 
        if (inspect && stdin_is_interactive &&
index ea1269ed9ea68b891815e5f4c29419817e50712a..c2a74996446e33bc2e97fba93dcd8d608b9eea65 100644 (file)
@@ -815,10 +815,7 @@ builtin_execfile(PyObject *self, PyObject *args)
                PyErr_SetFromErrno(PyExc_IOError);
                return NULL;
        }
-       res = PyRun_File(fp, filename, Py_file_input, globals, locals);
-       Py_BEGIN_ALLOW_THREADS
-       fclose(fp);
-       Py_END_ALLOW_THREADS
+       res = PyRun_FileEx(fp, filename, Py_file_input, globals, locals, 1);
        return res;
 }
 
index e951ccd1b7ac2cb3b129f714d283988e3bcad982..56b3ba82993733d2beb7dd09417ab032f74cb12f 100644 (file)
@@ -449,13 +449,23 @@ 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
@@ -541,6 +551,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 *