File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
186192For simple text calendars this module provides the following functions.
187193
Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff line change 33
44from test import support
55import time
6+ import locale
67
78result_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
254271class MonthCalendarTestCase (unittest .TestCase ):
255272 def setUp (self ):
Original file line number Diff line number Diff line change @@ -34,6 +34,8 @@ Core and Builtins
3434Library
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
You can’t perform that action at this time.
0 commit comments