summaryrefslogtreecommitdiff
path: root/Python
diff options
context:
space:
mode:
authorFred Drake <fdrake@acm.org>2002-10-17 21:24:58 +0000
committerFred Drake <fdrake@acm.org>2002-10-17 21:24:58 +0000
commitf639b1aca2e4692f8f55fca6761ef4ef295e7e06 (patch)
treea8ed8469c0784beef148d90939eff89baf7f56df /Python
parent03fb883327f25d404a59fec0d27d4395a9acc7d3 (diff)
downloadcpython-f639b1aca2e4692f8f55fca6761ef4ef295e7e06.tar.gz
If we have a filename and __main__.__file__ hasn't already been set,
set it. Closes SF issue #624729.
Diffstat (limited to 'Python')
-rw-r--r--Python/pythonrun.c12
1 files changed, 11 insertions, 1 deletions
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 80a3157804..b85c390a77 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -675,12 +675,22 @@ PyRun_SimpleFileExFlags(FILE *fp, char *filename, int closeit,
if (m == NULL)
return -1;
d = PyModule_GetDict(m);
+ if (PyDict_GetItemString(d, "__file__") == NULL) {
+ PyObject *f = PyString_FromString(filename);
+ if (f == NULL)
+ return -1;
+ if (PyDict_SetItemString(d, "__file__", f) < 0) {
+ Py_DECREF(f);
+ return -1;
+ }
+ Py_DECREF(f);
+ }
ext = filename + strlen(filename) - 4;
if (maybe_pyc_file(fp, filename, ext, closeit)) {
/* Try to run a pyc file. First, re-open in binary */
if (closeit)
fclose(fp);
- if( (fp = fopen(filename, "rb")) == NULL ) {
+ if ((fp = fopen(filename, "rb")) == NULL) {
fprintf(stderr, "python: Can't reopen .pyc file\n");
return -1;
}