From eabc3b6c8dd67e9ff49da9f2f41cc653898cd0a1 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Fri, 20 Dec 2013 11:04:58 +0100 Subject: [PATCH] Set stacklevel for the override_settings warning. Refs #19031. --- django/test/signals.py | 5 ++++- tests/settings_tests/tests.py | 10 +++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/django/test/signals.py b/django/test/signals.py index 4af408ea9d..8086d50b32 100644 --- a/django/test/signals.py +++ b/django/test/signals.py @@ -97,4 +97,7 @@ def file_storage_changed(**kwargs): @receiver(setting_changed) def complex_setting_changed(**kwargs): if kwargs['enter'] and kwargs['setting'] in COMPLEX_OVERRIDE_SETTINGS: - warnings.warn("Overriding setting %s can lead to unexpected behaviour." % kwargs['setting']) + # Considering the current implementation of the signals framework, + # stacklevel=5 shows the line containing the override_settings call. + warnings.warn("Overriding setting %s can lead to unexpected behaviour." + % kwargs['setting'], stacklevel=5) diff --git a/tests/settings_tests/tests.py b/tests/settings_tests/tests.py index 3597982457..b148887e34 100644 --- a/tests/settings_tests/tests.py +++ b/tests/settings_tests/tests.py @@ -227,13 +227,13 @@ class TestComplexSettingOverride(TestCase): with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") - override = override_settings(TEST_WARN='override') - override.enable() - self.assertEqual('override', settings.TEST_WARN) - override.disable() + with override_settings(TEST_WARN='override'): + self.assertEqual(settings.TEST_WARN, 'override') self.assertEqual(len(w), 1) - self.assertEqual('Overriding setting TEST_WARN can lead to unexpected behaviour.', str(w[-1].message)) + self.assertEqual(w[0].filename, __file__) + self.assertEqual(str(w[0].message), + 'Overriding setting TEST_WARN can lead to unexpected behaviour.') class UniqueSettingsTests(TestCase):