Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c9f3846

Browse files
committed
Issue #3067: Fix the error raised by locale.setlocale()
2 parents e30c0a1 + 3c85fe0 commit c9f3846

4 files changed

Lines changed: 23 additions & 7 deletions

File tree

Lib/locale.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -440,13 +440,17 @@ def _build_localename(localetuple):
440440
No aliasing or normalizing takes place.
441441
442442
"""
443-
language, encoding = localetuple
444-
if language is None:
445-
language = 'C'
446-
if encoding is None:
447-
return language
448-
else:
449-
return language + '.' + encoding
443+
try:
444+
language, encoding = localetuple
445+
446+
if language is None:
447+
language = 'C'
448+
if encoding is None:
449+
return language
450+
else:
451+
return language + '.' + encoding
452+
except (TypeError, ValueError):
453+
raise TypeError('Locale must be None, a string, or an iterable of two strings -- language code, encoding.')
450454

451455
def getdefaultlocale(envvars=('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE')):
452456

Lib/test/test_locale.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,14 @@ def test_getsetlocale_issue1813(self):
409409
locale.setlocale(locale.LC_CTYPE, loc)
410410
self.assertEqual(loc, locale.getlocale(locale.LC_CTYPE))
411411

412+
def test_invalid_locale_format_in_localetuple(self):
413+
with self.assertRaises(TypeError):
414+
locale.setlocale(locale.LC_ALL, b'fi_FI')
415+
416+
def test_invalid_iterable_in_localetuple(self):
417+
with self.assertRaises(TypeError):
418+
locale.setlocale(locale.LC_ALL, (b'not', b'valid'))
419+
412420

413421
def test_main():
414422
tests = [

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,7 @@ John Popplewell
768768
Amrit Prem
769769
Paul Prescod
770770
Donovan Preston
771+
Jyrki Pulliainen
771772
Steve Purcell
772773
Fernando Pérez
773774
Eduardo Pérez

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,9 @@ Core and Builtins
350350
Library
351351
-------
352352

353+
- Issue #3067: locale.setlocale() now raises TypeError if the second
354+
argument is an invalid iterable. Initial patch by Jyrki Pulliainen.
355+
353356
- Issue #13140: Fix the daemon_threads attribute of ThreadingMixIn.
354357

355358
- Issue #13339: Fix compile error in posixmodule.c due to missing semicolon.

0 commit comments

Comments
 (0)