[Zope-Checkins] SVN: Zope/branches/2.11/ Launchpad #290254, DateTime/DateTime.py
Tres Seaver
tseaver at palladion.com
Wed Mar 4 11:44:48 EST 2009
Log message for revision 97471:
Launchpad #290254, DateTime/DateTime.py
o Added '__setstate__' to cope with old pickles missing a '_micros'
attribute; Python's pickling support was creating a new instance,
*with* a '_micros' attribute, but not clearing that attribute before
updating the instance dict with the unpickled state.
Changed:
U Zope/branches/2.11/doc/CHANGES.txt
U Zope/branches/2.11/lib/python/DateTime/DateTime.py
U Zope/branches/2.11/lib/python/DateTime/tests/testDateTime.py
-=-
Modified: Zope/branches/2.11/doc/CHANGES.txt
===================================================================
--- Zope/branches/2.11/doc/CHANGES.txt 2009-03-04 07:56:49 UTC (rev 97470)
+++ Zope/branches/2.11/doc/CHANGES.txt 2009-03-04 16:44:48 UTC (rev 97471)
@@ -27,10 +27,16 @@
Bugs Fixed
- - Launchpad ##332168: Connection.py: do not expose DB connection strings
- through exceptions
+ - Launchpad #290254, DateTime/DateTime.py:
+ added '__setstate__' to cope with old pickles missing a '_micros'
+ attribute; Python's pickling support was creating a new instance,
+ *with* a '_micros' attribute, but not clearing that attribute before
+ updating the instance dict with the unpickled state.
- - LP/#324876: tighened regex for detecting the charset
+ - Launchpad #332168, Shared/DC/RDBMS/Connection.py:
+ do not expose DB connection strings through exceptions
+
+ - Launchpad #324876: tighened regex for detecting the charset
from a meta-equiv header
- configure script: setting ZOPE_VERS to '2.11'
Modified: Zope/branches/2.11/lib/python/DateTime/DateTime.py
===================================================================
--- Zope/branches/2.11/lib/python/DateTime/DateTime.py 2009-03-04 07:56:49 UTC (rev 97470)
+++ Zope/branches/2.11/lib/python/DateTime/DateTime.py 2009-03-04 16:44:48 UTC (rev 97471)
@@ -353,6 +353,10 @@
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)
+
def _parse_args(self, *args, **kw):
"""Return a new date-time object.
Modified: Zope/branches/2.11/lib/python/DateTime/tests/testDateTime.py
===================================================================
--- Zope/branches/2.11/lib/python/DateTime/tests/testDateTime.py 2009-03-04 07:56:49 UTC (rev 97470)
+++ Zope/branches/2.11/lib/python/DateTime/tests/testDateTime.py 2009-03-04 16:44:48 UTC (rev 97471)
@@ -211,14 +211,61 @@
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()
More information about the Zope-Checkins
mailing list