Mark Shannon added the comment: I think Nathaniel and Eugene argument that you cannot replace the module in sys.modules safely in erroneous.
Immediately after the module is created by importlib it is inserted into sys.modules. The code object for the module is then executed. At that point, when the module code starts executing, no code outside the module has executed since the module was created and the import lock is held so no other thread will be able to import the module. Therefore the only code that can see the module object is the module's own code. Given a custom subclass of module that overrides __getattr__ to fetch values from the original module's __dict__, then an instance of that class can be inserted into sys.modules before any imports or calls to code that would access sys.modules, and the substitution of the module can be done safely. ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue24912> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com