Skip to content
95 changes: 15 additions & 80 deletions Doc/library/importlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@ ABC hierarchy::
.. versionchanged:: 3.7
Introduced the optional :meth:`get_resource_reader` method.

.. versionchanged:: 3.15
Removed the ``load_module()`` method.

.. method:: create_module(spec)

A method that returns the module object to use when
Expand All @@ -344,47 +347,6 @@ ABC hierarchy::
.. versionchanged:: 3.6
:meth:`create_module` must also be defined.

.. method:: load_module(fullname)

A legacy method for loading a module. If the module cannot be
loaded, :exc:`ImportError` is raised, otherwise the loaded module is
returned.

If the requested module already exists in :data:`sys.modules`, that
module should be used and reloaded.
Otherwise the loader should create a new module and insert it into
:data:`sys.modules` before any loading begins, to prevent recursion
from the import. If the loader inserted a module and the load fails, it
must be removed by the loader from :data:`sys.modules`; modules already
in :data:`sys.modules` before the loader began execution should be left
alone.

The loader should set several attributes on the module
(note that some of these attributes can change when a module is
reloaded):

- :attr:`module.__name__`
- :attr:`module.__file__`
- :attr:`module.__cached__` *(deprecated)*
- :attr:`module.__path__`
- :attr:`module.__package__` *(deprecated)*
- :attr:`module.__loader__` *(deprecated)*

When :meth:`exec_module` is available then backwards-compatible
functionality is provided.

.. versionchanged:: 3.4
Raise :exc:`ImportError` when called instead of
:exc:`NotImplementedError`. Functionality provided when
:meth:`exec_module` is available.

.. deprecated-removed:: 3.4 3.15
The recommended API for loading a module is :meth:`exec_module`
(and :meth:`create_module`). Loaders should implement it instead of
:meth:`load_module`. The import machinery takes care of all the
other responsibilities of :meth:`load_module` when
:meth:`exec_module` is implemented.


.. class:: ResourceLoader

Expand Down Expand Up @@ -490,13 +452,6 @@ ABC hierarchy::

.. versionadded:: 3.4

.. method:: load_module(fullname)

Implementation of :meth:`Loader.load_module`.

.. deprecated-removed:: 3.4 3.15
use :meth:`exec_module` instead.


.. class:: ExecutionLoader

Expand Down Expand Up @@ -530,6 +485,9 @@ ABC hierarchy::

.. versionadded:: 3.3

.. versionchanged:: 3.15
Removed the ``load_module()`` method.

.. attribute:: name

The name of the module the loader can handle.
Expand All @@ -538,13 +496,6 @@ ABC hierarchy::

Path to the file of the module.

.. method:: load_module(fullname)

Calls super's ``load_module()``.

.. deprecated-removed:: 3.4 3.15
Use :meth:`Loader.exec_module` instead.

.. method:: get_filename(fullname)
:abstractmethod:

Expand Down Expand Up @@ -576,6 +527,9 @@ ABC hierarchy::
optimization to speed up loading by removing the parsing step of Python's
compiler, and so no bytecode-specific API is exposed.

.. versionchanged:: 3.15
Removed the ``load_module()`` method.

.. method:: path_stats(path)

Optional abstract method which returns a :class:`dict` containing
Expand Down Expand Up @@ -629,13 +583,6 @@ ABC hierarchy::

.. versionadded:: 3.4

.. method:: load_module(fullname)

Concrete implementation of :meth:`Loader.load_module`.

.. deprecated-removed:: 3.4 3.15
Use :meth:`exec_module` instead.

.. method:: get_source(fullname)

Concrete implementation of :meth:`InspectLoader.get_source`.
Expand Down Expand Up @@ -1059,6 +1006,9 @@ find and load modules.

.. versionadded:: 3.3

.. versionchanged:: 3.15
Removed the ``load_module()`` method.

.. attribute:: name

The name of the module that this loader will handle.
Expand All @@ -1079,15 +1029,6 @@ find and load modules.

Concrete implementation of :meth:`importlib.abc.SourceLoader.set_data`.

.. method:: load_module(name=None)

Concrete implementation of :meth:`importlib.abc.Loader.load_module` where
specifying the name of the module to load is optional.

.. deprecated-removed:: 3.6 3.15

Use :meth:`importlib.abc.Loader.exec_module` instead.


.. class:: SourcelessFileLoader(fullname, path)

Expand All @@ -1101,6 +1042,9 @@ find and load modules.

.. versionadded:: 3.3

.. versionchanged:: 3.15
Removed the ``load_module()`` method.

.. attribute:: name

The name of the module the loader will handle.
Expand All @@ -1122,15 +1066,6 @@ find and load modules.
Returns ``None`` as bytecode files have no source when this loader is
used.

.. method:: load_module(name=None)

Concrete implementation of :meth:`importlib.abc.Loader.load_module` where
specifying the name of the module to load is optional.

.. deprecated-removed:: 3.6 3.15

Use :meth:`importlib.abc.Loader.exec_module` instead.


.. class:: ExtensionFileLoader(fullname, path)

Expand Down
30 changes: 14 additions & 16 deletions Doc/reference/import.rst
Original file line number Diff line number Diff line change
Expand Up @@ -359,21 +359,16 @@ of what happens during the loading portion of import::
if spec.loader is None:
# unsupported
raise ImportError
if spec.origin is None and spec.submodule_search_locations is not None:
# namespace package
sys.modules[spec.name] = module
elif not hasattr(spec.loader, 'exec_module'):
module = spec.loader.load_module(spec.name)
else:
sys.modules[spec.name] = module

sys.modules[spec.name] = module
try:
spec.loader.exec_module(module)
except BaseException:
try:
spec.loader.exec_module(module)
except BaseException:
try:
del sys.modules[spec.name]
except KeyError:
pass
raise
del sys.modules[spec.name]
except KeyError:
pass
raise
return sys.modules[spec.name]

Note the following details:
Expand Down Expand Up @@ -408,7 +403,10 @@ Note the following details:
.. versionchanged:: 3.4
The import system has taken over the boilerplate responsibilities of
loaders. These were previously performed by the
:meth:`importlib.abc.Loader.load_module` method.
``importlib.abc.Loader.load_module`` method.

.. versionchanged:: 3.15
The ``load_module`` method is no longer used.

Loaders
-------
Expand Down Expand Up @@ -443,7 +441,7 @@ import machinery will create the new module itself.
The :meth:`~importlib.abc.Loader.create_module` method of loaders.

.. versionchanged:: 3.4
The :meth:`~importlib.abc.Loader.load_module` method was replaced by
The ``importlib.abc.Loader.load_module`` method was replaced by
:meth:`~importlib.abc.Loader.exec_module` and the import
machinery assumed all the boilerplate responsibilities of loading.

Expand Down
4 changes: 2 additions & 2 deletions Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1622,7 +1622,7 @@ Deprecated
compatibility. Specifically,
:meth:`!find_loader`/:meth:`!find_module`
(superseded by :meth:`~importlib.abc.MetaPathFinder.find_spec`),
:meth:`~importlib.abc.Loader.load_module`
``importlib.abc.Loader.load_module``
(superseded by :meth:`~importlib.abc.Loader.exec_module`),
:meth:`!module_repr` (which the import system
takes care of for you), the ``__package__`` attribute
Expand Down Expand Up @@ -1652,7 +1652,7 @@ Deprecated
preference for :meth:`~zipimport.zipimporter.exec_module`.
(Contributed by Brett Cannon in :issue:`26131`.)

* The use of :meth:`~importlib.abc.Loader.load_module` by the import
* The use of ``importlib.abc.Loader.load_module`` by the import
system now triggers an :exc:`ImportWarning` as
:meth:`~importlib.abc.Loader.exec_module` is preferred.
(Contributed by Brett Cannon in :issue:`26131`.)
Expand Down
8 changes: 4 additions & 4 deletions Doc/whatsnew/3.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2086,10 +2086,10 @@ Deprecations in the Python API
:meth:`!importlib.abc.PathEntryFinder.find_loader` and
:meth:`!find_module` are replaced by
:meth:`importlib.abc.PathEntryFinder.find_spec`; all of the :samp:`{xxx}Loader` ABC
``load_module`` methods (:meth:`!importlib.abc.Loader.load_module`,
:meth:`!importlib.abc.InspectLoader.load_module`,
:meth:`!importlib.abc.FileLoader.load_module`,
:meth:`!importlib.abc.SourceLoader.load_module`) should no longer be
``load_module`` methods (``importlib.abc.Loader.load_module``,
``importlib.abc.InspectLoader.load_module``,
``importlib.abc.FileLoader.load_module``,
``importlib.abc.SourceLoader.load_module``) should no longer be
implemented, instead loaders should implement an
``exec_module`` method
(:meth:`importlib.abc.Loader.exec_module`,
Expand Down
6 changes: 3 additions & 3 deletions Doc/whatsnew/3.6.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2006,10 +2006,10 @@ deprecated.
importlib
~~~~~~~~~

The :meth:`importlib.machinery.SourceFileLoader.load_module` and
:meth:`importlib.machinery.SourcelessFileLoader.load_module` methods
The ``importlib.machinery.SourceFileLoader.load_module`` and
``importlib.machinery.SourcelessFileLoader.load_module`` methods
are now deprecated. They were the only remaining implementations of
:meth:`importlib.abc.Loader.load_module` in :mod:`importlib` that had not
``importlib.abc.Loader.load_module`` in :mod:`importlib` that had not
been deprecated in previous versions of Python in favour of
:meth:`importlib.abc.Loader.exec_module`.

Expand Down
18 changes: 0 additions & 18 deletions Lib/importlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,3 @@ def create_module(self, spec):

# We don't define exec_module() here since that would break
# hasattr checks we do to support backward compatibility.

def load_module(self, fullname):
"""Return the loaded module.

The module must be added to sys.modules and have import-related
attributes set properly. The fullname is a str.

ImportError is raised on failure.

This method is deprecated in favor of loader.exec_module(). If
exec_module() exists then it is used to provide a backwards-compatible
functionality for this method.

"""
if not hasattr(self, 'exec_module'):
raise ImportError
# Warning implemented in _load_module_shim().
return _bootstrap._load_module_shim(self, fullname)
Loading
Loading