comparison Python/pythonrun.c @ 41044:c8ed4aea6c58 legacy-trunk

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.
author Georg Brandl <georg@python.org>
date Wed, 07 Mar 2007 00:40:28 +0000
parents 728c60e85efa
children a87bcc37bf0f
comparison
equal deleted inserted replaced
41043:728c60e85efa 41044:c8ed4aea6c58
847 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, 847 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
848 PyCompilerFlags *flags) 848 PyCompilerFlags *flags)
849 { 849 {
850 PyObject *m, *d, *v; 850 PyObject *m, *d, *v;
851 const char *ext; 851 const char *ext;
852 int set_file_name = 0, ret;
852 853
853 m = PyImport_AddModule("__main__"); 854 m = PyImport_AddModule("__main__");
854 if (m == NULL) 855 if (m == NULL)
855 return -1; 856 return -1;
856 d = PyModule_GetDict(m); 857 d = PyModule_GetDict(m);
860 return -1; 861 return -1;
861 if (PyDict_SetItemString(d, "__file__", f) < 0) { 862 if (PyDict_SetItemString(d, "__file__", f) < 0) {
862 Py_DECREF(f); 863 Py_DECREF(f);
863 return -1; 864 return -1;
864 } 865 }
866 set_file_name = 1;
865 Py_DECREF(f); 867 Py_DECREF(f);
866 } 868 }
867 ext = filename + strlen(filename) - 4; 869 ext = filename + strlen(filename) - 4;
868 if (maybe_pyc_file(fp, filename, ext, closeit)) { 870 if (maybe_pyc_file(fp, filename, ext, closeit)) {
869 /* Try to run a pyc file. First, re-open in binary */ 871 /* Try to run a pyc file. First, re-open in binary */
870 if (closeit) 872 if (closeit)
871 fclose(fp); 873 fclose(fp);
872 if ((fp = fopen(filename, "rb")) == NULL) { 874 if ((fp = fopen(filename, "rb")) == NULL) {
873 fprintf(stderr, "python: Can't reopen .pyc file\n"); 875 fprintf(stderr, "python: Can't reopen .pyc file\n");
874 return -1; 876 ret = -1;
877 goto done;
875 } 878 }
876 /* Turn on optimization if a .pyo file is given */ 879 /* Turn on optimization if a .pyo file is given */
877 if (strcmp(ext, ".pyo") == 0) 880 if (strcmp(ext, ".pyo") == 0)
878 Py_OptimizeFlag = 1; 881 Py_OptimizeFlag = 1;
879 v = run_pyc_file(fp, filename, d, d, flags); 882 v = run_pyc_file(fp, filename, d, d, flags);
881 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d, 884 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
882 closeit, flags); 885 closeit, flags);
883 } 886 }
884 if (v == NULL) { 887 if (v == NULL) {
885 PyErr_Print(); 888 PyErr_Print();
886 return -1; 889 ret = -1;
890 goto done;
887 } 891 }
888 Py_DECREF(v); 892 Py_DECREF(v);
889 if (Py_FlushLine()) 893 if (Py_FlushLine())
890 PyErr_Clear(); 894 PyErr_Clear();
891 return 0; 895 ret = 0;
896 done:
897 if (set_file_name && PyDict_DelItemString(d, "__file__"))
898 PyErr_Clear();
899 return ret;
892 } 900 }
893 901
894 int 902 int
895 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags) 903 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
896 { 904 {