Skip to content
Browse files

SettingsOverride context manager now works when one tries to override…

… a setting that did not previously exist.

Also, the setting is "re-deleted" when we exit the context.
  • Loading branch information...
1 parent eda9f94 commit bd94a6c3a5f26384ff40ca65d714d951c2872fcc @chrisglass chrisglass committed Jan 21, 2011
Showing with 8 additions and 2 deletions.
  1. +8 −2 cms/test/util/context_managers.py
View
10 cms/test/util/context_managers.py
@@ -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):

0 comments on commit bd94a6c

Please sign in to comment.
Something went wrong with that request. Please try again.