summaryrefslogtreecommitdiff
path: root/Lib/importlib
diff options
context:
space:
mode:
authorDino Viehland <dinoviehland@gmail.com>2020-01-22 16:42:38 -0800
committerGitHub <noreply@github.com>2020-01-22 16:42:38 -0800
commit9b6fec46513006d7b06fcb645cca6e4f5bf7c7b8 (patch)
treed159d657c67c4f4ef7a961a5e73a119644de8d3e /Lib/importlib
parentd3ae95e1e945ed20297e1c38ba43a18b7a868ab6 (diff)
downloadcpython-git-9b6fec46513006d7b06fcb645cca6e4f5bf7c7b8.tar.gz
bpo-39336: Allow packages to not let their child modules be set on them (#18006)
* bpo-39336: Allow setattr to fail on modules which aren't assignable When attaching a child module to a package if the object in sys.modules raises an AttributeError (e.g. because it is immutable) it causes the whole import to fail. This now allows immutable packages to exist and an ImportWarning is reported and the AttributeError exception is ignored.
Diffstat (limited to 'Lib/importlib')
-rw-r--r--Lib/importlib/_bootstrap.py7
1 files changed, 6 insertions, 1 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
index 8de0e9ee79..7b74e88820 100644
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -978,7 +978,12 @@ def _find_and_load_unlocked(name, import_):
if parent:
# Set the module as an attribute on its parent.
parent_module = sys.modules[parent]
- setattr(parent_module, name.rpartition('.')[2], module)
+ child = name.rpartition('.')[2]
+ try:
+ setattr(parent_module, child, module)
+ except AttributeError:
+ msg = f"Cannot set an attribute on {parent!r} for child module {child!r}"
+ _warnings.warn(msg, ImportWarning)
return module