Skip to content

Commit 3be7498

Browse files
Zheaoliethanfurman
andauthored
gh-126476: Raise IllegalMonthError for calendar.formatmonth() when the input month is not correct (GH-126484)
Co-authored-by: Ethan Furman <[email protected]>
1 parent a2c180f commit 3be7498

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

Lib/calendar.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
error = ValueError
2828

2929
# Exceptions raised for bad input
30-
class IllegalMonthError(ValueError):
30+
# This is trick for backward compatibility. Since 3.13, we will raise IllegalMonthError instead of
31+
# IndexError for bad month number(out of 1-12). But we can't remove IndexError for backward compatibility.
32+
class IllegalMonthError(ValueError, IndexError):
3133
def __init__(self, month):
3234
self.month = month
3335
def __str__(self):
@@ -158,11 +160,14 @@ def weekday(year, month, day):
158160
return Day(datetime.date(year, month, day).weekday())
159161

160162

163+
def _validate_month(month):
164+
if not 1 <= month <= 12:
165+
raise IllegalMonthError(month)
166+
161167
def monthrange(year, month):
162168
"""Return weekday of first day of month (0-6 ~ Mon-Sun)
163169
and number of days (28-31) for year, month."""
164-
if not 1 <= month <= 12:
165-
raise IllegalMonthError(month)
170+
_validate_month(month)
166171
day1 = weekday(year, month, 1)
167172
ndays = mdays[month] + (month == FEBRUARY and isleap(year))
168173
return day1, ndays
@@ -370,6 +375,8 @@ def formatmonthname(self, theyear, themonth, width, withyear=True):
370375
"""
371376
Return a formatted month name.
372377
"""
378+
_validate_month(themonth)
379+
373380
s = month_name[themonth]
374381
if withyear:
375382
s = "%s %r" % (s, theyear)
@@ -500,6 +507,7 @@ def formatmonthname(self, theyear, themonth, withyear=True):
500507
"""
501508
Return a month name as a table row.
502509
"""
510+
_validate_month(themonth)
503511
if withyear:
504512
s = '%s %s' % (month_name[themonth], theyear)
505513
else:
@@ -786,6 +794,8 @@ def main(args=None):
786794
if options.month is None:
787795
optdict["c"] = options.spacing
788796
optdict["m"] = options.months
797+
if options.month is not None:
798+
_validate_month(options.month)
789799
if options.year is None:
790800
result = cal.formatyear(datetime.date.today().year, **optdict)
791801
elif options.month is None:

Lib/test/test_calendar.py

+13-1
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]))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Raise :class:`calendar.IllegalMonthError` (now a subclass of :class:`IndexError`) for :func:`calendar.month`
2+
when the input month is not correct.

0 commit comments

Comments
 (0)