From 85710a40e7e9eab86060bedc3762ccf9ca8d26ca Mon Sep 17 00:00:00 2001 From: Ezio Melotti Date: Fri, 21 Sep 2012 17:26:35 +0300 Subject: =?UTF-8?q?#15421:=20fix=20an=20OverflowError=20in=20Calendar.iter?= =?UTF-8?q?monthdates()=20after=20datetime.MAXYEAR.=20=20Patch=20by=20C?= =?UTF-8?q?=C3=A9dric=20Krier.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Lib/calendar.py | 6 +++++- Lib/test/test_calendar.py | 6 ++++++ Misc/NEWS | 3 +++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Lib/calendar.py b/Lib/calendar.py index 0301d6b..3bbf399 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 d3093ac..948a119 100644 --- a/Lib/test/test_calendar.py +++ b/Lib/test/test_calendar.py @@ -5,6 +5,7 @@ from test import support from test.script_helper import assert_python_ok import time import locale +import datetime result_2004_text = """ 2004 @@ -265,6 +266,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 bebab93..74782aa 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -120,6 +120,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'. -- cgit v0.12