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):
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
"""
Return a formatted month name.
"""
+ _validate_month(themonth)
+
s = month_name[themonth]
if withyear:
s = "%s %r" % (s, theyear)
"""
Return a month name as a table row.
"""
+ _validate_month(themonth)
if withyear:
s = '%s %s' % (month_name[themonth], theyear)
else:
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:
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(
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)
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]))
--- /dev/null
+Raise :class:`calendar.IllegalMonthError` (now a subclass of :class:`IndexError`) for :func:`calendar.month`
+when the input month is not correct.