Serhiy Storchaka <storchaka+cpyt...@gmail.com> added the comment:

_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.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue40173>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to