[Python-checkins] bpo-46659: Enhance LocaleTextCalendar for C locale (GH-31214)
vstinner
webhook-mailer at python.org
Thu Feb 24 08:29:36 EST 2022
https://github.com/python/cpython/commit/4fccf910738d1442852cb900747e6dccb8fe03ef
commit: 4fccf910738d1442852cb900747e6dccb8fe03ef
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2022-02-24T14:29:08+01:00
summary:
bpo-46659: Enhance LocaleTextCalendar for C locale (GH-31214)
If the LC_TIME locale is "C", use the user preferred locale.
files:
M Doc/library/calendar.rst
M Lib/calendar.py
diff --git a/Doc/library/calendar.rst b/Doc/library/calendar.rst
index 86f5b142a6c34..66f59f0e2ced2 100644
--- a/Doc/library/calendar.rst
+++ b/Doc/library/calendar.rst
@@ -289,9 +289,10 @@ interpreted as prescribed by the ISO 8601 standard. Year 0 is 1 BC, year -1 is
.. note::
- The :meth:`formatweekday` and :meth:`formatmonthname` methods of these two
- classes temporarily change the ``LC_TIME`` locale to the given *locale*. Because
- the current locale is a process-wide setting, they are not thread-safe.
+ The constructor, :meth:`formatweekday` and :meth:`formatmonthname` methods
+ of these two classes temporarily change the ``LC_TIME`` locale to the given
+ *locale*. Because the current locale is a process-wide setting, they are
+ not thread-safe.
For simple text calendars this module provides the following functions.
diff --git a/Lib/calendar.py b/Lib/calendar.py
index 361898dc8161e..657396439c91f 100644
--- a/Lib/calendar.py
+++ b/Lib/calendar.py
@@ -548,15 +548,28 @@ def formatyearpage(self, theyear, width=3, css='calendar.css', encoding=None):
class different_locale:
def __init__(self, locale):
self.locale = locale
+ self.oldlocale = None
def __enter__(self):
- self.oldlocale = _locale.getlocale(_locale.LC_TIME)
+ self.oldlocale = _locale.setlocale(_locale.LC_TIME, None)
_locale.setlocale(_locale.LC_TIME, self.locale)
def __exit__(self, *args):
+ if self.oldlocale is None:
+ return
_locale.setlocale(_locale.LC_TIME, self.oldlocale)
+def _get_default_locale():
+ locale = _locale.setlocale(_locale.LC_TIME, None)
+ if locale == "C":
+ with different_locale(""):
+ # The LC_TIME locale does not seem to be configured:
+ # get the user preferred locale.
+ locale = _locale.setlocale(_locale.LC_TIME, None)
+ return locale
+
+
class LocaleTextCalendar(TextCalendar):
"""
This class can be passed a locale name in the constructor and will return
@@ -566,7 +579,7 @@ class LocaleTextCalendar(TextCalendar):
def __init__(self, firstweekday=0, locale=None):
TextCalendar.__init__(self, firstweekday)
if locale is None:
- locale = _locale.getlocale(_locale.LC_TIME)
+ locale = _get_default_locale()
self.locale = locale
def formatweekday(self, day, width):
@@ -586,7 +599,7 @@ class LocaleHTMLCalendar(HTMLCalendar):
def __init__(self, firstweekday=0, locale=None):
HTMLCalendar.__init__(self, firstweekday)
if locale is None:
- locale = _locale.getlocale(_locale.LC_TIME)
+ locale = _get_default_locale()
self.locale = locale
def formatweekday(self, day):
More information about the Python-checkins
mailing list