diff -r 0308f97616a3 Lib/calendar.py --- a/Lib/calendar.py Fri Sep 16 13:54:05 2016 -0400 +++ b/Lib/calendar.py Mon Sep 26 18:27:00 2016 -0400 @@ -176,22 +176,22 @@ Like itermonthdates(), but will yield (day number, weekday number) tuples. For days outside the specified month the day number is 0. """ - for date in self.itermonthdates(year, month): - if date.month != month: - yield (0, date.weekday()) - else: - yield (date.day, date.weekday()) + for i, d in enumerate(self.itermonthdays(year, month), self.firstweekday): + yield d, i % 7 def itermonthdays(self, year, month): """ Like itermonthdates(), but will yield day numbers. For days outside the specified month the day number is 0. """ - for date in self.itermonthdates(year, month): - if date.month != month: - yield 0 - else: - yield date.day + day1, ndays = monthrange(year, month) + days_before = (day1 - self.firstweekday) % 7 + for _ in range(days_before): + yield 0 + yield from range(1, ndays + 1) + days_after = (self.firstweekday - day1 - ndays) % 7 + for _ in range(days_after): + yield 0 def monthdatescalendar(self, year, month): """