diff options
author | Brett Cannon <brett@python.org> | 2020-12-04 23:39:21 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-04 23:39:21 (GMT) |
commit | 2de5097ba4c50eba90df55696a7b2e74c93834d4 (patch) | |
tree | 7a3d54477997114ef27136e9c9da0e380f835a34 /Lib/importlib | |
parent | 79c1849b9e5b635bd36b13e1be9dc7cbc2bd6312 (diff) | |
download | cpython-2de5097ba4c50eba90df55696a7b2e74c93834d4.zip cpython-2de5097ba4c50eba90df55696a7b2e74c93834d4.tar.gz cpython-2de5097ba4c50eba90df55696a7b2e74c93834d4.tar.bz2 |
bpo-26131: Deprecate usage of load_module() (GH-23469)
Raise an ImportWarning when the import system falls back on load_module(). As for implementations of load_module(), raise a DeprecationWarning.
Diffstat (limited to 'Lib/importlib')
-rw-r--r-- | Lib/importlib/_abc.py | 1 | ||||
-rw-r--r-- | Lib/importlib/_bootstrap.py | 24 | ||||
-rw-r--r-- | Lib/importlib/_bootstrap_external.py | 6 |
3 files changed, 23 insertions, 8 deletions
diff --git a/Lib/importlib/_abc.py b/Lib/importlib/_abc.py index fb5ec72..7591946 100644 --- a/Lib/importlib/_abc.py +++ b/Lib/importlib/_abc.py @@ -35,6 +35,7 @@ class Loader(metaclass=abc.ABCMeta): """ if not hasattr(self, 'exec_module'): raise ImportError + # Warning implemented in _load_module_shim(). return _bootstrap._load_module_shim(self, fullname) def module_repr(self, module): diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index 9b7335b..e4f893c 100644 --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -20,6 +20,12 @@ work. One should use importlib as the public-facing version of this module. # reference any injected objects! This includes not only global code but also # anything specified at the class level. +def _object_name(obj): + try: + return obj.__qualname__ + except AttributeError: + return type(obj).__qualname__ + # Bootstrap-related code ###################################################### # Modules injected manually by _setup() @@ -272,6 +278,9 @@ def _load_module_shim(self, fullname): This method is deprecated. Use loader.exec_module instead. """ + msg = ("the load_module() method is deprecated and slated for removal in " + "Python 3.12; use exec_module() instead") + _warnings.warn(msg, DeprecationWarning) spec = spec_from_loader(fullname, self) if fullname in sys.modules: module = sys.modules[fullname] @@ -612,9 +621,9 @@ def _exec(spec, module): else: _init_module_attrs(spec, module, override=True) if not hasattr(spec.loader, 'exec_module'): - # (issue19713) Once BuiltinImporter and ExtensionFileLoader - # have exec_module() implemented, we can add a deprecation - # warning here. + msg = (f"{_object_name(spec.loader)}.exec_module() not found; " + "falling back to load_module()") + _warnings.warn(msg, ImportWarning) spec.loader.load_module(name) else: spec.loader.exec_module(module) @@ -627,9 +636,8 @@ def _exec(spec, module): def _load_backward_compatible(spec): - # (issue19713) Once BuiltinImporter and ExtensionFileLoader - # have exec_module() implemented, we can add a deprecation - # warning here. + # It is assumed that all callers have been warned about using load_module() + # appropriately before calling this function. try: spec.loader.load_module(spec.name) except: @@ -668,6 +676,9 @@ def _load_unlocked(spec): if spec.loader is not None: # Not a namespace package. if not hasattr(spec.loader, 'exec_module'): + msg = (f"{_object_name(spec.loader)}.exec_module() not found; " + "falling back to load_module()") + _warnings.warn(msg, ImportWarning) return _load_backward_compatible(spec) module = module_from_spec(spec) @@ -851,6 +862,7 @@ class FrozenImporter: This method is deprecated. Use exec_module() instead. """ + # Warning about deprecation implemented in _load_module_shim(). return _load_module_shim(cls, fullname) @classmethod diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py index b8dd128..d9e44df 100644 --- a/Lib/importlib/_bootstrap_external.py +++ b/Lib/importlib/_bootstrap_external.py @@ -832,7 +832,8 @@ class _LoaderBasics: _bootstrap._call_with_frames_removed(exec, code, module.__dict__) def load_module(self, fullname): - """This module is deprecated.""" + """This method is deprecated.""" + # Warning implemented in _load_module_shim(). return _bootstrap._load_module_shim(self, fullname) @@ -1007,7 +1008,7 @@ class FileLoader: """ # The only reason for this method is for the name check. # Issue #14857: Avoid the zero-argument form of super so the implementation - # of that form can be updated without breaking the frozen module + # of that form can be updated without breaking the frozen module. return super(FileLoader, self).load_module(fullname) @_check_name @@ -1253,6 +1254,7 @@ class _NamespaceLoader: # The import system never calls this method. _bootstrap._verbose_message('namespace module loaded with path {!r}', self._path) + # Warning implemented in _load_module_shim(). return _bootstrap._load_module_shim(self, fullname) |