diff options
author | Barry Warsaw <barry@python.org> | 2015-04-22 18:38:26 -0400 |
---|---|---|
committer | Barry Warsaw <barry@python.org> | 2015-04-22 18:38:26 -0400 |
commit | 2d863acb562ba3c799ab4ed750c26dd41af8d67a (patch) | |
tree | 9fb1ca950e407ad5131c81be3a2122e7dfc06625 /Doc/reference/import.rst | |
parent | ecb69640e9579966b303202bc539f1405288be3e (diff) | |
parent | c110cbf4a2ae5890f8760ce7d1b70d0e40c0aa80 (diff) | |
download | cpython-2d863acb562ba3c799ab4ed750c26dd41af8d67a.tar.gz |
Issue #24029: Document the name binding behavior for submodule imports.
Diffstat (limited to 'Doc/reference/import.rst')
-rw-r--r-- | Doc/reference/import.rst | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/Doc/reference/import.rst b/Doc/reference/import.rst index 7966bc5133..50e46884a2 100644 --- a/Doc/reference/import.rst +++ b/Doc/reference/import.rst @@ -339,6 +339,7 @@ of what happens during the loading portion of import:: module = None if spec.loader is not None and hasattr(spec.loader, 'create_module'): + # It is assumed 'exec_module' will also be defined on the loader. module = spec.loader.create_module(spec) if module is None: module = ModuleType(spec.name) @@ -427,7 +428,7 @@ Module loaders may opt in to creating the module object during loading by implementing a :meth:`~importlib.abc.Loader.create_module` method. It takes one argument, the module spec, and returns the new module object to use during loading. ``create_module()`` does not need to set any attributes -on the module object. If the loader does not define ``create_module()``, the +on the module object. If the method returns ``None``, the import machinery will create the new module itself. .. versionadded:: 3.4 @@ -459,7 +460,13 @@ import machinery will create the new module itself. * If loading fails, the loader must remove any modules it has inserted into :data:`sys.modules`, but it must remove **only** the failing - module, and only if the loader itself has loaded it explicitly. + module(s), and only if the loader itself has loaded the module(s) + explicitly. + +.. versionchanged:: 3.5 + A :exc:`DeprecationWarning` is raised when ``exec_module()`` is defined but + ``create_module()`` is not. Starting in Python 3.6 it will be an error to not + define ``create_module()`` on a loader attached to a ModuleSpec. Submodules ---------- @@ -674,7 +681,7 @@ path entry finder that knows how to handle that particular kind of path. The default set of path entry finders implement all the semantics for finding modules on the file system, handling special file types such as Python source -code (``.py`` files), Python byte code (``.pyc`` and ``.pyo`` files) and +code (``.py`` files), Python byte code (``.pyc`` files) and shared libraries (e.g. ``.so`` files). When supported by the :mod:`zipimport` module in the standard library, the default path entry finders also handle loading all of these file types (other than shared libraries) from zipfiles. @@ -788,6 +795,15 @@ hook` callables on :data:`sys.path_hooks`, then the following protocol is used to ask the finder for a module spec, which is then used when loading the module. +The current working directory -- denoted by an empty string -- is handled +slightly differently from other entries on :data:`sys.path`. First, if the +current working directory is found to not exist, no value is stored in +:data:`sys.path_importer_cache`. Second, the value for the current working +directory is looked up fresh for each module lookup. Third, the path used for +:data:`sys.path_importer_cache` and returned by +:meth:`importlib.machinery.PathFinder.find_spec` will be the actual current +working directory and not the empty string. + Path entry finder protocol -------------------------- |