summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2011-07-24 00:51:01 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2011-07-24 00:51:01 (GMT)
commit4cfae027b39d14a5a43f337c1a38b42d7461dd3a (patch)
treea099cd8fcaabaf4c6cb3aff1e686bcae50f745f6
parenta620facc1f66654c1b3337ed6d7dfa158cfaf8f2 (diff)
downloadcpython-4cfae027b39d14a5a43f337c1a38b42d7461dd3a.zip
cpython-4cfae027b39d14a5a43f337c1a38b42d7461dd3a.tar.gz
cpython-4cfae027b39d14a5a43f337c1a38b42d7461dd3a.tar.bz2
Issue #1813: Fix codec lookup and setting/getting locales under Turkish locales.
-rw-r--r--Lib/locale.py9
-rw-r--r--Lib/test/test_codecs.py14
-rw-r--r--Lib/test/test_locale.py13
-rw-r--r--Misc/NEWS3
-rw-r--r--Python/codecs.c2
5 files changed, 39 insertions, 2 deletions
diff --git a/Lib/locale.py b/Lib/locale.py
index bb4aa37..166538d 100644
--- a/Lib/locale.py
+++ b/Lib/locale.py
@@ -331,6 +331,13 @@ def _test():
# 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 @@ def normalize(localename):
"""
# 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 --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py
index f7f27cc..d434f83 100644
--- a/Lib/test/test_codecs.py
+++ b/Lib/test/test_codecs.py
@@ -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 @@ class CodecsModuleTest(unittest.TestCase):
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 --git a/Lib/test/test_locale.py b/Lib/test/test_locale.py
index 8bb99cb..f3330d0 100644
--- a/Lib/test/test_locale.py
+++ b/Lib/test/test_locale.py
@@ -396,6 +396,19 @@ class TestMiscellaneous(unittest.TestCase):
# 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 --git a/Misc/NEWS b/Misc/NEWS
index 2ad6f63..8ee7221 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -37,6 +37,9 @@ Core and Builtins
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 --git a/Python/codecs.c b/Python/codecs.c
index 57bd0f4..7334eb3 100644
--- a/Python/codecs.c
+++ b/Python/codecs.c
@@ -70,7 +70,7 @@ PyObject *normalizestring(const char *string)
if (ch == ' ')
ch = '-';
else
- ch = tolower(Py_CHARMASK(ch));
+ ch = Py_TOLOWER(Py_CHARMASK(ch));
p[i] = ch;
}
return v;