[Python-checkins] bpo-46659: calendar uses locale.getlocale() (GH-31166)

vstinner webhook-mailer at python.org
Mon Feb 7 18:24:44 EST 2022


https://github.com/python/cpython/commit/7a0486eaa98083e0407ff491872db6d7a0da2635
commit: 7a0486eaa98083e0407ff491872db6d7a0da2635
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2022-02-08T00:24:09+01:00
summary:

bpo-46659: calendar uses locale.getlocale() (GH-31166)

The calendar.LocaleTextCalendar and calendar.LocaleHTMLCalendar
classes module now use locale.getlocale(), instead of using
locale.getdefaultlocale(), if no locale is specified.

files:
A Misc/NEWS.d/next/Library/2022-02-06-19-13-02.bpo-46659.q-vNL9.rst
M Doc/library/calendar.rst
M Doc/whatsnew/3.11.rst
M Lib/calendar.py
M Lib/test/test_calendar.py

diff --git a/Doc/library/calendar.rst b/Doc/library/calendar.rst
index b667c42e708fa..86f5b142a6c34 100644
--- a/Doc/library/calendar.rst
+++ b/Doc/library/calendar.rst
@@ -290,7 +290,7 @@ 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 current locale to the given *locale*.  Because
+   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.
 
 
diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst
index c1f267a4126a6..5738745ba1323 100644
--- a/Doc/whatsnew/3.11.rst
+++ b/Doc/whatsnew/3.11.rst
@@ -591,6 +591,12 @@ Changes in the Python API
   of sorting simply isn't well-defined in the absence of a total ordering
   on list elements.
 
+* :mod:`calendar`: The :class:`calendar.LocaleTextCalendar` and
+  :class:`calendar.LocaleHTMLCalendar` classes now use
+  :func:`locale.getlocale`, instead of using :func:`locale.getdefaultlocale`,
+  if no locale is specified.
+  (Contributed by Victor Stinner in :issue:`46659`.)
+
 
 Build Changes
 =============
diff --git a/Lib/calendar.py b/Lib/calendar.py
index 06c65a80cd80f..361898dc8161e 100644
--- a/Lib/calendar.py
+++ b/Lib/calendar.py
@@ -566,7 +566,7 @@ class LocaleTextCalendar(TextCalendar):
     def __init__(self, firstweekday=0, locale=None):
         TextCalendar.__init__(self, firstweekday)
         if locale is None:
-            locale = _locale.getdefaultlocale()
+            locale = _locale.getlocale(_locale.LC_TIME)
         self.locale = locale
 
     def formatweekday(self, day, width):
@@ -586,7 +586,7 @@ class LocaleHTMLCalendar(HTMLCalendar):
     def __init__(self, firstweekday=0, locale=None):
         HTMLCalendar.__init__(self, firstweekday)
         if locale is None:
-            locale = _locale.getdefaultlocale()
+            locale = _locale.getlocale(_locale.LC_TIME)
         self.locale = locale
 
     def formatweekday(self, day):
diff --git a/Lib/test/test_calendar.py b/Lib/test/test_calendar.py
index 39094ad6fd9ab..e6bd4d03e0f63 100644
--- a/Lib/test/test_calendar.py
+++ b/Lib/test/test_calendar.py
@@ -859,7 +859,8 @@ def test_option_locale(self):
         self.assertFailure('-L')
         self.assertFailure('--locale')
         self.assertFailure('-L', 'en')
-        lang, enc = locale.getdefaultlocale()
+
+        lang, enc = locale.getlocale()
         lang = lang or 'C'
         enc = enc or 'UTF-8'
         try:
diff --git a/Misc/NEWS.d/next/Library/2022-02-06-19-13-02.bpo-46659.q-vNL9.rst b/Misc/NEWS.d/next/Library/2022-02-06-19-13-02.bpo-46659.q-vNL9.rst
new file mode 100644
index 0000000000000..2e30de17626b2
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-02-06-19-13-02.bpo-46659.q-vNL9.rst
@@ -0,0 +1,4 @@
+The :class:`calendar.LocaleTextCalendar` and
+:class:`calendar.LocaleHTMLCalendar` classes now use :func:`locale.getlocale`,
+instead of using :func:`locale.getdefaultlocale`, if no locale is specified.
+Patch by Victor Stinner.



More information about the Python-checkins mailing list