summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorPetri Lehtinen <petri@digip.org>2011-11-04 19:35:07 (GMT)
committerPetri Lehtinen <petri@digip.org>2011-11-04 20:21:07 (GMT)
commit3c85fe07f40a18d9f5733e85b213a6992c5b2ed4 (patch)
treec142b9710010953017c497ee9f88e463ccbe54d3 /Lib
parent12b66b5217d7cbccbeb918683f6df8ab9ae84c3d (diff)
downloadcpython-3c85fe07f40a18d9f5733e85b213a6992c5b2ed4.zip
cpython-3c85fe07f40a18d9f5733e85b213a6992c5b2ed4.tar.gz
cpython-3c85fe07f40a18d9f5733e85b213a6992c5b2ed4.tar.bz2
Issue #3067: Fix the error raised by locale.setlocale()
Diffstat (limited to 'Lib')
-rw-r--r--Lib/locale.py18
-rw-r--r--Lib/test/test_locale.py8
2 files changed, 19 insertions, 7 deletions
diff --git a/Lib/locale.py b/Lib/locale.py
index 3dc4caf..58cf0a7 100644
--- a/Lib/locale.py
+++ b/Lib/locale.py
@@ -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')):
diff --git a/Lib/test/test_locale.py b/Lib/test/test_locale.py
index 5d4b5fb..30480c1 100644
--- a/Lib/test/test_locale.py
+++ b/Lib/test/test_locale.py
@@ -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 = [