diff --git a/django/conf/__init__.py b/django/conf/__init__.py index 76f1e9f9a2..ee9a1ee1c5 100644 --- a/django/conf/__init__.py +++ b/django/conf/__init__.py @@ -42,6 +42,14 @@ class LazySettings(LazyObject): self._wrapped = Settings(settings_module) + def __repr__(self): + # Hardcode the class name as otherwise it yields 'Settings'. + if self._wrapped is empty: + return '' + return '' % { + 'settings_module': self._wrapped.SETTINGS_MODULE, + } + def __getattr__(self, name): if self._wrapped is empty: self._setup(name) @@ -136,6 +144,12 @@ class Settings(BaseSettings): def is_overridden(self, setting): return setting in self._explicit_settings + def __repr__(self): + return '<%(cls)s "%(settings_module)s">' % { + 'cls': self.__class__.__name__, + 'settings_module': self.SETTINGS_MODULE, + } + class UserSettingsHolder(BaseSettings): """ @@ -176,4 +190,9 @@ class UserSettingsHolder(BaseSettings): set_on_default = getattr(self.default_settings, 'is_overridden', lambda s: False)(setting) return (deleted or set_locally or set_on_default) + def __repr__(self): + return '<%(cls)s>' % { + 'cls': self.__class__.__name__, + } + settings = LazySettings() diff --git a/tests/settings_tests/tests.py b/tests/settings_tests/tests.py index be1cb426d8..ed5ae785ec 100644 --- a/tests/settings_tests/tests.py +++ b/tests/settings_tests/tests.py @@ -4,7 +4,7 @@ import unittest import warnings from types import ModuleType -from django.conf import LazySettings, Settings, settings +from django.conf import ENVIRONMENT_VARIABLE, LazySettings, Settings, settings from django.core.exceptions import ImproperlyConfigured from django.http import HttpRequest from django.test import ( @@ -442,6 +442,31 @@ class IsOverriddenTest(SimpleTestCase): with override_settings(ALLOWED_HOSTS=[]): self.assertTrue(settings.is_overridden('ALLOWED_HOSTS')) + def test_unevaluated_lazysettings_repr(self): + lazy_settings = LazySettings() + expected = '' + self.assertEqual(repr(lazy_settings), expected) + + def test_evaluated_lazysettings_repr(self): + lazy_settings = LazySettings() + module = os.environ.get(ENVIRONMENT_VARIABLE) + expected = '' % module + # Force evaluation of the lazy object. + lazy_settings.APPEND_SLASH + self.assertEqual(repr(lazy_settings), expected) + + def test_usersettingsholder_repr(self): + lazy_settings = LazySettings() + lazy_settings.configure(APPEND_SLASH=False) + expected = '' + self.assertEqual(repr(lazy_settings._wrapped), expected) + + def test_settings_repr(self): + module = os.environ.get(ENVIRONMENT_VARIABLE) + lazy_settings = Settings(module) + expected = '' % module + self.assertEqual(repr(lazy_settings), expected) + class TestListSettings(unittest.TestCase): """