Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
Closed
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.
>>>importcalendar>>>calendar.month(2024,-1)Traceback (mostrecentcalllast):File"<python-input-12>",line1,in<module>calendar.month(2024,-1)~~~~~~~~~~~~~~^^^^^^^^^^File"/Users/gugu/Code/cpython/Lib/calendar.py",line395,informatmonthforweekinself.monthdays2calendar(theyear,themonth):~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^File"/Users/gugu/Code/cpython/Lib/calendar.py",line284,inmonthdays2calendardays=list(self.itermonthdays2(year,month))File"/Users/gugu/Code/cpython/Lib/calendar.py",line240,initermonthdays2fori,dinenumerate(self.itermonthdays(year,month),self.firstweekday):~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File"/Users/gugu/Code/cpython/Lib/calendar.py",line228,initermonthdaysday1,ndays=monthrange(year,month)~~~~~~~~~~^^^^^^^^^^^^^File"/Users/gugu/Code/cpython/Lib/calendar.py",line165,inmonthrangeraiseIllegalMonthError(month)calendar.IllegalMonthError:badmonthnumber-1;mustbe1-12>>>calendar.month(2024,13)Traceback (mostrecentcalllast):File"<python-input-13>",line1,in<module>calendar.month(2024,13)~~~~~~~~~~~~~~^^^^^^^^^^File"/Users/gugu/Code/cpython/Lib/calendar.py",line390,informatmonths=self.formatmonthname(theyear,themonth,7* (w+1)-1)File"/Users/gugu/Code/cpython/Lib/calendar.py",line373,informatmonthnames=month_name[themonth]~~~~~~~~~~^^^^^^^^^^File"/Users/gugu/Code/cpython/Lib/calendar.py",line103,in__getitem__funcs=self._months[i]~~~~~~~~~~~~^^^IndexError:listindexoutofrange
CPython 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