Opened 3 weeks ago

Last modified 12 hours ago

#29467 assigned Bug

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: felixxm, 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 (6)

comment:1 Changed 3 weeks ago by Sławek Ehlert

You can see what kind of troubles it may cause in https://code.djangoproject.com/ticket/29449#comment:2

comment:2 Changed 2 weeks ago by Claude Paroz

Triage Stage: UnreviewedAccepted

comment:3 Changed 2 weeks ago by Sławek Ehlert

Owner: changed from nobody to Sławek Ehlert
Status: newassigned

comment:4 Changed 34 hours ago by Sławek Ehlert

I've opened https://github.com/django/django/pull/10068 to address this issue.

comment:5 Changed 34 hours ago by felixxm

Cc: felixxm added

comment:6 Changed 12 hours ago by Carlton Gibson

Cc: Carlton Gibson added
Has patch: set
Note: See TracTickets for help on using tickets.
Back to Top