Issue #20383: Introduce importlib.util.module_from_spec().
Along the way, dismantle importlib._bootstrap._SpecMethods as it was
no longer relevant and constructing the new function required
partially dismantling the class anyway.
diff --git a/Doc/library/importlib.rst b/Doc/library/importlib.rst
index 0adeefb..0ca8490 100644
--- a/Doc/library/importlib.rst
+++ b/Doc/library/importlib.rst
@@ -1129,6 +1129,21 @@
.. versionadded:: 3.4
+.. function:: module_from_spec(spec)
+
+ Create a new module based on **spec**.
+
+ If the module object is from ``spec.loader.create_module()``, 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.
+
+ This function is preferred over using :class:`types.ModuleType` to create a
+ new module as **spec** is used to set as many import-controlled attributes on
+ the module as possible.
+
+ .. versionadded:: 3.5
+
.. decorator:: module_for_loader
A :term:`decorator` for :meth:`importlib.abc.Loader.load_module`
diff --git a/Doc/library/types.rst b/Doc/library/types.rst
index abdb939..34fffe6 100644
--- a/Doc/library/types.rst
+++ b/Doc/library/types.rst
@@ -115,6 +115,10 @@
The type of :term:`modules <module>`. Constructor takes the name of the
module to be created and optionally its :term:`docstring`.
+ .. note::
+ Use :func:`importlib.util.module_from_spec` to create a new module if you
+ wish to set the various import-controlled attributes.
+
.. attribute:: __doc__
The :term:`docstring` of the module. Defaults to ``None``.
diff --git a/Doc/whatsnew/3.5.rst b/Doc/whatsnew/3.5.rst
index 91cec97..77a6f7c 100644
--- a/Doc/whatsnew/3.5.rst
+++ b/Doc/whatsnew/3.5.rst
@@ -153,6 +153,10 @@
With a module object that you want to initialize you can then use
``exec(code, module.__dict__)`` to execute the code in the module.
+* :func:`importlib.util.module_from_spec` is now the preferred way to create a
+ new module. Compared to :class:`types.ModuleType`, this new function will set
+ the various import-controlled attributes based on the passed-in spec object.
+
inspect
-------