]> granicus.if.org Git - python/commitdiff
If we have a filename and __main__.__file__ hasn't already been set,
authorFred Drake <fdrake@acm.org>
Thu, 17 Oct 2002 21:24:58 +0000 (21:24 +0000)
committerFred Drake <fdrake@acm.org>
Thu, 17 Oct 2002 21:24:58 +0000 (21:24 +0000)
set it.
Closes SF issue #624729.

Python/pythonrun.c

index 80a315780407913ff0512103ea1fc4e1c42a24f1..b85c390a770214a022656af0b13bdb7498aa8526 100644 (file)
@@ -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;
                }