[Python-Dev] Fix import errors to have data
Delaney, Timothy C (Timothy)
tdelaney at avaya.com
Wed Jul 28 02:27:01 CEST 2004
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]
raise e
__builtin__.__import__ = __import__
# a.py
raise ImportError(__name__)
# b.py
try:
import a
except ImportError:
import a
print 'OK'
>>> 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
Tim Delaney
More information about the Python-Dev
mailing list