@ -47,7 +47,7 @@ class _ExtensionImporter(object):
return self
return self
def load_module ( self , fullname ) :
def load_module ( self , fullname ) :
from sys import modules
from sys import modules , exc_info
if fullname in modules :
if fullname in modules :
return modules [ fullname ]
return modules [ fullname ]
modname = fullname . split ( ' . ' , self . prefix_cutoff ) [ self . prefix_cutoff ]
modname = fullname . split ( ' . ' , self . prefix_cutoff ) [ self . prefix_cutoff ]
@ -56,12 +56,28 @@ class _ExtensionImporter(object):
try :
try :
__import__ ( realname )
__import__ ( realname )
except ImportError :
except ImportError :
exc_type , exc_value , tb = exc_info ( )
if self . is_important_traceback ( realname , tb ) :
raise exc_type , exc_value , tb
continue
continue
module = modules [ fullname ] = modules [ realname ]
module = modules [ fullname ] = modules [ realname ]
if ' . ' not in modname :
if ' . ' not in modname :
setattr ( modules [ __name__ ] , modname , module )
setattr ( modules [ __name__ ] , modname , module )
return module
return module
raise ImportError ( fullname )
raise ImportError ( ' No module named %s ' % fullname )
def is_important_traceback ( self , important_module , tb ) :
""" Walks a traceback ' s frames and checks if any of the frames
originated in the given important module . If that is the case
then we were able to import the module itself but apparently
something went wrong when the module was imported . ( Eg : import
of an import failed ) .
"""
while tb is not None :
if tb . tb_frame . f_globals . get ( ' __name__ ' ) == important_module :
return True
tb = tb . tb_next
return False
_ExtensionImporter ( )
_ExtensionImporter ( )