Introduce importlib.util.ModuleManager which is a context manager to
handle providing (and cleaning up if needed) the module to be loaded.
A future commit will use the context manager in
Lib/importlib/_bootstrap.py and thus why the code is placed there
instead of in Lib/importlib/util.py.
diff --git a/Doc/library/importlib.rst b/Doc/library/importlib.rst
index 45fc8ba..753030a 100644
--- a/Doc/library/importlib.rst
+++ b/Doc/library/importlib.rst
@@ -789,6 +789,15 @@
.. versionadded:: 3.3
+.. class:: ModuleManager(name)
+
+ A :term:`context manager` which provides the module to load. The module will
+ either come from :attr:`sys.modules` in the case of reloading or a fresh
+ module if loading a new module. Proper cleanup of :attr:`sys.modules` occurs
+ if the module was new and an exception was raised.
+
+ .. versionadded:: 3.4
+
.. decorator:: module_for_loader
A :term:`decorator` for a :term:`loader` method,
@@ -818,6 +827,10 @@
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.
+ .. note::
+ :class:`ModuleManager` subsumes the module management aspect of this
+ decorator.
+
.. versionchanged:: 3.3
:attr:`__loader__` and :attr:`__package__` are automatically set
(when possible).