]> granicus.if.org Git - python/commitdiff
Patch #703779: unset __file__ in __main__ after running a file. This
authorGeorg Brandl <georg@python.org>
Wed, 7 Mar 2007 00:40:28 +0000 (00:40 +0000)
committerGeorg Brandl <georg@python.org>
Wed, 7 Mar 2007 00:40:28 +0000 (00:40 +0000)
makes the filenames the warning module prints much more sensible when
a PYTHONSTARTUP file is used.

Misc/NEWS
Python/pythonrun.c

index a783add959de81dc66099d1dd280724ef201d29e..bb0290905713db51198e8f824075ca932e1a954c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,10 @@ What's New in Python 2.6 alpha 1?
 Core and builtins
 -----------------
 
+- Patch #703779: unset __file__ in __main__ after running a file. This
+  makes the filenames the warning module prints much more sensible when
+  a PYTHONSTARTUP file is used.
+
 - Variant of patch #697613: don't exit the interpreter on a SystemExit
   exception if the -i command line option or PYTHONINSPECT environment
   variable is given, but break into the interactive interpreter just like
index aa7e6245677e6689e69ebe6134a0449e90322e3c..454afe47f78d66f2d1302c558355f432201dd93f 100644 (file)
@@ -849,6 +849,7 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
 {
        PyObject *m, *d, *v;
        const char *ext;
+       int set_file_name = 0, ret;
 
        m = PyImport_AddModule("__main__");
        if (m == NULL)
@@ -862,6 +863,7 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
                        Py_DECREF(f);
                        return -1;
                }
+               set_file_name = 1;
                Py_DECREF(f);
        }
        ext = filename + strlen(filename) - 4;
@@ -871,7 +873,8 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
                        fclose(fp);
                if ((fp = fopen(filename, "rb")) == NULL) {
                        fprintf(stderr, "python: Can't reopen .pyc file\n");
-                       return -1;
+                       ret = -1;
+                       goto done;
                }
                /* Turn on optimization if a .pyo file is given */
                if (strcmp(ext, ".pyo") == 0)
@@ -883,12 +886,17 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
        }
        if (v == NULL) {
                PyErr_Print();
-               return -1;
+               ret = -1;
+               goto done;
        }
        Py_DECREF(v);
        if (Py_FlushLine())
                PyErr_Clear();
-       return 0;
+       ret = 0;
+  done:
+       if (set_file_name && PyDict_DelItemString(d, "__file__"))
+               PyErr_Clear();
+       return ret;
 }
 
 int