Skip to content

Commit 6018740

Browse files
bpo-33331: Clean modules in the reversed order in PyImport_Cleanup().
Modules imported last are now cleared first at interpreter shutdown.
1 parent d4c76d9 commit 6018740

2 files changed

Lines changed: 5 additions & 3 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Modules imported last are now cleared first at interpreter shutdown.

Python/import.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -543,9 +543,10 @@ PyImport_Cleanup(void)
543543
module last. Likewise, we don't delete sys until the very
544544
end because it is implicitly referenced (e.g. by print). */
545545
if (weaklist != NULL) {
546-
Py_ssize_t i, n;
547-
n = PyList_GET_SIZE(weaklist);
548-
for (i = 0; i < n; i++) {
546+
Py_ssize_t i;
547+
/* Since dict is ordered in CPython 3.6+, modules are saved in
548+
importing order. First clear modules imported later. */
549+
for (i = PyList_GET_SIZE(weaklist) - 1; i >= 0; i--) {
549550
PyObject *tup = PyList_GET_ITEM(weaklist, i);
550551
PyObject *name = PyTuple_GET_ITEM(tup, 0);
551552
PyObject *mod = PyWeakref_GET_OBJECT(PyTuple_GET_ITEM(tup, 1));

0 commit comments

Comments
 (0)