Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #5743 -- Tweaked the exceptions raised when importing settings …

…so that we

cooperate with Python's standard help() function. Patch from ionut_bizau and
Ben Slavin.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@6832 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 81832f594d7b0d3b671bf12878a77f7a34fb2f04 1 parent 49da9ab
Malcolm Tredinnick authored December 02, 2007
6  django/conf/__init__.py
@@ -52,7 +52,7 @@ def _import_settings(self):
52 52
             if not settings_module: # If it's set but is an empty string.
53 53
                 raise KeyError
54 54
         except KeyError:
55  
-            raise EnvironmentError, "Environment variable %s is undefined." % ENVIRONMENT_VARIABLE
  55
+            raise ImportError, "Environment variable %s is undefined so settings cannot be imported." % ENVIRONMENT_VARIABLE   # NOTE: This is arguably an EnvironmentError, but that causes problems with Python's interactive help
56 56
 
57 57
         self._target = Settings(settings_module)
58 58
 
@@ -63,7 +63,7 @@ def configure(self, default_settings=global_settings, **options):
63 63
         argument must support attribute access (__getattr__)).
64 64
         """
65 65
         if self._target != None:
66  
-            raise EnvironmentError, 'Settings already configured.'
  66
+            raise RuntimeError, 'Settings already configured.'
67 67
         holder = UserSettingsHolder(default_settings)
68 68
         for name, value in options.items():
69 69
             setattr(holder, name, value)
@@ -82,7 +82,7 @@ def __init__(self, settings_module):
82 82
         try:
83 83
             mod = __import__(self.SETTINGS_MODULE, {}, {}, [''])
84 84
         except ImportError, e:
85  
-            raise EnvironmentError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)
  85
+            raise ImportError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)
86 86
 
87 87
         # Settings that should be converted into tuples if they're mistakenly entered
88 88
         # as strings.
4  django/core/management/__init__.py
@@ -84,7 +84,7 @@ def get_commands():
84 84
         try:
85 85
             from django.conf import settings
86 86
             apps = settings.INSTALLED_APPS
87  
-        except (AttributeError, EnvironmentError):
  87
+        except (AttributeError, ImportError):
88 88
             apps = []
89 89
 
90 90
         for app_name in apps:
@@ -99,7 +99,7 @@ def get_commands():
99 99
         try:
100 100
             from django.conf import settings
101 101
             project_directory = setup_environ(__import__(settings.SETTINGS_MODULE))
102  
-        except (AttributeError, EnvironmentError, ImportError):
  102
+        except (AttributeError, ImportError):
103 103
             project_directory = None
104 104
 
105 105
         if project_directory:
2  django/newforms/fields.py
@@ -409,7 +409,7 @@ def __init__(self, max_length=None, min_length=None, *args, **kwargs):
409 409
 try:
410 410
     from django.conf import settings
411 411
     URL_VALIDATOR_USER_AGENT = settings.URL_VALIDATOR_USER_AGENT
412  
-except (ImportError, EnvironmentError):
  412
+except ImportError:
413 413
     # It's OK if Django settings aren't configured.
414 414
     URL_VALIDATOR_USER_AGENT = 'Django (http://www.djangoproject.com/)'
415 415
 
6  docs/settings.txt
@@ -1166,12 +1166,12 @@ If you're not setting the ``DJANGO_SETTINGS_MODULE`` environment variable, you
1166 1166
 settings.
1167 1167
 
1168 1168
 If you don't set ``DJANGO_SETTINGS_MODULE`` and don't call ``configure()``,
1169  
-Django will raise an ``EnvironmentError`` exception the first time a setting
  1169
+Django will raise an ``ImportError`` exception the first time a setting
1170 1170
 is accessed.
1171 1171
 
1172 1172
 If you set ``DJANGO_SETTINGS_MODULE``, access settings values somehow, *then*
1173  
-call ``configure()``, Django will raise an ``EnvironmentError`` saying settings
1174  
-have already been configured.
  1173
+call ``configure()``, Django will raise a ``RuntimeError`` indicating
  1174
+that settings have already been configured.
1175 1175
 
1176 1176
 Also, it's an error to call ``configure()`` more than once, or to call
1177 1177
 ``configure()`` after any setting has been accessed.

0 notes on commit 81832f5

Please sign in to comment.
Something went wrong with that request. Please try again.