Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.5.x] Fixed #19708 -- Exception in timezone.override(None).

Thanks rafales.

Backport of 9a4a1ce.
  • Loading branch information...
commit db49b6d2b298bc53b55df9ee7cf80e6492d3116b 1 parent 73bb966
Aymeric Augustin authored January 31, 2013
7  django/utils/timezone.py
@@ -111,6 +111,7 @@ def get_default_timezone():
111 111
         if isinstance(settings.TIME_ZONE, six.string_types) and pytz is not None:
112 112
             _localtime = pytz.timezone(settings.TIME_ZONE)
113 113
         else:
  114
+            # This relies on os.environ['TZ'] being set to settings.TIME_ZONE.
114 115
             _localtime = LocalTimezone()
115 116
     return _localtime
116 117
 
@@ -198,10 +199,10 @@ def __enter__(self):
198 199
             activate(self.timezone)
199 200
 
200 201
     def __exit__(self, exc_type, exc_value, traceback):
201  
-        if self.old_timezone is not None:
202  
-            _active.value = self.old_timezone
  202
+        if self.old_timezone is None:
  203
+            deactivate()
203 204
         else:
204  
-            del _active.value
  205
+            _active.value = self.old_timezone
205 206
 
206 207
 
207 208
 # Templates
30  tests/regressiontests/utils/timezone.py
@@ -3,9 +3,14 @@
3 3
 import pickle
4 4
 from django.test.utils import override_settings
5 5
 from django.utils import timezone
  6
+from django.utils.tzinfo import FixedOffset
6 7
 from django.utils import unittest
7 8
 
8 9
 
  10
+EAT = FixedOffset(180)      # Africa/Nairobi
  11
+ICT = FixedOffset(420)      # Asia/Bangkok
  12
+
  13
+
9 14
 class TimezoneTests(unittest.TestCase):
10 15
 
11 16
     def test_localtime(self):
@@ -20,6 +25,31 @@ def test_now(self):
20 25
         with override_settings(USE_TZ=False):
21 26
             self.assertTrue(timezone.is_naive(timezone.now()))
22 27
 
  28
+    def test_override(self):
  29
+        default = timezone.get_default_timezone()
  30
+        try:
  31
+            timezone.activate(ICT)
  32
+
  33
+            with timezone.override(EAT):
  34
+                self.assertIs(EAT, timezone.get_current_timezone())
  35
+            self.assertIs(ICT, timezone.get_current_timezone())
  36
+
  37
+            with timezone.override(None):
  38
+                self.assertIs(default, timezone.get_current_timezone())
  39
+            self.assertIs(ICT, timezone.get_current_timezone())
  40
+
  41
+            timezone.deactivate()
  42
+
  43
+            with timezone.override(EAT):
  44
+                self.assertIs(EAT, timezone.get_current_timezone())
  45
+            self.assertIs(default, timezone.get_current_timezone())
  46
+
  47
+            with timezone.override(None):
  48
+                self.assertIs(default, timezone.get_current_timezone())
  49
+            self.assertIs(default, timezone.get_current_timezone())
  50
+        finally:
  51
+            timezone.deactivate()
  52
+
23 53
     def test_copy(self):
24 54
         self.assertIsInstance(copy.copy(timezone.UTC()), timezone.UTC)
25 55
         self.assertIsInstance(copy.copy(timezone.LocalTimezone()), timezone.LocalTimezone)

0 notes on commit db49b6d

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