Skip to content

Commit

Permalink
Launchpad #290254: Forward-ported fix for '_micros'-less pickles from…
Browse files Browse the repository at this point in the history
… the Zope 2.11 branch version.
  • Loading branch information
tseaver committed Mar 4, 2009
1 parent 1da4cbd commit 50e5d71
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
5 changes: 4 additions & 1 deletion CHANGES.txt
@@ -1,9 +1,12 @@
Changelog
=========

2.11.3 (unreleased)
2.12.0 (unreleased)
-------------------

- Launchpad #290254: Forward-ported fix for '_micros'-less pickles from
the Zope 2.11 branch version.


2.11.2 (2009-02-02)
-------------------
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Expand Up @@ -11,13 +11,13 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Setup for the Acquisition egg package
"""Setup for the DateTime egg package
"""
import os
from setuptools import setup, find_packages, Extension

setup(name='DateTime',
version = '2.11.3dev',
version = '2.12.0dev',
url='http://pypi.python.org/pypi/DateTime',
license='ZPL 2.1',
description="""\
Expand Down
6 changes: 6 additions & 0 deletions src/DateTime/DateTime.py
Expand Up @@ -353,6 +353,12 @@ def __init__(self,*args, **kw):
except:
raise SyntaxError('Unable to parse %s, %s' % (args, kw))

def __setstate__(self, state):
self.__dict__.clear() # why doesn't Python's unpickler do this?
self.__dict__.update(state)
if '_micros' not in state:
self._micros = self._upgrade_old()

def _parse_args(self, *args, **kw):
"""Return a new date-time object.
Expand Down
49 changes: 48 additions & 1 deletion src/DateTime/tests/testDateTime.py
Expand Up @@ -211,14 +211,61 @@ def testCompareOperations(self, dt=None, dt1=None):
self.failUnless(dt != dt1)
self.failUnless(not (dt == dt1))

def testUpgradeOldInstances(self):
def test_compare_old_instances(self):
# Compare dates that don't have the _micros attribute yet
# (e.g., from old pickles).
dt = DateTime('1997/1/1')
dt1 = DateTime('1997/2/2')
dt._millis = dt._micros / 1000
del dt._micros
dt1._millis = dt1._micros / 1000
del dt1._micros
self.testCompareOperations(dt, dt1)

def test_compare_old_new_instances(self):
# Compare a date without _micros attribute (e.g., from an old
# pickle) with one that does.
dt = DateTime('1997/1/1')
dt1 = DateTime('1997/2/2')
dt._millis = dt._micros / 1000
del dt._micros
self.testCompareOperations(dt, dt1)

def test_compare_new_old_instances(self):
# Compare a date with _micros attribute with one that does not
# (e.g., from an old pickle).
dt = DateTime('1997/1/1')
dt1 = DateTime('1997/2/2')
dt1._millis = dt._micros / 1000
del dt1._micros
self.testCompareOperations(dt, dt1)

def test_strftime_old_instance(self):
# https://bugs.launchpad.net/zope2/+bug/290254
# Ensure that dates without _micros attribute (e.g., from old
# pickles) still render correctly in strftime.
ISO = '2001-10-10T00:00:00+02:00'
dt = DateTime(ISO)
dt._millis = dt._micros / 1000
del dt._micros
self.assertEqual(dt.strftime('%Y'), '2001')

# Now, create one via pickling / unpickling.
from cPickle import dumps, loads
self.assertEqual(loads(dumps(dt)).strftime('%Y'), '2001')

def test___setstate___without_micros(self):
ISO = '2001-10-10T00:00:00+02:00'
dt = DateTime(ISO)
micros = dt._micros
dt._millis = dt._micros / 1000
del dt._micros
state = dt.__dict__

dt1 = DateTime()
dt1.__setstate__(state)
self.assertEqual(dt1._micros, micros)

def testTZ2(self):
# Time zone manipulation test 2
dt = DateTime()
Expand Down

0 comments on commit 50e5d71

Please sign in to comment.