Commit c9f38462 authored by Petri Lehtinen's avatar Petri Lehtinen

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

parents e30c0a10 3c85fe07
...@@ -440,13 +440,17 @@ def _build_localename(localetuple): ...@@ -440,13 +440,17 @@ def _build_localename(localetuple):
No aliasing or normalizing takes place. No aliasing or normalizing takes place.
""" """
try:
language, encoding = localetuple language, encoding = localetuple
if language is None: if language is None:
language = 'C' language = 'C'
if encoding is None: if encoding is None:
return language return language
else: else:
return language + '.' + encoding 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')): def getdefaultlocale(envvars=('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE')):
......
...@@ -409,6 +409,14 @@ class TestMiscellaneous(unittest.TestCase): ...@@ -409,6 +409,14 @@ class TestMiscellaneous(unittest.TestCase):
locale.setlocale(locale.LC_CTYPE, loc) locale.setlocale(locale.LC_CTYPE, loc)
self.assertEqual(loc, locale.getlocale(locale.LC_CTYPE)) 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(): def test_main():
tests = [ tests = [
......
...@@ -768,6 +768,7 @@ John Popplewell ...@@ -768,6 +768,7 @@ John Popplewell
Amrit Prem Amrit Prem
Paul Prescod Paul Prescod
Donovan Preston Donovan Preston
Jyrki Pulliainen
Steve Purcell Steve Purcell
Fernando Pérez Fernando Pérez
Eduardo Pérez Eduardo Pérez
......
...@@ -350,6 +350,9 @@ Core and Builtins ...@@ -350,6 +350,9 @@ Core and Builtins
Library 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 #13140: Fix the daemon_threads attribute of ThreadingMixIn.
- Issue #13339: Fix compile error in posixmodule.c due to missing semicolon. - Issue #13339: Fix compile error in posixmodule.c due to missing semicolon.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment