Skip to content

Update calendar and test_calendar to 3.13.2 #5696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions Lib/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from enum import IntEnum, global_enum
import locale as _locale
from itertools import repeat
import warnings

__all__ = ["IllegalMonthError", "IllegalWeekdayError", "setfirstweekday",
"firstweekday", "isleap", "leapdays", "weekday", "monthrange",
Expand All @@ -28,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):
Expand All @@ -44,6 +45,7 @@ def __str__(self):

def __getattr__(name):
if name in ('January', 'February'):
import warnings
warnings.warn(f"The '{name}' attribute is deprecated, use '{name.upper()}' instead",
DeprecationWarning, stacklevel=2)
if name == 'January':
Expand Down Expand Up @@ -158,11 +160,14 @@ def weekday(year, month, day):
return Day(datetime.date(year, month, day).weekday())


def monthrange(year, month):
"""Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for
year, month."""
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."""
_validate_month(month)
day1 = weekday(year, month, 1)
ndays = mdays[month] + (month == FEBRUARY and isleap(year))
return day1, ndays
Expand Down Expand Up @@ -370,6 +375,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)
Expand Down Expand Up @@ -500,6 +507,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:
Expand Down Expand Up @@ -585,8 +593,6 @@ def __enter__(self):
_locale.setlocale(_locale.LC_TIME, self.locale)

def __exit__(self, *args):
if self.oldlocale is None:
return
_locale.setlocale(_locale.LC_TIME, self.oldlocale)


Expand Down Expand Up @@ -690,7 +696,7 @@ def timegm(tuple):
return seconds


def main(args):
def main(args=None):
import argparse
parser = argparse.ArgumentParser()
textgroup = parser.add_argument_group('text only arguments')
Expand Down Expand Up @@ -736,18 +742,23 @@ def main(args):
choices=("text", "html"),
help="output type (text or html)"
)
parser.add_argument(
"-f", "--first-weekday",
type=int, default=0,
help="weekday (0 is Monday, 6 is Sunday) to start each week (default 0)"
)
parser.add_argument(
"year",
nargs='?', type=int,
help="year number (1-9999)"
help="year number"
)
parser.add_argument(
"month",
nargs='?', type=int,
help="month number (1-12, text only)"
)

options = parser.parse_args(args[1:])
options = parser.parse_args(args)

if options.locale and not options.encoding:
parser.error("if --locale is specified --encoding is required")
Expand All @@ -756,31 +767,35 @@ def main(args):
locale = options.locale, options.encoding

if options.type == "html":
if options.month:
parser.error("incorrect number of arguments")
sys.exit(1)
if options.locale:
cal = LocaleHTMLCalendar(locale=locale)
else:
cal = HTMLCalendar()
cal.setfirstweekday(options.first_weekday)
encoding = options.encoding
if encoding is None:
encoding = sys.getdefaultencoding()
optdict = dict(encoding=encoding, css=options.css)
write = sys.stdout.buffer.write
if options.year is None:
write(cal.formatyearpage(datetime.date.today().year, **optdict))
elif options.month is None:
write(cal.formatyearpage(options.year, **optdict))
else:
parser.error("incorrect number of arguments")
sys.exit(1)
write(cal.formatyearpage(options.year, **optdict))
else:
if options.locale:
cal = LocaleTextCalendar(locale=locale)
else:
cal = TextCalendar()
cal.setfirstweekday(options.first_weekday)
optdict = dict(w=options.width, l=options.lines)
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:
Expand All @@ -795,4 +810,4 @@ def main(args):


if __name__ == "__main__":
main(sys.argv)
main()
Loading
Loading