diff --git a/Lib/calendar.py b/Lib/calendar.py index 0301d6b5d6c..3bbf399649f 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 e0f63997d5f..e594e01be0e 100644 --- a/Lib/test/test_calendar.py +++ b/Lib/test/test_calendar.py @@ -6,6 +6,7 @@ from test.script_helper import assert_python_ok import time import locale import sys +import datetime result_2004_01_text = """ January 2004 @@ -464,6 +465,11 @@ class CalendarTestCase(unittest.TestCase): new_october = calendar.TextCalendar().formatmonthname(2010, 10, 10) self.assertEqual(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 5d6ca0f923e..abe8eb8028f 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -36,6 +36,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'.