Skip to content

Commit fcc1725

Browse files
committed
Update calendar.py from CPython 3.11
1 parent 6edd370 commit fcc1725

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

Lib/calendar.py

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
"monthcalendar", "prmonth", "month", "prcal", "calendar",
1616
"timegm", "month_name", "month_abbr", "day_name", "day_abbr",
1717
"Calendar", "TextCalendar", "HTMLCalendar", "LocaleTextCalendar",
18-
"LocaleHTMLCalendar", "weekheader"]
18+
"LocaleHTMLCalendar", "weekheader",
19+
"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY",
20+
"SATURDAY", "SUNDAY"]
1921

2022
# Exception raised for bad input (with string parameter for details)
2123
error = ValueError
@@ -546,71 +548,67 @@ def formatyearpage(self, theyear, width=3, css='calendar.css', encoding=None):
546548
class different_locale:
547549
def __init__(self, locale):
548550
self.locale = locale
551+
self.oldlocale = None
549552

550553
def __enter__(self):
551-
self.oldlocale = _locale.getlocale(_locale.LC_TIME)
554+
self.oldlocale = _locale.setlocale(_locale.LC_TIME, None)
552555
_locale.setlocale(_locale.LC_TIME, self.locale)
553556

554557
def __exit__(self, *args):
558+
if self.oldlocale is None:
559+
return
555560
_locale.setlocale(_locale.LC_TIME, self.oldlocale)
556561

557562

563+
def _get_default_locale():
564+
locale = _locale.setlocale(_locale.LC_TIME, None)
565+
if locale == "C":
566+
with different_locale(""):
567+
# The LC_TIME locale does not seem to be configured:
568+
# get the user preferred locale.
569+
locale = _locale.setlocale(_locale.LC_TIME, None)
570+
return locale
571+
572+
558573
class LocaleTextCalendar(TextCalendar):
559574
"""
560575
This class can be passed a locale name in the constructor and will return
561-
month and weekday names in the specified locale. If this locale includes
562-
an encoding all strings containing month and weekday names will be returned
563-
as unicode.
576+
month and weekday names in the specified locale.
564577
"""
565578

566579
def __init__(self, firstweekday=0, locale=None):
567580
TextCalendar.__init__(self, firstweekday)
568581
if locale is None:
569-
locale = _locale.getdefaultlocale()
582+
locale = _get_default_locale()
570583
self.locale = locale
571584

572585
def formatweekday(self, day, width):
573586
with different_locale(self.locale):
574-
if width >= 9:
575-
names = day_name
576-
else:
577-
names = day_abbr
578-
name = names[day]
579-
return name[:width].center(width)
587+
return super().formatweekday(day, width)
580588

581589
def formatmonthname(self, theyear, themonth, width, withyear=True):
582590
with different_locale(self.locale):
583-
s = month_name[themonth]
584-
if withyear:
585-
s = "%s %r" % (s, theyear)
586-
return s.center(width)
591+
return super().formatmonthname(theyear, themonth, width, withyear)
587592

588593

589594
class LocaleHTMLCalendar(HTMLCalendar):
590595
"""
591596
This class can be passed a locale name in the constructor and will return
592-
month and weekday names in the specified locale. If this locale includes
593-
an encoding all strings containing month and weekday names will be returned
594-
as unicode.
597+
month and weekday names in the specified locale.
595598
"""
596599
def __init__(self, firstweekday=0, locale=None):
597600
HTMLCalendar.__init__(self, firstweekday)
598601
if locale is None:
599-
locale = _locale.getdefaultlocale()
602+
locale = _get_default_locale()
600603
self.locale = locale
601604

602605
def formatweekday(self, day):
603606
with different_locale(self.locale):
604-
s = day_abbr[day]
605-
return '<th class="%s">%s</th>' % (self.cssclasses[day], s)
607+
return super().formatweekday(day)
606608

607609
def formatmonthname(self, theyear, themonth, withyear=True):
608610
with different_locale(self.locale):
609-
s = month_name[themonth]
610-
if withyear:
611-
s = '%s %s' % (s, theyear)
612-
return '<tr><th colspan="7" class="month">%s</th></tr>' % s
613-
611+
return super().formatmonthname(theyear, themonth, withyear)
614612

615613
# Support for old module level interface
616614
c = TextCalendar()

0 commit comments

Comments
 (0)