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

alexander.belopolsky python-checkins at python.org
Thu May 27 23:53:17 CEST 2010


Author: alexander.belopolsky
Date: Thu May 27 23:53:16 2010
New Revision: 81569

Log:
Merged revisions 81566 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r81566 | alexander.belopolsky | 2010-05-27 16:55:27 -0400 (Thu, 27 May 2010) | 3 lines
  
  Issue #7150: Raise OverflowError if the result of adding or subtracting
  timedelta from date or datetime falls outside of the MINYEAR:MAXYEAR range.
........


Modified:
   python/branches/release26-maint/   (props changed)
   python/branches/release26-maint/Lib/test/test_datetime.py
   python/branches/release26-maint/Misc/NEWS
   python/branches/release26-maint/Modules/datetimemodule.c

Modified: python/branches/release26-maint/Lib/test/test_datetime.py
==============================================================================
--- python/branches/release26-maint/Lib/test/test_datetime.py	(original)
+++ python/branches/release26-maint/Lib/test/test_datetime.py	Thu May 27 23:53:16 2010
@@ -709,15 +709,16 @@
     def test_overflow(self):
         tiny = self.theclass.resolution
 
-        dt = self.theclass.min + tiny
-        dt -= tiny  # no problem
-        self.assertRaises(OverflowError, dt.__sub__, tiny)
-        self.assertRaises(OverflowError, dt.__add__, -tiny)
-
-        dt = self.theclass.max - tiny
-        dt += tiny  # no problem
-        self.assertRaises(OverflowError, dt.__add__, tiny)
-        self.assertRaises(OverflowError, dt.__sub__, -tiny)
+        for delta in [tiny, timedelta(1), timedelta(2)]:
+            dt = self.theclass.min + delta
+            dt -= delta  # no problem
+            self.assertRaises(OverflowError, dt.__sub__, delta)
+            self.assertRaises(OverflowError, dt.__add__, -delta)
+
+            dt = self.theclass.max - delta
+            dt += delta  # no problem
+            self.assertRaises(OverflowError, dt.__add__, delta)
+            self.assertRaises(OverflowError, dt.__sub__, -delta)
 
     def test_fromtimestamp(self):
         import time

Modified: python/branches/release26-maint/Misc/NEWS
==============================================================================
--- python/branches/release26-maint/Misc/NEWS	(original)
+++ python/branches/release26-maint/Misc/NEWS	Thu May 27 23:53:16 2010
@@ -58,6 +58,9 @@
 Library
 -------
 
+- Issue #7150: Raise OverflowError if the result of adding or subtracting
+  timedelta from date or datetime falls outside of the MINYEAR:MAXYEAR range.
+
 - Issue #6662: Fix parsing of malformatted charref (&#bad;), patch written by
   Fredrik Håård
 

Modified: python/branches/release26-maint/Modules/datetimemodule.c
==============================================================================
--- python/branches/release26-maint/Modules/datetimemodule.c	(original)
+++ python/branches/release26-maint/Modules/datetimemodule.c	Thu May 27 23:53:16 2010
@@ -32,6 +32,7 @@
 
 #define MINYEAR 1
 #define MAXYEAR 9999
+#define MAXORDINAL 3652059 /* date(9999,12,31).toordinal() */
 
 /* Nine decimal digits is easy to communicate, and leaves enough room
  * so that two delta days can be added w/o fear of overflowing a signed
@@ -482,7 +483,7 @@
  * The input values must be such that the internals don't overflow.
  * The way this routine is used, we don't get close.
  */
-static void
+static int
 normalize_y_m_d(int *y, int *m, int *d)
 {
     int dim;            /* # of days in month */
@@ -536,11 +537,23 @@
         else {
             int ordinal = ymd_to_ord(*y, *m, 1) +
                                       *d - 1;
-            ord_to_ymd(ordinal, y, m, d);
+            if (ordinal < 1 || ordinal > MAXORDINAL) {
+                goto error;
+            } else {
+                ord_to_ymd(ordinal, y, m, d);
+                return 0;
+            }
         }
     }
     assert(*m > 0);
     assert(*d > 0);
+    if (MINYEAR <= *y && *y <= MAXYEAR)
+        return 0;
+ error:
+    PyErr_SetString(PyExc_OverflowError,
+            "date value out of range");
+    return -1;
+
 }
 
 /* Fiddle out-of-bounds months and days so that the result makes some kind
@@ -550,17 +563,7 @@
 static int
 normalize_date(int *year, int *month, int *day)
 {
-    int result;
-
-    normalize_y_m_d(year, month, day);
-    if (MINYEAR <= *year && *year <= MAXYEAR)
-        result = 0;
-    else {
-        PyErr_SetString(PyExc_OverflowError,
-                        "date value out of range");
-        result = -1;
-    }
-    return result;
+    return normalize_y_m_d(year, month, day);
 }
 
 /* Force all the datetime fields into range.  The parameters are both


More information about the Python-checkins mailing list