diff options
author | Georg Brandl <georg@python.org> | 2007-03-07 00:40:28 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2007-03-07 00:40:28 (GMT) |
commit | aa2321b0f8f8d51fe6aa1e8b9c81d989e642982d (patch) | |
tree | da24f798ec16aa968cfa6fed0f3844eb7e81b0e8 /Python | |
parent | 49aafc9f2ce7d41be677cdefbcf0af6d9bd1e752 (diff) | |
download | cpython-aa2321b0f8f8d51fe6aa1e8b9c81d989e642982d.zip cpython-aa2321b0f8f8d51fe6aa1e8b9c81d989e642982d.tar.gz cpython-aa2321b0f8f8d51fe6aa1e8b9c81d989e642982d.tar.bz2 |
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.
Diffstat (limited to 'Python')
-rw-r--r-- | Python/pythonrun.c | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/Python/pythonrun.c b/Python/pythonrun.c index aa7e624..454afe4 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -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 |