]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #3067: Fix the error raised by locale.setlocale()
authorPetri Lehtinen <petri@digip.org>
Fri, 4 Nov 2011 19:35:07 +0000 (21:35 +0200)
committerPetri Lehtinen <petri@digip.org>
Fri, 4 Nov 2011 20:21:07 +0000 (22:21 +0200)
Lib/locale.py
Lib/test/test_locale.py
Misc/ACKS
Misc/NEWS

index 3dc4cafcb152030f83c6d089f18128d36de1fb1c..58cf0a75b37c42b9f7e22ba7756b96ba60a03ed9 100644 (file)
@@ -440,13 +440,17 @@ def _build_localename(localetuple):
         No aliasing or normalizing takes place.
 
     """
-    language, encoding = localetuple
-    if language is None:
-        language = 'C'
-    if encoding is None:
-        return language
-    else:
-        return language + '.' + encoding
+    try:
+        language, encoding = localetuple
+
+        if language is None:
+            language = 'C'
+        if encoding is None:
+            return language
+        else:
+            return language + '.' + encoding
+    except (TypeError, ValueError):
+        raise TypeError('Locale must be None, a string, or an iterable of two strings -- language code, encoding.')
 
 def getdefaultlocale(envvars=('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE')):
 
index 5d4b5fb420a6cea394775a8cf938c444f53ce7cf..30480c15b964716461ca0e804471ae6410df02ef 100644 (file)
@@ -407,6 +407,14 @@ class TestMiscellaneous(unittest.TestCase):
         locale.setlocale(locale.LC_CTYPE, loc)
         self.assertEqual(loc, locale.getlocale(locale.LC_CTYPE))
 
+    def test_invalid_locale_format_in_localetuple(self):
+        with self.assertRaises(TypeError):
+            locale.setlocale(locale.LC_ALL, b'fi_FI')
+
+    def test_invalid_iterable_in_localetuple(self):
+        with self.assertRaises(TypeError):
+            locale.setlocale(locale.LC_ALL, (b'not', b'valid'))
+
 
 def test_main():
     tests = [
index 5536ac60f552caa3584014509b04b3170a970d3a..6f2c2a11e44361b762af4020438c30a1a24f490e 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -717,6 +717,7 @@ John Popplewell
 Amrit Prem
 Paul Prescod
 Donovan Preston
+Jyrki Pulliainen
 Steve Purcell
 Fernando Pérez
 Eduardo Pérez
index c91c094a2fa489605593127dda5822602b83a2db..7ef197731728308a8ffd2d056dde12a3d937cd92 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -66,6 +66,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #3067: locale.setlocale() now raises TypeError if the second
+  argument is an invalid iterable. Initial patch by Jyrki Pulliainen.
+
 - Issue #13140: Fix the daemon_threads attribute of ThreadingMixIn.
 
 - Issue #13339: Fix compile error in posixmodule.c due to missing semicolon.