Opened 6 years ago
Closed 6 years ago
#29467 closed Bug (fixed)
override_settings context manager can leave undesired settings state
Reported by: | Sławek Ehlert | Owned by: | Sławek Ehlert |
---|---|---|---|
Component: | Testing framework | Version: | 2.0 |
Severity: | Normal | Keywords: | settings override_settings |
Cc: | Mariusz Felisiak, Carlton Gibson | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
When connecting to the setting_changed
signal a receiver function that throws an error, one can break the override_settings
decorator/context manager and leave the current settings in the modified state. override_settings
context manager should be more defensive inside the "enter" part when the setting_changed
signal is sent.
Example:
## settings SETTING_A = 'A' SETTING_B = 'B' ## usage from django.test import override_settings from django.conf import settings from django.core import signals def setting_changed_receiver(*args, **kwargs): if kwargs['setting'] == 'SETTING_B': raise Exception("I'm here for the lulz") signals.setting_changed.connect(setting_changed_receiver) try: with override_settings(SETTING_A='X', SETTING_B='X'): pass except: pass print(settings.SETTING_A) print(settings.SETTING_B)
prints out:
X X
instead of the expected:
A B
This may have some repercussions while running tests, since one failing test can influence a different, unrelated one.
Change History (7)
comment:1 by , 6 years ago
comment:2 by , 6 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:3 by , 6 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:4 by , 6 years ago
I've opened https://github.com/django/django/pull/10068 to address this issue.
comment:5 by , 6 years ago
Cc: | added |
---|
comment:6 by , 6 years ago
Cc: | added |
---|---|
Has patch: | set |
You can see what kind of troubles it may cause in https://code.djangoproject.com/ticket/29449#comment:2