Issue #3067: Fix the error raised by locale.setlocale()

This commit is contained in:
Petri Lehtinen 2011-11-04 22:21:52 +02:00
commit c9f38462ee
4 changed files with 23 additions and 7 deletions

View 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')):

View File

@ -409,6 +409,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 = [

View File

@ -768,6 +768,7 @@ John Popplewell
Amrit Prem
Paul Prescod
Donovan Preston
Jyrki Pulliainen
Steve Purcell
Fernando Pérez
Eduardo Pérez

View File

@ -350,6 +350,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.