Skip to content

Commit db88b24

Browse files
Move a module to the end of sys.modules when it have finished execution.
1 parent 6018740 commit db88b24

2 files changed

Lines changed: 859 additions & 851 deletions

File tree

Lib/importlib/_bootstrap.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,9 @@ def _exec(spec, module):
628628
spec.loader.load_module(name)
629629
else:
630630
spec.loader.exec_module(module)
631-
return sys.modules[name]
631+
module = sys.modules.pop(spec.name)
632+
sys.modules[spec.name] = module
633+
return module
632634

633635

634636
def _load_backward_compatible(spec):
@@ -676,10 +678,13 @@ def _load_unlocked(spec):
676678
else:
677679
spec.loader.exec_module(module)
678680

681+
# Move the module to the end of sys.modules.
679682
# We don't ensure that the import-related module attributes get
680683
# set in the sys.modules replacement case. Such modules are on
681684
# their own.
682-
return sys.modules[spec.name]
685+
module = sys.modules.pop(spec.name)
686+
sys.modules[spec.name] = module
687+
return module
683688

684689
# A method used during testing of _load_unlocked() and by
685690
# _load_module_shim().

0 commit comments

Comments
 (0)