mirror of
https://github.com/python/cpython.git
synced 2025-07-10 04:45:36 +00:00
Issue #15001: fix segfault on "del sys.module['__main__']"
Patch by Victor Stinner.
This commit is contained in:
parent
692b023f77
commit
5c6b3e214c
3 changed files with 21 additions and 6 deletions
|
@ -1257,25 +1257,26 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
|
|||
{
|
||||
PyObject *m, *d, *v;
|
||||
const char *ext;
|
||||
int set_file_name = 0, ret;
|
||||
int set_file_name = 0, ret = -1;
|
||||
size_t len;
|
||||
|
||||
m = PyImport_AddModule("__main__");
|
||||
if (m == NULL)
|
||||
return -1;
|
||||
Py_INCREF(m);
|
||||
d = PyModule_GetDict(m);
|
||||
if (PyDict_GetItemString(d, "__file__") == NULL) {
|
||||
PyObject *f;
|
||||
f = PyUnicode_DecodeFSDefault(filename);
|
||||
if (f == NULL)
|
||||
return -1;
|
||||
goto done;
|
||||
if (PyDict_SetItemString(d, "__file__", f) < 0) {
|
||||
Py_DECREF(f);
|
||||
return -1;
|
||||
goto done;
|
||||
}
|
||||
if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
|
||||
Py_DECREF(f);
|
||||
return -1;
|
||||
goto done;
|
||||
}
|
||||
set_file_name = 1;
|
||||
Py_DECREF(f);
|
||||
|
@ -1288,7 +1289,6 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
|
|||
fclose(fp);
|
||||
if ((fp = fopen(filename, "rb")) == NULL) {
|
||||
fprintf(stderr, "python: Can't reopen .pyc file\n");
|
||||
ret = -1;
|
||||
goto done;
|
||||
}
|
||||
/* Turn on optimization if a .pyo file is given */
|
||||
|
@ -1302,7 +1302,6 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
|
|||
flush_io();
|
||||
if (v == NULL) {
|
||||
PyErr_Print();
|
||||
ret = -1;
|
||||
goto done;
|
||||
}
|
||||
Py_DECREF(v);
|
||||
|
@ -1310,6 +1309,7 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
|
|||
done:
|
||||
if (set_file_name && PyDict_DelItemString(d, "__file__"))
|
||||
PyErr_Clear();
|
||||
Py_DECREF(m);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue