Issue #9051: Instances of timezone class can now be pickled.

This commit is contained in:
Alexander Belopolsky 2010-06-23 21:40:15 +00:00
parent b7e1010096
commit 1b7046b99e
3 changed files with 32 additions and 16 deletions

View File

@ -19,8 +19,8 @@ from datetime import timezone
from datetime import date, datetime
import time as _time
pickle_choices = [(pickle, pickle, proto) for proto in range(3)]
assert len(pickle_choices) == 3
pickle_choices = [(pickle, pickle, proto) for proto in range(4)]
assert len(pickle_choices) == 4
# An arbitrary collection of objects of non-datetime types, for testing
# mixed-type comparisons.
@ -122,18 +122,23 @@ class TestTZInfo(unittest.TestCase):
def test_pickling_subclass(self):
# Make sure we can pickle/unpickle an instance of a subclass.
offset = timedelta(minutes=-300)
orig = PicklableFixedOffset(offset, 'cookie')
for otype, args in [
(PicklableFixedOffset, (offset, 'cookie')),
(timezone, (offset,)),
(timezone, (offset, "EST"))]:
orig = otype(*args)
oname = orig.tzname(None)
self.assertIsInstance(orig, tzinfo)
self.assertTrue(type(orig) is PicklableFixedOffset)
self.assertIs(type(orig), otype)
self.assertEqual(orig.utcoffset(None), offset)
self.assertEqual(orig.tzname(None), 'cookie')
self.assertEqual(orig.tzname(None), oname)
for pickler, unpickler, proto in pickle_choices:
green = pickler.dumps(orig, proto)
derived = unpickler.loads(green)
self.assertIsInstance(derived, tzinfo)
self.assertTrue(type(derived) is PicklableFixedOffset)
self.assertIs(type(derived), otype)
self.assertEqual(derived.utcoffset(None), offset)
self.assertEqual(derived.tzname(None), 'cookie')
self.assertEqual(derived.tzname(None), oname)
class TestTimeZone(unittest.TestCase):

View File

@ -3469,6 +3469,14 @@ timezone_fromutc(PyDateTime_TimeZone *self, PyDateTime_DateTime *dt)
return add_datetime_timedelta(dt, (PyDateTime_Delta *)self->offset, 1);
}
static PyObject *
timezone_getinitargs(PyDateTime_TimeZone *self)
{
if (self->name == NULL)
return Py_BuildValue("(O)", self->offset);
return Py_BuildValue("(OO)", self->offset, self->name);
}
static PyMethodDef timezone_methods[] = {
{"tzname", (PyCFunction)timezone_tzname, METH_O,
PyDoc_STR("If name is specified when timezone is created, returns the name."
@ -3483,6 +3491,9 @@ static PyMethodDef timezone_methods[] = {
{"fromutc", (PyCFunction)timezone_fromutc, METH_O,
PyDoc_STR("datetime in UTC -> datetime in local time.")},
{"__getinitargs__", (PyCFunction)timezone_getinitargs, METH_NOARGS,
PyDoc_STR("pickle support")},
{NULL, NULL}
};

View File

@ -2,7 +2,7 @@
/*
__version__ 73626.
__version__ 82163.
This module must be committed separately after each AST grammar change;
The __version__ number is set to the revision number of the commit
@ -6773,7 +6773,7 @@ PyInit__ast(void)
NULL;
if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
return NULL;
if (PyModule_AddStringConstant(m, "__version__", "73626") < 0)
if (PyModule_AddStringConstant(m, "__version__", "82163") < 0)
return NULL;
if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return
NULL;