Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c7df344

Browse files
arihant2mathyouknowone
authored andcommitted
update calendar and test_calendar to 3.13.2
1 parent 4094c5b commit c7df344

File tree

2 files changed

+238
-62
lines changed

2 files changed

+238
-62
lines changed

Lib/calendar.py

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from enum import IntEnum, global_enum
1111
import locale as _locale
1212
from itertools import repeat
13-
import warnings
1413

1514
__all__ = ["IllegalMonthError", "IllegalWeekdayError", "setfirstweekday",
1615
"firstweekday", "isleap", "leapdays", "weekday", "monthrange",
@@ -28,7 +27,9 @@
2827
error = ValueError
2928

3029
# Exceptions raised for bad input
31-
class IllegalMonthError(ValueError):
30+
# This is trick for backward compatibility. Since 3.13, we will raise IllegalMonthError instead of
31+
# IndexError for bad month number(out of 1-12). But we can't remove IndexError for backward compatibility.
32+
class IllegalMonthError(ValueError, IndexError):
3233
def __init__(self, month):
3334
self.month = month
3435
def __str__(self):
@@ -44,6 +45,7 @@ def __str__(self):
4445

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

160162

161-
def monthrange(year, month):
162-
"""Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for
163-
year, month."""
163+
def _validate_month(month):
164164
if not 1 <= month <= 12:
165165
raise IllegalMonthError(month)
166+
167+
def monthrange(year, month):
168+
"""Return weekday of first day of month (0-6 ~ Mon-Sun)
169+
and number of days (28-31) for year, month."""
170+
_validate_month(month)
166171
day1 = weekday(year, month, 1)
167172
ndays = mdays[month] + (month == FEBRUARY and isleap(year))
168173
return day1, ndays
@@ -370,6 +375,8 @@ def formatmonthname(self, theyear, themonth, width, withyear=True):
370375
"""
371376
Return a formatted month name.
372377
"""
378+
_validate_month(themonth)
379+
373380
s = month_name[themonth]
374381
if withyear:
375382
s = "%s %r" % (s, theyear)
@@ -500,6 +507,7 @@ def formatmonthname(self, theyear, themonth, withyear=True):
500507
"""
501508
Return a month name as a table row.
502509
"""
510+
_validate_month(themonth)
503511
if withyear:
504512
s = '%s %s' % (month_name[themonth], theyear)
505513
else:
@@ -585,8 +593,6 @@ def __enter__(self):
585593
_locale.setlocale(_locale.LC_TIME, self.locale)
586594

587595
def __exit__(self, *args):
588-
if self.oldlocale is None:
589-
return
590596
_locale.setlocale(_locale.LC_TIME, self.oldlocale)
591597

592598

@@ -690,7 +696,7 @@ def timegm(tuple):
690696
return seconds
691697

692698

693-
def main(args):
699+
def main(args=None):
694700
import argparse
695701
parser = argparse.ArgumentParser()
696702
textgroup = parser.add_argument_group('text only arguments')
@@ -736,18 +742,23 @@ def main(args):
736742
choices=("text", "html"),
737743
help="output type (text or html)"
738744
)
745+
parser.add_argument(
746+
"-f", "--first-weekday",
747+
type=int, default=0,
748+
help="weekday (0 is Monday, 6 is Sunday) to start each week (default 0)"
749+
)
739750
parser.add_argument(
740751
"year",
741752
nargs='?', type=int,
742-
help="year number (1-9999)"
753+
help="year number"
743754
)
744755
parser.add_argument(
745756
"month",
746757
nargs='?', type=int,
747758
help="month number (1-12, text only)"
748759
)
749760

750-
options = parser.parse_args(args[1:])
761+
options = parser.parse_args(args)
751762

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

758769
if options.type == "html":
770+
if options.month:
771+
parser.error("incorrect number of arguments")
772+
sys.exit(1)
759773
if options.locale:
760774
cal = LocaleHTMLCalendar(locale=locale)
761775
else:
762776
cal = HTMLCalendar()
777+
cal.setfirstweekday(options.first_weekday)
763778
encoding = options.encoding
764779
if encoding is None:
765780
encoding = sys.getdefaultencoding()
766781
optdict = dict(encoding=encoding, css=options.css)
767782
write = sys.stdout.buffer.write
768783
if options.year is None:
769784
write(cal.formatyearpage(datetime.date.today().year, **optdict))
770-
elif options.month is None:
771-
write(cal.formatyearpage(options.year, **optdict))
772785
else:
773-
parser.error("incorrect number of arguments")
774-
sys.exit(1)
786+
write(cal.formatyearpage(options.year, **optdict))
775787
else:
776788
if options.locale:
777789
cal = LocaleTextCalendar(locale=locale)
778790
else:
779791
cal = TextCalendar()
792+
cal.setfirstweekday(options.first_weekday)
780793
optdict = dict(w=options.width, l=options.lines)
781794
if options.month is None:
782795
optdict["c"] = options.spacing
783796
optdict["m"] = options.months
797+
if options.month is not None:
798+
_validate_month(options.month)
784799
if options.year is None:
785800
result = cal.formatyear(datetime.date.today().year, **optdict)
786801
elif options.month is None:
@@ -795,4 +810,4 @@ def main(args):
795810

796811

797812
if __name__ == "__main__":
798-
main(sys.argv)
813+
main()

0 commit comments

Comments
 (0)