Skip to content

Commit 880a790

Browse files
authored
GH-97850: Remove all uses and definitions of load_module() from importlib (#142205)
1 parent dc3ece2 commit 880a790

File tree

21 files changed

+52
-870
lines changed

21 files changed

+52
-870
lines changed

Doc/library/importlib.rst

Lines changed: 15 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,9 @@ ABC hierarchy::
320320
.. versionchanged:: 3.7
321321
Introduced the optional :meth:`get_resource_reader` method.
322322

323+
.. versionchanged:: 3.15
324+
Removed the ``load_module()`` method.
325+
323326
.. method:: create_module(spec)
324327

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

347-
.. method:: load_module(fullname)
348-
349-
A legacy method for loading a module. If the module cannot be
350-
loaded, :exc:`ImportError` is raised, otherwise the loaded module is
351-
returned.
352-
353-
If the requested module already exists in :data:`sys.modules`, that
354-
module should be used and reloaded.
355-
Otherwise the loader should create a new module and insert it into
356-
:data:`sys.modules` before any loading begins, to prevent recursion
357-
from the import. If the loader inserted a module and the load fails, it
358-
must be removed by the loader from :data:`sys.modules`; modules already
359-
in :data:`sys.modules` before the loader began execution should be left
360-
alone.
361-
362-
The loader should set several attributes on the module
363-
(note that some of these attributes can change when a module is
364-
reloaded):
365-
366-
- :attr:`module.__name__`
367-
- :attr:`module.__file__`
368-
- :attr:`module.__cached__` *(deprecated)*
369-
- :attr:`module.__path__`
370-
- :attr:`module.__package__` *(deprecated)*
371-
- :attr:`module.__loader__` *(deprecated)*
372-
373-
When :meth:`exec_module` is available then backwards-compatible
374-
functionality is provided.
375-
376-
.. versionchanged:: 3.4
377-
Raise :exc:`ImportError` when called instead of
378-
:exc:`NotImplementedError`. Functionality provided when
379-
:meth:`exec_module` is available.
380-
381-
.. deprecated-removed:: 3.4 3.15
382-
The recommended API for loading a module is :meth:`exec_module`
383-
(and :meth:`create_module`). Loaders should implement it instead of
384-
:meth:`load_module`. The import machinery takes care of all the
385-
other responsibilities of :meth:`load_module` when
386-
:meth:`exec_module` is implemented.
387-
388350

389351
.. class:: ResourceLoader
390352

@@ -490,13 +452,6 @@ ABC hierarchy::
490452

491453
.. versionadded:: 3.4
492454

493-
.. method:: load_module(fullname)
494-
495-
Implementation of :meth:`Loader.load_module`.
496-
497-
.. deprecated-removed:: 3.4 3.15
498-
use :meth:`exec_module` instead.
499-
500455

501456
.. class:: ExecutionLoader
502457

@@ -530,6 +485,9 @@ ABC hierarchy::
530485

531486
.. versionadded:: 3.3
532487

488+
.. versionchanged:: 3.15
489+
Removed the ``load_module()`` method.
490+
533491
.. attribute:: name
534492

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

539497
Path to the file of the module.
540498

541-
.. method:: load_module(fullname)
542-
543-
Calls super's ``load_module()``.
544-
545-
.. deprecated-removed:: 3.4 3.15
546-
Use :meth:`Loader.exec_module` instead.
547-
548499
.. method:: get_filename(fullname)
549500
:abstractmethod:
550501

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

530+
.. versionchanged:: 3.15
531+
Removed the ``load_module()`` method.
532+
579533
.. method:: path_stats(path)
580534

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

630584
.. versionadded:: 3.4
631585

632-
.. method:: load_module(fullname)
633-
634-
Concrete implementation of :meth:`Loader.load_module`.
635-
636-
.. deprecated-removed:: 3.4 3.15
637-
Use :meth:`exec_module` instead.
638-
639586
.. method:: get_source(fullname)
640587

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

10601007
.. versionadded:: 3.3
10611008

1009+
.. versionchanged:: 3.15
1010+
Removed the ``load_module()`` method.
1011+
10621012
.. attribute:: name
10631013

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

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

1082-
.. method:: load_module(name=None)
1083-
1084-
Concrete implementation of :meth:`importlib.abc.Loader.load_module` where
1085-
specifying the name of the module to load is optional.
1086-
1087-
.. deprecated-removed:: 3.6 3.15
1088-
1089-
Use :meth:`importlib.abc.Loader.exec_module` instead.
1090-
10911032

10921033
.. class:: SourcelessFileLoader(fullname, path)
10931034

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

11021043
.. versionadded:: 3.3
11031044

1045+
.. versionchanged:: 3.15
1046+
Removed the ``load_module()`` method.
1047+
11041048
.. attribute:: name
11051049

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

1125-
.. method:: load_module(name=None)
1126-
1127-
Concrete implementation of :meth:`importlib.abc.Loader.load_module` where
1128-
specifying the name of the module to load is optional.
1129-
1130-
.. deprecated-removed:: 3.6 3.15
1131-
1132-
Use :meth:`importlib.abc.Loader.exec_module` instead.
1133-
11341069

11351070
.. class:: ExtensionFileLoader(fullname, path)
11361071

Doc/reference/import.rst

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -359,21 +359,16 @@ of what happens during the loading portion of import::
359359
if spec.loader is None:
360360
# unsupported
361361
raise ImportError
362-
if spec.origin is None and spec.submodule_search_locations is not None:
363-
# namespace package
364-
sys.modules[spec.name] = module
365-
elif not hasattr(spec.loader, 'exec_module'):
366-
module = spec.loader.load_module(spec.name)
367-
else:
368-
sys.modules[spec.name] = module
362+
363+
sys.modules[spec.name] = module
364+
try:
365+
spec.loader.exec_module(module)
366+
except BaseException:
369367
try:
370-
spec.loader.exec_module(module)
371-
except BaseException:
372-
try:
373-
del sys.modules[spec.name]
374-
except KeyError:
375-
pass
376-
raise
368+
del sys.modules[spec.name]
369+
except KeyError:
370+
pass
371+
raise
377372
return sys.modules[spec.name]
378373

379374
Note the following details:
@@ -408,7 +403,10 @@ Note the following details:
408403
.. versionchanged:: 3.4
409404
The import system has taken over the boilerplate responsibilities of
410405
loaders. These were previously performed by the
411-
:meth:`importlib.abc.Loader.load_module` method.
406+
``importlib.abc.Loader.load_module`` method.
407+
408+
.. versionchanged:: 3.15
409+
The ``load_module`` method is no longer used.
412410

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

445443
.. versionchanged:: 3.4
446-
The :meth:`~importlib.abc.Loader.load_module` method was replaced by
444+
The ``importlib.abc.Loader.load_module`` method was replaced by
447445
:meth:`~importlib.abc.Loader.exec_module` and the import
448446
machinery assumed all the boilerplate responsibilities of loading.
449447

Doc/whatsnew/3.10.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,7 +1622,7 @@ Deprecated
16221622
compatibility. Specifically,
16231623
:meth:`!find_loader`/:meth:`!find_module`
16241624
(superseded by :meth:`~importlib.abc.MetaPathFinder.find_spec`),
1625-
:meth:`~importlib.abc.Loader.load_module`
1625+
``importlib.abc.Loader.load_module``
16261626
(superseded by :meth:`~importlib.abc.Loader.exec_module`),
16271627
:meth:`!module_repr` (which the import system
16281628
takes care of for you), the ``__package__`` attribute
@@ -1652,7 +1652,7 @@ Deprecated
16521652
preference for :meth:`~zipimport.zipimporter.exec_module`.
16531653
(Contributed by Brett Cannon in :issue:`26131`.)
16541654
1655-
* The use of :meth:`~importlib.abc.Loader.load_module` by the import
1655+
* The use of ``importlib.abc.Loader.load_module`` by the import
16561656
system now triggers an :exc:`ImportWarning` as
16571657
:meth:`~importlib.abc.Loader.exec_module` is preferred.
16581658
(Contributed by Brett Cannon in :issue:`26131`.)

Doc/whatsnew/3.4.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,10 +2086,10 @@ Deprecations in the Python API
20862086
:meth:`!importlib.abc.PathEntryFinder.find_loader` and
20872087
:meth:`!find_module` are replaced by
20882088
:meth:`importlib.abc.PathEntryFinder.find_spec`; all of the :samp:`{xxx}Loader` ABC
2089-
``load_module`` methods (:meth:`!importlib.abc.Loader.load_module`,
2090-
:meth:`!importlib.abc.InspectLoader.load_module`,
2091-
:meth:`!importlib.abc.FileLoader.load_module`,
2092-
:meth:`!importlib.abc.SourceLoader.load_module`) should no longer be
2089+
``load_module`` methods (``importlib.abc.Loader.load_module``,
2090+
``importlib.abc.InspectLoader.load_module``,
2091+
``importlib.abc.FileLoader.load_module``,
2092+
``importlib.abc.SourceLoader.load_module``) should no longer be
20932093
implemented, instead loaders should implement an
20942094
``exec_module`` method
20952095
(:meth:`importlib.abc.Loader.exec_module`,

Doc/whatsnew/3.6.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,10 +2006,10 @@ deprecated.
20062006
importlib
20072007
~~~~~~~~~
20082008

2009-
The :meth:`importlib.machinery.SourceFileLoader.load_module` and
2010-
:meth:`importlib.machinery.SourcelessFileLoader.load_module` methods
2009+
The ``importlib.machinery.SourceFileLoader.load_module`` and
2010+
``importlib.machinery.SourcelessFileLoader.load_module`` methods
20112011
are now deprecated. They were the only remaining implementations of
2012-
:meth:`importlib.abc.Loader.load_module` in :mod:`importlib` that had not
2012+
``importlib.abc.Loader.load_module`` in :mod:`importlib` that had not
20132013
been deprecated in previous versions of Python in favour of
20142014
:meth:`importlib.abc.Loader.exec_module`.
20152015

Lib/importlib/_abc.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,3 @@ def create_module(self, spec):
1919

2020
# We don't define exec_module() here since that would break
2121
# hasattr checks we do to support backward compatibility.
22-
23-
def load_module(self, fullname):
24-
"""Return the loaded module.
25-
26-
The module must be added to sys.modules and have import-related
27-
attributes set properly. The fullname is a str.
28-
29-
ImportError is raised on failure.
30-
31-
This method is deprecated in favor of loader.exec_module(). If
32-
exec_module() exists then it is used to provide a backwards-compatible
33-
functionality for this method.
34-
35-
"""
36-
if not hasattr(self, 'exec_module'):
37-
raise ImportError
38-
# Warning implemented in _load_module_shim().
39-
return _bootstrap._load_module_shim(self, fullname)

0 commit comments

Comments
 (0)