Delaney, Timothy C (Timothy) wrote:
Perhaps the import machinery could keep a (weak?) mapping from module *object* to the ImportError it raised. If the module that would be returned by the import is in the mapping, raise the corresponding ImportError instead. The appropriate line, etc in the module would thus be shown each time the module was imported.
I believe this would work properly for reload() as well.
Of course it won't ... (no name to reload). This would effectively prevent ever loading that module in that session, even if it got fixed.
Instead, perhaps the following would be the right semantics:
# import replacement
import __builtin__ import sys
_builtin_import = __import__
def __import__(name, *p): try: return _builtin_import(name, *p) except ImportError, e: module = sys.modules[name]
for n, m in sys.modules.items(): if m is module: del sys.modules[n]
__builtin__.__import__ = __import__
try: import a except ImportError: import a
>>> try: ... import a ... except ImportError: ... import a ... Traceback (most recent call last): File "<stdin>", line 4, in ? File "<stdin>", line 9, in __import__ ImportError: a