summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2010-10-19 18:54:25 (GMT)
committerGeorg Brandl <georg@python.org>2010-10-19 18:54:25 (GMT)
commit7004bd1a3de06330761d164bae0d82c597bf39cf (patch)
tree4c27a09a59a559026e5f1cf157df2573fbdb9304
parentf87cc0448158fedad9ba0a1edcec3664d9f90eb4 (diff)
downloadcpython-7004bd1a3de06330761d164bae0d82c597bf39cf.zip
cpython-7004bd1a3de06330761d164bae0d82c597bf39cf.tar.gz
cpython-7004bd1a3de06330761d164bae0d82c597bf39cf.tar.bz2
#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.
-rw-r--r--Doc/library/calendar.rst12
-rw-r--r--Lib/calendar.py4
-rw-r--r--Lib/test/test_calendar.py17
-rw-r--r--Misc/NEWS2
4 files changed, 30 insertions, 5 deletions
diff --git a/Doc/library/calendar.rst b/Doc/library/calendar.rst
index 2228920..c8dac49 100644
--- a/Doc/library/calendar.rst
+++ b/Doc/library/calendar.rst
@@ -170,9 +170,9 @@ it's the base calendar for all computations.
.. class:: LocaleTextCalendar(firstweekday=0, locale=None)
This subclass of :class:`TextCalendar` can be passed a locale name in the
- constructor and will return month and weekday names in the specified
- locale. If this locale includes an encoding all strings containing month and
- weekday names will be returned as unicode.
+ constructor and will return month and weekday names in the specified locale.
+ If this locale includes an encoding all strings containing month and weekday
+ names will be returned as unicode.
.. class:: LocaleHTMLCalendar(firstweekday=0, locale=None)
@@ -182,6 +182,12 @@ it's the base calendar for all computations.
locale. If this locale includes an encoding all strings containing month and
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.
diff --git a/Lib/calendar.py b/Lib/calendar.py
index 12bd1a8..84aa3a4 100644
--- a/Lib/calendar.py
+++ b/Lib/calendar.py
@@ -486,8 +486,8 @@ class different_locale:
self.locale = locale
def __enter__(self):
- self.oldlocale = _locale.setlocale(_locale.LC_TIME, self.locale)
- #return _locale.getlocale(_locale.LC_TIME)[1]
+ self.oldlocale = _locale.getlocale(_locale.LC_TIME)
+ _locale.setlocale(_locale.LC_TIME, self.locale)
def __exit__(self, *args):
_locale.setlocale(_locale.LC_TIME, self.oldlocale)
diff --git a/Lib/test/test_calendar.py b/Lib/test/test_calendar.py
index b936acb..f906bc3 100644
--- a/Lib/test/test_calendar.py
+++ b/Lib/test/test_calendar.py
@@ -3,6 +3,7 @@ import unittest
from test import support
import time
+import locale
result_2004_text = """
2004
@@ -250,6 +251,22 @@ class CalendarTestCase(unittest.TestCase):
# verify it "acts like a sequence" in two forms of iteration
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):
def setUp(self):
diff --git a/Misc/NEWS b/Misc/NEWS
index 4b92ef6..066c667 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -34,6 +34,8 @@ Core and Builtins
Library
-------
+- Issue #10092: Properly reset locale in calendar.Locale*Calendar classes.
+
- logging: Added _logRecordClass, getLogRecordClass, setLogRecordClass to
increase flexibility of LogRecord creation.