[Python-checkins] r87919 - in python/branches/py3k: Lib/test/test_time.py Modules/timemodule.c

alexander.belopolsky python-checkins at python.org
Tue Jan 11 02:21:25 CET 2011


Author: alexander.belopolsky
Date: Tue Jan 11 02:21:25 2011
New Revision: 87919

Log:
Issue #1726687: time.mktime() will now correctly compute value one
second before epoch.  Original patch by Peter Wang, reported by Martin
Blais.


Modified:
   python/branches/py3k/Lib/test/test_time.py
   python/branches/py3k/Modules/timemodule.c

Modified: python/branches/py3k/Lib/test/test_time.py
==============================================================================
--- python/branches/py3k/Lib/test/test_time.py	(original)
+++ python/branches/py3k/Lib/test/test_time.py	Tue Jan 11 02:21:25 2011
@@ -339,6 +339,19 @@
         self.assertEqual(self.yearstr(-1234), '-1234')
         self.assertEqual(self.yearstr(-123456), '-123456')
 
+
+    def test_mktime(self):
+        # Issue #1726687
+        for t in (-2, -1, 0, 1):
+            try:
+                tt = time.localtime(t)
+            except (OverflowError, ValueError):
+                pass
+            self.assertEqual(time.mktime(tt), t)
+        # Hopefully year = -1 is enough to make OS mktime fail
+        self.assertRaises(OverflowError, time.mktime,
+                          (-1, 1, 1, 0, 0, 0, -1, -1, -1))
+
 class TestAsctimeAccept2dYear(_TestAsctimeYear, _Test2dYear):
     pass
 

Modified: python/branches/py3k/Modules/timemodule.c
==============================================================================
--- python/branches/py3k/Modules/timemodule.c	(original)
+++ python/branches/py3k/Modules/timemodule.c	Tue Jan 11 02:21:25 2011
@@ -694,8 +694,11 @@
     time_t tt;
     if (!gettmarg(tup, &buf))
         return NULL;
+    buf.tm_wday = -1;  /* sentinel; original value ignored */
     tt = mktime(&buf);
-    if (tt == (time_t)(-1)) {
+    /* Return value of -1 does not necessarily mean an error, but tm_wday
+     * cannot remain set to -1 if mktime succedded. */
+    if (tt == (time_t)(-1) && buf.tm_wday == -1) {
         PyErr_SetString(PyExc_OverflowError,
                         "mktime argument out of range");
         return NULL;


More information about the Python-checkins mailing list