From 292d0e26cf88b29d0d168bd1028e8e9ab7026635 Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Sun, 17 Jul 2011 04:18:24 +0200 Subject: [PATCH] Fix minor issue in get_class bug caused by bad user input. --- mopidy/utils/__init__.py | 4 +++- tests/utils/init_test.py | 9 +++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/mopidy/utils/__init__.py b/mopidy/utils/__init__.py index acbb4664..9d7532a0 100644 --- a/mopidy/utils/__init__.py +++ b/mopidy/utils/__init__.py @@ -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) diff --git a/tests/utils/init_test.py b/tests/utils/init_test.py index fb38e2ea..70dd7e36 100644 --- a/tests/utils/init_test.py +++ b/tests/utils/init_test.py @@ -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: