blob: 576642d2134020b7ed7ee9137fb97b58de823632 [file] [log] [blame]
Georg Brandla4f46e12010-02-07 17:03:15 +00001from test.test_support import run_unittest
Hirokazu Yamamotofbf63a72009-06-05 05:15:58 +00002from _locale import (setlocale, LC_NUMERIC, localeconv, Error)
3try:
4 from _locale import (RADIXCHAR, THOUSEP, nl_langinfo)
5except ImportError:
6 nl_langinfo = None
7
Brett Cannon2ad68e62004-09-06 23:30:27 +00008import unittest
Hirokazu Yamamotofbf63a72009-06-05 05:15:58 +00009import sys
Skip Montanarof8948ca2005-09-19 03:54:46 +000010from platform import uname
11
12if uname()[0] == "Darwin":
13 maj, min, mic = [int(part) for part in uname()[2].split(".")]
14 if (maj, min, mic) < (8, 0, 0):
Benjamin Petersonbec087f2009-03-26 21:10:30 +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',
20 'ka_GE', 'es_CL', 'hu_HU', 'wa_BE', 'lt_LT', 'sl_SI', 'hr_HR', 'es_AR',
21 '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 Storchakaad9a1ba2015-02-18 08:04:26 +020025 'eu_ES', 'vi_VN', 'af_ZA', 'nb_NO', 'en_DK', 'tg_TJ', 'ps_AF.UTF-8', 'en_US',
Serhiy Storchaka637c8e82015-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
Hirokazu Yamamotofbf63a72009-06-05 05:15:58 +000029# Workaround for MSVC6(debug) crash bug
30if "MSC v.1200" in sys.version:
31 def accept(loc):
32 a = loc.split(".")
33 return not(len(a) == 2 and len(a[-1]) >= 9)
34 candidate_locales = [loc for loc in candidate_locales if accept(loc)]
35
Brett Cannone94e74a2005-03-01 03:15:50 +000036# List known locale values to test against when available.
37# Dict formatted as ``<locale> : (<decimal_point>, <thousands_sep>)``. If a
38# value is not known, use '' .
Serhiy Storchakaad9a1ba2015-02-18 08:04:26 +020039known_numerics = {
40 'en_US': ('.', ','),
Serhiy Storchakaad9a1ba2015-02-18 08:04:26 +020041 'de_DE' : (',', '.'),
Martin Panter7b9e7ed2015-12-12 06:53:34 +000042 # The French thousands separator may be a breaking or non-breaking space
43 # depending on the platform, so do not test it
44 'fr_FR' : (',', ''),
Serhiy Storchakaad9a1ba2015-02-18 08:04:26 +020045 'ps_AF.UTF-8' : ('\xd9\xab', '\xd9\xac'),
46}
Brett Cannone94e74a2005-03-01 03:15:50 +000047
Brett Cannon2ad68e62004-09-06 23:30:27 +000048class _LocaleTests(unittest.TestCase):
49
50 def setUp(self):
51 self.oldlocale = setlocale(LC_NUMERIC)
52
53 def tearDown(self):
54 setlocale(LC_NUMERIC, self.oldlocale)
55
Brett Cannone94e74a2005-03-01 03:15:50 +000056 # Want to know what value was calculated, what it was compared against,
57 # what function was used for the calculation, what type of data was used,
58 # the locale that was supposedly set, and the actual locale that is set.
59 lc_numeric_err_msg = "%s != %s (%s for %s; set to %s, using %s)"
60
61 def numeric_tester(self, calc_type, calc_value, data_type, used_locale):
62 """Compare calculation against known value, if available"""
63 try:
64 set_locale = setlocale(LC_NUMERIC)
65 except Error:
66 set_locale = "<not able to determine>"
67 known_value = known_numerics.get(used_locale,
Benjamin Petersonb3af6012009-01-16 02:55:24 +000068 ('', ''))[data_type == 'thousands_sep']
Brett Cannone94e74a2005-03-01 03:15:50 +000069 if known_value and calc_value:
Ezio Melotti2623a372010-11-21 13:34:58 +000070 self.assertEqual(calc_value, known_value,
Brett Cannone94e74a2005-03-01 03:15:50 +000071 self.lc_numeric_err_msg % (
72 calc_value, known_value,
73 calc_type, data_type, set_locale,
74 used_locale))
Serhiy Storchakaad9a1ba2015-02-18 08:04:26 +020075 return True
Brett Cannone94e74a2005-03-01 03:15:50 +000076
Hirokazu Yamamotofbf63a72009-06-05 05:15:58 +000077 @unittest.skipUnless(nl_langinfo, "nl_langinfo is not available")
Brett Cannone94e74a2005-03-01 03:15:50 +000078 def test_lc_numeric_nl_langinfo(self):
79 # Test nl_langinfo against known values
Serhiy Storchakaad9a1ba2015-02-18 08:04:26 +020080 tested = False
Brett Cannone94e74a2005-03-01 03:15:50 +000081 for loc in candidate_locales:
82 try:
83 setlocale(LC_NUMERIC, loc)
84 except Error:
85 continue
86 for li, lc in ((RADIXCHAR, "decimal_point"),
87 (THOUSEP, "thousands_sep")):
Serhiy Storchakaad9a1ba2015-02-18 08:04:26 +020088 if self.numeric_tester('nl_langinfo', nl_langinfo(li), lc, loc):
89 tested = True
90 if not tested:
91 self.skipTest('no suitable locales')
Brett Cannone94e74a2005-03-01 03:15:50 +000092
93 def test_lc_numeric_localeconv(self):
94 # Test localeconv against known values
Serhiy Storchakaad9a1ba2015-02-18 08:04:26 +020095 tested = False
Brett Cannone94e74a2005-03-01 03:15:50 +000096 for loc in candidate_locales:
97 try:
98 setlocale(LC_NUMERIC, loc)
99 except Error:
100 continue
Serhiy Storchakaad9a1ba2015-02-18 08:04:26 +0200101 formatting = localeconv()
Georg Brandl9be59982009-06-06 05:54:34 +0000102 for lc in ("decimal_point", "thousands_sep"):
Serhiy Storchakaad9a1ba2015-02-18 08:04:26 +0200103 if self.numeric_tester('localeconv', formatting[lc], lc, loc):
104 tested = True
105 if not tested:
106 self.skipTest('no suitable locales')
Brett Cannone94e74a2005-03-01 03:15:50 +0000107
Hirokazu Yamamotofbf63a72009-06-05 05:15:58 +0000108 @unittest.skipUnless(nl_langinfo, "nl_langinfo is not available")
Brett Cannone94e74a2005-03-01 03:15:50 +0000109 def test_lc_numeric_basic(self):
110 # Test nl_langinfo against localeconv
Serhiy Storchakaad9a1ba2015-02-18 08:04:26 +0200111 tested = False
Brett Cannon2ad68e62004-09-06 23:30:27 +0000112 for loc in candidate_locales:
113 try:
114 setlocale(LC_NUMERIC, loc)
115 except Error:
116 continue
117 for li, lc in ((RADIXCHAR, "decimal_point"),
118 (THOUSEP, "thousands_sep")):
119 nl_radixchar = nl_langinfo(li)
120 li_radixchar = localeconv()[lc]
Brett Cannon85ae1a62004-09-08 02:02:41 +0000121 try:
122 set_locale = setlocale(LC_NUMERIC)
123 except Error:
124 set_locale = "<not able to determine>"
Ezio Melotti2623a372010-11-21 13:34:58 +0000125 self.assertEqual(nl_radixchar, li_radixchar,
Brett Cannone94e74a2005-03-01 03:15:50 +0000126 "%s (nl_langinfo) != %s (localeconv) "
127 "(set to %s, using %s)" % (
128 nl_radixchar, li_radixchar,
129 loc, set_locale))
Serhiy Storchakaad9a1ba2015-02-18 08:04:26 +0200130 tested = True
131 if not tested:
132 self.skipTest('no suitable locales')
Brett Cannone94e74a2005-03-01 03:15:50 +0000133
Fredrik Lundh24f0fa92005-12-29 20:35:52 +0000134 def test_float_parsing(self):
135 # Bug #1391872: Test whether float parsing is okay on European
136 # locales.
Serhiy Storchakaad9a1ba2015-02-18 08:04:26 +0200137 tested = False
Fredrik Lundh24f0fa92005-12-29 20:35:52 +0000138 for loc in candidate_locales:
139 try:
140 setlocale(LC_NUMERIC, loc)
141 except Error:
142 continue
Neal Norwitzbb459732006-02-19 00:13:15 +0000143
144 # Ignore buggy locale databases. (Mac OS 10.4 and some other BSDs)
145 if loc == 'eu_ES' and localeconv()['decimal_point'] == "' ":
146 continue
147
Ezio Melotti2623a372010-11-21 13:34:58 +0000148 self.assertEqual(int(eval('3.14') * 100), 314,
Brett Cannon2dbf2a92006-01-19 07:09:09 +0000149 "using eval('3.14') failed for %s" % loc)
Ezio Melotti2623a372010-11-21 13:34:58 +0000150 self.assertEqual(int(float('3.14') * 100), 314,
Brett Cannon2dbf2a92006-01-19 07:09:09 +0000151 "using float('3.14') failed for %s" % loc)
Martin v. Löwisfcfff0a2006-07-03 12:19:50 +0000152 if localeconv()['decimal_point'] != '.':
153 self.assertRaises(ValueError, float,
154 localeconv()['decimal_point'].join(['1', '23']))
Serhiy Storchakaad9a1ba2015-02-18 08:04:26 +0200155 tested = True
156 if not tested:
157 self.skipTest('no suitable locales')
158
Fredrik Lundh24f0fa92005-12-29 20:35:52 +0000159
Brett Cannon2ad68e62004-09-06 23:30:27 +0000160def test_main():
161 run_unittest(_LocaleTests)
162
163if __name__ == '__main__':
164 test_main()