changeset: 71492:739958134fe5 branch: 2.7 parent: 71480:dbacafae72d8 user: Antoine Pitrou date: Sun Jul 24 02:51:01 2011 +0200 files: Lib/locale.py Lib/test/test_codecs.py Lib/test/test_locale.py Misc/NEWS Python/codecs.c description: Issue #1813: Fix codec lookup and setting/getting locales under Turkish locales. diff -r dbacafae72d8 -r 739958134fe5 Lib/locale.py --- a/Lib/locale.py Sat Jul 23 17:04:42 2011 +0200 +++ b/Lib/locale.py Sun Jul 24 02:51:01 2011 +0200 @@ -331,6 +331,13 @@ # overridden below) _setlocale = setlocale +# Avoid relying on the locale-dependent .lower() method +# (see issue #1813). +_ascii_lower_map = ''.join( + chr(x + 32 if x >= ord('A') and x <= ord('Z') else x) + for x in range(256) +) + def normalize(localename): """ Returns a normalized locale code for the given locale @@ -348,7 +355,7 @@ """ # Normalize the locale name and extract the encoding - fullname = localename.lower() + fullname = localename.translate(_ascii_lower_map) if ':' in fullname: # ':' is sometimes used as encoding delimiter. fullname = fullname.replace(':', '.') diff -r dbacafae72d8 -r 739958134fe5 Lib/test/test_codecs.py --- a/Lib/test/test_codecs.py Sat Jul 23 17:04:42 2011 +0200 +++ b/Lib/test/test_codecs.py Sun Jul 24 02:51:01 2011 +0200 @@ -1,6 +1,7 @@ from test import test_support import unittest import codecs +import locale import sys, StringIO, _testcapi class Queue(object): @@ -1153,6 +1154,19 @@ self.assertRaises(TypeError, codecs.getwriter) self.assertRaises(LookupError, codecs.getwriter, "__spam__") + def test_lookup_issue1813(self): + # Issue #1813: under Turkish locales, lookup of some codecs failed + # because 'I' is lowercased as a dotless "i" + oldlocale = locale.getlocale(locale.LC_CTYPE) + self.addCleanup(locale.setlocale, locale.LC_CTYPE, oldlocale) + try: + locale.setlocale(locale.LC_CTYPE, 'tr_TR') + except locale.Error: + # Unsupported locale on this system + self.skipTest('test needs Turkish locale') + c = codecs.lookup('ASCII') + self.assertEqual(c.name, 'ascii') + class StreamReaderTest(unittest.TestCase): def setUp(self): diff -r dbacafae72d8 -r 739958134fe5 Lib/test/test_locale.py --- a/Lib/test/test_locale.py Sat Jul 23 17:04:42 2011 +0200 +++ b/Lib/test/test_locale.py Sun Jul 24 02:51:01 2011 +0200 @@ -396,6 +396,19 @@ # crasher from bug #7419 self.assertRaises(locale.Error, locale.setlocale, 12345) + def test_getsetlocale_issue1813(self): + # Issue #1813: setting and getting the locale under a Turkish locale + oldlocale = locale.getlocale() + self.addCleanup(locale.setlocale, locale.LC_CTYPE, oldlocale) + try: + locale.setlocale(locale.LC_CTYPE, 'tr_TR') + except locale.Error: + # Unsupported locale on this system + self.skipTest('test needs Turkish locale') + loc = locale.getlocale() + locale.setlocale(locale.LC_CTYPE, loc) + self.assertEqual(loc, locale.getlocale()) + def test_main(): tests = [ diff -r dbacafae72d8 -r 739958134fe5 Misc/NEWS --- a/Misc/NEWS Sat Jul 23 17:04:42 2011 +0200 +++ b/Misc/NEWS Sun Jul 24 02:51:01 2011 +0200 @@ -37,6 +37,9 @@ Library ------- +- Issue #1813: Fix codec lookup and setting/getting locales under Turkish + locales. + - Issue #10883: Fix socket leaks in urllib when using FTP. - Issue #12592: Make Python build on OpenBSD 5 (and future major releases). diff -r dbacafae72d8 -r 739958134fe5 Python/codecs.c --- a/Python/codecs.c Sat Jul 23 17:04:42 2011 +0200 +++ b/Python/codecs.c Sun Jul 24 02:51:01 2011 +0200 @@ -70,7 +70,7 @@ if (ch == ' ') ch = '-'; else - ch = tolower(Py_CHARMASK(ch)); + ch = Py_TOLOWER(Py_CHARMASK(ch)); p[i] = ch; } return v;