Mercurial > cpython-lru_cache-weakref
comparison Python/pythonrun.c @ 80292:4ebe1ede981e 2.7
Issue #15001: fix segfault on "del sys.modules['__main__']"
Patch by Victor Stinner.
| author | Hynek Schlawack <hs@ox.cx> |
|---|---|
| date | Wed, 07 Nov 2012 09:41:28 +0100 |
| parents | 013766e7a6eb |
| children | dc9f17f10899 |
comparison
equal
deleted
inserted
replaced
| 80258:1c54def5947c | 80292:4ebe1ede981e |
|---|---|
| 905 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, | 905 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, |
| 906 PyCompilerFlags *flags) | 906 PyCompilerFlags *flags) |
| 907 { | 907 { |
| 908 PyObject *m, *d, *v; | 908 PyObject *m, *d, *v; |
| 909 const char *ext; | 909 const char *ext; |
| 910 int set_file_name = 0, ret, len; | 910 int set_file_name = 0, len, ret = -1; |
| 911 | 911 |
| 912 m = PyImport_AddModule("__main__"); | 912 m = PyImport_AddModule("__main__"); |
| 913 if (m == NULL) | 913 if (m == NULL) |
| 914 return -1; | 914 return -1; |
| 915 Py_INCREF(m); | |
| 915 d = PyModule_GetDict(m); | 916 d = PyModule_GetDict(m); |
| 916 if (PyDict_GetItemString(d, "__file__") == NULL) { | 917 if (PyDict_GetItemString(d, "__file__") == NULL) { |
| 917 PyObject *f = PyString_FromString(filename); | 918 PyObject *f = PyString_FromString(filename); |
| 918 if (f == NULL) | 919 if (f == NULL) |
| 919 return -1; | 920 goto done; |
| 920 if (PyDict_SetItemString(d, "__file__", f) < 0) { | 921 if (PyDict_SetItemString(d, "__file__", f) < 0) { |
| 921 Py_DECREF(f); | 922 Py_DECREF(f); |
| 922 return -1; | 923 goto done; |
| 923 } | 924 } |
| 924 set_file_name = 1; | 925 set_file_name = 1; |
| 925 Py_DECREF(f); | 926 Py_DECREF(f); |
| 926 } | 927 } |
| 927 len = strlen(filename); | 928 len = strlen(filename); |
| 930 /* Try to run a pyc file. First, re-open in binary */ | 931 /* Try to run a pyc file. First, re-open in binary */ |
| 931 if (closeit) | 932 if (closeit) |
| 932 fclose(fp); | 933 fclose(fp); |
| 933 if ((fp = fopen(filename, "rb")) == NULL) { | 934 if ((fp = fopen(filename, "rb")) == NULL) { |
| 934 fprintf(stderr, "python: Can't reopen .pyc file\n"); | 935 fprintf(stderr, "python: Can't reopen .pyc file\n"); |
| 935 ret = -1; | |
| 936 goto done; | 936 goto done; |
| 937 } | 937 } |
| 938 /* Turn on optimization if a .pyo file is given */ | 938 /* Turn on optimization if a .pyo file is given */ |
| 939 if (strcmp(ext, ".pyo") == 0) | 939 if (strcmp(ext, ".pyo") == 0) |
| 940 Py_OptimizeFlag = 1; | 940 Py_OptimizeFlag = 1; |
| 943 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d, | 943 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d, |
| 944 closeit, flags); | 944 closeit, flags); |
| 945 } | 945 } |
| 946 if (v == NULL) { | 946 if (v == NULL) { |
| 947 PyErr_Print(); | 947 PyErr_Print(); |
| 948 ret = -1; | |
| 949 goto done; | 948 goto done; |
| 950 } | 949 } |
| 951 Py_DECREF(v); | 950 Py_DECREF(v); |
| 952 if (Py_FlushLine()) | 951 if (Py_FlushLine()) |
| 953 PyErr_Clear(); | 952 PyErr_Clear(); |
| 954 ret = 0; | 953 ret = 0; |
| 955 done: | 954 done: |
| 956 if (set_file_name && PyDict_DelItemString(d, "__file__")) | 955 if (set_file_name && PyDict_DelItemString(d, "__file__")) |
| 957 PyErr_Clear(); | 956 PyErr_Clear(); |
| 957 Py_DECREF(m); | |
| 958 return ret; | 958 return ret; |
| 959 } | 959 } |
| 960 | 960 |
| 961 int | 961 int |
| 962 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags) | 962 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags) |
