[Python-checkins] cpython (2.7): #15421: fix an OverflowError in Calendar.itermonthdates() after

ezio.melotti python-checkins at python.org
Fri Sep 21 16:29:41 CEST 2012


http://hg.python.org/cpython/rev/bfdf366a779a
changeset:   79088:bfdf366a779a
branch:      2.7
parent:      79083:7a125913a375
user:        Ezio Melotti <ezio.melotti at gmail.com>
date:        Fri Sep 21 17:26:35 2012 +0300
summary:
   #15421: fix an OverflowError in Calendar.itermonthdates() after datetime.MAXYEAR.  Patch by Cédric Krier.

files:
  Lib/calendar.py           |  6 +++++-
  Lib/test/test_calendar.py |  6 ++++++
  Misc/NEWS                 |  3 +++
  3 files changed, 14 insertions(+), 1 deletions(-)


diff --git a/Lib/calendar.py b/Lib/calendar.py
--- a/Lib/calendar.py
+++ b/Lib/calendar.py
@@ -161,7 +161,11 @@
         oneday = datetime.timedelta(days=1)
         while True:
             yield date
-            date += oneday
+            try:
+                date += oneday
+            except OverflowError:
+                # Adding one day could fail after datetime.MAXYEAR
+                break
             if date.month != month and date.weekday() == self.firstweekday:
                 break
 
diff --git a/Lib/test/test_calendar.py b/Lib/test/test_calendar.py
--- a/Lib/test/test_calendar.py
+++ b/Lib/test/test_calendar.py
@@ -3,6 +3,7 @@
 
 from test import test_support
 import locale
+import datetime
 
 
 result_2004_text = """
@@ -262,6 +263,11 @@
         new_october = calendar.TextCalendar().formatmonthname(2010, 10, 10)
         self.assertEquals(old_october, new_october)
 
+    def test_itermonthdates(self):
+        # ensure itermonthdates doesn't overflow after datetime.MAXYEAR
+        # see #15421
+        list(calendar.Calendar().itermonthdates(datetime.MAXYEAR, 12))
+
 
 class MonthCalendarTestCase(unittest.TestCase):
     def setUp(self):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -103,6 +103,9 @@
 Library
 -------
 
+- Issue #15421: fix an OverflowError in Calendar.itermonthdates() after
+  datetime.MAXYEAR.  Patch by Cédric Krier.
+
 - Issue #15970: xml.etree.ElementTree now serializes correctly the empty HTML
   elements 'meta' and 'param'.
 

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


More information about the Python-checkins mailing list