Author: adrian
Date: 2009-03-12 23:41:45 -0500 (Thu, 12 Mar 2009)
New Revision: 10045

Modified:
   django/trunk/django/db/__init__.py
Log:
Fixed #10487 -- Refactored the database-backend-selection logic into a 
function, django.db.load_backend. Thanks to Alex Gaynor for the initial patch

Modified: django/trunk/django/db/__init__.py
===================================================================
--- django/trunk/django/db/__init__.py  2009-03-12 23:41:27 UTC (rev 10044)
+++ django/trunk/django/db/__init__.py  2009-03-13 04:41:45 UTC (rev 10045)
@@ -9,33 +9,35 @@
 if not settings.DATABASE_ENGINE:
     settings.DATABASE_ENGINE = 'dummy'
 
-try:
-    # Most of the time, the database backend will be one of the official
-    # backends that ships with Django, so look there first.
-    _import_path = 'django.db.backends.'
-    backend = __import__('%s%s.base' % (_import_path, 
settings.DATABASE_ENGINE), {}, {}, [''])
-except ImportError, e:
-    # If the import failed, we might be looking for a database backend
-    # distributed external to Django. So we'll try that next.
+def load_backend(backend_name):
     try:
-        _import_path = ''
-        backend = __import__('%s.base' % settings.DATABASE_ENGINE, {}, {}, 
[''])
-    except ImportError, e_user:
-        # The database backend wasn't found. Display a helpful error message
-        # listing all possible (built-in) database backends.
-        backend_dir = os.path.join(__path__[0], 'backends')
+        # Most of the time, the database backend will be one of the official
+        # backends that ships with Django, so look there first.
+        return __import__('django.db.backends.%s.base' % backend_name, {}, {}, 
[''])
+    except ImportError, e:
+        # If the import failed, we might be looking for a database backend
+        # distributed external to Django. So we'll try that next.
         try:
-            available_backends = [f for f in os.listdir(backend_dir)
-                    if os.path.isdir(os.path.join(backend_dir, f))]
-        except EnvironmentError:
-            available_backends = []
-        available_backends.sort()
-        if settings.DATABASE_ENGINE not in available_backends:
-            raise ImproperlyConfigured, "%r isn't an available database 
backend. Available options are: %s\nError was: %s" % \
-                (settings.DATABASE_ENGINE, ", ".join(map(repr, 
available_backends)), e_user)
-        else:
-            raise # If there's some other error, this must be an error in 
Django itself.
+            return __import__('%s.base' % backend_name, {}, {}, [''])
+        except ImportError, e_user:
+            # The database backend wasn't found. Display a helpful error 
message
+            # listing all possible (built-in) database backends.
+            backend_dir = os.path.join(__path__[0], 'backends')
+            try:
+                available_backends = [f for f in os.listdir(backend_dir)
+                        if os.path.isdir(os.path.join(backend_dir, f))]
+            except EnvironmentError:
+                available_backends = []
+            available_backends.sort()
+            if backend_name not in available_backends:
+                error_msg = "%r isn't an available database backend. Available 
options are: %s\nError was: %s" % \
+                    (backend_name, ", ".join(map(repr, available_backends)), 
e_user)
+                raise ImproperlyConfigured(error_msg)
+            else:
+                raise # If there's some other error, this must be an error in 
Django itself.
 
+backend = load_backend(settings.DATABASE_ENGINE)
+
 # `connection`, `DatabaseError` and `IntegrityError` are convenient aliases
 # for backend bits.
 


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to