Skip to content

Commit a1a69b5

Browse files
vstinnerasvetlov
authored andcommitted
bpo-46659: Enhance LocaleTextCalendar for C locale (GH-31214)
If the LC_TIME locale is "C", use the user preferred locale.
1 parent e8b17e1 commit a1a69b5

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

Doc/library/calendar.rst

+4-3
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,10 @@ interpreted as prescribed by the ISO 8601 standard. Year 0 is 1 BC, year -1 is
289289

290290
.. note::
291291

292-
The :meth:`formatweekday` and :meth:`formatmonthname` methods of these two
293-
classes temporarily change the ``LC_TIME`` locale to the given *locale*. Because
294-
the current locale is a process-wide setting, they are not thread-safe.
292+
The constructor, :meth:`formatweekday` and :meth:`formatmonthname` methods
293+
of these two classes temporarily change the ``LC_TIME`` locale to the given
294+
*locale*. Because the current locale is a process-wide setting, they are
295+
not thread-safe.
295296

296297

297298
For simple text calendars this module provides the following functions.

Lib/calendar.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -548,15 +548,28 @@ def formatyearpage(self, theyear, width=3, css='calendar.css', encoding=None):
548548
class different_locale:
549549
def __init__(self, locale):
550550
self.locale = locale
551+
self.oldlocale = None
551552

552553
def __enter__(self):
553-
self.oldlocale = _locale.getlocale(_locale.LC_TIME)
554+
self.oldlocale = _locale.setlocale(_locale.LC_TIME, None)
554555
_locale.setlocale(_locale.LC_TIME, self.locale)
555556

556557
def __exit__(self, *args):
558+
if self.oldlocale is None:
559+
return
557560
_locale.setlocale(_locale.LC_TIME, self.oldlocale)
558561

559562

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+
560573
class LocaleTextCalendar(TextCalendar):
561574
"""
562575
This class can be passed a locale name in the constructor and will return
@@ -566,7 +579,7 @@ class LocaleTextCalendar(TextCalendar):
566579
def __init__(self, firstweekday=0, locale=None):
567580
TextCalendar.__init__(self, firstweekday)
568581
if locale is None:
569-
locale = _locale.getlocale(_locale.LC_TIME)
582+
locale = _get_default_locale()
570583
self.locale = locale
571584

572585
def formatweekday(self, day, width):
@@ -586,7 +599,7 @@ class LocaleHTMLCalendar(HTMLCalendar):
586599
def __init__(self, firstweekday=0, locale=None):
587600
HTMLCalendar.__init__(self, firstweekday)
588601
if locale is None:
589-
locale = _locale.getlocale(_locale.LC_TIME)
602+
locale = _get_default_locale()
590603
self.locale = locale
591604

592605
def formatweekday(self, day):

0 commit comments

Comments
 (0)