[Python-checkins] cpython (3.2): Issue #3067: Fix the error raised by locale.setlocale()

petri.lehtinen python-checkins at python.org
Fri Nov 4 21:41:47 CET 2011


http://hg.python.org/cpython/rev/931ae170e51c
changeset:   73360:931ae170e51c
branch:      3.2
parent:      73348:94017ce9304d
user:        Petri Lehtinen <petri at digip.org>
date:        Fri Nov 04 21:35:07 2011 +0200
summary:
  Issue #3067: Fix the error raised by locale.setlocale()

files:
  Lib/locale.py           |  18 +++++++++++-------
  Lib/test/test_locale.py |   8 ++++++++
  Misc/ACKS               |   1 +
  Misc/NEWS               |   3 +++
  4 files changed, 23 insertions(+), 7 deletions(-)


diff --git a/Lib/locale.py b/Lib/locale.py
--- a/Lib/locale.py
+++ b/Lib/locale.py
@@ -440,13 +440,17 @@
         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')):
 
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
@@ -407,6 +407,14 @@
         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 = [
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -717,6 +717,7 @@
 Amrit Prem
 Paul Prescod
 Donovan Preston
+Jyrki Pulliainen
 Steve Purcell
 Fernando Pérez
 Eduardo Pérez
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -66,6 +66,9 @@
 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.

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


More information about the Python-checkins mailing list