[Python-checkins] cpython (3.4): Closes issue #23600: Wrong results from tzinfo.fromutc().

alexander.belopolsky python-checkins at python.org
Mon Sep 28 04:35:32 CEST 2015


https://hg.python.org/cpython/rev/ff68705c56a8
changeset:   98334:ff68705c56a8
branch:      3.4
parent:      98327:c5ee47b28a50
user:        Alexander Belopolsky <alexander.belopolsky at gmail.com>
date:        Sun Sep 27 21:41:55 2015 -0400
summary:
  Closes issue #23600: Wrong results from tzinfo.fromutc().

files:
  Lib/test/datetimetester.py |  23 +++++++++++++++++++++++
  Misc/NEWS                  |   3 +++
  Modules/_datetimemodule.c  |   2 +-
  3 files changed, 27 insertions(+), 1 deletions(-)


diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py
--- a/Lib/test/datetimetester.py
+++ b/Lib/test/datetimetester.py
@@ -180,6 +180,29 @@
                 self.assertEqual(derived.utcoffset(None), offset)
                 self.assertEqual(derived.tzname(None), oname)
 
+    def test_issue23600(self):
+        DSTDIFF = DSTOFFSET = timedelta(hours=1)
+
+        class UKSummerTime(tzinfo):
+            """Simple time zone which pretends to always be in summer time, since
+                that's what shows the failure.
+            """
+
+            def utcoffset(self, dt):
+                return DSTOFFSET
+
+            def dst(self, dt):
+                return DSTDIFF
+
+            def tzname(self, dt):
+                return 'UKSummerTime'
+
+        tz = UKSummerTime()
+        u = datetime(2014, 4, 26, 12, 1, tzinfo=tz)
+        t = tz.fromutc(u)
+        self.assertEqual(t - t.utcoffset(), u)
+
+
 class TestTimeZone(unittest.TestCase):
 
     def setUp(self):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -81,6 +81,9 @@
 Library
 -------
 
+- Issue #23600: Default implementation of tzinfo.fromutc() was returning
+  wrong results in some cases. 
+
 - Prevent overflow in _Unpickler_Read.
 
 - Issue #25047: The XML encoding declaration written by Element Tree now
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -3040,7 +3040,7 @@
         goto Fail;
     if (dst == Py_None)
         goto Inconsistent;
-    if (delta_bool(delta) != 0) {
+    if (delta_bool((PyDateTime_Delta *)dst) != 0) {
         PyObject *temp = result;
         result = add_datetime_timedelta((PyDateTime_DateTime *)result,
                                         (PyDateTime_Delta *)dst, 1);

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list