Skip to content

Commit

Permalink
SettingsOverride context manager now works when one tries to override…
Browse files Browse the repository at this point in the history
… a setting that did not previously exist.

Also, the setting is "re-deleted" when we exit the context.
  • Loading branch information
Chris Glass committed Jan 21, 2011
1 parent eda9f94 commit bd94a6c
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions cms/test/util/context_managers.py
Expand Up @@ -15,18 +15,24 @@ class SettingsOverride(object):
with SettingsOverride(DEBUG=True):
# do something
"""
class DoesNotExist:
pass

def __init__(self, **overrides):
self.overrides = overrides

def __enter__(self):
self.old = {}
for key, value in self.overrides.items():
self.old[key] = getattr(settings, key)
self.old[key] = getattr(settings, key, self.DoesNotExist)
setattr(settings, key, value)

def __exit__(self, type, value, traceback):
for key, value in self.old.items():
setattr(settings, key, value)
if value is not self.DoesNotExist:
setattr(settings, key, value)
else:
del settings[key] # do not pollute the context!


class StdoutOverride(object):
Expand Down

0 comments on commit bd94a6c

Please sign in to comment.