summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2016-11-21 22:30:32 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2016-11-21 22:30:32 (GMT)
commite425bd95e9f3ceb767e1894cce8cc01a63e7013b (patch)
tree8435dbd2639fe696b8c65fcb728a1281717daff7 /Lib
parent4ea37703acae519fcf0bcd23a6821e2972993789 (diff)
parent546ce65968921f52f1c79a7218e57dc237dbe436 (diff)
downloadcpython-e425bd95e9f3ceb767e1894cce8cc01a63e7013b.zip
cpython-e425bd95e9f3ceb767e1894cce8cc01a63e7013b.tar.gz
cpython-e425bd95e9f3ceb767e1894cce8cc01a63e7013b.tar.bz2
Issue #28752: Restored the __reduce__() methods of datetime objects.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/datetime.py12
-rw-r--r--Lib/test/datetimetester.py7
2 files changed, 16 insertions, 3 deletions
diff --git a/Lib/datetime.py b/Lib/datetime.py
index 36374aa..7540109 100644
--- a/Lib/datetime.py
+++ b/Lib/datetime.py
@@ -932,7 +932,7 @@ class date:
# Pickle support.
- def _getstate(self, protocol=3):
+ def _getstate(self):
yhi, ylo = divmod(self._year, 256)
return bytes([yhi, ylo, self._month, self._day]),
@@ -940,8 +940,8 @@ class date:
yhi, ylo, self._month, self._day = string
self._year = yhi * 256 + ylo
- def __reduce_ex__(self, protocol):
- return (self.__class__, self._getstate(protocol))
+ def __reduce__(self):
+ return (self.__class__, self._getstate())
_date_class = date # so functions w/ args named "date" can get at the class
@@ -1348,6 +1348,9 @@ class time:
def __reduce_ex__(self, protocol):
return (time, self._getstate(protocol))
+ def __reduce__(self):
+ return self.__reduce_ex__(2)
+
_time_class = time # so functions w/ args named "time" can get at the class
time.min = time(0, 0, 0)
@@ -1923,6 +1926,9 @@ class datetime(date):
def __reduce_ex__(self, protocol):
return (self.__class__, self._getstate(protocol))
+ def __reduce__(self):
+ return self.__reduce_ex__(2)
+
datetime.min = datetime(1, 1, 1)
datetime.max = datetime(9999, 12, 31, 23, 59, 59, 999999)
diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py
index 988c6f7..d65186d 100644
--- a/Lib/test/datetimetester.py
+++ b/Lib/test/datetimetester.py
@@ -1329,6 +1329,7 @@ class TestDate(HarmlessMixedComparison, unittest.TestCase):
green = pickler.dumps(orig, proto)
derived = unpickler.loads(green)
self.assertEqual(orig, derived)
+ self.assertEqual(orig.__reduce__(), orig.__reduce_ex__(2))
def test_compare(self):
t1 = self.theclass(2, 3, 4)
@@ -1830,6 +1831,7 @@ class TestDateTime(TestDate):
green = pickler.dumps(orig, proto)
derived = unpickler.loads(green)
self.assertEqual(orig, derived)
+ self.assertEqual(orig.__reduce__(), orig.__reduce_ex__(2))
def test_more_pickling(self):
a = self.theclass(2003, 2, 7, 16, 48, 37, 444116)
@@ -2469,6 +2471,7 @@ class TestTime(HarmlessMixedComparison, unittest.TestCase):
green = pickler.dumps(orig, proto)
derived = unpickler.loads(green)
self.assertEqual(orig, derived)
+ self.assertEqual(orig.__reduce__(), orig.__reduce_ex__(2))
def test_pickling_subclass_time(self):
args = 20, 59, 16, 64**2
@@ -2829,6 +2832,7 @@ class TestTimeTZ(TestTime, TZInfoBase, unittest.TestCase):
green = pickler.dumps(orig, proto)
derived = unpickler.loads(green)
self.assertEqual(orig, derived)
+ self.assertEqual(orig.__reduce__(), orig.__reduce_ex__(2))
# Try one with a tzinfo.
tinfo = PicklableFixedOffset(-300, 'cookie')
@@ -2840,6 +2844,7 @@ class TestTimeTZ(TestTime, TZInfoBase, unittest.TestCase):
self.assertIsInstance(derived.tzinfo, PicklableFixedOffset)
self.assertEqual(derived.utcoffset(), timedelta(minutes=-300))
self.assertEqual(derived.tzname(), 'cookie')
+ self.assertEqual(orig.__reduce__(), orig.__reduce_ex__(2))
def test_more_bool(self):
# time is always True.
@@ -3043,6 +3048,7 @@ class TestDateTimeTZ(TestDateTime, TZInfoBase, unittest.TestCase):
green = pickler.dumps(orig, proto)
derived = unpickler.loads(green)
self.assertEqual(orig, derived)
+ self.assertEqual(orig.__reduce__(), orig.__reduce_ex__(2))
# Try one with a tzinfo.
tinfo = PicklableFixedOffset(-300, 'cookie')
@@ -3055,6 +3061,7 @@ class TestDateTimeTZ(TestDateTime, TZInfoBase, unittest.TestCase):
self.assertIsInstance(derived.tzinfo, PicklableFixedOffset)
self.assertEqual(derived.utcoffset(), timedelta(minutes=-300))
self.assertEqual(derived.tzname(), 'cookie')
+ self.assertEqual(orig.__reduce__(), orig.__reduce_ex__(2))
def test_extreme_hashes(self):
# If an attempt is made to hash these via subtracting the offset