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 eric.snow
Recipients brett.cannon, eric.snow, ezio.melotti, ncoghlan, pitrou, python-dev, serhiy.storchaka, terry.reedy
Date 2013-08-17.00:51:59
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1376700721.07.0.52481823238.issue18706@psf.upfronthosting.co.za>
In-reply-to
Content
Regarding this:

>>> def f():
...     import html.entities
...     del sys.modules['html']
...
>>> f()
>>> import html.entities
>>> html.entities
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'entities'

This is due to the fact that the code to add the submodule to the package's namespace is associated with the import of the submodule, not the parent (aka, package).  So the import after calling f() happily bypasses all that since the submodule already exists in sys.modules.

skip loading the module: 

http://hg.python.org/cpython/file/default/Lib/importlib/_bootstrap.py#l1612

bind the submodule to the parent:

http://hg.python.org/cpython/file/default/Lib/importlib/_bootstrap.py#l1568

I wouldn't necessarily say the import machinery is acting weird.  Directly messing with sys.modules, particularly for submodules, isn't something the rest of the import code explicitly accommodates (outside of loaders).  In this case the code doing the removing needs to make sure submodules are removed too.

If anybody thinks we should support this sort of interaction with sys.modules then we could address it in another issue.  Outside of testing, what are the use cases for removing modules from sys.modules?  Regardless, a solution might be as simple as moving the parent-binding code in _find_and_load_unlocked() to _gcd_import().
History
Date User Action Args
2013-08-17 00:52:01eric.snowsetrecipients: + eric.snow, brett.cannon, terry.reedy, ncoghlan, pitrou, ezio.melotti, python-dev, serhiy.storchaka
2013-08-17 00:52:01eric.snowsetmessageid: <1376700721.07.0.52481823238.issue18706@psf.upfronthosting.co.za>
2013-08-17 00:52:00eric.snowlinkissue18706 messages
2013-08-17 00:52:00eric.snowcreate