Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b523f84

Browse files
Implement PEP 451 (ModuleSpec).
1 parent 9e6097e commit b523f84

37 files changed

Lines changed: 6983 additions & 4398 deletions

Doc/library/importlib.rst

Lines changed: 80 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,11 @@ ABC hierarchy::
310310
from the import. If the loader inserted a module and the load fails, it
311311
must be removed by the loader from :data:`sys.modules`; modules already
312312
in :data:`sys.modules` before the loader began execution should be left
313-
alone (see :func:`importlib.util.module_to_load`).
313+
alone (see :func:`importlib.util.module_for_loader`).
314314

315315
The loader should set several attributes on the module.
316316
(Note that some of these attributes can change when a module is
317-
reloaded; see :meth:`init_module_attrs`):
317+
reloaded):
318318

319319
- :attr:`__name__`
320320
The name of the module.
@@ -357,17 +357,6 @@ ABC hierarchy::
357357
.. versionchanged:: 3.4
358358
Made optional instead of an abstractmethod.
359359

360-
.. method:: init_module_attrs(module)
361-
362-
Set the :attr:`__loader__` attribute on the module.
363-
364-
Subclasses overriding this method should set whatever appropriate
365-
attributes it can, getting the module's name from :attr:`__name__` when
366-
needed. All values should also be overridden so that reloading works as
367-
expected.
368-
369-
.. versionadded:: 3.4
370-
371360

372361
.. class:: ResourceLoader
373362

@@ -442,14 +431,6 @@ ABC hierarchy::
442431

443432
.. versionadded:: 3.4
444433

445-
.. method:: init_module_attrs(module)
446-
447-
Set the :attr:`__package__` attribute and :attr:`__path__` attribute to
448-
the empty list if appropriate along with what
449-
:meth:`importlib.abc.Loader.init_module_attrs` sets.
450-
451-
.. versionadded:: 3.4
452-
453434
.. method:: load_module(fullname)
454435

455436
Implementation of :meth:`Loader.load_module`.
@@ -474,15 +455,6 @@ ABC hierarchy::
474455
.. versionchanged:: 3.4
475456
Raises :exc:`ImportError` instead of :exc:`NotImplementedError`.
476457

477-
.. method:: init_module_attrs(module)
478-
479-
Set :attr:`__file__` and if initializing a package then set
480-
:attr:`__path__` to ``[os.path.dirname(__file__)]`` along with
481-
all attributes set by
482-
:meth:`importlib.abc.InspectLoader.init_module_attrs`.
483-
484-
.. versionadded:: 3.4
485-
486458

487459
.. class:: FileLoader(fullname, path)
488460

@@ -599,14 +571,6 @@ ABC hierarchy::
599571
``__init__`` when the file extension is removed **and** the module name
600572
itself does not end in ``__init__``.
601573

602-
.. method:: init_module_attr(module)
603-
604-
Set :attr:`__cached__` using :func:`imp.cache_from_source`. Other
605-
attributes set by
606-
:meth:`importlib.abc.ExecutionLoader.init_module_attrs`.
607-
608-
.. versionadded:: 3.4
609-
610574

611575
:mod:`importlib.machinery` -- Importers and path hooks
612576
------------------------------------------------------
@@ -882,6 +846,64 @@ find and load modules.
882846
.. versionadded:: 3.4
883847

884848

849+
.. class:: ModuleSpec(name, loader, *, origin=None, loader_state=None, is_package=None)
850+
851+
A specification for a module's import-system-related state.
852+
853+
.. versionadded:: 3.4
854+
855+
.. attribute:: name
856+
857+
(``__name__``)
858+
859+
A string for the fully-qualified name of the module.
860+
861+
.. attribute:: loader
862+
863+
(``__loader__``)
864+
865+
The loader to use for loading. For namespace packages this should be
866+
set to None.
867+
868+
.. attribute:: origin
869+
870+
(``__file__``)
871+
872+
Name of the place from which the module is loaded, e.g. "builtin" for
873+
built-in modules and the filename for modules loaded from source.
874+
Normally "origin" should be set, but it may be None (the default)
875+
which indicates it is unspecified.
876+
877+
.. attribute:: submodule_search_locations
878+
879+
(``__path__``)
880+
881+
List of strings for where to find submodules, if a package (None
882+
otherwise).
883+
884+
.. attribute:: loader_state
885+
886+
Container of extra module-specific data for use during loading (or
887+
None).
888+
889+
.. attribute:: cached
890+
891+
(``__cached__``)
892+
893+
String for where the compiled module should be stored (or None).
894+
895+
.. attribute:: parent
896+
897+
(``__package__``)
898+
899+
(Read-only) Fully-qualified name of the package to which the module
900+
belongs as a submodule (or None).
901+
902+
.. attribute:: has_location
903+
904+
(Read-only) Boolean indicating whether or not the module's "origin"
905+
attribute refers to a loadable location.
906+
885907
:mod:`importlib.util` -- Utility code for importers
886908
---------------------------------------------------
887909

@@ -952,20 +974,6 @@ an :term:`importer`.
952974

953975
.. versionadded:: 3.3
954976

955-
.. function:: module_to_load(name, *, reset_name=True)
956-
957-
Returns a :term:`context manager` which provides the module to load. The
958-
module will either come from :attr:`sys.modules` in the case of reloading or
959-
a fresh module if loading a new module. Proper cleanup of
960-
:attr:`sys.modules` occurs if the module was new and an exception was
961-
raised.
962-
963-
If **reset_name** is true and the module requested is being reloaded then
964-
the module's :attr:`__name__` attribute will
965-
be reset to **name**, else it will be left untouched.
966-
967-
.. versionadded:: 3.4
968-
969977
.. decorator:: module_for_loader
970978

971979
A :term:`decorator` for :meth:`importlib.abc.Loader.load_module`
@@ -999,9 +1007,8 @@ an :term:`importer`.
9991007
unconditionally to support reloading.
10001008

10011009
.. deprecated:: 3.4
1002-
For the benefit of :term:`loader` subclasses, please use
1003-
:func:`module_to_load` and
1004-
:meth:`importlib.abc.Loader.init_module_attrs` instead.
1010+
The import machinery now directly performs all the functionality
1011+
provided by this function.
10051012

10061013
.. decorator:: set_loader
10071014

@@ -1012,11 +1019,6 @@ an :term:`importer`.
10121019
the wrapped method (i.e. ``self``) is what :attr:`__loader__` should be set
10131020
to.
10141021

1015-
.. note::
1016-
As this decorator sets :attr:`__loader__` after loading the module, it is
1017-
recommended to use :meth:`importlib.abc.Loader.init_module_attrs` instead
1018-
when appropriate.
1019-
10201022
.. versionchanged:: 3.4
10211023
Set ``__loader__`` if set to ``None``, as if the attribute does not
10221024
exist.
@@ -1026,7 +1028,21 @@ an :term:`importer`.
10261028
A :term:`decorator` for :meth:`importlib.abc.Loader.load_module` to set the :attr:`__package__` attribute on the returned module. If :attr:`__package__`
10271029
is set and has a value other than ``None`` it will not be changed.
10281030

1029-
.. note::
1030-
As this decorator sets :attr:`__package__` after loading the module, it is
1031-
recommended to use :meth:`importlib.abc.Loader.init_module_attrs` instead
1032-
when appropriate.
1031+
.. function:: spec_from_loader(name, loader, *, origin=None, is_package=None)
1032+
1033+
A factory function for creating a :class:`ModuleSpec` instance based
1034+
on a loader. The parameters have the same meaning as they do for
1035+
ModuleSpec. The function uses available :term:`loader` APIs, such as
1036+
:meth:`InspectLoader.is_package`, to fill in any missing
1037+
information on the spec.
1038+
1039+
.. versionadded:: 3.4
1040+
1041+
.. function:: spec_from_file_location(name, location, *, loader=None, submodule_search_locations=None)
1042+
1043+
A factory function for creating a :class:`ModuleSpec` instance based
1044+
on the path to a file. Missing information will be filled in on the
1045+
spec by making use of loader APIs and by the implication that the
1046+
module will be file-based.
1047+
1048+
.. versionadded:: 3.4

0 commit comments

Comments
 (0)