[Python-checkins] r82184 - in python/branches/py3k: Lib/test/test_datetime.py Modules/datetimemodule.c Python/Python-ast.c

alexander.belopolsky python-checkins at python.org
Wed Jun 23 23:40:15 CEST 2010


Author: alexander.belopolsky
Date: Wed Jun 23 23:40:15 2010
New Revision: 82184

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

Modified:
   python/branches/py3k/Lib/test/test_datetime.py
   python/branches/py3k/Modules/datetimemodule.c
   python/branches/py3k/Python/Python-ast.c

Modified: python/branches/py3k/Lib/test/test_datetime.py
==============================================================================
--- python/branches/py3k/Lib/test/test_datetime.py	(original)
+++ python/branches/py3k/Lib/test/test_datetime.py	Wed Jun 23 23:40:15 2010
@@ -19,8 +19,8 @@
 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 @@
     def test_pickling_subclass(self):
         # Make sure we can pickle/unpickle an instance of a subclass.
         offset = timedelta(minutes=-300)
-        orig = PicklableFixedOffset(offset, 'cookie')
-        self.assertIsInstance(orig, tzinfo)
-        self.assertTrue(type(orig) is PicklableFixedOffset)
-        self.assertEqual(orig.utcoffset(None), offset)
-        self.assertEqual(orig.tzname(None), 'cookie')
-        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.assertEqual(derived.utcoffset(None), offset)
-            self.assertEqual(derived.tzname(None), '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.assertIs(type(orig), otype)
+            self.assertEqual(orig.utcoffset(None), offset)
+            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.assertIs(type(derived), otype)
+                self.assertEqual(derived.utcoffset(None), offset)
+                self.assertEqual(derived.tzname(None), oname)
 
 class TestTimeZone(unittest.TestCase):
 

Modified: python/branches/py3k/Modules/datetimemodule.c
==============================================================================
--- python/branches/py3k/Modules/datetimemodule.c	(original)
+++ python/branches/py3k/Modules/datetimemodule.c	Wed Jun 23 23:40:15 2010
@@ -3469,6 +3469,14 @@
     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 @@
     {"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}
 };
 

Modified: python/branches/py3k/Python/Python-ast.c
==============================================================================
--- python/branches/py3k/Python/Python-ast.c	(original)
+++ python/branches/py3k/Python/Python-ast.c	Wed Jun 23 23:40:15 2010
@@ -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 @@
             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;


More information about the Python-checkins mailing list