Commit 0de0a05e authored by Serhiy Storchaka's avatar Serhiy Storchaka

Issue #28752: Restored the __reduce__() methods of datetime objects.

parent 75808963
...@@ -932,7 +932,7 @@ class date: ...@@ -932,7 +932,7 @@ class date:
# Pickle support. # Pickle support.
def _getstate(self, protocol=3): def _getstate(self):
yhi, ylo = divmod(self._year, 256) yhi, ylo = divmod(self._year, 256)
return bytes([yhi, ylo, self._month, self._day]), return bytes([yhi, ylo, self._month, self._day]),
...@@ -940,8 +940,8 @@ class date: ...@@ -940,8 +940,8 @@ class date:
yhi, ylo, self._month, self._day = string yhi, ylo, self._month, self._day = string
self._year = yhi * 256 + ylo self._year = yhi * 256 + ylo
def __reduce_ex__(self, protocol): def __reduce__(self):
return (self.__class__, self._getstate(protocol)) return (self.__class__, self._getstate())
_date_class = date # so functions w/ args named "date" can get at the class _date_class = date # so functions w/ args named "date" can get at the class
...@@ -1348,6 +1348,9 @@ class time: ...@@ -1348,6 +1348,9 @@ class time:
def __reduce_ex__(self, protocol): def __reduce_ex__(self, protocol):
return (time, self._getstate(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_class = time # so functions w/ args named "time" can get at the class
time.min = time(0, 0, 0) time.min = time(0, 0, 0)
...@@ -1923,6 +1926,9 @@ class datetime(date): ...@@ -1923,6 +1926,9 @@ class datetime(date):
def __reduce_ex__(self, protocol): def __reduce_ex__(self, protocol):
return (self.__class__, self._getstate(protocol)) return (self.__class__, self._getstate(protocol))
def __reduce__(self):
return self.__reduce_ex__(2)
datetime.min = datetime(1, 1, 1) datetime.min = datetime(1, 1, 1)
datetime.max = datetime(9999, 12, 31, 23, 59, 59, 999999) datetime.max = datetime(9999, 12, 31, 23, 59, 59, 999999)
......
...@@ -1329,6 +1329,7 @@ class TestDate(HarmlessMixedComparison, unittest.TestCase): ...@@ -1329,6 +1329,7 @@ class TestDate(HarmlessMixedComparison, unittest.TestCase):
green = pickler.dumps(orig, proto) green = pickler.dumps(orig, proto)
derived = unpickler.loads(green) derived = unpickler.loads(green)
self.assertEqual(orig, derived) self.assertEqual(orig, derived)
self.assertEqual(orig.__reduce__(), orig.__reduce_ex__(2))
def test_compare(self): def test_compare(self):
t1 = self.theclass(2, 3, 4) t1 = self.theclass(2, 3, 4)
...@@ -1830,6 +1831,7 @@ class TestDateTime(TestDate): ...@@ -1830,6 +1831,7 @@ class TestDateTime(TestDate):
green = pickler.dumps(orig, proto) green = pickler.dumps(orig, proto)
derived = unpickler.loads(green) derived = unpickler.loads(green)
self.assertEqual(orig, derived) self.assertEqual(orig, derived)
self.assertEqual(orig.__reduce__(), orig.__reduce_ex__(2))
def test_more_pickling(self): def test_more_pickling(self):
a = self.theclass(2003, 2, 7, 16, 48, 37, 444116) a = self.theclass(2003, 2, 7, 16, 48, 37, 444116)
...@@ -2469,6 +2471,7 @@ class TestTime(HarmlessMixedComparison, unittest.TestCase): ...@@ -2469,6 +2471,7 @@ class TestTime(HarmlessMixedComparison, unittest.TestCase):
green = pickler.dumps(orig, proto) green = pickler.dumps(orig, proto)
derived = unpickler.loads(green) derived = unpickler.loads(green)
self.assertEqual(orig, derived) self.assertEqual(orig, derived)
self.assertEqual(orig.__reduce__(), orig.__reduce_ex__(2))
def test_pickling_subclass_time(self): def test_pickling_subclass_time(self):
args = 20, 59, 16, 64**2 args = 20, 59, 16, 64**2
...@@ -2829,6 +2832,7 @@ class TestTimeTZ(TestTime, TZInfoBase, unittest.TestCase): ...@@ -2829,6 +2832,7 @@ class TestTimeTZ(TestTime, TZInfoBase, unittest.TestCase):
green = pickler.dumps(orig, proto) green = pickler.dumps(orig, proto)
derived = unpickler.loads(green) derived = unpickler.loads(green)
self.assertEqual(orig, derived) self.assertEqual(orig, derived)
self.assertEqual(orig.__reduce__(), orig.__reduce_ex__(2))
# Try one with a tzinfo. # Try one with a tzinfo.
tinfo = PicklableFixedOffset(-300, 'cookie') tinfo = PicklableFixedOffset(-300, 'cookie')
...@@ -2840,6 +2844,7 @@ class TestTimeTZ(TestTime, TZInfoBase, unittest.TestCase): ...@@ -2840,6 +2844,7 @@ class TestTimeTZ(TestTime, TZInfoBase, unittest.TestCase):
self.assertIsInstance(derived.tzinfo, PicklableFixedOffset) self.assertIsInstance(derived.tzinfo, PicklableFixedOffset)
self.assertEqual(derived.utcoffset(), timedelta(minutes=-300)) self.assertEqual(derived.utcoffset(), timedelta(minutes=-300))
self.assertEqual(derived.tzname(), 'cookie') self.assertEqual(derived.tzname(), 'cookie')
self.assertEqual(orig.__reduce__(), orig.__reduce_ex__(2))
def test_more_bool(self): def test_more_bool(self):
# time is always True. # time is always True.
...@@ -3043,6 +3048,7 @@ class TestDateTimeTZ(TestDateTime, TZInfoBase, unittest.TestCase): ...@@ -3043,6 +3048,7 @@ class TestDateTimeTZ(TestDateTime, TZInfoBase, unittest.TestCase):
green = pickler.dumps(orig, proto) green = pickler.dumps(orig, proto)
derived = unpickler.loads(green) derived = unpickler.loads(green)
self.assertEqual(orig, derived) self.assertEqual(orig, derived)
self.assertEqual(orig.__reduce__(), orig.__reduce_ex__(2))
# Try one with a tzinfo. # Try one with a tzinfo.
tinfo = PicklableFixedOffset(-300, 'cookie') tinfo = PicklableFixedOffset(-300, 'cookie')
...@@ -3055,6 +3061,7 @@ class TestDateTimeTZ(TestDateTime, TZInfoBase, unittest.TestCase): ...@@ -3055,6 +3061,7 @@ class TestDateTimeTZ(TestDateTime, TZInfoBase, unittest.TestCase):
self.assertIsInstance(derived.tzinfo, PicklableFixedOffset) self.assertIsInstance(derived.tzinfo, PicklableFixedOffset)
self.assertEqual(derived.utcoffset(), timedelta(minutes=-300)) self.assertEqual(derived.utcoffset(), timedelta(minutes=-300))
self.assertEqual(derived.tzname(), 'cookie') self.assertEqual(derived.tzname(), 'cookie')
self.assertEqual(orig.__reduce__(), orig.__reduce_ex__(2))
def test_extreme_hashes(self): def test_extreme_hashes(self):
# If an attempt is made to hash these via subtracting the offset # If an attempt is made to hash these via subtracting the offset
......
...@@ -42,6 +42,8 @@ Core and Builtins ...@@ -42,6 +42,8 @@ Core and Builtins
Library Library
------- -------
- Issue #28752: Restored the __reduce__() methods of datetime objects.
- Issue #28727: Regular expression patterns, _sre.SRE_Pattern objects created - Issue #28727: Regular expression patterns, _sre.SRE_Pattern objects created
by re.compile(), become comparable (only x==y and x!=y operators). This by re.compile(), become comparable (only x==y and x!=y operators). This
change should fix the issue #18383: don't duplicate warning filters when the change should fix the issue #18383: don't duplicate warning filters when the
......
...@@ -3955,15 +3955,21 @@ time_getstate(PyDateTime_Time *self, int proto) ...@@ -3955,15 +3955,21 @@ time_getstate(PyDateTime_Time *self, int proto)
} }
static PyObject * static PyObject *
time_reduce(PyDateTime_Time *self, PyObject *args) time_reduce_ex(PyDateTime_Time *self, PyObject *args)
{ {
int proto = 0; int proto;
if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto)) if (!PyArg_ParseTuple(args, "i:__reduce_ex__", &proto))
return NULL; return NULL;
return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self, proto)); return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self, proto));
} }
static PyObject *
time_reduce(PyDateTime_Time *self, PyObject *arg)
{
return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self, 2));
}
static PyMethodDef time_methods[] = { static PyMethodDef time_methods[] = {
{"isoformat", (PyCFunction)time_isoformat, METH_VARARGS | METH_KEYWORDS, {"isoformat", (PyCFunction)time_isoformat, METH_VARARGS | METH_KEYWORDS,
...@@ -3989,9 +3995,12 @@ static PyMethodDef time_methods[] = { ...@@ -3989,9 +3995,12 @@ static PyMethodDef time_methods[] = {
{"replace", (PyCFunction)time_replace, METH_VARARGS | METH_KEYWORDS, {"replace", (PyCFunction)time_replace, METH_VARARGS | METH_KEYWORDS,
PyDoc_STR("Return time with new specified fields.")}, PyDoc_STR("Return time with new specified fields.")},
{"__reduce_ex__", (PyCFunction)time_reduce, METH_VARARGS, {"__reduce_ex__", (PyCFunction)time_reduce_ex, METH_VARARGS,
PyDoc_STR("__reduce_ex__(proto) -> (cls, state)")}, PyDoc_STR("__reduce_ex__(proto) -> (cls, state)")},
{"__reduce__", (PyCFunction)time_reduce, METH_NOARGS,
PyDoc_STR("__reduce__() -> (cls, state)")},
{NULL, NULL} {NULL, NULL}
}; };
...@@ -5420,15 +5429,21 @@ datetime_getstate(PyDateTime_DateTime *self, int proto) ...@@ -5420,15 +5429,21 @@ datetime_getstate(PyDateTime_DateTime *self, int proto)
} }
static PyObject * static PyObject *
datetime_reduce(PyDateTime_DateTime *self, PyObject *args) datetime_reduce_ex(PyDateTime_DateTime *self, PyObject *args)
{ {
int proto = 0; int proto;
if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto)) if (!PyArg_ParseTuple(args, "i:__reduce_ex__", &proto))
return NULL; return NULL;
return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self, proto)); return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self, proto));
} }
static PyObject *
datetime_reduce(PyDateTime_DateTime *self, PyObject *arg)
{
return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self, 2));
}
static PyMethodDef datetime_methods[] = { static PyMethodDef datetime_methods[] = {
/* Class methods: */ /* Class methods: */
...@@ -5503,9 +5518,12 @@ static PyMethodDef datetime_methods[] = { ...@@ -5503,9 +5518,12 @@ static PyMethodDef datetime_methods[] = {
{"astimezone", (PyCFunction)datetime_astimezone, METH_VARARGS | METH_KEYWORDS, {"astimezone", (PyCFunction)datetime_astimezone, METH_VARARGS | METH_KEYWORDS,
PyDoc_STR("tz -> convert to local time in new timezone tz\n")}, PyDoc_STR("tz -> convert to local time in new timezone tz\n")},
{"__reduce_ex__", (PyCFunction)datetime_reduce, METH_VARARGS, {"__reduce_ex__", (PyCFunction)datetime_reduce_ex, METH_VARARGS,
PyDoc_STR("__reduce_ex__(proto) -> (cls, state)")}, PyDoc_STR("__reduce_ex__(proto) -> (cls, state)")},
{"__reduce__", (PyCFunction)datetime_reduce, METH_NOARGS,
PyDoc_STR("__reduce__() -> (cls, state)")},
{NULL, NULL} {NULL, NULL}
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment