Skip to content

Commit cdc1dff

Browse files
miss-islingtonZheaoliethanfurman
authored
[3.12] gh-126476: Raise IllegalMonthError for calendar.formatmonth() when the input month is not correct (GH-126484) (GH-126878)
gh-126476: Raise IllegalMonthError for calendar.formatmonth() when the input month is not correct (GH-126484) (cherry picked from commit 3be7498) Co-authored-by: Nadeshiko Manju <[email protected]> Co-authored-by: Ethan Furman <[email protected]>
1 parent 797a632 commit cdc1dff

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
@@ -28,7 +28,9 @@
2828
error = ValueError
2929

3030
# Exceptions raised for bad input
31-
class IllegalMonthError(ValueError):
31+
# This is trick for backward compatibility. Since 3.13, we will raise IllegalMonthError instead of
32+
# IndexError for bad month number(out of 1-12). But we can't remove IndexError for backward compatibility.
33+
class IllegalMonthError(ValueError, IndexError):
3234
def __init__(self, month):
3335
self.month = month
3436
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:
@@ -781,6 +789,8 @@ def main(args):
781789
if options.month is None:
782790
optdict["c"] = options.spacing
783791
optdict["m"] = options.months
792+
if options.month is not None:
793+
_validate_month(options.month)
784794
if options.year is None:
785795
result = cal.formatyear(datetime.date.today().year, **optdict)
786796
elif options.month is None:

Lib/test/test_calendar.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,11 @@ def test_formatmonth(self):
456456
calendar.TextCalendar().formatmonth(0, 2),
457457
result_0_02_text
458458
)
459+
def test_formatmonth_with_invalid_month(self):
460+
with self.assertRaises(calendar.IllegalMonthError):
461+
calendar.TextCalendar().formatmonth(2017, 13)
462+
with self.assertRaises(calendar.IllegalMonthError):
463+
calendar.TextCalendar().formatmonth(2017, -1)
459464

460465
def test_formatmonthname_with_year(self):
461466
self.assertEqual(
@@ -972,7 +977,7 @@ def test__all__(self):
972977
not_exported = {
973978
'mdays', 'January', 'February', 'EPOCH',
974979
'different_locale', 'c', 'prweek', 'week', 'format',
975-
'formatstring', 'main', 'monthlen', 'prevmonth', 'nextmonth'}
980+
'formatstring', 'main', 'monthlen', 'prevmonth', 'nextmonth', ""}
976981
support.check__all__(self, calendar, not_exported=not_exported)
977982

978983

@@ -1000,6 +1005,13 @@ def test_formatmonth(self):
10001005
self.assertIn('class="text-center month"',
10011006
self.cal.formatmonth(2017, 5))
10021007

1008+
def test_formatmonth_with_invalid_month(self):
1009+
with self.assertRaises(calendar.IllegalMonthError):
1010+
self.cal.formatmonth(2017, 13)
1011+
with self.assertRaises(calendar.IllegalMonthError):
1012+
self.cal.formatmonth(2017, -1)
1013+
1014+
10031015
def test_formatweek(self):
10041016
weeks = self.cal.monthdays2calendar(2017, 5)
10051017
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)