Brett Cannon added the comment:

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.

----------
components: +Documentation -Interpreter Core
keywords: +easy
stage:  -> test needed
type: behavior -> 
versions: +Python 3.4

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

Reply via email to