Skip to content

Commit

Permalink
pythongh-126476: Raise IllegalMonthError for Calendar.formatmonth m…
Browse files Browse the repository at this point in the history
…ethod when the input month is not corret
  • Loading branch information
Zheaoli committed Nov 6, 2024
1 parent 7587260 commit e8ed750
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
10 changes: 8 additions & 2 deletions Lib/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,14 @@ def weekday(year, month, day):
return Day(datetime.date(year, month, day).weekday())


def _validate_month(month):
if not 1 <= month <= 12:
raise IllegalMonthError(month)

def monthrange(year, month):
"""Return weekday of first day of month (0-6 ~ Mon-Sun)
and number of days (28-31) for year, month."""
if not 1 <= month <= 12:
raise IllegalMonthError(month)
_validate_month(month)
day1 = weekday(year, month, 1)
ndays = mdays[month] + (month == FEBRUARY and isleap(year))
return day1, ndays
Expand Down Expand Up @@ -370,6 +373,8 @@ def formatmonthname(self, theyear, themonth, width, withyear=True):
"""
Return a formatted month name.
"""
_validate_month(themonth)

s = month_name[themonth]
if withyear:
s = "%s %r" % (s, theyear)
Expand Down Expand Up @@ -500,6 +505,7 @@ def formatmonthname(self, theyear, themonth, withyear=True):
"""
Return a month name as a table row.
"""
_validate_month(themonth)
if withyear:
s = '%s %s' % (month_name[themonth], theyear)
else:
Expand Down
14 changes: 13 additions & 1 deletion Lib/test/test_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,11 @@ def test_formatmonth(self):
calendar.TextCalendar().formatmonth(0, 2),
result_0_02_text
)
def test_formatmonth_with_invalid_month(self):
with self.assertRaises(calendar.IllegalMonthError):
calendar.TextCalendar().formatmonth(2017, 13)
with self.assertRaises(calendar.IllegalMonthError):
calendar.TextCalendar().formatmonth(2017, -1)

def test_formatmonthname_with_year(self):
self.assertEqual(
Expand Down Expand Up @@ -1121,7 +1126,7 @@ def test__all__(self):
not_exported = {
'mdays', 'January', 'February', 'EPOCH',
'different_locale', 'c', 'prweek', 'week', 'format',
'formatstring', 'main', 'monthlen', 'prevmonth', 'nextmonth'}
'formatstring', 'main', 'monthlen', 'prevmonth', 'nextmonth', ""}
support.check__all__(self, calendar, not_exported=not_exported)


Expand Down Expand Up @@ -1149,6 +1154,13 @@ def test_formatmonth(self):
self.assertIn('class="text-center month"',
self.cal.formatmonth(2017, 5))

def test_formatmonth_with_invalid_month(self):
with self.assertRaises(calendar.IllegalMonthError):
self.cal.formatmonth(2017, 13)
with self.assertRaises(calendar.IllegalMonthError):
self.cal.formatmonth(2017, -1)


def test_formatweek(self):
weeks = self.cal.monthdays2calendar(2017, 5)
self.assertIn('class="wed text-nowrap"', self.cal.formatweek(weeks[0]))
Expand Down

0 comments on commit e8ed750

Please sign in to comment.