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 Mark.Shannon
Recipients Guido.van.Rossum, Mark.Shannon, benjamin.peterson, eltoder, gvanrossum, larry, lemburg, njs, pitrou, rhettinger, serhiy.storchaka, vstinner
Date 2015-09-01.19:25:49
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1441135549.57.0.347439414566.issue24912@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2015-09-01 19:25:49Mark.Shannonsetrecipients: + Mark.Shannon, lemburg, gvanrossum, rhettinger, pitrou, vstinner, larry, benjamin.peterson, njs, eltoder, serhiy.storchaka, Guido.van.Rossum
2015-09-01 19:25:49Mark.Shannonsetmessageid: <1441135549.57.0.347439414566.issue24912@psf.upfronthosting.co.za>
2015-09-01 19:25:49Mark.Shannonlinkissue24912 messages
2015-09-01 19:25:49Mark.Shannoncreate