Skip to content

Commit e8ed750

Browse files
committed
pythongh-126476: Raise IllegalMonthError for Calendar.formatmonth method when the input month is not corret
1 parent 7587260 commit e8ed750

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

Lib/calendar.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,14 @@ def weekday(year, month, day):
158158
return Day(datetime.date(year, month, day).weekday())
159159

160160

161+
def _validate_month(month):
162+
if not 1 <= month <= 12:
163+
raise IllegalMonthError(month)
164+
161165
def monthrange(year, month):
162166
"""Return weekday of first day of month (0-6 ~ Mon-Sun)
163167
and number of days (28-31) for year, month."""
164-
if not 1 <= month <= 12:
165-
raise IllegalMonthError(month)
168+
_validate_month(month)
166169
day1 = weekday(year, month, 1)
167170
ndays = mdays[month] + (month == FEBRUARY and isleap(year))
168171
return day1, ndays
@@ -370,6 +373,8 @@ def formatmonthname(self, theyear, themonth, width, withyear=True):
370373
"""
371374
Return a formatted month name.
372375
"""
376+
_validate_month(themonth)
377+
373378
s = month_name[themonth]
374379
if withyear:
375380
s = "%s %r" % (s, theyear)
@@ -500,6 +505,7 @@ def formatmonthname(self, theyear, themonth, withyear=True):
500505
"""
501506
Return a month name as a table row.
502507
"""
508+
_validate_month(themonth)
503509
if withyear:
504510
s = '%s %s' % (month_name[themonth], theyear)
505511
else:

Lib/test/test_calendar.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,11 @@ def test_formatmonth(self):
457457
calendar.TextCalendar().formatmonth(0, 2),
458458
result_0_02_text
459459
)
460+
def test_formatmonth_with_invalid_month(self):
461+
with self.assertRaises(calendar.IllegalMonthError):
462+
calendar.TextCalendar().formatmonth(2017, 13)
463+
with self.assertRaises(calendar.IllegalMonthError):
464+
calendar.TextCalendar().formatmonth(2017, -1)
460465

461466
def test_formatmonthname_with_year(self):
462467
self.assertEqual(
@@ -1121,7 +1126,7 @@ def test__all__(self):
11211126
not_exported = {
11221127
'mdays', 'January', 'February', 'EPOCH',
11231128
'different_locale', 'c', 'prweek', 'week', 'format',
1124-
'formatstring', 'main', 'monthlen', 'prevmonth', 'nextmonth'}
1129+
'formatstring', 'main', 'monthlen', 'prevmonth', 'nextmonth', ""}
11251130
support.check__all__(self, calendar, not_exported=not_exported)
11261131

11271132

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

1157+
def test_formatmonth_with_invalid_month(self):
1158+
with self.assertRaises(calendar.IllegalMonthError):
1159+
self.cal.formatmonth(2017, 13)
1160+
with self.assertRaises(calendar.IllegalMonthError):
1161+
self.cal.formatmonth(2017, -1)
1162+
1163+
11521164
def test_formatweek(self):
11531165
weeks = self.cal.monthdays2calendar(2017, 5)
11541166
self.assertIn('class="wed text-nowrap"', self.cal.formatweek(weeks[0]))

0 commit comments

Comments
 (0)