This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author serhiy.storchaka
Recipients brett.cannon, eric.snow, ncoghlan, p-ganssle, serhiy.storchaka, shihai1991
Date 2021-09-30.17:32:19
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1633023140.09.0.140034569859.issue40173@roundup.psfhosted.org>
In-reply-to
Content
_save_and_remove_module in the old code did too much and too little. It tested that the fresh module is importable, saved the current state of the fresh module and its submodules and removed the fresh module and its submodules. Since it tested importability before saving all states, it can have a side effect of importing other fresh module, and since it did it before blocking, the imported "fresh" module could import the blocked modules.

> 1. Switching the order of how "fresh" and "blocked" are resolved or

It does not help, because the step of resolving "fresh" includes saving the sys.modules state, and the step of resolving "blocked" modifies sys.modules. Also the step of resolving "fresh" can modify sys.modules before saving its sate.

2. Deleting `sys.modules[name]` if it exists immediately before calling `importlib.import_module(name)

It would be not enough, because the problem can occur in one of additional fresh modules imported by the initial module. We need to remove all fresh modules before attempting to import them.

The new import_fresh_module() consists of the following steps:

1. Save the state of all fresh and blocked modules and their submodules.
2. Remove the fresh modules and their submodules and block the blocked modules and their submodules.
3. Import all additional fresh modules and return None if any of them is not importable.
4. Import the initial module and raise ImportError if it is not importable.
5. Restore states of all fresh and blocked modules and their submodules.

It does not save and restore the state of all modules, because some indirectly imported modules can not support repeated importing. It can be reconsidered in new versions.
History
Date User Action Args
2021-09-30 17:32:20serhiy.storchakasetrecipients: + serhiy.storchaka, brett.cannon, ncoghlan, eric.snow, p-ganssle, shihai1991
2021-09-30 17:32:20serhiy.storchakasetmessageid: <1633023140.09.0.140034569859.issue40173@roundup.psfhosted.org>
2021-09-30 17:32:20serhiy.storchakalinkissue40173 messages
2021-09-30 17:32:19serhiy.storchakacreate