Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b57f8f0

Browse files
committed
There's no good reason for datetime objects to expose __getstate__()
anymore either, so don't. This also allows to get rid of obscure code making __getnewargs__ identical to __getstate__ (hmm ... hope there wasn't more to this than I realize!).
1 parent 874d9bc commit b57f8f0

3 files changed

Lines changed: 6 additions & 66 deletions

File tree

Lib/test/test_datetime.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,6 @@ def test_hash_equality(self):
278278
def test_pickling(self):
279279
args = 12, 34, 56
280280
orig = timedelta(*args)
281-
state = orig.__getstate__()
282-
self.assertEqual(args, state)
283281
for pickler, unpickler, proto in pickle_choices:
284282
green = pickler.dumps(orig, proto)
285283
derived = unpickler.loads(green)
@@ -832,8 +830,6 @@ def test_timetuple(self):
832830
def test_pickling(self):
833831
args = 6, 7, 23
834832
orig = self.theclass(*args)
835-
state = orig.__getstate__()
836-
self.assertEqual(state, ('\x00\x06\x07\x17',), self.theclass)
837833
for pickler, unpickler, proto in pickle_choices:
838834
green = pickler.dumps(orig, proto)
839835
derived = unpickler.loads(green)
@@ -1186,8 +1182,6 @@ def test_computations(self):
11861182
def test_pickling(self):
11871183
args = 6, 7, 23, 20, 59, 1, 64**2
11881184
orig = self.theclass(*args)
1189-
state = orig.__getstate__()
1190-
self.assertEqual(state, ('\x00\x06\x07\x17\x14\x3b\x01\x00\x10\x00',))
11911185
for pickler, unpickler, proto in pickle_choices:
11921186
green = pickler.dumps(orig, proto)
11931187
derived = unpickler.loads(green)
@@ -1567,8 +1561,6 @@ def test_resolution_info(self):
15671561
def test_pickling(self):
15681562
args = 20, 59, 16, 64**2
15691563
orig = self.theclass(*args)
1570-
state = orig.__getstate__()
1571-
self.assertEqual(state, ('\x14\x3b\x10\x00\x10\x00',))
15721564
for pickler, unpickler, proto in pickle_choices:
15731565
green = pickler.dumps(orig, proto)
15741566
derived = unpickler.loads(green)
@@ -1877,8 +1869,6 @@ def test_pickling(self):
18771869
# Try one without a tzinfo.
18781870
args = 20, 59, 16, 64**2
18791871
orig = self.theclass(*args)
1880-
state = orig.__getstate__()
1881-
self.assertEqual(state, ('\x14\x3b\x10\x00\x10\x00',))
18821872
for pickler, unpickler, proto in pickle_choices:
18831873
green = pickler.dumps(orig, proto)
18841874
derived = unpickler.loads(green)
@@ -2080,8 +2070,6 @@ def test_pickling(self):
20802070
# Try one without a tzinfo.
20812071
args = 6, 7, 23, 20, 59, 1, 64**2
20822072
orig = self.theclass(*args)
2083-
state = orig.__getstate__()
2084-
self.assertEqual(state, ('\x00\x06\x07\x17\x14\x3b\x01\x00\x10\x00',))
20852073
for pickler, unpickler, proto in pickle_choices:
20862074
green = pickler.dumps(orig, proto)
20872075
derived = unpickler.loads(green)

Misc/NEWS

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ Extension modules
131131

132132
The pickle format of date, time and datetime objects has changed
133133
completely. The undocumented pickler and unpickler functions no
134-
longer exist. The undocumented __setstate__() methods no longer
135-
exist either.
134+
longer exist. The undocumented __setstate__() and __getstate__()
135+
methods no longer exist either.
136136

137137
Library
138138
-------

Modules/datetimemodule.c

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,6 +1949,7 @@ delta_str(PyDateTime_Delta *self)
19491949

19501950
/* Pickle support, a simple use of __reduce__. */
19511951

1952+
/* __getstate__ isn't exposed */
19521953
static PyObject *
19531954
delta_getstate(PyDateTime_Delta *self)
19541955
{
@@ -1979,9 +1980,6 @@ static PyMemberDef delta_members[] = {
19791980
};
19801981

19811982
static PyMethodDef delta_methods[] = {
1982-
{"__getstate__", (PyCFunction)delta_getstate, METH_NOARGS,
1983-
PyDoc_STR("__getstate__() -> state")},
1984-
19851983
{"__reduce__", (PyCFunction)delta_reduce, METH_NOARGS,
19861984
PyDoc_STR("__reduce__() -> (cls, state)")},
19871985

@@ -2525,6 +2523,7 @@ date_weekday(PyDateTime_Date *self)
25252523

25262524
/* Pickle support, a simple use of __reduce__. */
25272525

2526+
/* __getstate__ isn't exposed */
25282527
static PyObject *
25292528
date_getstate(PyDateTime_Date *self)
25302529
{
@@ -2591,9 +2590,6 @@ static PyMethodDef date_methods[] = {
25912590
{"replace", (PyCFunction)date_replace, METH_KEYWORDS,
25922591
PyDoc_STR("Return date with new specified fields.")},
25932592

2594-
{"__getstate__", (PyCFunction)date_getstate, METH_NOARGS,
2595-
PyDoc_STR("__getstate__() -> state")},
2596-
25972593
{"__reduce__", (PyCFunction)date_reduce, METH_NOARGS,
25982594
PyDoc_STR("__reduce__() -> (cls, state)")},
25992595

@@ -3340,6 +3336,7 @@ time_nonzero(PyDateTime_Time *self)
33403336
/* Let basestate be the non-tzinfo data string.
33413337
* If tzinfo is None, this returns (basestate,), else (basestate, tzinfo).
33423338
* So it's a tuple in any (non-error) case.
3339+
* __getstate__ isn't exposed.
33433340
*/
33443341
static PyObject *
33453342
time_getstate(PyDateTime_Time *self)
@@ -3386,9 +3383,6 @@ static PyMethodDef time_methods[] = {
33863383
{"replace", (PyCFunction)time_replace, METH_KEYWORDS,
33873384
PyDoc_STR("Return time with new specified fields.")},
33883385

3389-
{"__getstate__", (PyCFunction)time_getstate, METH_NOARGS,
3390-
PyDoc_STR("__getstate__() -> state")},
3391-
33923386
{"__reduce__", (PyCFunction)time_reduce, METH_NOARGS,
33933387
PyDoc_STR("__reduce__() -> (cls, state)")},
33943388

@@ -4340,6 +4334,7 @@ datetime_utctimetuple(PyDateTime_DateTime *self)
43404334
/* Let basestate be the non-tzinfo data string.
43414335
* If tzinfo is None, this returns (basestate,), else (basestate, tzinfo).
43424336
* So it's a tuple in any (non-error) case.
4337+
* __getstate__ isn't exposed.
43434338
*/
43444339
static PyObject *
43454340
datetime_getstate(PyDateTime_DateTime *self)
@@ -4431,9 +4426,6 @@ static PyMethodDef datetime_methods[] = {
44314426
{"astimezone", (PyCFunction)datetime_astimezone, METH_KEYWORDS,
44324427
PyDoc_STR("tz -> convert to local time in new timezone tz\n")},
44334428

4434-
{"__getstate__", (PyCFunction)datetime_getstate, METH_NOARGS,
4435-
PyDoc_STR("__getstate__() -> state")},
4436-
44374429
{"__reduce__", (PyCFunction)datetime_reduce, METH_NOARGS,
44384430
PyDoc_STR("__reduce__() -> (cls, state)")},
44394431

@@ -4530,46 +4522,6 @@ initdatetime(void)
45304522
if (PyType_Ready(&PyDateTime_TZInfoType) < 0)
45314523
return;
45324524

4533-
/* Make __getnewargs__ a true alias for __getstate__ */
4534-
{
4535-
PyObject *d, *f;
4536-
4537-
d = PyDateTime_DateType.tp_dict;
4538-
f = PyDict_GetItemString(d, "__getstate__");
4539-
if (f != NULL) {
4540-
if (PyDict_SetItemString(d, "__getnewargs__", f) < 0)
4541-
return;
4542-
}
4543-
4544-
d = PyDateTime_DateTimeType.tp_dict;
4545-
f = PyDict_GetItemString(d, "__getstate__");
4546-
if (f != NULL) {
4547-
if (PyDict_SetItemString(d, "__getnewargs__", f) < 0)
4548-
return;
4549-
}
4550-
4551-
d = PyDateTime_DeltaType.tp_dict;
4552-
f = PyDict_GetItemString(d, "__getstate__");
4553-
if (f != NULL) {
4554-
if (PyDict_SetItemString(d, "__getnewargs__", f) < 0)
4555-
return;
4556-
}
4557-
4558-
d = PyDateTime_TimeType.tp_dict;
4559-
f = PyDict_GetItemString(d, "__getstate__");
4560-
if (f != NULL) {
4561-
if (PyDict_SetItemString(d, "__getnewargs__", f) < 0)
4562-
return;
4563-
}
4564-
4565-
d = PyDateTime_TZInfoType.tp_dict;
4566-
f = PyDict_GetItemString(d, "__getstate__");
4567-
if (f != NULL) {
4568-
if (PyDict_SetItemString(d, "__getnewargs__", f) < 0)
4569-
return;
4570-
}
4571-
}
4572-
45734525
/* timedelta values */
45744526
d = PyDateTime_DeltaType.tp_dict;
45754527

0 commit comments

Comments
 (0)