From a6eda3a5fc524333b476deed5057ee9803d91108 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sat, 12 Apr 2014 15:31:37 +0200 Subject: [PATCH] [1.7.x] Also allowed a non-overridden setting to be deleted Refs #20032, #18824. Thanks ztorstri at gmail.com for the report. Backport of 3417ba0309 from master. --- django/conf/__init__.py | 5 +++-- docs/topics/testing/tools.txt | 5 +++++ tests/settings_tests/tests.py | 5 +++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/django/conf/__init__.py b/django/conf/__init__.py index 012df218b8..2a4fae8878 100644 --- a/django/conf/__init__.py +++ b/django/conf/__init__.py @@ -153,11 +153,12 @@ class UserSettingsHolder(BaseSettings): def __setattr__(self, name, value): self._deleted.discard(name) - return super(UserSettingsHolder, self).__setattr__(name, value) + super(UserSettingsHolder, self).__setattr__(name, value) def __delattr__(self, name): self._deleted.add(name) - return super(UserSettingsHolder, self).__delattr__(name) + if hasattr(self, name): + super(UserSettingsHolder, self).__delattr__(name) def __dir__(self): return list(self.__dict__) + dir(self.default_settings) diff --git a/docs/topics/testing/tools.txt b/docs/topics/testing/tools.txt index 25e1519c99..e176387ef2 100644 --- a/docs/topics/testing/tools.txt +++ b/docs/topics/testing/tools.txt @@ -1193,6 +1193,11 @@ have been overridden, like this:: del settings.LOGIN_URL ... +.. versionchanged:: 1.7 + + Previously, you could only simulate the deletion of a setting which was + explicitely overridden. + When overriding settings, make sure to handle the cases in which your app's code uses a cache or similar feature that retains state even if the setting is changed. Django provides the :data:`django.test.signals.setting_changed` diff --git a/tests/settings_tests/tests.py b/tests/settings_tests/tests.py index 754fdd32ab..6b2ca3e0a5 100644 --- a/tests/settings_tests/tests.py +++ b/tests/settings_tests/tests.py @@ -232,10 +232,15 @@ class SettingsTests(TestCase): Allow deletion of a setting in an overridden settings set (#18824) """ previous_i18n = settings.USE_I18N + previous_l10n = settings.USE_L10N with self.settings(USE_I18N=False): del settings.USE_I18N self.assertRaises(AttributeError, getattr, settings, 'USE_I18N') + # Should also work for a non-overridden setting + del settings.USE_L10N + self.assertRaises(AttributeError, getattr, settings, 'USE_L10N') self.assertEqual(settings.USE_I18N, previous_i18n) + self.assertEqual(settings.USE_L10N, previous_l10n) def test_override_settings_nested(self): """