[issue12554] Failed imports clean up module, but not sub modules

Calvin Spealman report at bugs.python.org
Wed Jul 13 21:44:23 CEST 2011


New submission from Calvin Spealman <ironfroggy at gmail.com>:

I came across this behavior when it was related to the shadowing of a small typo bug and took me forever to find. In my case, the original error was shadowed in a way that is unrelated to this bug, but lead to the module being imported twice (because it was removed when it failed the first time) and then the second import caused a completely unexpected error, because the state of the submodules conflicted with the import-time logic of the top-level package.

I think when a module fails to load, all of its sub-modules should be removed from sys.modules, not just itself.

calvin at willow-delta:/tmp$ mkdir foo/
calvin at willow-delta:/tmp$ cat >> foo/__init__.py
import foo.bar
1/0
calvin at willow-delta:/tmp$ cat >> foo/bar.py
name = "bar"
calvin at willow-delta:/tmp$ python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import foo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "foo/__init__.py", line 2, in <module>
    1/0
ZeroDivisionError: integer division or modulo by zero
>>> import sys
>>> sys.modules['foo.bar']
<module 'foo.bar' from 'foo/bar.py'>
>>>

----------
components: Interpreter Core
messages: 140298
nosy: Calvin.Spealman
priority: normal
severity: normal
status: open
title: Failed imports clean up module, but not sub modules
type: behavior

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue12554>
_______________________________________


More information about the Python-bugs-list mailing list