Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #23381 -- Context manager restored state should be determined i…

…n __enter__
  • Loading branch information...
commit efcbf3e095dce3491eb52774978afe3f7bbdf217 1 parent 569e0a2
@tchaumeny tchaumeny authored charettes committed
View
2  django/utils/timezone.py
@@ -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
2  django/utils/translation/__init__.py
@@ -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
20 tests/i18n/tests.py
@@ -74,7 +74,6 @@ def test_override(self):
deactivate()
def test_override_decorator(self):
- activate('de')
@translation.override('pl')
def func_pl():
@@ -85,6 +84,7 @@ def func_none():
self.assertEqual(get_language(), settings.LANGUAGE_CODE)
try:
+ activate('de')
func_pl()
self.assertEqual(get_language(), 'de')
func_none()
@@ -92,6 +92,24 @@ def func_none():
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.
Please sign in to comment.
Something went wrong with that request. Please try again.