[Python-checkins] r82617 - in python/branches/py3k: Lib/test/test_datetime.py Misc/NEWS Modules/datetimemodule.c

alexander.belopolsky python-checkins at python.org
Wed Jul 7 01:19:45 CEST 2010


Author: alexander.belopolsky
Date: Wed Jul  7 01:19:45 2010
New Revision: 82617

Log:
Issue #9000: datetime.timezone objects now have eval-friendly repr.

Modified:
   python/branches/py3k/Lib/test/test_datetime.py
   python/branches/py3k/Misc/NEWS
   python/branches/py3k/Modules/datetimemodule.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 Jul  7 01:19:45 2010
@@ -153,6 +153,15 @@
                    timezone.min, timezone.max]:
             self.assertEqual(str(tz), tz.tzname(None))
 
+    def test_repr(self):
+        import datetime
+        for tz in [self.ACDT, self.EST, timezone.utc,
+                   timezone.min, timezone.max]:
+            # test round-trip
+            tzrep = repr(tz)
+            self.assertEqual(tz, eval(tzrep))
+
+
     def test_class_members(self):
         limit = timedelta(hours=23, minutes=59)
         self.assertEqual(timezone.utc.utcoffset(None), ZERO)

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Wed Jul  7 01:19:45 2010
@@ -1394,6 +1394,8 @@
 Extension Modules
 -----------------
 
+- Issue #9000: datetime.timezone objects now have eval-friendly repr.
+
 - In the math module, correctly lookup __trunc__, __ceil__, and __floor__ as
   special methods.
 

Modified: python/branches/py3k/Modules/datetimemodule.c
==============================================================================
--- python/branches/py3k/Modules/datetimemodule.c	(original)
+++ python/branches/py3k/Modules/datetimemodule.c	Wed Jul  7 01:19:45 2010
@@ -780,6 +780,8 @@
     PyObject *name;
 } PyDateTime_TimeZone;
 
+PyObject *PyDateTime_TimeZone_UTC;
+
 /* Create new timezone instance checking offset range.  This
    function does not check the name argument.  Caller must assure
    that offset is a timedelta instance and name is either NULL
@@ -3380,6 +3382,24 @@
 }
 
 static PyObject *
+timezone_repr(PyDateTime_TimeZone *self)
+{
+    /* Note that although timezone is not subclassable, it is convenient
+       to use Py_TYPE(self)->tp_name here. */
+    const char *type_name = Py_TYPE(self)->tp_name;
+
+    if (((PyObject *)self) == PyDateTime_TimeZone_UTC)
+        return PyUnicode_FromFormat("%s.utc", type_name);
+
+    if (self->name == NULL)
+        return PyUnicode_FromFormat("%s(%R)", type_name, self->offset);
+
+    return PyUnicode_FromFormat("%s(%R, %R)", type_name, self->offset,
+                                self->name);
+}
+
+
+static PyObject *
 timezone_str(PyDateTime_TimeZone *self)
 {
     char buf[10];
@@ -3505,7 +3525,7 @@
     0,                                /* tp_getattr */
     0,                                /* tp_setattr */
     0,                                /* tp_reserved */
-    0,                                /* tp_repr */
+    (reprfunc)timezone_repr,          /* tp_repr */
     0,                                /* tp_as_number */
     0,                                /* tp_as_sequence */
     0,                                /* tp_as_mapping */
@@ -5260,7 +5280,7 @@
     Py_DECREF(delta);
     if (x == NULL || PyDict_SetItemString(d, "utc", x) < 0)
         return NULL;
-    Py_DECREF(x);
+    PyDateTime_TimeZone_UTC = x;
 
     delta = new_delta(-1, 60, 0, 1); /* -23:59 */
     if (delta == NULL)


More information about the Python-checkins mailing list