Skip to content

Commit 7a0486e

Browse files
authored
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.
1 parent 7ba1cc8 commit 7a0486e

File tree

5 files changed

+15
-4
lines changed

5 files changed

+15
-4
lines changed

Doc/library/calendar.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ interpreted as prescribed by the ISO 8601 standard. Year 0 is 1 BC, year -1 is
290290
.. note::
291291

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

296296

Doc/whatsnew/3.11.rst

+6
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,12 @@ Changes in the Python API
591591
of sorting simply isn't well-defined in the absence of a total ordering
592592
on list elements.
593593

594+
* :mod:`calendar`: The :class:`calendar.LocaleTextCalendar` and
595+
:class:`calendar.LocaleHTMLCalendar` classes now use
596+
:func:`locale.getlocale`, instead of using :func:`locale.getdefaultlocale`,
597+
if no locale is specified.
598+
(Contributed by Victor Stinner in :issue:`46659`.)
599+
594600

595601
Build Changes
596602
=============

Lib/calendar.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ class LocaleTextCalendar(TextCalendar):
566566
def __init__(self, firstweekday=0, locale=None):
567567
TextCalendar.__init__(self, firstweekday)
568568
if locale is None:
569-
locale = _locale.getdefaultlocale()
569+
locale = _locale.getlocale(_locale.LC_TIME)
570570
self.locale = locale
571571

572572
def formatweekday(self, day, width):
@@ -586,7 +586,7 @@ class LocaleHTMLCalendar(HTMLCalendar):
586586
def __init__(self, firstweekday=0, locale=None):
587587
HTMLCalendar.__init__(self, firstweekday)
588588
if locale is None:
589-
locale = _locale.getdefaultlocale()
589+
locale = _locale.getlocale(_locale.LC_TIME)
590590
self.locale = locale
591591

592592
def formatweekday(self, day):

Lib/test/test_calendar.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,8 @@ def test_option_locale(self):
859859
self.assertFailure('-L')
860860
self.assertFailure('--locale')
861861
self.assertFailure('-L', 'en')
862-
lang, enc = locale.getdefaultlocale()
862+
863+
lang, enc = locale.getlocale()
863864
lang = lang or 'C'
864865
enc = enc or 'UTF-8'
865866
try:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The :class:`calendar.LocaleTextCalendar` and
2+
:class:`calendar.LocaleHTMLCalendar` classes now use :func:`locale.getlocale`,
3+
instead of using :func:`locale.getdefaultlocale`, if no locale is specified.
4+
Patch by Victor Stinner.

0 commit comments

Comments
 (0)