Fix minor issue in get_class bug caused by bad user input.

This commit is contained in:
Thomas Adamcik 2011-07-17 04:18:24 +02:00
parent 83ccee0bf5
commit 292d0e26cf
2 changed files with 8 additions and 5 deletions

View File

@ -18,9 +18,11 @@ def import_module(name):
return sys.modules[name]
def get_class(name):
logger.debug('Loading: %s', name)
if '.' not in name:
raise ImportError("Couldn't load: %s" % name)
module_name = name[:name.rindex('.')]
class_name = name[name.rindex('.') + 1:]
logger.debug('Loading: %s', name)
try:
module = import_module(module_name)
class_object = getattr(module, class_name)

View File

@ -4,12 +4,13 @@ from mopidy.utils import get_class
class GetClassTest(unittest.TestCase):
def test_loading_module_that_does_not_exist(self):
test = lambda: get_class('foo.bar.Baz')
self.assertRaises(ImportError, test)
self.assertRaises(ImportError, get_class, 'foo.bar.Baz')
def test_loading_class_that_does_not_exist(self):
test = lambda: get_class('unittest.FooBarBaz')
self.assertRaises(ImportError, test)
self.assertRaises(ImportError, get_class, 'unittest.FooBarBaz')
def test_loading_incorrect_class_path(self):
self.assertRaises(ImportError, get_class, 'foobarbaz')
def test_import_error_message_contains_complete_class_path(self):
try: