Fixed #23381 -- Context manager restored state should be determined in __enter__
This commit is contained in:
parent
569e0a299d
commit
efcbf3e095
|
@ -262,9 +262,9 @@ class override(object):
|
||||||
"""
|
"""
|
||||||
def __init__(self, timezone):
|
def __init__(self, timezone):
|
||||||
self.timezone = timezone
|
self.timezone = timezone
|
||||||
self.old_timezone = getattr(_active, 'value', None)
|
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
|
self.old_timezone = getattr(_active, 'value', None)
|
||||||
if self.timezone is None:
|
if self.timezone is None:
|
||||||
deactivate()
|
deactivate()
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -154,9 +154,9 @@ class override(ContextDecorator):
|
||||||
def __init__(self, language, deactivate=False):
|
def __init__(self, language, deactivate=False):
|
||||||
self.language = language
|
self.language = language
|
||||||
self.deactivate = deactivate
|
self.deactivate = deactivate
|
||||||
self.old_language = get_language()
|
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
|
self.old_language = get_language()
|
||||||
if self.language is not None:
|
if self.language is not None:
|
||||||
activate(self.language)
|
activate(self.language)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -74,7 +74,6 @@ class TranslationTests(TestCase):
|
||||||
deactivate()
|
deactivate()
|
||||||
|
|
||||||
def test_override_decorator(self):
|
def test_override_decorator(self):
|
||||||
activate('de')
|
|
||||||
|
|
||||||
@translation.override('pl')
|
@translation.override('pl')
|
||||||
def func_pl():
|
def func_pl():
|
||||||
|
@ -85,6 +84,7 @@ class TranslationTests(TestCase):
|
||||||
self.assertEqual(get_language(), settings.LANGUAGE_CODE)
|
self.assertEqual(get_language(), settings.LANGUAGE_CODE)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
activate('de')
|
||||||
func_pl()
|
func_pl()
|
||||||
self.assertEqual(get_language(), 'de')
|
self.assertEqual(get_language(), 'de')
|
||||||
func_none()
|
func_none()
|
||||||
|
@ -92,6 +92,24 @@ class TranslationTests(TestCase):
|
||||||
finally:
|
finally:
|
||||||
deactivate()
|
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):
|
def test_lazy_objects(self):
|
||||||
"""
|
"""
|
||||||
Format string interpolation should work with *_lazy objects.
|
Format string interpolation should work with *_lazy objects.
|
||||||
|
|
Loading…
Reference in New Issue