summaryrefslogtreecommitdiffstats
path: root/Doc/library/importlib.rst
diff options
context:
space:
mode:
Diffstat (limited to 'Doc/library/importlib.rst')
-rw-r--r--Doc/library/importlib.rst693
1 files changed, 477 insertions, 216 deletions
diff --git a/Doc/library/importlib.rst b/Doc/library/importlib.rst
index 92339dc..a3373e0 100644
--- a/Doc/library/importlib.rst
+++ b/Doc/library/importlib.rst
@@ -89,11 +89,27 @@ Functions
.. versionchanged:: 3.3
Parent packages are automatically imported.
+.. function:: find_spec(name, path=None)
+
+ Find the :term:`spec <module spec>` for a module, optionally within the
+ specified *path*. If the module is in :attr:`sys.modules`, then
+ ``sys.modules[name].__spec__`` is returned (unless the spec would be
+ ``None`` or is not set, in which case :exc:`ValueError` is raised).
+ Otherwise a search using :attr:`sys.meta_path` is done. ``None`` is
+ returned if no spec is found.
+
+ A dotted name does not have its parent implicitly imported as that requires
+ loading them and that may not be desired. To properly import a submodule you
+ will need to import all parent packages of the submodule and use the correct
+ argument to *path*.
+
+ .. versionadded:: 3.4
+
.. function:: find_loader(name, path=None)
Find the loader for a module, optionally within the specified *path*. If the
module is in :attr:`sys.modules`, then ``sys.modules[name].__loader__`` is
- returned (unless the loader would be ``None``, in which case
+ returned (unless the loader would be ``None`` or is not set, in which case
:exc:`ValueError` is raised). Otherwise a search using :attr:`sys.meta_path`
is done. ``None`` is returned if no loader is found.
@@ -102,6 +118,15 @@ Functions
will need to import all parent packages of the submodule and use the correct
argument to *path*.
+ .. versionadded:: 3.3
+
+ .. versionchanged:: 3.4
+ If ``__loader__`` is not set, raise :exc:`ValueError`, just like when the
+ attribute is set to ``None``.
+
+ .. deprecated:: 3.4
+ Use :func:`find_spec` instead.
+
.. function:: invalidate_caches()
Invalidate the internal caches of finders stored at
@@ -112,6 +137,74 @@ Functions
.. versionadded:: 3.3
+.. function:: reload(module)
+
+ Reload a previously imported *module*. The argument must be a module object,
+ so it must have been successfully imported before. This is useful if you
+ have edited the module source file using an external editor and want to try
+ out the new version without leaving the Python interpreter. The return value
+ is the module object (which can be different if re-importing causes a
+ different object to be placed in :data:`sys.modules`).
+
+ When :func:`reload` is executed:
+
+ * Python modules' code is recompiled and the module-level code re-executed,
+ defining a new set of objects which are bound to names in the module's
+ dictionary by reusing the :term:`loader` which originally loaded the
+ module. The ``init`` function of extension modules is not called a second
+ time.
+
+ * As with all other objects in Python the old objects are only reclaimed
+ after their reference counts drop to zero.
+
+ * The names in the module namespace are updated to point to any new or
+ changed objects.
+
+ * Other references to the old objects (such as names external to the module) are
+ not rebound to refer to the new objects and must be updated in each namespace
+ where they occur if that is desired.
+
+ There are a number of other caveats:
+
+ If a module is syntactically correct but its initialization fails, the first
+ :keyword:`import` statement for it does not bind its name locally, but does
+ store a (partially initialized) module object in ``sys.modules``. To reload
+ the module you must first :keyword:`import` it again (this will bind the name
+ to the partially initialized module object) before you can :func:`reload` it.
+
+ When a module is reloaded, its dictionary (containing the module's global
+ variables) is retained. Redefinitions of names will override the old
+ definitions, so this is generally not a problem. If the new version of a
+ module does not define a name that was defined by the old version, the old
+ definition remains. This feature can be used to the module's advantage if it
+ maintains a global table or cache of objects --- with a :keyword:`try`
+ statement it can test for the table's presence and skip its initialization if
+ desired::
+
+ try:
+ cache
+ except NameError:
+ cache = {}
+
+ It is legal though generally not very useful to reload built-in or
+ dynamically loaded modules (this is not true for e.g. :mod:`sys`,
+ :mod:`__main__`, :mod:`builtins` and other key modules where reloading is
+ frowned upon). In many cases, however, extension modules are not designed to
+ be initialized more than once, and may fail in arbitrary ways when reloaded.
+
+ If a module imports objects from another module using :keyword:`from` ...
+ :keyword:`import` ..., calling :func:`reload` for the other module does not
+ redefine the objects imported from it --- one way around this is to
+ re-execute the :keyword:`from` statement, another is to use :keyword:`import`
+ and qualified names (*module.name*) instead.
+
+ If a module instantiates instances of a class, reloading the module that
+ defines the class does not affect the method definitions of the instances ---
+ they continue to use the old class definition. The same is true for derived
+ classes.
+
+ .. versionadded:: 3.4
+
:mod:`importlib.abc` -- Abstract base classes related to import
---------------------------------------------------------------
@@ -135,8 +228,6 @@ ABC hierarchy::
+-- ExecutionLoader --+
+-- FileLoader
+-- SourceLoader
- +-- PyLoader (deprecated)
- +-- PyPycLoader (deprecated)
.. class:: Finder
@@ -152,6 +243,10 @@ ABC hierarchy::
module. Originally specified in :pep:`302`, this method was meant
for use in :data:`sys.meta_path` and in the path-based import subsystem.
+ .. versionchanged:: 3.4
+ Returns ``None`` when called instead of raising
+ :exc:`NotImplementedError`.
+
.. class:: MetaPathFinder
@@ -160,20 +255,46 @@ ABC hierarchy::
.. versionadded:: 3.3
+ .. method:: find_spec(fullname, path, target=None)
+
+ An abstract method for finding a :term:`spec <module spec>` for
+ the specified module. If this is a top-level import, *path* will
+ be ``None``. Otherwise, this is a search for a subpackage or
+ module and *path* will be the value of :attr:`__path__` from the
+ parent package. If a spec cannot be found, ``None`` is returned.
+ When passed in, ``target`` is a module object that the finder may
+ use to make a more educated about what spec to return.
+
+ .. versionadded:: 3.4
+
.. method:: find_module(fullname, path)
- An abstract method for finding a :term:`loader` for the specified
+ A legacy method for finding a :term:`loader` for the specified
module. If this is a top-level import, *path* will be ``None``.
Otherwise, this is a search for a subpackage or module and *path*
will be the value of :attr:`__path__` from the parent
package. If a loader cannot be found, ``None`` is returned.
+ If :meth:`find_spec` is defined, backwards-compatible functionality is
+ provided.
+
+ .. versionchanged:: 3.4
+ Returns ``None`` when called instead of raising
+ :exc:`NotImplementedError`. Can use :meth:`find_spec` to provide
+ functionality.
+
+ .. deprecated:: 3.4
+ Use :meth:`find_spec` instead.
+
.. method:: invalidate_caches()
An optional method which, when called, should invalidate any internal
cache used by the finder. Used by :func:`importlib.invalidate_caches`
when invalidating the caches of all finders on :data:`sys.meta_path`.
+ .. versionchanged:: 3.4
+ Returns ``None`` when called instead of ``NotImplemented``.
+
.. class:: PathEntryFinder
@@ -181,27 +302,51 @@ ABC hierarchy::
it bears some similarities to :class:`MetaPathFinder`, ``PathEntryFinder``
is meant for use only within the path-based import subsystem provided
by :class:`PathFinder`. This ABC is a subclass of :class:`Finder` for
- compatibility.
+ compatibility reasons only.
.. versionadded:: 3.3
+ .. method:: find_spec(fullname, target=None)
+
+ An abstract method for finding a :term:`spec <module spec>` for
+ the specified module. The finder will search for the module only
+ within the :term:`path entry` to which it is assigned. If a spec
+ cannot be found, ``None`` is returned. When passed in, ``target``
+ is a module object that the finder may use to make a more educated
+ about what spec to return.
+
+ .. versionadded:: 3.4
+
.. method:: find_loader(fullname)
- An abstract method for finding a :term:`loader` for the specified
+ A legacy method for finding a :term:`loader` for the specified
module. Returns a 2-tuple of ``(loader, portion)`` where ``portion``
is a sequence of file system locations contributing to part of a namespace
package. The loader may be ``None`` while specifying ``portion`` to
signify the contribution of the file system locations to a namespace
package. An empty list can be used for ``portion`` to signify the loader
- is not part of a package. If ``loader`` is ``None`` and ``portion`` is
- the empty list then no loader or location for a namespace package were
- found (i.e. failure to find anything for the module).
+ is not part of a namespace package. If ``loader`` is ``None`` and
+ ``portion`` is the empty list then no loader or location for a namespace
+ package were found (i.e. failure to find anything for the module).
+
+ If :meth:`find_spec` is defined then backwards-compatible functionality is
+ provided.
+
+ .. versionchanged:: 3.4
+ Returns ``(None, [])`` instead of raising :exc:`NotImplementedError`.
+ Uses :meth:`find_spec` when available to provide functionality.
+
+ .. deprecated:: 3.4
+ Use :meth:`find_spec` instead.
.. method:: find_module(fullname)
A concrete implementation of :meth:`Finder.find_module` which is
equivalent to ``self.find_loader(fullname)[0]``.
+ .. deprecated:: 3.4
+ Use :meth:`find_spec` instead.
+
.. method:: invalidate_caches()
An optional method which, when called, should invalidate any internal
@@ -214,9 +359,26 @@ ABC hierarchy::
An abstract base class for a :term:`loader`.
See :pep:`302` for the exact definition for a loader.
+ .. method:: create_module(spec)
+
+ An optional method that returns the module object to use when
+ importing a module. create_module() may also return ``None``,
+ indicating that the default module creation should take place
+ instead.
+
+ .. versionadded:: 3.4
+
+ .. method:: exec_module(module)
+
+ An abstract method that executes the module in its own namespace
+ when a module is imported or reloaded. The module should already
+ be initialized when exec_module() is called.
+
+ .. versionadded:: 3.4
+
.. method:: load_module(fullname)
- An abstract method for loading a module. If the module cannot be
+ A legacy method for loading a module. If the module cannot be
loaded, :exc:`ImportError` is raised, otherwise the loaded module is
returned.
@@ -227,12 +389,11 @@ ABC hierarchy::
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 :func:`importlib.util.module_for_loader` decorator handles
- all of these details.
+ alone (see :func:`importlib.util.module_for_loader`).
The loader should set several attributes on the module.
(Note that some of these attributes can change when a module is
- reloaded.)
+ reloaded):
- :attr:`__name__`
The name of the module.
@@ -252,20 +413,42 @@ ABC hierarchy::
- :attr:`__package__`
The parent package for the module/package. If the module is
top-level then it has a value of the empty string. The
- :func:`importlib.util.set_package` decorator can handle the details
- for :attr:`__package__`.
+ :func:`importlib.util.module_for_loader` decorator can handle the
+ details for :attr:`__package__`.
- :attr:`__loader__`
- The loader used to load the module.
- (This is not set by the built-in import machinery,
- but it should be set whenever a :term:`loader` is used.)
+ The loader used to load the module. The
+ :func:`importlib.util.module_for_loader` decorator can handle the
+ details for :attr:`__package__`.
+
+ 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:: 3.4
+ The recommended API for loading a module is :meth:`exec_module`
+ (and optionally :meth:`create_module`). Loaders should implement
+ it instead of load_module(). The import machinery takes care of
+ all the other responsibilities of load_module() when exec_module()
+ is implemented.
.. method:: module_repr(module)
- An abstract method which when implemented calculates and returns the
- given module's repr, as a string.
+ A legacy method which when implemented calculates and returns the
+ given module's repr, as a string. The module type's default repr() will
+ use the result of this method as appropriate.
+
+ .. versionadded:: 3.3
+
+ .. versionchanged:: 3.4
+ Made optional instead of an abstractmethod.
- .. versionadded: 3.3
+ .. deprecated:: 3.4
+ The import machinery now takes care of this automatically.
.. class:: ResourceLoader
@@ -284,6 +467,9 @@ ABC hierarchy::
be found. The *path* is expected to be constructed using a module's
:attr:`__file__` attribute or an item from a package's :attr:`__path__`.
+ .. versionchanged:: 3.4
+ Raises :exc:`IOError` instead of :exc:`NotImplementedError`.
+
.. class:: InspectLoader
@@ -292,14 +478,21 @@ ABC hierarchy::
.. method:: get_code(fullname)
- An abstract method to return the :class:`code` object for a module.
- ``None`` is returned if the module does not have a code object
- (e.g. built-in module). :exc:`ImportError` is raised if loader cannot
- find the requested module.
+ Return the code object for a module, or ``None`` if the module does not
+ have a code object (as would be the case, for example, for a built-in
+ module). Raise an :exc:`ImportError` if loader cannot find the
+ requested module.
+
+ .. note::
+ While the method has a default implementation, it is suggested that
+ it be overridden if possible for performance.
.. index::
single: universal newlines; importlib.abc.InspectLoader.get_source method
+ .. versionchanged:: 3.4
+ No longer abstract and a concrete implementation is provided.
+
.. method:: get_source(fullname)
An abstract method to return the source of a module. It is returned as
@@ -308,12 +501,42 @@ ABC hierarchy::
if no source is available (e.g. a built-in module). Raises
:exc:`ImportError` if the loader cannot find the module specified.
+ .. versionchanged:: 3.4
+ Raises :exc:`ImportError` instead of :exc:`NotImplementedError`.
+
.. method:: is_package(fullname)
An abstract method to return a true value if the module is a package, a
false value otherwise. :exc:`ImportError` is raised if the
:term:`loader` cannot find the module.
+ .. versionchanged:: 3.4
+ Raises :exc:`ImportError` instead of :exc:`NotImplementedError`.
+
+ .. method:: source_to_code(data, path='<string>')
+
+ Create a code object from Python source.
+
+ The *data* argument can be whatever the :func:`compile` function
+ supports (i.e. string or bytes). The *path* argument should be
+ the "path" to where the source code originated from, which can be an
+ abstract concept (e.g. location in a zip file).
+
+ .. versionadded:: 3.4
+
+ .. method:: exec_module(module)
+
+ Implementation of :meth:`Loader.exec_module`.
+
+ .. versionadded:: 3.4
+
+ .. method:: load_module(fullname)
+
+ Implementation of :meth:`Loader.load_module`.
+
+ .. deprecated:: 3.4
+ use :meth:`exec_module` instead.
+
.. class:: ExecutionLoader
@@ -331,6 +554,9 @@ ABC hierarchy::
the source file, regardless of whether a bytecode was used to load the
module.
+ .. versionchanged:: 3.4
+ Raises :exc:`ImportError` instead of :exc:`NotImplementedError`.
+
.. class:: FileLoader(fullname, path)
@@ -355,13 +581,16 @@ ABC hierarchy::
Calls super's ``load_module()``.
+ .. deprecated:: 3.4
+ Use :meth:`Loader.exec_module` instead.
+
.. method:: get_filename(fullname)
Returns :attr:`path`.
.. method:: get_data(path)
- Returns the open, binary file for *path*.
+ Reads *path* as a binary file and returns the bytes from it.
.. class:: SourceLoader
@@ -376,7 +605,8 @@ ABC hierarchy::
loading is not supported.
The abstract methods defined by this class are to add optional bytecode
- file support. Not implementing these optional methods causes the loader to
+ file support. Not implementing these optional methods (or causing them to
+ raise :exc:`NotImplementedError`) causes the loader to
only work with source code. Implementing the methods allows the loader to
work with source *and* bytecode files; it does not allow for *sourceless*
loading where only bytecode is provided. Bytecode files are an
@@ -393,10 +623,13 @@ ABC hierarchy::
- ``'size'`` (optional): the size in bytes of the source code.
Any other keys in the dictionary are ignored, to allow for future
- extensions.
+ extensions. If the path cannot be handled, :exc:`IOError` is raised.
.. versionadded:: 3.3
+ .. versionchanged:: 3.4
+ Raise :exc:`IOError` instead of :exc:`NotImplementedError`.
+
.. method:: path_mtime(path)
Optional abstract method which returns the modification time for the
@@ -405,7 +638,10 @@ ABC hierarchy::
.. deprecated:: 3.3
This method is deprecated in favour of :meth:`path_stats`. You don't
have to implement it, but it is still available for compatibility
- purposes.
+ purposes. Raise :exc:`IOError` if the path cannot be handled.
+
+ .. versionchanged:: 3.4
+ Raise :exc:`IOError` instead of :exc:`NotImplementedError`.
.. method:: set_data(path, data)
@@ -417,13 +653,25 @@ ABC hierarchy::
(:attr:`errno.EACCES`/:exc:`PermissionError`), do not propagate the
exception.
+ .. versionchanged:: 3.4
+ No longer raises :exc:`NotImplementedError` when called.
+
.. method:: get_code(fullname)
Concrete implementation of :meth:`InspectLoader.get_code`.
+ .. method:: exec_module(module)
+
+ Concrete implementation of :meth:`Loader.exec_module`.
+
+ .. versionadded:: 3.4
+
.. method:: load_module(fullname)
- Concrete implementation of :meth:`Loader.load_module`.
+ Concrete implementation of :meth:`Loader.load_module`.
+
+ .. deprecated:: 3.4
+ Use :meth:`exec_module` instead.
.. method:: get_source(fullname)
@@ -438,142 +686,6 @@ ABC hierarchy::
itself does not end in ``__init__``.
-.. class:: PyLoader
-
- An abstract base class inheriting from
- :class:`ExecutionLoader` and
- :class:`ResourceLoader` designed to ease the loading of
- Python source modules (bytecode is not handled; see
- :class:`SourceLoader` for a source/bytecode ABC). A subclass
- implementing this ABC will only need to worry about exposing how the source
- code is stored; all other details for loading Python source code will be
- handled by the concrete implementations of key methods.
-
- .. deprecated:: 3.2
- This class has been deprecated in favor of :class:`SourceLoader` and is
- slated for removal in Python 3.4. See below for how to create a
- subclass that is compatible with Python 3.1 onwards.
-
- If compatibility with Python 3.1 is required, then use the following idiom
- to implement a subclass that will work with Python 3.1 onwards (make sure
- to implement :meth:`ExecutionLoader.get_filename`)::
-
- try:
- from importlib.abc import SourceLoader
- except ImportError:
- from importlib.abc import PyLoader as SourceLoader
-
-
- class CustomLoader(SourceLoader):
- def get_filename(self, fullname):
- """Return the path to the source file."""
- # Implement ...
-
- def source_path(self, fullname):
- """Implement source_path in terms of get_filename."""
- try:
- return self.get_filename(fullname)
- except ImportError:
- return None
-
- def is_package(self, fullname):
- """Implement is_package by looking for an __init__ file
- name as returned by get_filename."""
- filename = os.path.basename(self.get_filename(fullname))
- return os.path.splitext(filename)[0] == '__init__'
-
-
- .. method:: source_path(fullname)
-
- An abstract method that returns the path to the source code for a
- module. Should return ``None`` if there is no source code.
- Raises :exc:`ImportError` if the loader knows it cannot handle the
- module.
-
- .. method:: get_filename(fullname)
-
- A concrete implementation of
- :meth:`importlib.abc.ExecutionLoader.get_filename` that
- relies on :meth:`source_path`. If :meth:`source_path` returns
- ``None``, then :exc:`ImportError` is raised.
-
- .. method:: load_module(fullname)
-
- A concrete implementation of :meth:`importlib.abc.Loader.load_module`
- that loads Python source code. All needed information comes from the
- abstract methods required by this ABC. The only pertinent assumption
- made by this method is that when loading a package
- :attr:`__path__` is set to ``[os.path.dirname(__file__)]``.
-
- .. method:: get_code(fullname)
-
- A concrete implementation of
- :meth:`importlib.abc.InspectLoader.get_code` that creates code objects
- from Python source code, by requesting the source code (using
- :meth:`source_path` and :meth:`get_data`) and compiling it with the
- built-in :func:`compile` function.
-
- .. method:: get_source(fullname)
-
- A concrete implementation of
- :meth:`importlib.abc.InspectLoader.get_source`. Uses
- :meth:`importlib.abc.ResourceLoader.get_data` and :meth:`source_path`
- to get the source code. It tries to guess the source encoding using
- :func:`tokenize.detect_encoding`.
-
-
-.. class:: PyPycLoader
-
- An abstract base class inheriting from :class:`PyLoader`.
- This ABC is meant to help in creating loaders that support both Python
- source and bytecode.
-
- .. deprecated:: 3.2
- This class has been deprecated in favor of :class:`SourceLoader` and to
- properly support :pep:`3147`. If compatibility is required with
- Python 3.1, implement both :class:`SourceLoader` and :class:`PyLoader`;
- instructions on how to do so are included in the documentation for
- :class:`PyLoader`. Do note that this solution will not support
- sourceless/bytecode-only loading; only source *and* bytecode loading.
-
- .. versionchanged:: 3.3
- Updated to parse (but not use) the new source size field in bytecode
- files when reading and to write out the field properly when writing.
-
- .. method:: source_mtime(fullname)
-
- An abstract method which returns the modification time for the source
- code of the specified module. The modification time should be an
- integer. If there is no source code, return ``None``. If the
- module cannot be found then :exc:`ImportError` is raised.
-
- .. method:: bytecode_path(fullname)
-
- An abstract method which returns the path to the bytecode for the
- specified module, if it exists. It returns ``None``
- if no bytecode exists (yet).
- Raises :exc:`ImportError` if the loader knows it cannot handle the
- module.
-
- .. method:: get_filename(fullname)
-
- A concrete implementation of
- :meth:`ExecutionLoader.get_filename` that relies on
- :meth:`PyLoader.source_path` and :meth:`bytecode_path`.
- If :meth:`source_path` returns a path, then that value is returned.
- Else if :meth:`bytecode_path` returns a path, that path will be
- returned. If a path is not available from both methods,
- :exc:`ImportError` is raised.
-
- .. method:: write_bytecode(fullname, bytecode)
-
- An abstract method which has the loader write *bytecode* for future
- use. If the bytecode is written, return ``True``. Return
- ``False`` if the bytecode could not be written. This method
- should not be called if :data:`sys.dont_write_bytecode` is true.
- The *bytecode* argument should be a bytes string or bytes array.
-
-
:mod:`importlib.machinery` -- Importers and path hooks
------------------------------------------------------
@@ -640,6 +752,10 @@ find and load modules.
Only class methods are defined by this class to alleviate the need for
instantiation.
+ .. note::
+ Due to limitations in the extension module C-API, for now
+ BuiltinImporter does not implement :meth:`Loader.exec_module`.
+
.. class:: FrozenImporter
@@ -670,24 +786,37 @@ find and load modules.
Only class methods are defined by this class to alleviate the need for
instantiation.
+ .. classmethod:: find_spec(fullname, path=None, target=None)
+
+ Class method that attempts to find a :term:`spec <module spec>`
+ for the module specified by *fullname* on :data:`sys.path` or, if
+ defined, on *path*. For each path entry that is searched,
+ :data:`sys.path_importer_cache` is checked. If a non-false object
+ is found then it is used as the :term:`path entry finder` to look
+ for the module being searched for. If no entry is found in
+ :data:`sys.path_importer_cache`, then :data:`sys.path_hooks` is
+ searched for a finder for the path entry and, if found, is stored
+ in :data:`sys.path_importer_cache` along with being queried about
+ the module. If no finder is ever found then ``None`` is both
+ stored in the cache and returned.
+
+ .. versionadded:: 3.4
+
.. classmethod:: find_module(fullname, path=None)
- Class method that attempts to find a :term:`loader` for the module
- specified by *fullname* on :data:`sys.path` or, if defined, on
- *path*. For each path entry that is searched,
- :data:`sys.path_importer_cache` is checked. If a non-false object is
- found then it is used as the :term:`path entry finder` to look for the
- module being searched for. If no entry is found in
- :data:`sys.path_importer_cache`, then :data:`sys.path_hooks` is
- searched for a finder for the path entry and, if found, is stored in
- :data:`sys.path_importer_cache` along with being queried about the
- module. If no finder is ever found then ``None`` is both stored in
- the cache and returned.
+ A legacy wrapper around :meth:`find_spec`.
+
+ .. deprecated:: 3.4
+ Use :meth:`find_spec` instead.
.. classmethod:: invalidate_caches()
- Calls :meth:`importlib.abc.PathEntryFinder.invalidate_caches` on all
- finders stored in :attr:`sys.path_importer_cache`.
+ Calls :meth:`importlib.abc.PathEntryFinder.invalidate_caches` on all
+ finders stored in :attr:`sys.path_importer_cache`.
+
+ .. versionchanged:: 3.4
+ Calls objects in :data:`sys.path_hooks` with the current working
+ directory for ``''`` (i.e. the empty string).
.. class:: FileFinder(path, \*loader_details)
@@ -719,6 +848,12 @@ find and load modules.
The path the finder will search in.
+ .. method:: find_spec(fullname, target=None)
+
+ Attempt to find the spec to handle *fullname* within :attr:`path`.
+
+ .. versionadded:: 3.4
+
.. method:: find_loader(fullname)
Attempt to find the loader to handle *fullname* within :attr:`path`.
@@ -803,7 +938,7 @@ find and load modules.
.. class:: ExtensionFileLoader(fullname, path)
- A concrete implementation of :class:`importlib.abc.InspectLoader` for
+ A concrete implementation of :class:`importlib.abc.ExecutionLoader` for
extension modules.
The *fullname* argument specifies the name of the module the loader is to
@@ -824,6 +959,10 @@ find and load modules.
Loads the extension module if and only if *fullname* is the same as
:attr:`name` or is ``None``.
+ .. note::
+ Due to limitations in the extension module C-API, for now
+ ExtensionFileLoader does not implement :meth:`Loader.exec_module`.
+
.. method:: is_package(fullname)
Returns ``True`` if the file path points to a package's ``__init__``
@@ -837,6 +976,70 @@ find and load modules.
Returns ``None`` as extension modules do not have source code.
+ .. method:: get_filename(fullname)
+
+ Returns :attr:`path`.
+
+ .. versionadded:: 3.4
+
+
+.. class:: ModuleSpec(name, loader, *, origin=None, loader_state=None, is_package=None)
+
+ A specification for a module's import-system-related state.
+
+ .. versionadded:: 3.4
+
+ .. attribute:: name
+
+ (``__name__``)
+
+ A string for the fully-qualified name of the module.
+
+ .. attribute:: loader
+
+ (``__loader__``)
+
+ The loader to use for loading. For namespace packages this should be
+ set to None.
+
+ .. attribute:: origin
+
+ (``__file__``)
+
+ Name of the place from which the module is loaded, e.g. "builtin" for
+ built-in modules and the filename for modules loaded from source.
+ Normally "origin" should be set, but it may be None (the default)
+ which indicates it is unspecified.
+
+ .. attribute:: submodule_search_locations
+
+ (``__path__``)
+
+ List of strings for where to find submodules, if a package (None
+ otherwise).
+
+ .. attribute:: loader_state
+
+ Container of extra module-specific data for use during loading (or
+ None).
+
+ .. attribute:: cached
+
+ (``__cached__``)
+
+ String for where the compiled module should be stored (or None).
+
+ .. attribute:: parent
+
+ (``__package__``)
+
+ (Read-only) Fully-qualified name of the package to which the module
+ belongs as a submodule (or None).
+
+ .. attribute:: has_location
+
+ Boolean indicating whether or not the module's "origin"
+ attribute refers to a loadable location.
:mod:`importlib.util` -- Utility code for importers
---------------------------------------------------
@@ -847,6 +1050,51 @@ find and load modules.
This module contains the various objects that help in the construction of
an :term:`importer`.
+.. attribute:: MAGIC_NUMBER
+
+ The bytes which represent the bytecode version number. If you need help with
+ loading/writing bytecode then consider :class:`importlib.abc.SourceLoader`.
+
+ .. versionadded:: 3.4
+
+.. function:: cache_from_source(path, debug_override=None)
+
+ Return the :pep:`3147` path to the byte-compiled file associated with the
+ source *path*. For example, if *path* is ``/foo/bar/baz.py`` the return
+ value would be ``/foo/bar/__pycache__/baz.cpython-32.pyc`` for Python 3.2.
+ The ``cpython-32`` string comes from the current magic tag (see
+ :func:`get_tag`; if :attr:`sys.implementation.cache_tag` is not defined then
+ :exc:`NotImplementedError` will be raised). The returned path will end in
+ ``.pyc`` when ``__debug__`` is ``True`` or ``.pyo`` for an optimized Python
+ (i.e. ``__debug__`` is ``False``). By passing in ``True`` or ``False`` for
+ *debug_override* you can override the system's value for ``__debug__`` for
+ extension selection.
+
+ *path* need not exist.
+
+ .. versionadded:: 3.4
+
+
+.. function:: source_from_cache(path)
+
+ Given the *path* to a :pep:`3147` file name, return the associated source code
+ file path. For example, if *path* is
+ ``/foo/bar/__pycache__/baz.cpython-32.pyc`` the returned path would be
+ ``/foo/bar/baz.py``. *path* need not exist, however if it does not conform
+ to :pep:`3147` format, a ``ValueError`` is raised. If
+ :attr:`sys.implementation.cache_tag` is not defined,
+ :exc:`NotImplementedError` is raised.
+
+ .. versionadded:: 3.4
+
+.. function:: decode_source(source_bytes)
+
+ Decode the given bytes representing source code and return it as a string
+ with universal newlines (as required by
+ :meth:`importlib.abc.InspectLoader.get_source`).
+
+ .. versionadded:: 3.4
+
.. function:: resolve_name(name, package)
Resolve a relative module name to an absolute one.
@@ -865,7 +1113,7 @@ an :term:`importer`.
.. decorator:: module_for_loader
- A :term:`decorator` for a :term:`loader` method,
+ A :term:`decorator` for :meth:`importlib.abc.Loader.load_module`
to handle selecting the proper
module object to load with. The decorated method is expected to have a call
signature taking two positional arguments
@@ -876,55 +1124,68 @@ an :term:`importer`.
The decorated method will take in the **name** of the module to be loaded
as expected for a :term:`loader`. If the module is not found in
- :data:`sys.modules` then a new one is constructed with its
- :attr:`__name__` attribute set to **name**, :attr:`__loader__` set to
- **self**, and :attr:`__package__` set if
- :meth:`importlib.abc.InspectLoader.is_package` is defined for **self** and
- does not raise :exc:`ImportError` for **name**. If a new module is not
- needed then the module found in :data:`sys.modules` will be passed into the
- method.
+ :data:`sys.modules` then a new one is constructed. Regardless of where the
+ module came from, :attr:`__loader__` set to **self** and :attr:`__package__`
+ is set based on what :meth:`importlib.abc.InspectLoader.is_package` returns
+ (if available). These attributes are set unconditionally to support
+ reloading.
If an exception is raised by the decorated method and a module was added to
- :data:`sys.modules` it will be removed to prevent a partially initialized
- module from being in left in :data:`sys.modules`. If the module was already
- in :data:`sys.modules` then it is left alone.
-
- Use of this decorator handles all the details of which module object a
- loader should initialize as specified by :pep:`302` as best as possible.
+ :data:`sys.modules`, then the module will be removed to prevent a partially
+ initialized module from being in left in :data:`sys.modules`. If the module
+ was already in :data:`sys.modules` then it is left alone.
.. versionchanged:: 3.3
:attr:`__loader__` and :attr:`__package__` are automatically set
(when possible).
-.. decorator:: set_loader
+ .. versionchanged:: 3.4
+ Set :attr:`__name__`, :attr:`__loader__` :attr:`__package__`
+ unconditionally to support reloading.
- A :term:`decorator` for a :term:`loader` method,
- to set the :attr:`__loader__`
- attribute on loaded modules. If the attribute is already set the decorator
- does nothing. It is assumed that the first positional argument to the
- wrapped method (i.e. ``self``) is what :attr:`__loader__` should be set to.
+ .. deprecated:: 3.4
+ The import machinery now directly performs all the functionality
+ provided by this function.
- .. note::
+.. decorator:: set_loader
- It is recommended that :func:`module_for_loader` be used over this
- decorator as it subsumes this functionality.
+ A :term:`decorator` for :meth:`importlib.abc.Loader.load_module`
+ to set the :attr:`__loader__`
+ attribute on the returned module. If the attribute is already set the
+ decorator does nothing. It is assumed that the first positional argument to
+ the wrapped method (i.e. ``self``) is what :attr:`__loader__` should be set
+ to.
+ .. versionchanged:: 3.4
+ Set ``__loader__`` if set to ``None``, as if the attribute does not
+ exist.
+
+ .. deprecated:: 3.4
+ The import machinery takes care of this automatically.
.. decorator:: set_package
- A :term:`decorator` for a :term:`loader` to set the :attr:`__package__`
- attribute on the module returned by the loader. If :attr:`__package__` is
- set and has a value other than ``None`` it will not be changed.
- Note that the module returned by the loader is what has the attribute
- set on and not the module found in :data:`sys.modules`.
+ A :term:`decorator` for :meth:`importlib.abc.Loader.load_module` to set the :attr:`__package__` attribute on the returned module. If :attr:`__package__`
+ is set and has a value other than ``None`` it will not be changed.
+
+ .. deprecated:: 3.4
+ The import machinery takes care of this automatically.
+
+.. function:: spec_from_loader(name, loader, *, origin=None, is_package=None)
+
+ A factory function for creating a :class:`ModuleSpec` instance based
+ on a loader. The parameters have the same meaning as they do for
+ ModuleSpec. The function uses available :term:`loader` APIs, such as
+ :meth:`InspectLoader.is_package`, to fill in any missing
+ information on the spec.
+
+ .. versionadded:: 3.4
- Reliance on this decorator is discouraged when it is possible to set
- :attr:`__package__` before importing. By
- setting it beforehand the code for the module is executed with the
- attribute set and thus can be used by global level code during
- initialization.
+.. function:: spec_from_file_location(name, location, *, loader=None, submodule_search_locations=None)
- .. note::
+ A factory function for creating a :class:`ModuleSpec` instance based
+ on the path to a file. Missing information will be filled in on the
+ spec by making use of loader APIs and by the implication that the
+ module will be file-based.
- It is recommended that :func:`module_for_loader` be used over this
- decorator as it subsumes this functionality.
+ .. versionadded:: 3.4