Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7004bd1

Browse files
committed
#10092: Properly reset locale in Locale*Calendar classes. The context manager was buggy because setlocale() returns the *new* locale, not the old. Also add a test for this.
1 parent f87cc04 commit 7004bd1

4 files changed

Lines changed: 30 additions & 5 deletions

File tree

Doc/library/calendar.rst

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ it's the base calendar for all computations.
170170
.. class:: LocaleTextCalendar(firstweekday=0, locale=None)
171171

172172
This subclass of :class:`TextCalendar` can be passed a locale name in the
173-
constructor and will return month and weekday names in the specified
174-
locale. If this locale includes an encoding all strings containing month and
175-
weekday names will be returned as unicode.
173+
constructor and will return month and weekday names in the specified locale.
174+
If this locale includes an encoding all strings containing month and weekday
175+
names will be returned as unicode.
176176

177177

178178
.. class:: LocaleHTMLCalendar(firstweekday=0, locale=None)
@@ -182,6 +182,12 @@ it's the base calendar for all computations.
182182
locale. If this locale includes an encoding all strings containing month and
183183
weekday names will be returned as unicode.
184184

185+
.. note::
186+
187+
The :meth:`formatweekday` and :meth:`formatmonthname` methods of these two
188+
classes temporarily change the current locale to the given *locale*. Because
189+
the current locale is a process-wide setting, they are not thread-safe.
190+
185191

186192
For simple text calendars this module provides the following functions.
187193

Lib/calendar.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,8 @@ def __init__(self, locale):
486486
self.locale = locale
487487

488488
def __enter__(self):
489-
self.oldlocale = _locale.setlocale(_locale.LC_TIME, self.locale)
490-
#return _locale.getlocale(_locale.LC_TIME)[1]
489+
self.oldlocale = _locale.getlocale(_locale.LC_TIME)
490+
_locale.setlocale(_locale.LC_TIME, self.locale)
491491

492492
def __exit__(self, *args):
493493
_locale.setlocale(_locale.LC_TIME, self.oldlocale)

Lib/test/test_calendar.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from test import support
55
import time
6+
import locale
67

78
result_2004_text = """
89
2004
@@ -250,6 +251,22 @@ def test_months(self):
250251
# verify it "acts like a sequence" in two forms of iteration
251252
self.assertEqual(value[::-1], list(reversed(value)))
252253

254+
def test_localecalendars(self):
255+
# ensure that Locale{Text,HTML}Calendar resets the locale properly
256+
# (it is still not thread-safe though)
257+
try:
258+
def_locale = locale.getdefaultlocale()
259+
except locale.Error:
260+
# cannot determine a default locale -- skip test
261+
return
262+
old_october = calendar.TextCalendar().formatmonthname(2010, 10, 10)
263+
calendar.LocaleTextCalendar(
264+
locale=def_locale).formatmonthname(2010, 10, 10)
265+
calendar.LocaleHTMLCalendar(
266+
locale=def_locale).formatmonthname(2010, 10)
267+
new_october = calendar.TextCalendar().formatmonthname(2010, 10, 10)
268+
self.assertEquals(old_october, new_october)
269+
253270

254271
class MonthCalendarTestCase(unittest.TestCase):
255272
def setUp(self):

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ Core and Builtins
3434
Library
3535
-------
3636

37+
- Issue #10092: Properly reset locale in calendar.Locale*Calendar classes.
38+
3739
- logging: Added _logRecordClass, getLogRecordClass, setLogRecordClass to
3840
increase flexibility of LogRecord creation.
3941

0 commit comments

Comments
 (0)