summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2015-01-09 16:39:21 (GMT)
committerBrett Cannon <brett@python.org>2015-01-09 16:39:21 (GMT)
commit02d845400275076ef5ba2791c74b7670ac21f8a9 (patch)
tree36077086a1bd393ede07d99c61ed7137bd5bdcc3 /Doc
parent863c69cfebcafbf97ba401e0f3cd5cb077e0e8f2 (diff)
downloadcpython-02d845400275076ef5ba2791c74b7670ac21f8a9.zip
cpython-02d845400275076ef5ba2791c74b7670ac21f8a9.tar.gz
cpython-02d845400275076ef5ba2791c74b7670ac21f8a9.tar.bz2
Issue #23014: Make importlib.abc.Loader.create_module() required when
importlib.abc.Loader.exec_module() is also defined. Before this change, create_module() was optional **and** could return None to trigger default semantics. This change now reduces the options for choosing default semantics to one and in the most backporting-friendly way (define create_module() to return None).
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/importlib.rst24
-rw-r--r--Doc/reference/import.rst8
-rw-r--r--Doc/whatsnew/3.5.rst8
3 files changed, 29 insertions, 11 deletions
diff --git a/Doc/library/importlib.rst b/Doc/library/importlib.rst
index 3af8d62..9c6b280 100644
--- a/Doc/library/importlib.rst
+++ b/Doc/library/importlib.rst
@@ -347,13 +347,16 @@ ABC hierarchy::
.. 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.
+ A method that returns the module object to use when
+ importing a module. This method may return ``None``,
+ indicating that default module creation semantics should take place.
.. versionadded:: 3.4
+ .. versionchanged:: 3.5
+ Starting in Python 3.6, this method will not be optional when
+ :meth:`exec_module` is defined.
+
.. method:: exec_module(module)
An abstract method that executes the module in its own namespace
@@ -417,7 +420,7 @@ ABC hierarchy::
.. deprecated:: 3.4
The recommended API for loading a module is :meth:`exec_module`
- (and optionally :meth:`create_module`). Loaders should implement
+ (and :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.
@@ -1136,9 +1139,9 @@ an :term:`importer`.
.. function:: module_from_spec(spec)
- Create a new module based on **spec**.
+ Create a new module based on **spec** and ``spec.loader.create_module()``.
- If the module object is from ``spec.loader.create_module()``, then any
+ If ``spec.loader.create_module()`` does not return ``None``, then any
pre-existing attributes will not be reset. Also, no :exc:`AttributeError`
will be raised if triggered while accessing **spec** or setting an attribute
on the module.
@@ -1234,9 +1237,10 @@ an :term:`importer`.
module has an attribute accessed.
This class **only** works with loaders that define
- :meth:`importlib.abc.Loader.exec_module` as control over what module type
- is used for the module is required. For the same reasons, the loader
- **cannot** define :meth:`importlib.abc.Loader.create_module`. Finally,
+ :meth:`~importlib.abc.Loader.exec_module` as control over what module type
+ is used for the module is required. For those same reasons, the loader's
+ :meth:`~importlib.abc.Loader.create_module` method will be ignored (i.e., the
+ loader's method should only return ``None``). Finally,
modules which substitute the object placed into :attr:`sys.modules` will
not work as there is no way to properly replace the module references
throughout the interpreter safely; :exc:`ValueError` is raised if such a
diff --git a/Doc/reference/import.rst b/Doc/reference/import.rst
index 99b77be..2d60e62 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
@@ -462,6 +463,11 @@ import machinery will create the new module itself.
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.
+
Module spec
-----------
diff --git a/Doc/whatsnew/3.5.rst b/Doc/whatsnew/3.5.rst
index def4add..f3cc514 100644
--- a/Doc/whatsnew/3.5.rst
+++ b/Doc/whatsnew/3.5.rst
@@ -456,6 +456,14 @@ Changes in the Python API
`http.client` and `http.server` remain available for backwards compatibility.
(Contributed by Demian Brecht in :issue:`21793`.)
+* When an import loader defines :meth:`~importlib.machinery.Loader.exec_module`
+ it is now expected to also define
+ :meth:`~importlib.machinery.Loader.create_module` (raises a
+ :exc:`DeprecationWarning` now, will be an error in Python 3.6). If the loader
+ inherits from :class:`importlib.abc.Loader` then there is nothing to do, else
+ simply define :meth:`~importlib.machinery.Loader.create_module` to return
+ ``None`` (:issue:`23014`).
+
Changes in the C API
--------------------