Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[2.7] bpo-33330: Improve error handling in PyImport_Cleanup(). (GH-6564). #6605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[2.7] bpo-33330: Improve error handling in PyImport_Cleanup(). (GH-6564
…).

(cherry picked from commit e9d9494)

Co-authored-by: Serhiy Storchaka <[email protected]>
  • Loading branch information
serhiy-storchaka committed Apr 25, 2018
commit 94f84b6f7c34806c8b16c6dc07c3aa44bdbfc80c
38 changes: 30 additions & 8 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,9 @@ PyImport_Cleanup(void)
dict = PyModule_GetDict(value);
if (Py_VerboseFlag)
PySys_WriteStderr("# clear __builtin__._\n");
PyDict_SetItemString(dict, "_", Py_None);
if (PyDict_SetItemString(dict, "_", Py_None) < 0) {
PyErr_Clear();
}
}
value = PyDict_GetItemString(modules, "sys");
if (value != NULL && PyModule_Check(value)) {
Expand All @@ -457,15 +459,19 @@ PyImport_Cleanup(void)
for (p = sys_deletes; *p != NULL; p++) {
if (Py_VerboseFlag)
PySys_WriteStderr("# clear sys.%s\n", *p);
PyDict_SetItemString(dict, *p, Py_None);
if (PyDict_SetItemString(dict, *p, Py_None) < 0) {
PyErr_Clear();
}
}
for (p = sys_files; *p != NULL; p+=2) {
if (Py_VerboseFlag)
PySys_WriteStderr("# restore sys.%s\n", *p);
v = PyDict_GetItemString(dict, *(p+1));
if (v == NULL)
v = Py_None;
PyDict_SetItemString(dict, *p, v);
if (PyDict_SetItemString(dict, *p, v) < 0) {
PyErr_Clear();
}
}
}

Expand All @@ -475,7 +481,9 @@ PyImport_Cleanup(void)
if (Py_VerboseFlag)
PySys_WriteStderr("# cleanup __main__\n");
_PyModule_Clear(value);
PyDict_SetItemString(modules, "__main__", Py_None);
if (PyDict_SetItemString(modules, "__main__", Py_None) < 0) {
PyErr_Clear();
}
}

/* The special treatment of __builtin__ here is because even
Expand Down Expand Up @@ -510,10 +518,15 @@ PyImport_Cleanup(void)
PySys_WriteStderr(
"# cleanup[1] %s\n", name);
_PyModule_Clear(value);
PyDict_SetItem(modules, key, Py_None);
if (PyDict_SetItem(modules, key, Py_None) < 0) {
PyErr_Clear();
}
ndone++;
}
}
if (PyErr_Occurred()) {
PyErr_Clear();
}
} while (ndone > 0);

/* Next, delete all modules (still skipping __builtin__ and sys) */
Expand All @@ -528,7 +541,12 @@ PyImport_Cleanup(void)
if (Py_VerboseFlag)
PySys_WriteStderr("# cleanup[2] %s\n", name);
_PyModule_Clear(value);
PyDict_SetItem(modules, key, Py_None);
if (PyDict_SetItem(modules, key, Py_None) < 0) {
PyErr_Clear();
}
}
if (PyErr_Occurred()) {
PyErr_Clear();
}
}

Expand All @@ -538,14 +556,18 @@ PyImport_Cleanup(void)
if (Py_VerboseFlag)
PySys_WriteStderr("# cleanup sys\n");
_PyModule_Clear(value);
PyDict_SetItemString(modules, "sys", Py_None);
if (PyDict_SetItemString(modules, "sys", Py_None) < 0) {
PyErr_Clear();
}
}
value = PyDict_GetItemString(modules, "__builtin__");
if (value != NULL && PyModule_Check(value)) {
if (Py_VerboseFlag)
PySys_WriteStderr("# cleanup __builtin__\n");
_PyModule_Clear(value);
PyDict_SetItemString(modules, "__builtin__", Py_None);
if (PyDict_SetItemString(modules, "__builtin__", Py_None) < 0) {
PyErr_Clear();
}
}

/* Finally, clear and delete the modules directory */
Expand Down