blob: 59a00bad7d98a3d9fce6188d59c6f7684110b901 [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 locale
Benjamin Peterson7847cd62009-05-05 22:49:38 +00008import sys
Victor Stinner69291c42011-12-09 10:28:45 +01009import unittest
Skip Montanarof8948ca2005-09-19 03:54:46 +000010from platform import uname
11
Larry Hastings68386bc2012-06-24 14:30:41 -070012if uname().system == "Darwin":
13 maj, min, mic = [int(part) for part in uname().release.split(".")]
Skip Montanarof8948ca2005-09-19 03:54:46 +000014 if (maj, min, mic) < (8, 0, 0):
Benjamin Petersone549ead2009-03-28 21:42:05 +000015 raise unittest.SkipTest("locale support broken for OS X < 10.4")
Martin v. Löwisf5b93732003-09-04 18:24:47 +000016
17candidate_locales = ['es_UY', 'fr_FR', 'fi_FI', 'es_CO', 'pt_PT', 'it_IT',
Jeremy Hyltonb7b1db92003-09-10 20:19:54 +000018 'et_EE', 'es_PY', 'no_NO', 'nl_NL', 'lv_LV', 'el_GR', 'be_BY', 'fr_BE',
19 'ro_RO', 'ru_UA', 'ru_RU', 'es_VE', 'ca_ES', 'se_NO', 'es_EC', 'id_ID',
Victor Stinner69291c42011-12-09 10:28:45 +010020 'ka_GE', 'es_CL', 'wa_BE', 'hu_HU', 'lt_LT', 'sl_SI', 'hr_HR', 'es_AR',
Jeremy Hyltonb7b1db92003-09-10 20:19:54 +000021 'es_ES', 'oc_FR', 'gl_ES', 'bg_BG', 'is_IS', 'mk_MK', 'de_AT', 'pt_BR',
22 'da_DK', 'nn_NO', 'cs_CZ', 'de_LU', 'es_BO', 'sq_AL', 'sk_SK', 'fr_CH',
23 'de_DE', 'sr_YU', 'br_FR', 'nl_BE', 'sv_FI', 'pl_PL', 'fr_CA', 'fo_FO',
24 'bs_BA', 'fr_LU', 'kl_GL', 'fa_IR', 'de_BE', 'sv_SE', 'it_CH', 'uk_UA',
Serhiy Storchaka09545852015-02-18 08:04:37 +020025 'eu_ES', 'vi_VN', 'af_ZA', 'nb_NO', 'en_DK', 'tg_TJ', 'ps_AF', 'en_US',
Serhiy Storchaka07c00252015-06-08 18:48:33 +030026 'fr_FR.ISO8859-1', 'fr_FR.UTF-8', 'fr_FR.ISO8859-15@euro',
27 'ru_RU.KOI8-R', 'ko_KR.eucKR']
Martin v. Löwisf5b93732003-09-04 18:24:47 +000028
Serhiy Storchaka09545852015-02-18 08:04:37 +020029def setUpModule():
30 global candidate_locales
31 # Issue #13441: Skip some locales (e.g. cs_CZ and hu_HU) on Solaris to
32 # workaround a mbstowcs() bug. For example, on Solaris, the hu_HU locale uses
Tim Gates2ad76512019-12-10 09:16:01 +110033 # the locale encoding ISO-8859-2, the thousands separator is b'\xA0' and it is
Serhiy Storchaka09545852015-02-18 08:04:37 +020034 # decoded as U+30000020 (an invalid character) by mbstowcs().
35 if sys.platform == 'sunos5':
36 old_locale = locale.setlocale(locale.LC_ALL)
37 try:
38 locales = []
39 for loc in candidate_locales:
40 try:
41 locale.setlocale(locale.LC_ALL, loc)
42 except Error:
43 continue
44 encoding = locale.getpreferredencoding(False)
45 try:
46 localeconv()
47 except Exception as err:
48 print("WARNING: Skip locale %s (encoding %s): [%s] %s"
49 % (loc, encoding, type(err), err))
50 else:
51 locales.append(loc)
52 candidate_locales = locales
53 finally:
54 locale.setlocale(locale.LC_ALL, old_locale)
Victor Stinner5446bba2011-12-09 01:20:03 +010055
Serhiy Storchaka09545852015-02-18 08:04:37 +020056 # Workaround for MSVC6(debug) crash bug
57 if "MSC v.1200" in sys.version:
58 def accept(loc):
59 a = loc.split(".")
60 return not(len(a) == 2 and len(a[-1]) >= 9)
61 candidate_locales = [loc for loc in candidate_locales if accept(loc)]
Benjamin Peterson7847cd62009-05-05 22:49:38 +000062
Brett Cannone94e74a2005-03-01 03:15:50 +000063# List known locale values to test against when available.
64# Dict formatted as ``<locale> : (<decimal_point>, <thousands_sep>)``. If a
65# value is not known, use '' .
Serhiy Storchaka09545852015-02-18 08:04:37 +020066known_numerics = {
67 'en_US': ('.', ','),
Serhiy Storchaka09545852015-02-18 08:04:37 +020068 'de_DE' : (',', '.'),
Martin Panter6a109362015-12-12 06:53:34 +000069 # The French thousands separator may be a breaking or non-breaking space
70 # depending on the platform, so do not test it
71 'fr_FR' : (',', ''),
Serhiy Storchaka09545852015-02-18 08:04:37 +020072 'ps_AF': ('\u066b', '\u066c'),
73}
Brett Cannone94e74a2005-03-01 03:15:50 +000074
TIGirardif2312032020-10-20 08:39:52 -030075if sys.platform == 'win32':
76 # ps_AF doesn't work on Windows: see bpo-38324 (msg361830)
77 del known_numerics['ps_AF']
78
Brett Cannon2ad68e62004-09-06 23:30:27 +000079class _LocaleTests(unittest.TestCase):
80
81 def setUp(self):
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +000082 self.oldlocale = setlocale(LC_ALL)
Brett Cannon2ad68e62004-09-06 23:30:27 +000083
84 def tearDown(self):
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +000085 setlocale(LC_ALL, self.oldlocale)
Brett Cannon2ad68e62004-09-06 23:30:27 +000086
Brett Cannone94e74a2005-03-01 03:15:50 +000087 # Want to know what value was calculated, what it was compared against,
88 # what function was used for the calculation, what type of data was used,
89 # the locale that was supposedly set, and the actual locale that is set.
90 lc_numeric_err_msg = "%s != %s (%s for %s; set to %s, using %s)"
91
92 def numeric_tester(self, calc_type, calc_value, data_type, used_locale):
93 """Compare calculation against known value, if available"""
94 try:
95 set_locale = setlocale(LC_NUMERIC)
96 except Error:
97 set_locale = "<not able to determine>"
98 known_value = known_numerics.get(used_locale,
Guido van Rossumf40e5762007-05-17 20:58:33 +000099 ('', ''))[data_type == 'thousands_sep']
Brett Cannone94e74a2005-03-01 03:15:50 +0000100 if known_value and calc_value:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000101 self.assertEqual(calc_value, known_value,
Brett Cannone94e74a2005-03-01 03:15:50 +0000102 self.lc_numeric_err_msg % (
103 calc_value, known_value,
104 calc_type, data_type, set_locale,
105 used_locale))
Serhiy Storchaka09545852015-02-18 08:04:37 +0200106 return True
Brett Cannone94e74a2005-03-01 03:15:50 +0000107
Benjamin Peterson7847cd62009-05-05 22:49:38 +0000108 @unittest.skipUnless(nl_langinfo, "nl_langinfo is not available")
Brett Cannone94e74a2005-03-01 03:15:50 +0000109 def test_lc_numeric_nl_langinfo(self):
110 # Test nl_langinfo against known values
Serhiy Storchaka09545852015-02-18 08:04:37 +0200111 tested = False
Brett Cannone94e74a2005-03-01 03:15:50 +0000112 for loc in candidate_locales:
113 try:
114 setlocale(LC_NUMERIC, loc)
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +0000115 setlocale(LC_CTYPE, loc)
Brett Cannone94e74a2005-03-01 03:15:50 +0000116 except Error:
117 continue
Benjamin Peterson7847cd62009-05-05 22:49:38 +0000118 for li, lc in ((RADIXCHAR, "decimal_point"),
119 (THOUSEP, "thousands_sep")):
Serhiy Storchaka09545852015-02-18 08:04:37 +0200120 if self.numeric_tester('nl_langinfo', nl_langinfo(li), lc, loc):
121 tested = True
122 if not tested:
123 self.skipTest('no suitable locales')
Brett Cannone94e74a2005-03-01 03:15:50 +0000124
125 def test_lc_numeric_localeconv(self):
126 # Test localeconv against known values
Serhiy Storchaka09545852015-02-18 08:04:37 +0200127 tested = False
Brett Cannone94e74a2005-03-01 03:15:50 +0000128 for loc in candidate_locales:
129 try:
130 setlocale(LC_NUMERIC, loc)
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +0000131 setlocale(LC_CTYPE, loc)
Brett Cannone94e74a2005-03-01 03:15:50 +0000132 except Error:
133 continue
Victor Stinner69291c42011-12-09 10:28:45 +0100134 formatting = localeconv()
Benjamin Peterson7847cd62009-05-05 22:49:38 +0000135 for lc in ("decimal_point",
136 "thousands_sep"):
Serhiy Storchaka09545852015-02-18 08:04:37 +0200137 if self.numeric_tester('localeconv', formatting[lc], lc, loc):
138 tested = True
139 if not tested:
140 self.skipTest('no suitable locales')
Brett Cannone94e74a2005-03-01 03:15:50 +0000141
Benjamin Peterson7847cd62009-05-05 22:49:38 +0000142 @unittest.skipUnless(nl_langinfo, "nl_langinfo is not available")
Brett Cannone94e74a2005-03-01 03:15:50 +0000143 def test_lc_numeric_basic(self):
144 # Test nl_langinfo against localeconv
Serhiy Storchaka09545852015-02-18 08:04:37 +0200145 tested = False
Brett Cannon2ad68e62004-09-06 23:30:27 +0000146 for loc in candidate_locales:
147 try:
148 setlocale(LC_NUMERIC, loc)
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +0000149 setlocale(LC_CTYPE, loc)
Brett Cannon2ad68e62004-09-06 23:30:27 +0000150 except Error:
151 continue
Benjamin Peterson7847cd62009-05-05 22:49:38 +0000152 for li, lc in ((RADIXCHAR, "decimal_point"),
153 (THOUSEP, "thousands_sep")):
Brett Cannon2ad68e62004-09-06 23:30:27 +0000154 nl_radixchar = nl_langinfo(li)
155 li_radixchar = localeconv()[lc]
Brett Cannon85ae1a62004-09-08 02:02:41 +0000156 try:
157 set_locale = setlocale(LC_NUMERIC)
158 except Error:
159 set_locale = "<not able to determine>"
Ezio Melottib3aedd42010-11-20 19:04:17 +0000160 self.assertEqual(nl_radixchar, li_radixchar,
Brett Cannone94e74a2005-03-01 03:15:50 +0000161 "%s (nl_langinfo) != %s (localeconv) "
162 "(set to %s, using %s)" % (
163 nl_radixchar, li_radixchar,
164 loc, set_locale))
Serhiy Storchaka09545852015-02-18 08:04:37 +0200165 tested = True
166 if not tested:
167 self.skipTest('no suitable locales')
Brett Cannone94e74a2005-03-01 03:15:50 +0000168
Fredrik Lundh24f0fa92005-12-29 20:35:52 +0000169 def test_float_parsing(self):
170 # Bug #1391872: Test whether float parsing is okay on European
171 # locales.
Serhiy Storchaka09545852015-02-18 08:04:37 +0200172 tested = False
Fredrik Lundh24f0fa92005-12-29 20:35:52 +0000173 for loc in candidate_locales:
174 try:
175 setlocale(LC_NUMERIC, loc)
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +0000176 setlocale(LC_CTYPE, loc)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +0000177 except Error:
178 continue
Neal Norwitzbb459732006-02-19 00:13:15 +0000179
180 # Ignore buggy locale databases. (Mac OS 10.4 and some other BSDs)
181 if loc == 'eu_ES' and localeconv()['decimal_point'] == "' ":
182 continue
183
Ezio Melottib3aedd42010-11-20 19:04:17 +0000184 self.assertEqual(int(eval('3.14') * 100), 314,
Brett Cannon2dbf2a92006-01-19 07:09:09 +0000185 "using eval('3.14') failed for %s" % loc)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000186 self.assertEqual(int(float('3.14') * 100), 314,
Brett Cannon2dbf2a92006-01-19 07:09:09 +0000187 "using float('3.14') failed for %s" % loc)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000188 if localeconv()['decimal_point'] != '.':
189 self.assertRaises(ValueError, float,
190 localeconv()['decimal_point'].join(['1', '23']))
Serhiy Storchaka09545852015-02-18 08:04:37 +0200191 tested = True
192 if not tested:
193 self.skipTest('no suitable locales')
Fredrik Lundh24f0fa92005-12-29 20:35:52 +0000194
Brett Cannon2ad68e62004-09-06 23:30:27 +0000195
196if __name__ == '__main__':
Serhiy Storchaka09545852015-02-18 08:04:37 +0200197 unittest.main()