blob: 58f2f04fcdf935fee486ff50619703b1f7a0bca9 [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
12
Larry Hastings68386bc2012-06-24 14:30:41 -070013if uname().system == "Darwin":
14 maj, min, mic = [int(part) for part in uname().release.split(".")]
Skip Montanarof8948ca2005-09-19 03:54:46 +000015 if (maj, min, mic) < (8, 0, 0):
Benjamin Petersone549ead2009-03-28 21:42:05 +000016 raise unittest.SkipTest("locale support broken for OS X < 10.4")
Martin v. Löwisf5b93732003-09-04 18:24:47 +000017
18candidate_locales = ['es_UY', 'fr_FR', 'fi_FI', 'es_CO', 'pt_PT', 'it_IT',
Jeremy Hyltonb7b1db92003-09-10 20:19:54 +000019 'et_EE', 'es_PY', 'no_NO', 'nl_NL', 'lv_LV', 'el_GR', 'be_BY', 'fr_BE',
20 'ro_RO', 'ru_UA', 'ru_RU', 'es_VE', 'ca_ES', 'se_NO', 'es_EC', 'id_ID',
Victor Stinner69291c42011-12-09 10:28:45 +010021 'ka_GE', 'es_CL', 'wa_BE', 'hu_HU', 'lt_LT', 'sl_SI', 'hr_HR', 'es_AR',
Jeremy Hyltonb7b1db92003-09-10 20:19:54 +000022 'es_ES', 'oc_FR', 'gl_ES', 'bg_BG', 'is_IS', 'mk_MK', 'de_AT', 'pt_BR',
23 'da_DK', 'nn_NO', 'cs_CZ', 'de_LU', 'es_BO', 'sq_AL', 'sk_SK', 'fr_CH',
24 'de_DE', 'sr_YU', 'br_FR', 'nl_BE', 'sv_FI', 'pl_PL', 'fr_CA', 'fo_FO',
25 'bs_BA', 'fr_LU', 'kl_GL', 'fa_IR', 'de_BE', 'sv_SE', 'it_CH', 'uk_UA',
Serhiy Storchaka09545852015-02-18 08:04:37 +020026 'eu_ES', 'vi_VN', 'af_ZA', 'nb_NO', 'en_DK', 'tg_TJ', 'ps_AF', 'en_US',
Serhiy Storchaka07c00252015-06-08 18:48:33 +030027 'fr_FR.ISO8859-1', 'fr_FR.UTF-8', 'fr_FR.ISO8859-15@euro',
28 'ru_RU.KOI8-R', 'ko_KR.eucKR']
Martin v. Löwisf5b93732003-09-04 18:24:47 +000029
Serhiy Storchaka09545852015-02-18 08:04:37 +020030def setUpModule():
31 global candidate_locales
32 # Issue #13441: Skip some locales (e.g. cs_CZ and hu_HU) on Solaris to
33 # workaround a mbstowcs() bug. For example, on Solaris, the hu_HU locale uses
34 # the locale encoding ISO-8859-2, the thousauds separator is b'\xA0' and it is
35 # decoded as U+30000020 (an invalid character) by mbstowcs().
36 if sys.platform == 'sunos5':
37 old_locale = locale.setlocale(locale.LC_ALL)
38 try:
39 locales = []
40 for loc in candidate_locales:
41 try:
42 locale.setlocale(locale.LC_ALL, loc)
43 except Error:
44 continue
45 encoding = locale.getpreferredencoding(False)
46 try:
47 localeconv()
48 except Exception as err:
49 print("WARNING: Skip locale %s (encoding %s): [%s] %s"
50 % (loc, encoding, type(err), err))
51 else:
52 locales.append(loc)
53 candidate_locales = locales
54 finally:
55 locale.setlocale(locale.LC_ALL, old_locale)
Victor Stinner5446bba2011-12-09 01:20:03 +010056
Serhiy Storchaka09545852015-02-18 08:04:37 +020057 # Workaround for MSVC6(debug) crash bug
58 if "MSC v.1200" in sys.version:
59 def accept(loc):
60 a = loc.split(".")
61 return not(len(a) == 2 and len(a[-1]) >= 9)
62 candidate_locales = [loc for loc in candidate_locales if accept(loc)]
Benjamin Peterson7847cd62009-05-05 22:49:38 +000063
Brett Cannone94e74a2005-03-01 03:15:50 +000064# List known locale values to test against when available.
65# Dict formatted as ``<locale> : (<decimal_point>, <thousands_sep>)``. If a
66# value is not known, use '' .
Serhiy Storchaka09545852015-02-18 08:04:37 +020067known_numerics = {
68 'en_US': ('.', ','),
Serhiy Storchaka09545852015-02-18 08:04:37 +020069 'de_DE' : (',', '.'),
Martin Panter6a109362015-12-12 06:53:34 +000070 # The French thousands separator may be a breaking or non-breaking space
71 # depending on the platform, so do not test it
72 'fr_FR' : (',', ''),
Serhiy Storchaka09545852015-02-18 08:04:37 +020073 'ps_AF': ('\u066b', '\u066c'),
74}
Brett Cannone94e74a2005-03-01 03:15:50 +000075
Brett Cannon2ad68e62004-09-06 23:30:27 +000076class _LocaleTests(unittest.TestCase):
77
78 def setUp(self):
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +000079 self.oldlocale = setlocale(LC_ALL)
Brett Cannon2ad68e62004-09-06 23:30:27 +000080
81 def tearDown(self):
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +000082 setlocale(LC_ALL, self.oldlocale)
Brett Cannon2ad68e62004-09-06 23:30:27 +000083
Brett Cannone94e74a2005-03-01 03:15:50 +000084 # Want to know what value was calculated, what it was compared against,
85 # what function was used for the calculation, what type of data was used,
86 # the locale that was supposedly set, and the actual locale that is set.
87 lc_numeric_err_msg = "%s != %s (%s for %s; set to %s, using %s)"
88
89 def numeric_tester(self, calc_type, calc_value, data_type, used_locale):
90 """Compare calculation against known value, if available"""
91 try:
92 set_locale = setlocale(LC_NUMERIC)
93 except Error:
94 set_locale = "<not able to determine>"
95 known_value = known_numerics.get(used_locale,
Guido van Rossumf40e5762007-05-17 20:58:33 +000096 ('', ''))[data_type == 'thousands_sep']
Brett Cannone94e74a2005-03-01 03:15:50 +000097 if known_value and calc_value:
Ezio Melottib3aedd42010-11-20 19:04:17 +000098 self.assertEqual(calc_value, known_value,
Brett Cannone94e74a2005-03-01 03:15:50 +000099 self.lc_numeric_err_msg % (
100 calc_value, known_value,
101 calc_type, data_type, set_locale,
102 used_locale))
Serhiy Storchaka09545852015-02-18 08:04:37 +0200103 return True
Brett Cannone94e74a2005-03-01 03:15:50 +0000104
Benjamin Peterson7847cd62009-05-05 22:49:38 +0000105 @unittest.skipUnless(nl_langinfo, "nl_langinfo is not available")
Brett Cannone94e74a2005-03-01 03:15:50 +0000106 def test_lc_numeric_nl_langinfo(self):
107 # Test nl_langinfo against known values
Serhiy Storchaka09545852015-02-18 08:04:37 +0200108 tested = False
Brett Cannone94e74a2005-03-01 03:15:50 +0000109 for loc in candidate_locales:
110 try:
111 setlocale(LC_NUMERIC, loc)
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +0000112 setlocale(LC_CTYPE, loc)
Brett Cannone94e74a2005-03-01 03:15:50 +0000113 except Error:
114 continue
Benjamin Peterson7847cd62009-05-05 22:49:38 +0000115 for li, lc in ((RADIXCHAR, "decimal_point"),
116 (THOUSEP, "thousands_sep")):
Serhiy Storchaka09545852015-02-18 08:04:37 +0200117 if self.numeric_tester('nl_langinfo', nl_langinfo(li), lc, loc):
118 tested = True
119 if not tested:
120 self.skipTest('no suitable locales')
Brett Cannone94e74a2005-03-01 03:15:50 +0000121
122 def test_lc_numeric_localeconv(self):
123 # Test localeconv against known values
Serhiy Storchaka09545852015-02-18 08:04:37 +0200124 tested = False
Brett Cannone94e74a2005-03-01 03:15:50 +0000125 for loc in candidate_locales:
126 try:
127 setlocale(LC_NUMERIC, loc)
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +0000128 setlocale(LC_CTYPE, loc)
Brett Cannone94e74a2005-03-01 03:15:50 +0000129 except Error:
130 continue
Victor Stinner69291c42011-12-09 10:28:45 +0100131 formatting = localeconv()
Benjamin Peterson7847cd62009-05-05 22:49:38 +0000132 for lc in ("decimal_point",
133 "thousands_sep"):
Serhiy Storchaka09545852015-02-18 08:04:37 +0200134 if self.numeric_tester('localeconv', formatting[lc], lc, loc):
135 tested = True
136 if not tested:
137 self.skipTest('no suitable locales')
Brett Cannone94e74a2005-03-01 03:15:50 +0000138
Benjamin Peterson7847cd62009-05-05 22:49:38 +0000139 @unittest.skipUnless(nl_langinfo, "nl_langinfo is not available")
Brett Cannone94e74a2005-03-01 03:15:50 +0000140 def test_lc_numeric_basic(self):
141 # Test nl_langinfo against localeconv
Serhiy Storchaka09545852015-02-18 08:04:37 +0200142 tested = False
Brett Cannon2ad68e62004-09-06 23:30:27 +0000143 for loc in candidate_locales:
144 try:
145 setlocale(LC_NUMERIC, loc)
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +0000146 setlocale(LC_CTYPE, loc)
Brett Cannon2ad68e62004-09-06 23:30:27 +0000147 except Error:
148 continue
Benjamin Peterson7847cd62009-05-05 22:49:38 +0000149 for li, lc in ((RADIXCHAR, "decimal_point"),
150 (THOUSEP, "thousands_sep")):
Brett Cannon2ad68e62004-09-06 23:30:27 +0000151 nl_radixchar = nl_langinfo(li)
152 li_radixchar = localeconv()[lc]
Brett Cannon85ae1a62004-09-08 02:02:41 +0000153 try:
154 set_locale = setlocale(LC_NUMERIC)
155 except Error:
156 set_locale = "<not able to determine>"
Ezio Melottib3aedd42010-11-20 19:04:17 +0000157 self.assertEqual(nl_radixchar, li_radixchar,
Brett Cannone94e74a2005-03-01 03:15:50 +0000158 "%s (nl_langinfo) != %s (localeconv) "
159 "(set to %s, using %s)" % (
160 nl_radixchar, li_radixchar,
161 loc, set_locale))
Serhiy Storchaka09545852015-02-18 08:04:37 +0200162 tested = True
163 if not tested:
164 self.skipTest('no suitable locales')
Brett Cannone94e74a2005-03-01 03:15:50 +0000165
Fredrik Lundh24f0fa92005-12-29 20:35:52 +0000166 def test_float_parsing(self):
167 # Bug #1391872: Test whether float parsing is okay on European
168 # locales.
Serhiy Storchaka09545852015-02-18 08:04:37 +0200169 tested = False
Fredrik Lundh24f0fa92005-12-29 20:35:52 +0000170 for loc in candidate_locales:
171 try:
172 setlocale(LC_NUMERIC, loc)
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +0000173 setlocale(LC_CTYPE, loc)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +0000174 except Error:
175 continue
Neal Norwitzbb459732006-02-19 00:13:15 +0000176
177 # Ignore buggy locale databases. (Mac OS 10.4 and some other BSDs)
178 if loc == 'eu_ES' and localeconv()['decimal_point'] == "' ":
179 continue
180
Ezio Melottib3aedd42010-11-20 19:04:17 +0000181 self.assertEqual(int(eval('3.14') * 100), 314,
Brett Cannon2dbf2a92006-01-19 07:09:09 +0000182 "using eval('3.14') failed for %s" % loc)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000183 self.assertEqual(int(float('3.14') * 100), 314,
Brett Cannon2dbf2a92006-01-19 07:09:09 +0000184 "using float('3.14') failed for %s" % loc)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000185 if localeconv()['decimal_point'] != '.':
186 self.assertRaises(ValueError, float,
187 localeconv()['decimal_point'].join(['1', '23']))
Serhiy Storchaka09545852015-02-18 08:04:37 +0200188 tested = True
189 if not tested:
190 self.skipTest('no suitable locales')
Fredrik Lundh24f0fa92005-12-29 20:35:52 +0000191
Brett Cannon2ad68e62004-09-06 23:30:27 +0000192
193if __name__ == '__main__':
Serhiy Storchaka09545852015-02-18 08:04:37 +0200194 unittest.main()