diff -r bb11a3aef5b0 Lib/calendar.py --- a/Lib/calendar.py Fri Sep 23 11:32:40 2016 +0200 +++ b/Lib/calendar.py Fri Sep 23 21:11:24 2016 +0800 @@ -124,6 +124,18 @@ ndays = mdays[month] + (month == February and isleap(year)) return day1, ndays +class Dummy: + """ + Dummy class to handle extreme dates that can't be represented by + datetime.date but we still need them to work properly. + """ + + def __init__(self, weekday): + self.month = 13 + self._weekday = weekday + + def weekday(self): + return self._weekday class Calendar(object): """ @@ -159,7 +171,13 @@ date = datetime.date(year, month, 1) # Go back to the beginning of the week days = (date.weekday() - self.firstweekday) % 7 - date -= datetime.timedelta(days=days) + try: + date -= datetime.timedelta(days=days) + except OverflowError: + # Minus days could fail before datetime.MINYEAR + weekday = date.weekday() + for i in range(days, 0, -1): + yield Dummy((weekday - i) % 7) oneday = datetime.timedelta(days=1) while True: yield date @@ -167,6 +185,11 @@ date += oneday except OverflowError: # Adding one day could fail after datetime.MAXYEAR + weekday = date.weekday() + for i in range(1, 7): + if (weekday + i) % 7 == self.firstweekday: + break + yield Dummy((weekday + i) % 7) break if date.month != month and date.weekday() == self.firstweekday: break