blob: f7f1abdab2088b930a1914f5af736e14e88ead8a [file] [log] [blame]
Benjamin Peterson7847cd62009-05-05 22:49:38 +00001from _locale import (setlocale, LC_ALL, LC_CTYPE, LC_NUMERIC, localeconv, Error)
2try:
3 from _locale import (RADIXCHAR, THOUSEP, nl_langinfo)
4except ImportError:
5 nl_langinfo = None
6
Victor Stinner69291c42011-12-09 10:28:45 +01007import codecs
8import locale
Benjamin Peterson7847cd62009-05-05 22:49:38 +00009import sys
Victor Stinner69291c42011-12-09 10:28:45 +010010import unittest
Skip Montanarof8948ca2005-09-19 03:54:46 +000011from platform import uname
Victor Stinner69291c42011-12-09 10:28:45 +010012from test.support import run_unittest
Skip Montanarof8948ca2005-09-19 03:54:46 +000013
14if uname()[0] == "Darwin":
15 maj, min, mic = [int(part) for part in uname()[2].split(".")]
16 if (maj, min, mic) < (8, 0, 0):
Benjamin Petersone549ead2009-03-28 21:42:05 +000017 raise unittest.SkipTest("locale support broken for OS X < 10.4")
Martin v. Löwisf5b93732003-09-04 18:24:47 +000018
19candidate_locales = ['es_UY', 'fr_FR', 'fi_FI', 'es_CO', 'pt_PT', 'it_IT',
Jeremy Hyltonb7b1db92003-09-10 20:19:54 +000020 'et_EE', 'es_PY', 'no_NO', 'nl_NL', 'lv_LV', 'el_GR', 'be_BY', 'fr_BE',
21 'ro_RO', 'ru_UA', 'ru_RU', 'es_VE', 'ca_ES', 'se_NO', 'es_EC', 'id_ID',
Victor Stinner69291c42011-12-09 10:28:45 +010022 'ka_GE', 'es_CL', 'wa_BE', 'hu_HU', 'lt_LT', 'sl_SI', 'hr_HR', 'es_AR',
Jeremy Hyltonb7b1db92003-09-10 20:19:54 +000023 'es_ES', 'oc_FR', 'gl_ES', 'bg_BG', 'is_IS', 'mk_MK', 'de_AT', 'pt_BR',
24 'da_DK', 'nn_NO', 'cs_CZ', 'de_LU', 'es_BO', 'sq_AL', 'sk_SK', 'fr_CH',
25 'de_DE', 'sr_YU', 'br_FR', 'nl_BE', 'sv_FI', 'pl_PL', 'fr_CA', 'fo_FO',
26 'bs_BA', 'fr_LU', 'kl_GL', 'fa_IR', 'de_BE', 'sv_SE', 'it_CH', 'uk_UA',
Brett Cannone94e74a2005-03-01 03:15:50 +000027 'eu_ES', 'vi_VN', 'af_ZA', 'nb_NO', 'en_DK', 'tg_TJ', 'en_US',
Hye-Shik Chang8d2e08d2003-12-19 01:16:03 +000028 'es_ES.ISO8859-1', 'fr_FR.ISO8859-15', 'ru_RU.KOI8-R', 'ko_KR.eucKR']
Martin v. Löwisf5b93732003-09-04 18:24:47 +000029
Victor Stinner69291c42011-12-09 10:28:45 +010030# Issue #13441: Skip some locales (e.g. cs_CZ and hu_HU) on Solaris to
31# workaround a mbstowcs() bug. For example, on Solaris, the hu_HU locale uses
32# the locale encoding ISO-8859-2, the thousauds separator is b'\xA0' and it is
33# decoded as U+30000020 (an invalid character) by mbstowcs().
34if sys.platform == 'sunos5':
35 old_locale = locale.setlocale(locale.LC_ALL)
36 try:
37 locales = []
38 for loc in candidate_locales:
39 try:
40 locale.setlocale(locale.LC_ALL, loc)
41 except Error:
42 continue
43 encoding = locale.getpreferredencoding(False)
44 try:
45 localeconv()
46 except Exception as err:
47 print("WARNING: Skip locale %s (encoding %s): [%s] %s"
48 % (loc, encoding, type(err), err))
49 else:
50 locales.append(loc)
Victor Stinner7f54f752011-12-09 11:29:44 +010051 candidate_locales = locales
Victor Stinner69291c42011-12-09 10:28:45 +010052 finally:
53 locale.setlocale(locale.LC_ALL, old_locale)
Victor Stinner5446bba2011-12-09 01:20:03 +010054
Benjamin Peterson7847cd62009-05-05 22:49:38 +000055# Workaround for MSVC6(debug) crash bug
56if "MSC v.1200" in sys.version:
57 def accept(loc):
58 a = loc.split(".")
59 return not(len(a) == 2 and len(a[-1]) >= 9)
60 candidate_locales = [loc for loc in candidate_locales if accept(loc)]
61
Brett Cannone94e74a2005-03-01 03:15:50 +000062# List known locale values to test against when available.
63# Dict formatted as ``<locale> : (<decimal_point>, <thousands_sep>)``. If a
64# value is not known, use '' .
65known_numerics = {'fr_FR' : (',', ''), 'en_US':('.', ',')}
66
Brett Cannon2ad68e62004-09-06 23:30:27 +000067class _LocaleTests(unittest.TestCase):
68
69 def setUp(self):
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +000070 self.oldlocale = setlocale(LC_ALL)
Brett Cannon2ad68e62004-09-06 23:30:27 +000071
72 def tearDown(self):
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +000073 setlocale(LC_ALL, self.oldlocale)
Brett Cannon2ad68e62004-09-06 23:30:27 +000074
Brett Cannone94e74a2005-03-01 03:15:50 +000075 # Want to know what value was calculated, what it was compared against,
76 # what function was used for the calculation, what type of data was used,
77 # the locale that was supposedly set, and the actual locale that is set.
78 lc_numeric_err_msg = "%s != %s (%s for %s; set to %s, using %s)"
79
80 def numeric_tester(self, calc_type, calc_value, data_type, used_locale):
81 """Compare calculation against known value, if available"""
82 try:
83 set_locale = setlocale(LC_NUMERIC)
84 except Error:
85 set_locale = "<not able to determine>"
86 known_value = known_numerics.get(used_locale,
Guido van Rossumf40e5762007-05-17 20:58:33 +000087 ('', ''))[data_type == 'thousands_sep']
Brett Cannone94e74a2005-03-01 03:15:50 +000088 if known_value and calc_value:
Ezio Melottib3aedd42010-11-20 19:04:17 +000089 self.assertEqual(calc_value, known_value,
Brett Cannone94e74a2005-03-01 03:15:50 +000090 self.lc_numeric_err_msg % (
91 calc_value, known_value,
92 calc_type, data_type, set_locale,
93 used_locale))
94
Benjamin Peterson7847cd62009-05-05 22:49:38 +000095 @unittest.skipUnless(nl_langinfo, "nl_langinfo is not available")
Brett Cannone94e74a2005-03-01 03:15:50 +000096 def test_lc_numeric_nl_langinfo(self):
97 # Test nl_langinfo against known values
98 for loc in candidate_locales:
99 try:
100 setlocale(LC_NUMERIC, loc)
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +0000101 setlocale(LC_CTYPE, loc)
Brett Cannone94e74a2005-03-01 03:15:50 +0000102 except Error:
103 continue
Benjamin Peterson7847cd62009-05-05 22:49:38 +0000104 for li, lc in ((RADIXCHAR, "decimal_point"),
105 (THOUSEP, "thousands_sep")):
Brett Cannone94e74a2005-03-01 03:15:50 +0000106 self.numeric_tester('nl_langinfo', nl_langinfo(li), lc, loc)
107
108 def test_lc_numeric_localeconv(self):
109 # Test localeconv against known values
110 for loc in candidate_locales:
111 try:
112 setlocale(LC_NUMERIC, loc)
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +0000113 setlocale(LC_CTYPE, loc)
Brett Cannone94e74a2005-03-01 03:15:50 +0000114 except Error:
115 continue
Victor Stinner69291c42011-12-09 10:28:45 +0100116 formatting = localeconv()
Benjamin Peterson7847cd62009-05-05 22:49:38 +0000117 for lc in ("decimal_point",
118 "thousands_sep"):
Victor Stinner70614132011-12-08 23:42:52 +0100119 self.numeric_tester('localeconv', formatting[lc], lc, loc)
Brett Cannone94e74a2005-03-01 03:15:50 +0000120
Benjamin Peterson7847cd62009-05-05 22:49:38 +0000121 @unittest.skipUnless(nl_langinfo, "nl_langinfo is not available")
Brett Cannone94e74a2005-03-01 03:15:50 +0000122 def test_lc_numeric_basic(self):
123 # Test nl_langinfo against localeconv
Brett Cannon2ad68e62004-09-06 23:30:27 +0000124 for loc in candidate_locales:
125 try:
126 setlocale(LC_NUMERIC, loc)
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +0000127 setlocale(LC_CTYPE, loc)
Brett Cannon2ad68e62004-09-06 23:30:27 +0000128 except Error:
129 continue
Benjamin Peterson7847cd62009-05-05 22:49:38 +0000130 for li, lc in ((RADIXCHAR, "decimal_point"),
131 (THOUSEP, "thousands_sep")):
Brett Cannon2ad68e62004-09-06 23:30:27 +0000132 nl_radixchar = nl_langinfo(li)
133 li_radixchar = localeconv()[lc]
Brett Cannon85ae1a62004-09-08 02:02:41 +0000134 try:
135 set_locale = setlocale(LC_NUMERIC)
136 except Error:
137 set_locale = "<not able to determine>"
Ezio Melottib3aedd42010-11-20 19:04:17 +0000138 self.assertEqual(nl_radixchar, li_radixchar,
Brett Cannone94e74a2005-03-01 03:15:50 +0000139 "%s (nl_langinfo) != %s (localeconv) "
140 "(set to %s, using %s)" % (
141 nl_radixchar, li_radixchar,
142 loc, set_locale))
143
Fredrik Lundh24f0fa92005-12-29 20:35:52 +0000144 def test_float_parsing(self):
145 # Bug #1391872: Test whether float parsing is okay on European
146 # locales.
147 for loc in candidate_locales:
148 try:
149 setlocale(LC_NUMERIC, loc)
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +0000150 setlocale(LC_CTYPE, loc)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +0000151 except Error:
152 continue
Neal Norwitzbb459732006-02-19 00:13:15 +0000153
154 # Ignore buggy locale databases. (Mac OS 10.4 and some other BSDs)
155 if loc == 'eu_ES' and localeconv()['decimal_point'] == "' ":
156 continue
157
Ezio Melottib3aedd42010-11-20 19:04:17 +0000158 self.assertEqual(int(eval('3.14') * 100), 314,
Brett Cannon2dbf2a92006-01-19 07:09:09 +0000159 "using eval('3.14') failed for %s" % loc)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000160 self.assertEqual(int(float('3.14') * 100), 314,
Brett Cannon2dbf2a92006-01-19 07:09:09 +0000161 "using float('3.14') failed for %s" % loc)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000162 if localeconv()['decimal_point'] != '.':
163 self.assertRaises(ValueError, float,
164 localeconv()['decimal_point'].join(['1', '23']))
Fredrik Lundh24f0fa92005-12-29 20:35:52 +0000165
Brett Cannon2ad68e62004-09-06 23:30:27 +0000166def test_main():
167 run_unittest(_LocaleTests)
168
169if __name__ == '__main__':
170 test_main()