]> granicus.if.org Git - python/commitdiff
Closes #25742: locale.setlocale() now accepts a Unicode string for its second
authorVictor Stinner <victor.stinner@gmail.com>
Fri, 27 Nov 2015 22:54:36 +0000 (23:54 +0100)
committerVictor Stinner <victor.stinner@gmail.com>
Fri, 27 Nov 2015 22:54:36 +0000 (23:54 +0100)
parameter.

Lib/locale.py
Lib/test/test_locale.py
Misc/NEWS

index 15c53babcb6d80d7d89832ba1696f7bdf01af19b..f547babf84b4b299d16653540d99c4059e55edb8 100644 (file)
@@ -18,6 +18,10 @@ import re
 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 @@ def setlocale(category, locale=None):
         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)
index 719175bed8509cbd77e04135d446066f2b12e584..140388a156f2b617bdeb0bd2ca47eb1daefedb35 100644 (file)
@@ -493,6 +493,16 @@ class TestMiscellaneous(unittest.TestCase):
         # 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 = [
index 8cbddff97edc255af52db40005d42010afc2f2dc..4a32e47272207d0ac88a0cdc704772b765c59e58 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -20,6 +20,9 @@ Core and Builtins
 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.