Fixed #23381 -- Context manager restored state should be determined in __enter__

This commit is contained in:
Thomas Chaumeny 2014-08-28 22:26:37 +02:00 committed by Simon Charette
parent 569e0a299d
commit efcbf3e095
3 changed files with 21 additions and 3 deletions

View File

@ -262,9 +262,9 @@ class override(object):
"""
def __init__(self, timezone):
self.timezone = timezone
self.old_timezone = getattr(_active, 'value', None)
def __enter__(self):
self.old_timezone = getattr(_active, 'value', None)
if self.timezone is None:
deactivate()
else:

View File

@ -154,9 +154,9 @@ class override(ContextDecorator):
def __init__(self, language, deactivate=False):
self.language = language
self.deactivate = deactivate
self.old_language = get_language()
def __enter__(self):
self.old_language = get_language()
if self.language is not None:
activate(self.language)
else:

View File

@ -74,7 +74,6 @@ class TranslationTests(TestCase):
deactivate()
def test_override_decorator(self):
activate('de')
@translation.override('pl')
def func_pl():
@ -85,6 +84,7 @@ class TranslationTests(TestCase):
self.assertEqual(get_language(), settings.LANGUAGE_CODE)
try:
activate('de')
func_pl()
self.assertEqual(get_language(), 'de')
func_none()
@ -92,6 +92,24 @@ class TranslationTests(TestCase):
finally:
deactivate()
def test_override_exit(self):
"""
Test that the language restored is the one used when the function was
called, not the one used when the decorator was initialized. refs #23381
"""
activate('fr')
@translation.override('pl')
def func_pl():
pass
deactivate()
try:
activate('en')
func_pl()
self.assertEqual(get_language(), 'en')
finally:
deactivate()
def test_lazy_objects(self):
"""
Format string interpolation should work with *_lazy objects.