Commit 7004bd1a authored by Georg Brandl's avatar Georg Brandl

#10092: Properly reset locale in Locale*Calendar classes. The context manager...

#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.
parent f87cc044
...@@ -170,9 +170,9 @@ it's the base calendar for all computations. ...@@ -170,9 +170,9 @@ it's the base calendar for all computations.
.. class:: LocaleTextCalendar(firstweekday=0, locale=None) .. class:: LocaleTextCalendar(firstweekday=0, locale=None)
This subclass of :class:`TextCalendar` can be passed a locale name in the This subclass of :class:`TextCalendar` can be passed a locale name in the
constructor and will return month and weekday names in the specified constructor and will return month and weekday names in the specified locale.
locale. If this locale includes an encoding all strings containing month and If this locale includes an encoding all strings containing month and weekday
weekday names will be returned as unicode. names will be returned as unicode.
.. class:: LocaleHTMLCalendar(firstweekday=0, locale=None) .. class:: LocaleHTMLCalendar(firstweekday=0, locale=None)
...@@ -182,6 +182,12 @@ it's the base calendar for all computations. ...@@ -182,6 +182,12 @@ it's the base calendar for all computations.
locale. If this locale includes an encoding all strings containing month and locale. If this locale includes an encoding all strings containing month and
weekday names will be returned as unicode. weekday names will be returned as unicode.
.. note::
The :meth:`formatweekday` and :meth:`formatmonthname` methods of these two
classes temporarily change the current 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. For simple text calendars this module provides the following functions.
......
...@@ -486,8 +486,8 @@ class different_locale: ...@@ -486,8 +486,8 @@ class different_locale:
self.locale = locale self.locale = locale
def __enter__(self): def __enter__(self):
self.oldlocale = _locale.setlocale(_locale.LC_TIME, self.locale) self.oldlocale = _locale.getlocale(_locale.LC_TIME)
#return _locale.getlocale(_locale.LC_TIME)[1] _locale.setlocale(_locale.LC_TIME, self.locale)
def __exit__(self, *args): def __exit__(self, *args):
_locale.setlocale(_locale.LC_TIME, self.oldlocale) _locale.setlocale(_locale.LC_TIME, self.oldlocale)
......
...@@ -3,6 +3,7 @@ import unittest ...@@ -3,6 +3,7 @@ import unittest
from test import support from test import support
import time import time
import locale
result_2004_text = """ result_2004_text = """
2004 2004
...@@ -250,6 +251,22 @@ class CalendarTestCase(unittest.TestCase): ...@@ -250,6 +251,22 @@ class CalendarTestCase(unittest.TestCase):
# verify it "acts like a sequence" in two forms of iteration # verify it "acts like a sequence" in two forms of iteration
self.assertEqual(value[::-1], list(reversed(value))) self.assertEqual(value[::-1], list(reversed(value)))
def test_localecalendars(self):
# ensure that Locale{Text,HTML}Calendar resets the locale properly
# (it is still not thread-safe though)
try:
def_locale = locale.getdefaultlocale()
except locale.Error:
# cannot determine a default locale -- skip test
return
old_october = calendar.TextCalendar().formatmonthname(2010, 10, 10)
calendar.LocaleTextCalendar(
locale=def_locale).formatmonthname(2010, 10, 10)
calendar.LocaleHTMLCalendar(
locale=def_locale).formatmonthname(2010, 10)
new_october = calendar.TextCalendar().formatmonthname(2010, 10, 10)
self.assertEquals(old_october, new_october)
class MonthCalendarTestCase(unittest.TestCase): class MonthCalendarTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
......
...@@ -34,6 +34,8 @@ Core and Builtins ...@@ -34,6 +34,8 @@ Core and Builtins
Library Library
------- -------
- Issue #10092: Properly reset locale in calendar.Locale*Calendar classes.
- logging: Added _logRecordClass, getLogRecordClass, setLogRecordClass to - logging: Added _logRecordClass, getLogRecordClass, setLogRecordClass to
increase flexibility of LogRecord creation. increase flexibility of LogRecord creation.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment