[Python-checkins] cpython (2.7): Closes #25742: locale.setlocale() now accepts a Unicode string for its second

victor.stinner python-checkins at python.org
Fri Nov 27 17:55:37 EST 2015


https://hg.python.org/cpython/rev/7841e9b614eb
changeset:   99379:7841e9b614eb
branch:      2.7
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Fri Nov 27 23:54:36 2015 +0100
summary:
  Closes #25742: locale.setlocale() now accepts a Unicode string for its second
parameter.

files:
  Lib/locale.py           |   6 +++++-
  Lib/test/test_locale.py |  10 ++++++++++
  Misc/NEWS               |   3 +++
  3 files changed, 18 insertions(+), 1 deletions(-)


diff --git a/Lib/locale.py b/Lib/locale.py
--- a/Lib/locale.py
+++ b/Lib/locale.py
@@ -18,6 +18,10 @@
 import operator
 import functools
 
+# keep a copy of the builtin str type, because 'str' name is overriden
+# in globals by a function below
+_str = str
+
 try:
     _unicode = unicode
 except NameError:
@@ -573,7 +577,7 @@
         category may be given as one of the LC_* values.
 
     """
-    if locale and type(locale) is not type(""):
+    if locale and not isinstance(locale, (_str, _unicode)):
         # convert to string
         locale = normalize(_build_localename(locale))
     return _setlocale(category, locale)
diff --git a/Lib/test/test_locale.py b/Lib/test/test_locale.py
--- a/Lib/test/test_locale.py
+++ b/Lib/test/test_locale.py
@@ -493,6 +493,16 @@
         # longer accept unicode strings.
         self.assertEqual(locale.normalize(u'en_US'), 'en_US.ISO8859-1')
 
+    def test_setlocale_unicode(self):
+        old_loc = locale.getlocale(locale.LC_ALL)
+        try:
+            user_locale = locale.setlocale(locale.LC_ALL, '')
+            unicode_locale = user_locale.decode('utf-8')
+            user_locale2 = locale.setlocale(locale.LC_ALL, unicode_locale)
+            self.assertEqual(user_locale, user_locale2)
+        finally:
+            locale.setlocale(locale.LC_ALL, old_loc)
+
 
 def test_main():
     tests = [
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -20,6 +20,9 @@
 Library
 -------
 
+- Issue #25742: :func:`locale.setlocale` now accepts a Unicode string for
+  its second parameter.
+
 - Issue #10131: Fixed deep copying of minidom documents.  Based on patch
   by Marian Ganisin.
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list