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
This commit is contained in:
Malcolm Tredinnick 2007-12-02 15:27:44 +00:00
parent 49da9ab57e
commit 81832f594d
4 changed files with 9 additions and 9 deletions

View File

@ -52,7 +52,7 @@ class LazySettings(object):
if not settings_module: # If it's set but is an empty string. if not settings_module: # If it's set but is an empty string.
raise KeyError raise KeyError
except KeyError: except KeyError:
raise EnvironmentError, "Environment variable %s is undefined." % ENVIRONMENT_VARIABLE 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
self._target = Settings(settings_module) self._target = Settings(settings_module)
@ -63,7 +63,7 @@ class LazySettings(object):
argument must support attribute access (__getattr__)). argument must support attribute access (__getattr__)).
""" """
if self._target != None: if self._target != None:
raise EnvironmentError, 'Settings already configured.' raise RuntimeError, 'Settings already configured.'
holder = UserSettingsHolder(default_settings) holder = UserSettingsHolder(default_settings)
for name, value in options.items(): for name, value in options.items():
setattr(holder, name, value) setattr(holder, name, value)
@ -82,7 +82,7 @@ class Settings(object):
try: try:
mod = __import__(self.SETTINGS_MODULE, {}, {}, ['']) mod = __import__(self.SETTINGS_MODULE, {}, {}, [''])
except ImportError, e: except ImportError, e:
raise EnvironmentError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e) raise ImportError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)
# Settings that should be converted into tuples if they're mistakenly entered # Settings that should be converted into tuples if they're mistakenly entered
# as strings. # as strings.

View File

@ -84,7 +84,7 @@ def get_commands():
try: try:
from django.conf import settings from django.conf import settings
apps = settings.INSTALLED_APPS apps = settings.INSTALLED_APPS
except (AttributeError, EnvironmentError): except (AttributeError, ImportError):
apps = [] apps = []
for app_name in apps: for app_name in apps:
@ -99,7 +99,7 @@ def get_commands():
try: try:
from django.conf import settings from django.conf import settings
project_directory = setup_environ(__import__(settings.SETTINGS_MODULE)) project_directory = setup_environ(__import__(settings.SETTINGS_MODULE))
except (AttributeError, EnvironmentError, ImportError): except (AttributeError, ImportError):
project_directory = None project_directory = None
if project_directory: if project_directory:

View File

@ -409,7 +409,7 @@ class EmailField(RegexField):
try: try:
from django.conf import settings from django.conf import settings
URL_VALIDATOR_USER_AGENT = settings.URL_VALIDATOR_USER_AGENT URL_VALIDATOR_USER_AGENT = settings.URL_VALIDATOR_USER_AGENT
except (ImportError, EnvironmentError): except ImportError:
# It's OK if Django settings aren't configured. # It's OK if Django settings aren't configured.
URL_VALIDATOR_USER_AGENT = 'Django (http://www.djangoproject.com/)' URL_VALIDATOR_USER_AGENT = 'Django (http://www.djangoproject.com/)'

View File

@ -1166,12 +1166,12 @@ If you're not setting the ``DJANGO_SETTINGS_MODULE`` environment variable, you
settings. settings.
If you don't set ``DJANGO_SETTINGS_MODULE`` and don't call ``configure()``, If you don't set ``DJANGO_SETTINGS_MODULE`` and don't call ``configure()``,
Django will raise an ``EnvironmentError`` exception the first time a setting Django will raise an ``ImportError`` exception the first time a setting
is accessed. is accessed.
If you set ``DJANGO_SETTINGS_MODULE``, access settings values somehow, *then* If you set ``DJANGO_SETTINGS_MODULE``, access settings values somehow, *then*
call ``configure()``, Django will raise an ``EnvironmentError`` saying settings call ``configure()``, Django will raise a ``RuntimeError`` indicating
have already been configured. that settings have already been configured.
Also, it's an error to call ``configure()`` more than once, or to call Also, it's an error to call ``configure()`` more than once, or to call
``configure()`` after any setting has been accessed. ``configure()`` after any setting has been accessed.