mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	Tidied djang.db.utils.load_backend().
Removed an unneeded EnvironmentError catching and used "raise from exc" syntax.
This commit is contained in:
		| @@ -111,23 +111,19 @@ def load_backend(backend_name): | ||||
|         return import_module('%s.base' % backend_name) | ||||
|     except ImportError as e_user: | ||||
|         # The database backend wasn't found. Display a helpful error message | ||||
|         # listing all possible (built-in) database backends. | ||||
|         # listing all built-in database backends. | ||||
|         backend_dir = os.path.join(os.path.dirname(__file__), 'backends') | ||||
|         try: | ||||
|             builtin_backends = [ | ||||
|                 name for _, name, ispkg in pkgutil.iter_modules([backend_dir]) | ||||
|                 if ispkg and name not in {'base', 'dummy', 'postgresql_psycopg2'} | ||||
|             ] | ||||
|         except EnvironmentError: | ||||
|             builtin_backends = [] | ||||
|         if backend_name not in ['django.db.backends.%s' % b for b in | ||||
|                                 builtin_backends]: | ||||
|         builtin_backends = [ | ||||
|             name for _, name, ispkg in pkgutil.iter_modules([backend_dir]) | ||||
|             if ispkg and name not in {'base', 'dummy', 'postgresql_psycopg2'} | ||||
|         ] | ||||
|         if backend_name not in ['django.db.backends.%s' % b for b in builtin_backends]: | ||||
|             backend_reprs = map(repr, sorted(builtin_backends)) | ||||
|             error_msg = ("%r isn't an available database backend.\n" | ||||
|                          "Try using 'django.db.backends.XXX', where XXX " | ||||
|                          "is one of:\n    %s\nError was: %s" % | ||||
|                          (backend_name, ", ".join(backend_reprs), e_user)) | ||||
|             raise ImproperlyConfigured(error_msg) | ||||
|             raise ImproperlyConfigured( | ||||
|                 "%r isn't an available database backend.\n" | ||||
|                 "Try using 'django.db.backends.XXX', where XXX is one of:\n" | ||||
|                 "    %s" % (backend_name, ", ".join(backend_reprs)) | ||||
|             ) from e_user | ||||
|         else: | ||||
|             # If there's some other error, this must be an error in Django | ||||
|             raise | ||||
|   | ||||
| @@ -8,8 +8,8 @@ class TestLoadBackend(SimpleTestCase): | ||||
|         msg = ( | ||||
|             "'foo' isn't an available database backend.\n" | ||||
|             "Try using 'django.db.backends.XXX', where XXX is one of:\n" | ||||
|             "    'mysql', 'oracle', 'postgresql', 'sqlite3'\n" | ||||
|             "Error was: No module named 'foo'" | ||||
|             "    'mysql', 'oracle', 'postgresql', 'sqlite3'" | ||||
|         ) | ||||
|         with self.assertRaisesMessage(ImproperlyConfigured, msg): | ||||
|         with self.assertRaisesMessage(ImproperlyConfigured, msg) as cm: | ||||
|             load_backend('foo') | ||||
|         self.assertEqual(str(cm.exception.__cause__), "No module named 'foo'") | ||||
|   | ||||
		Reference in New Issue
	
	Block a user