From e8ed750167b3016c050d77fb46de779d2c8b9361 Mon Sep 17 00:00:00 2001 From: Manjusaka Date: Wed, 6 Nov 2024 18:26:07 +0800 Subject: [PATCH 1/4] gh-126476: Raise `IllegalMonthError` for Calendar.formatmonth method when the input month is not corret --- Lib/calendar.py | 10 ++++++++-- Lib/test/test_calendar.py | 14 +++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Lib/calendar.py b/Lib/calendar.py index 069dd5174112ae..36d8997510d569 100644 --- a/Lib/calendar.py +++ b/Lib/calendar.py @@ -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 @@ -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) @@ -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: diff --git a/Lib/test/test_calendar.py b/Lib/test/test_calendar.py index f119d89c0ec39a..073df310bb49eb 100644 --- a/Lib/test/test_calendar.py +++ b/Lib/test/test_calendar.py @@ -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( @@ -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) @@ -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])) From 5202e20a7beb51caa8dae955cf54ed8da34a3db8 Mon Sep 17 00:00:00 2001 From: Manjusaka Date: Wed, 6 Nov 2024 18:30:57 +0800 Subject: [PATCH 2/4] Update news --- .../next/Library/2024-11-06-18-30-50.gh-issue-126476.F1wh3c.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2024-11-06-18-30-50.gh-issue-126476.F1wh3c.rst diff --git a/Misc/NEWS.d/next/Library/2024-11-06-18-30-50.gh-issue-126476.F1wh3c.rst b/Misc/NEWS.d/next/Library/2024-11-06-18-30-50.gh-issue-126476.F1wh3c.rst new file mode 100644 index 00000000000000..75ef374b03568b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-11-06-18-30-50.gh-issue-126476.F1wh3c.rst @@ -0,0 +1,2 @@ +Raise :class:`calendar.IllegalMonthError` for :function:`calendar.month` +when the input month is not correct. From a9a9606e671e5ceb00fbc34e5906643e8be3ee44 Mon Sep 17 00:00:00 2001 From: Manjusaka Date: Wed, 6 Nov 2024 18:43:01 +0800 Subject: [PATCH 3/4] Update news --- .../next/Library/2024-11-06-18-30-50.gh-issue-126476.F1wh3c.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-11-06-18-30-50.gh-issue-126476.F1wh3c.rst b/Misc/NEWS.d/next/Library/2024-11-06-18-30-50.gh-issue-126476.F1wh3c.rst index 75ef374b03568b..0b816dca80693a 100644 --- a/Misc/NEWS.d/next/Library/2024-11-06-18-30-50.gh-issue-126476.F1wh3c.rst +++ b/Misc/NEWS.d/next/Library/2024-11-06-18-30-50.gh-issue-126476.F1wh3c.rst @@ -1,2 +1,2 @@ -Raise :class:`calendar.IllegalMonthError` for :function:`calendar.month` +Raise :class:`calendar.IllegalMonthError` for :func:`calendar.month` when the input month is not correct. From 76f8ed65cf52aa53c72fa29eceaf770f997c98cd Mon Sep 17 00:00:00 2001 From: Manjusaka Date: Thu, 7 Nov 2024 02:46:50 +0800 Subject: [PATCH 4/4] Fix review idea --- Lib/calendar.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Lib/calendar.py b/Lib/calendar.py index 36d8997510d569..8c1c646da46a98 100644 --- a/Lib/calendar.py +++ b/Lib/calendar.py @@ -27,7 +27,9 @@ error = ValueError # Exceptions raised for bad input -class IllegalMonthError(ValueError): +# This is trick for backward compatibility. Since 3.13, we will raise IllegalMonthError instead of +# IndexError for bad month number(out of 1-12). But we can't remove IndexError for backward compatibility. +class IllegalMonthError(ValueError, IndexError): def __init__(self, month): self.month = month def __str__(self): @@ -792,6 +794,8 @@ def main(args=None): if options.month is None: optdict["c"] = options.spacing optdict["m"] = options.months + if options.month is not None: + _validate_month(options.month) if options.year is None: result = cal.formatyear(datetime.date.today().year, **optdict) elif options.month is None: