summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2019-07-12 22:51:48 (GMT)
committerGitHub <noreply@github.com>2019-07-12 22:51:48 (GMT)
commitbfb709b771230e7e466961b5ddf4852962602450 (patch)
tree0de45accf58e315f6dfbac3559a996784f90b9f4
parent3d58b78481e0238593f85cc182b798fe3b77648c (diff)
downloadcpython-bfb709b771230e7e466961b5ddf4852962602450.zip
cpython-bfb709b771230e7e466961b5ddf4852962602450.tar.gz
cpython-bfb709b771230e7e466961b5ddf4852962602450.tar.bz2
[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 0827064c955f88df8ba5621d8e3d81be3cfc26a9) Co-authored-by: Brett Cannon <54418+brettcannon@users.noreply.github.com> https://bugs.python.org/issue37521
-rw-r--r--Doc/library/importlib.rst18
-rw-r--r--Misc/NEWS.d/next/Documentation/2019-07-12-15-09-56.bpo-37521.7tiFR-.rst5
2 files changed, 14 insertions, 9 deletions
diff --git a/Doc/library/importlib.rst b/Doc/library/importlib.rst
index df184b3..9fab077 100644
--- a/Doc/library/importlib.rst
+++ b/Doc/library/importlib.rst
@@ -1638,15 +1638,16 @@ import, then you should use :func:`importlib.util.find_spec`.
# For illustrative purposes.
name = 'itertools'
- spec = importlib.util.find_spec(name)
- if spec is None:
- print("can't find the itertools module")
+ if name in sys.modules:
+ print(f"{name!r} already in sys.modules")
+ elif (spec := importlib.util.find_spec(name)) is None:
+ print(f"can't find the {name!r} module")
else:
# If you chose to perform the actual import ...
module = importlib.util.module_from_spec(spec)
- spec.loader.exec_module(module)
- # Adding the module to sys.modules is optional.
sys.modules[name] = module
+ spec.loader.exec_module(module)
+ print(f"{name!r} has been imported")
Importing a source file directly
@@ -1665,10 +1666,9 @@ To import a Python source file directly, use the following recipe
spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
- spec.loader.exec_module(module)
- # Optional; only necessary if you want to be able to import the module
- # by name later.
sys.modules[module_name] = module
+ spec.loader.exec_module(module)
+
Setting up an importer
@@ -1740,8 +1740,8 @@ Python 3.6 and newer for other parts of the code).
msg = f'No module named {absolute_name!r}'
raise ModuleNotFoundError(msg, name=absolute_name)
module = importlib.util.module_from_spec(spec)
- spec.loader.exec_module(module)
sys.modules[absolute_name] = module
+ spec.loader.exec_module(module)
if path is not None:
setattr(parent_module, child_name, module)
return module
diff --git a/Misc/NEWS.d/next/Documentation/2019-07-12-15-09-56.bpo-37521.7tiFR-.rst b/Misc/NEWS.d/next/Documentation/2019-07-12-15-09-56.bpo-37521.7tiFR-.rst
new file mode 100644
index 0000000..35d7f56
--- /dev/null
+++ b/Misc/NEWS.d/next/Documentation/2019-07-12-15-09-56.bpo-37521.7tiFR-.rst
@@ -0,0 +1,5 @@
+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.