comparison Python/pythonrun.c @ 25827:093fe5b9f6f9 legacy-trunk

If we have a filename and __main__.__file__ hasn't already been set, set it. Closes SF issue #624729.
author Fred Drake <fdrake@acm.org>
date Thu, 17 Oct 2002 21:24:58 +0000
parents 0231cc54f87e
children 244e8fbe4e20
comparison
equal deleted inserted replaced
25826:c2dd41301841 25827:093fe5b9f6f9
673 673
674 m = PyImport_AddModule("__main__"); 674 m = PyImport_AddModule("__main__");
675 if (m == NULL) 675 if (m == NULL)
676 return -1; 676 return -1;
677 d = PyModule_GetDict(m); 677 d = PyModule_GetDict(m);
678 if (PyDict_GetItemString(d, "__file__") == NULL) {
679 PyObject *f = PyString_FromString(filename);
680 if (f == NULL)
681 return -1;
682 if (PyDict_SetItemString(d, "__file__", f) < 0) {
683 Py_DECREF(f);
684 return -1;
685 }
686 Py_DECREF(f);
687 }
678 ext = filename + strlen(filename) - 4; 688 ext = filename + strlen(filename) - 4;
679 if (maybe_pyc_file(fp, filename, ext, closeit)) { 689 if (maybe_pyc_file(fp, filename, ext, closeit)) {
680 /* Try to run a pyc file. First, re-open in binary */ 690 /* Try to run a pyc file. First, re-open in binary */
681 if (closeit) 691 if (closeit)
682 fclose(fp); 692 fclose(fp);
683 if( (fp = fopen(filename, "rb")) == NULL ) { 693 if ((fp = fopen(filename, "rb")) == NULL) {
684 fprintf(stderr, "python: Can't reopen .pyc file\n"); 694 fprintf(stderr, "python: Can't reopen .pyc file\n");
685 return -1; 695 return -1;
686 } 696 }
687 /* Turn on optimization if a .pyo file is given */ 697 /* Turn on optimization if a .pyo file is given */
688 if (strcmp(ext, ".pyo") == 0) 698 if (strcmp(ext, ".pyo") == 0)