Added more information to api import exception handling. Port of http://svn.secondlife.com/trac/eventlet/changeset/147

This commit is contained in:
rdw
2008-09-25 19:03:34 -07:00
parent a355052956
commit 51443412a0

View File

@@ -574,22 +574,24 @@ def named(name):
""" """
toimport = name toimport = name
obj = None obj = None
import_err_strings = []
while toimport: while toimport:
try: try:
obj = __import__(toimport) obj = __import__(toimport)
break break
except ImportError, err: except ImportError, err:
# print 'Import error on %s: %s' % (toimport, err) # debugging spam # print 'Import error on %s: %s' % (toimport, err) # debugging spam
import_err_strings.append(err.__str__())
toimport = '.'.join(toimport.split('.')[:-1]) toimport = '.'.join(toimport.split('.')[:-1])
if obj is None: if obj is None:
raise ImportError('%s could not be imported' % (name, )) raise ImportError('%s could not be imported. Import errors: %r' % (name, import_err_strings))
for seg in name.split('.')[1:]: for seg in name.split('.')[1:]:
try: try:
obj = getattr(obj, seg) obj = getattr(obj, seg)
except AttributeError: except AttributeError:
dirobj = dir(obj) dirobj = dir(obj)
dirobj.sort() dirobj.sort()
raise AttributeError('attribute %r missing from %r (%r) %r' % ( raise AttributeError('attribute %r missing from %r (%r) %r. Import errors: %r' % (
seg, obj, dirobj, name)) seg, obj, dirobj, name, import_err_strings))
return obj return obj