better error handling on import statement itself

This commit is contained in:
Sandy Walsh 2014-05-26 16:27:15 +00:00
parent eb162afa55
commit 407ada2367
2 changed files with 9 additions and 2 deletions

View File

@ -35,6 +35,10 @@ class MissingMethodOrFunction(Exception):
pass
class ImportFailed(Exception):
pass
def _get_module(target):
"""Import a named class, module, method or function.
@ -74,7 +78,10 @@ def _get_module(target):
if not class_or_function:
raise MissingMethodOrFunction("No Method or Function specified in '%s'" % target)
__import__(module)
try:
__import__(module)
except ImportError as e:
raise ImportFailed("Failed to import '%s'. Error: %s" % (module, e))
klass, sep, function = class_or_function.rpartition('.')
return module, klass, function

View File

@ -39,7 +39,7 @@ class TestSimport(unittest.TestCase):
simport._get_module("tests|"
"localmodule:Foo.method_a"))
self.assertRaises(ImportError, simport._get_module,
self.assertRaises(simport.ImportFailed, simport._get_module,
"tests|that_module:function_a")
def test_bad_load(self):