Ensures UTC stays a singleton

This commit is contained in:
Ross Lawley 2011-12-19 08:40:37 +00:00 committed by behackett
parent 8c68bde1f8
commit aadd9e4eb4
2 changed files with 14 additions and 0 deletions

View File

@ -38,6 +38,9 @@ class FixedOffset(tzinfo):
def __getinitargs__(self):
return self.__offset, self.__name
def __reduce__(self):
return _UTC, ()
def utcoffset(self, dt):
return self.__offset
@ -50,3 +53,12 @@ class FixedOffset(tzinfo):
utc = FixedOffset(0, "UTC")
"""Fixed offset timezone representing UTC."""
def _UTC():
"""Factory function for utc unpickling.
Makes sure that unpickling a utc instance always returns the same
module global.
"""
return utc

View File

@ -48,9 +48,11 @@ class TestTimestamp(unittest.TestCase):
dc = copy.deepcopy(d)
self.assertEqual(dc, t.as_datetime())
self.assertEqual(d.tzinfo, dc.tzinfo)
dp = pickle.loads(pickle.dumps(d))
self.assertEqual(dp, t.as_datetime())
self.assertEqual(d.tzinfo, dp.tzinfo)
def test_exceptions(self):
self.assertRaises(TypeError, Timestamp)