Message188945
| Author |
brett.cannon |
| Recipients |
Valentin.Lorentz, brett.cannon, pitrou |
| Date |
2013年05月11日.18:53:41 |
| SpamBayes Score |
-1.0 |
| Marked as misclassified |
Yes |
| Message-id |
<1368298421.94.0.993447512503.issue17953@psf.upfronthosting.co.za> |
| In-reply-to |
| Content |
There is no good way to solve this. At the C level there interpreter struct has two key fields, sysdict and modules. The former is sys.__dict__ and the latter is sys.modules. But when you re-assign sys.modules you then break the assumption that sys.modules is the same dict as that contained in interp->modules. And this all goes out the window as the C code is expected to use interp->modules while the Python code in importlib only has access to sys.modules. The reason this used to "work" is your new dictionary was completely ignored and so we basically a no-op from the perspective of import (done in Python 2.7 but same result in any version up to Python 3.3)::
>>> import sys
>>> original_modules = sys.modules
>>> new_modules = sys.modules.copy()
>>> sys.modules = new_modules
>>> import pkg
>>> 'pkg' in original_modules
True
>>> 'pkg' in new_modules
False
What really needs to happen is that sys.modules needs to be documented as something that cannot be replaced. If you really want to update it cleanly then do ``sys.modules.clear(); sys.modules.update(new_modules)``, but even that is tricky because removing certain modules will flat-out break Python.
I have updated the issue to be a documentation one and added Python 3.4 to the affected versions. |
|
History
|
|---|
| Date |
User |
Action |
Args |
| 2013年05月11日 18:53:41 | brett.cannon | set | recipients:
+ brett.cannon, pitrou, Valentin.Lorentz |
| 2013年05月11日 18:53:41 | brett.cannon | set | messageid: <1368298421.94.0.993447512503.issue17953@psf.upfronthosting.co.za> |
| 2013年05月11日 18:53:41 | brett.cannon | link | issue17953 messages |
| 2013年05月11日 18:53:41 | brett.cannon | create |
|