Skip to content

Commit bfb709b

Browse files
[3.8] bpo-37521: No longer treat insertion into sys.modules as optional in importlib examples (GH-14723) (GH-14724)
Fix importlib examples to insert any newly created modules via importlib.util.module_from_spec() immediately into sys.modules instead of after calling loader.exec_module(). Thanks to Benjamin Mintz for finding the bug. https://bugs.python.org/issue37521 (cherry picked from commit 0827064) Co-authored-by: Brett Cannon <[email protected]> https://bugs.python.org/issue37521
1 parent 3d58b78 commit bfb709b

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

Doc/library/importlib.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,15 +1638,16 @@ import, then you should use :func:`importlib.util.find_spec`.
16381638
# For illustrative purposes.
16391639
name = 'itertools'
16401640

1641-
spec = importlib.util.find_spec(name)
1642-
if spec is None:
1643-
print("can't find the itertools module")
1641+
if name in sys.modules:
1642+
print(f"{name!r} already in sys.modules")
1643+
elif (spec := importlib.util.find_spec(name)) is None:
1644+
print(f"can't find the {name!r} module")
16441645
else:
16451646
# If you chose to perform the actual import ...
16461647
module = importlib.util.module_from_spec(spec)
1647-
spec.loader.exec_module(module)
1648-
# Adding the module to sys.modules is optional.
16491648
sys.modules[name] = module
1649+
spec.loader.exec_module(module)
1650+
print(f"{name!r} has been imported")
16501651

16511652

16521653
Importing a source file directly
@@ -1665,10 +1666,9 @@ To import a Python source file directly, use the following recipe
16651666

16661667
spec = importlib.util.spec_from_file_location(module_name, file_path)
16671668
module = importlib.util.module_from_spec(spec)
1668-
spec.loader.exec_module(module)
1669-
# Optional; only necessary if you want to be able to import the module
1670-
# by name later.
16711669
sys.modules[module_name] = module
1670+
spec.loader.exec_module(module)
1671+
16721672

16731673

16741674
Setting up an importer
@@ -1740,8 +1740,8 @@ Python 3.6 and newer for other parts of the code).
17401740
msg = f'No module named {absolute_name!r}'
17411741
raise ModuleNotFoundError(msg, name=absolute_name)
17421742
module = importlib.util.module_from_spec(spec)
1743-
spec.loader.exec_module(module)
17441743
sys.modules[absolute_name] = module
1744+
spec.loader.exec_module(module)
17451745
if path is not None:
17461746
setattr(parent_module, child_name, module)
17471747
return module
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fix `importlib` examples to insert any newly created modules via
2+
importlib.util.module_from_spec() immediately into sys.modules instead of
3+
after calling loader.exec_module().
4+
5+
Thanks to Benjamin Mintz for finding the bug.

0 commit comments

Comments
 (0)