summaryrefslogtreecommitdiff
path: root/Python/import.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2018-10-29 19:30:16 +0200
committerGitHub <noreply@github.com>2018-10-29 19:30:16 +0200
commitc93c58b5d560cfe44d9884ff02c9b18e06333360 (patch)
tree5a52914e13a0d5990b3644a61da2e7d948d655e8 /Python/import.c
parent542497aa9f71c664768c3d5b7398c03679d3a7e1 (diff)
downloadcpython-git-c93c58b5d560cfe44d9884ff02c9b18e06333360.tar.gz
bpo-33331: Clean modules in the reversed order in PyImport_Cleanup(). (GH-6565)
Modules imported last are now cleared first at interpreter shutdown. A newly imported module is moved to the end of sys.modules, behind modules on which it depends.
Diffstat (limited to 'Python/import.c')
-rw-r--r--Python/import.c7
1 files changed, 4 insertions, 3 deletions
diff --git a/Python/import.c b/Python/import.c
index 2a9a576658..1e8c07b0b2 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -543,9 +543,10 @@ PyImport_Cleanup(void)
module last. Likewise, we don't delete sys until the very
end because it is implicitly referenced (e.g. by print). */
if (weaklist != NULL) {
- Py_ssize_t i, n;
- n = PyList_GET_SIZE(weaklist);
- for (i = 0; i < n; i++) {
+ Py_ssize_t i;
+ /* Since dict is ordered in CPython 3.6+, modules are saved in
+ importing order. First clear modules imported later. */
+ for (i = PyList_GET_SIZE(weaklist) - 1; i >= 0; i--) {
PyObject *tup = PyList_GET_ITEM(weaklist, i);
PyObject *name = PyTuple_GET_ITEM(tup, 0);
PyObject *mod = PyWeakref_GET_OBJECT(PyTuple_GET_ITEM(tup, 1));