From: Ezio Melotti Date: Fri, 21 Sep 2012 14:26:35 +0000 (+0300) Subject: #15421: fix an OverflowError in Calendar.itermonthdates() after datetime.MAXYEAR... X-Git-Tag: v2.7.5~109^2~290 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=cadff70ba5a761e5899d09c918ad6c87cfb07b7d;p=python #15421: fix an OverflowError in Calendar.itermonthdates() after datetime.MAXYEAR. Patch by Cédric Krier. --- diff --git a/Lib/calendar.py b/Lib/calendar.py index 3106ef273e..232957829f 100644 --- a/Lib/calendar.py +++ b/Lib/calendar.py @@ -161,7 +161,11 @@ class Calendar(object): 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 index 2a56268810..5d6549c6cd 100644 --- a/Lib/test/test_calendar.py +++ b/Lib/test/test_calendar.py @@ -3,6 +3,7 @@ import unittest from test import test_support import locale +import datetime result_2004_text = """ @@ -262,6 +263,11 @@ class CalendarTestCase(unittest.TestCase): 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 index e734998104..84d4bd1910 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -103,6 +103,9 @@ Core and Builtins 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'.