-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
Closed
Labels
3.14bugs and security fixesbugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
In the calendar module, IllegalMonthError is not handled properly. For months greater than 12, an IndexError is raised instead.
formatmonthname do not raise IndexError for negative integer because list support negative integer indexing.
>>> import calendar
>>> calendar.month(2024, -1)
Traceback (most recent call last):
File "<python-input-12>", line 1, in <module>
calendar.month(2024, -1)
~~~~~~~~~~~~~~^^^^^^^^^^
File "/Users/gugu/Code/cpython/Lib/calendar.py", line 395, in formatmonth
for week in self.monthdays2calendar(theyear, themonth):
~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
File "/Users/gugu/Code/cpython/Lib/calendar.py", line 284, in monthdays2calendar
days = list(self.itermonthdays2(year, month))
File "/Users/gugu/Code/cpython/Lib/calendar.py", line 240, in itermonthdays2
for i, d in enumerate(self.itermonthdays(year, month), self.firstweekday):
~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/gugu/Code/cpython/Lib/calendar.py", line 228, in itermonthdays
day1, ndays = monthrange(year, month)
~~~~~~~~~~^^^^^^^^^^^^^
File "/Users/gugu/Code/cpython/Lib/calendar.py", line 165, in monthrange
raise IllegalMonthError(month)
calendar.IllegalMonthError: bad month number -1; must be 1-12
>>> calendar.month(2024, 13)
Traceback (most recent call last):
File "<python-input-13>", line 1, in <module>
calendar.month(2024, 13)
~~~~~~~~~~~~~~^^^^^^^^^^
File "/Users/gugu/Code/cpython/Lib/calendar.py", line 390, in formatmonth
s = self.formatmonthname(theyear, themonth, 7 * (w + 1) - 1)
File "/Users/gugu/Code/cpython/Lib/calendar.py", line 373, in formatmonthname
s = month_name[themonth]
~~~~~~~~~~^^^^^^^^^^
File "/Users/gugu/Code/cpython/Lib/calendar.py", line 103, in __getitem__
funcs = self._months[i]
~~~~~~~~~~~~^^^
IndexError: list index out of rangeCPython versions tested on:
CPython main branch
Operating systems tested on:
macOS
Linked PRs
- gh-126476: Raise
IllegalMonthErrorfor calendar.formatmonth method when the input month is not corret #126484 - [3.12] gh-126476: Raise IllegalMonthError for calendar.formatmonth() when the input month is not correct (GH-126484) #126878
- [3.13] gh-126476: Raise IllegalMonthError for calendar.formatmonth() when the input month is not correct (GH-126484) #126879
Metadata
Metadata
Assignees
Labels
3.14bugs and security fixesbugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error