diff options
author | Brett Cannon <brett@python.org> | 2013-05-28 22:35:54 (GMT) |
---|---|---|
committer | Brett Cannon <brett@python.org> | 2013-05-28 22:35:54 (GMT) |
commit | 3dc48d6f6937f110388efd0f257fa12c323763a6 (patch) | |
tree | 58de10b0ae6755796ce557ea2f11941a3de02073 /Doc | |
parent | a22faca7144bdba5269e309965d926d155fc667b (diff) | |
download | cpython-3dc48d6f6937f110388efd0f257fa12c323763a6.zip cpython-3dc48d6f6937f110388efd0f257fa12c323763a6.tar.gz cpython-3dc48d6f6937f110388efd0f257fa12c323763a6.tar.bz2 |
Issue #18070: importlib.util.module_for_loader() now sets __loader__
and __package__ unconditionally in order to do the right thing for
reloading.
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/importlib.rst | 22 | ||||
-rw-r--r-- | Doc/whatsnew/3.4.rst | 7 |
2 files changed, 20 insertions, 9 deletions
diff --git a/Doc/library/importlib.rst b/Doc/library/importlib.rst index 799bc85..e0d1a29 100644 --- a/Doc/library/importlib.rst +++ b/Doc/library/importlib.rst @@ -811,12 +811,11 @@ 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 based on what - :meth:`importlib.abc.InspectLoader.is_package` returns (if available). 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 @@ -831,6 +830,10 @@ an :term:`importer`. :attr:`__loader__` and :attr:`__package__` are automatically set (when possible). + .. versionchanged:: 3.4 + Set :attr:`__loader__` :attr:`__package__` unconditionally to support + reloading. + .. decorator:: set_loader A :term:`decorator` for :meth:`importlib.abc.Loader.load_module` @@ -843,12 +846,13 @@ an :term:`importer`. .. note:: As this decorator sets :attr:`__loader__` after loading the module, it is recommended to use :func:`module_for_loader` instead when appropriate. + This decorator is also redundant as of Python 3.3 as import itself will + set these attributes post-import if necessary. .. versionchanged:: 3.4 Set ``__loader__`` if set to ``None``, as if the attribute does not exist. - .. decorator:: set_package A :term:`decorator` for :meth:`importlib.abc.Loader.load_module` to set the :attr:`__package__` attribute on the returned module. If :attr:`__package__` @@ -856,4 +860,6 @@ an :term:`importer`. .. note:: As this decorator sets :attr:`__package__` after loading the module, it is - recommended to use :func:`module_for_loader` instead when appropriate. + recommended to use :func:`module_for_loader` instead when appropriate. As + of Python 3.3 this decorator is also redundant as import will set + :attr:`__package__` post-import if necessary. diff --git a/Doc/whatsnew/3.4.rst b/Doc/whatsnew/3.4.rst index c6b1645..cca8b93 100644 --- a/Doc/whatsnew/3.4.rst +++ b/Doc/whatsnew/3.4.rst @@ -250,4 +250,9 @@ that may require changes to your code. * The module type now initializes the :attr:`__package__` and :attr:`__loader__` attributes to ``None`` by default. To determine if these attributes were set in a backwards-compatible fashion, use e.g. - ``getattr(module, '__loader__', None) is not None``.
\ No newline at end of file + ``getattr(module, '__loader__', None) is not None``. + +* :meth:`importlib.util.module_for_loader` now sets ``__loader__`` and + ``__package__`` unconditionally to properly support reloading. If this is not + desired then you will need to set these attributes manually. You can use + :class:`importlib.util.ModuleManager` for module management. |