1 | n/a | from _locale import (setlocale, LC_ALL, LC_CTYPE, LC_NUMERIC, localeconv, Error) |
---|
2 | n/a | try: |
---|
3 | n/a | from _locale import (RADIXCHAR, THOUSEP, nl_langinfo) |
---|
4 | n/a | except ImportError: |
---|
5 | n/a | nl_langinfo = None |
---|
6 | n/a | |
---|
7 | n/a | import locale |
---|
8 | n/a | import sys |
---|
9 | n/a | import unittest |
---|
10 | n/a | from platform import uname |
---|
11 | n/a | |
---|
12 | n/a | if uname().system == "Darwin": |
---|
13 | n/a | maj, min, mic = [int(part) for part in uname().release.split(".")] |
---|
14 | n/a | if (maj, min, mic) < (8, 0, 0): |
---|
15 | n/a | raise unittest.SkipTest("locale support broken for OS X < 10.4") |
---|
16 | n/a | |
---|
17 | n/a | candidate_locales = ['es_UY', 'fr_FR', 'fi_FI', 'es_CO', 'pt_PT', 'it_IT', |
---|
18 | n/a | 'et_EE', 'es_PY', 'no_NO', 'nl_NL', 'lv_LV', 'el_GR', 'be_BY', 'fr_BE', |
---|
19 | n/a | 'ro_RO', 'ru_UA', 'ru_RU', 'es_VE', 'ca_ES', 'se_NO', 'es_EC', 'id_ID', |
---|
20 | n/a | 'ka_GE', 'es_CL', 'wa_BE', 'hu_HU', 'lt_LT', 'sl_SI', 'hr_HR', 'es_AR', |
---|
21 | n/a | 'es_ES', 'oc_FR', 'gl_ES', 'bg_BG', 'is_IS', 'mk_MK', 'de_AT', 'pt_BR', |
---|
22 | n/a | 'da_DK', 'nn_NO', 'cs_CZ', 'de_LU', 'es_BO', 'sq_AL', 'sk_SK', 'fr_CH', |
---|
23 | n/a | 'de_DE', 'sr_YU', 'br_FR', 'nl_BE', 'sv_FI', 'pl_PL', 'fr_CA', 'fo_FO', |
---|
24 | n/a | 'bs_BA', 'fr_LU', 'kl_GL', 'fa_IR', 'de_BE', 'sv_SE', 'it_CH', 'uk_UA', |
---|
25 | n/a | 'eu_ES', 'vi_VN', 'af_ZA', 'nb_NO', 'en_DK', 'tg_TJ', 'ps_AF', 'en_US', |
---|
26 | n/a | 'fr_FR.ISO8859-1', 'fr_FR.UTF-8', 'fr_FR.ISO8859-15@euro', |
---|
27 | n/a | 'ru_RU.KOI8-R', 'ko_KR.eucKR'] |
---|
28 | n/a | |
---|
29 | n/a | def setUpModule(): |
---|
30 | n/a | global candidate_locales |
---|
31 | n/a | # Issue #13441: Skip some locales (e.g. cs_CZ and hu_HU) on Solaris to |
---|
32 | n/a | # workaround a mbstowcs() bug. For example, on Solaris, the hu_HU locale uses |
---|
33 | n/a | # the locale encoding ISO-8859-2, the thousauds separator is b'\xA0' and it is |
---|
34 | n/a | # decoded as U+30000020 (an invalid character) by mbstowcs(). |
---|
35 | n/a | if sys.platform == 'sunos5': |
---|
36 | n/a | old_locale = locale.setlocale(locale.LC_ALL) |
---|
37 | n/a | try: |
---|
38 | n/a | locales = [] |
---|
39 | n/a | for loc in candidate_locales: |
---|
40 | n/a | try: |
---|
41 | n/a | locale.setlocale(locale.LC_ALL, loc) |
---|
42 | n/a | except Error: |
---|
43 | n/a | continue |
---|
44 | n/a | encoding = locale.getpreferredencoding(False) |
---|
45 | n/a | try: |
---|
46 | n/a | localeconv() |
---|
47 | n/a | except Exception as err: |
---|
48 | n/a | print("WARNING: Skip locale %s (encoding %s): [%s] %s" |
---|
49 | n/a | % (loc, encoding, type(err), err)) |
---|
50 | n/a | else: |
---|
51 | n/a | locales.append(loc) |
---|
52 | n/a | candidate_locales = locales |
---|
53 | n/a | finally: |
---|
54 | n/a | locale.setlocale(locale.LC_ALL, old_locale) |
---|
55 | n/a | |
---|
56 | n/a | # Workaround for MSVC6(debug) crash bug |
---|
57 | n/a | if "MSC v.1200" in sys.version: |
---|
58 | n/a | def accept(loc): |
---|
59 | n/a | a = loc.split(".") |
---|
60 | n/a | return not(len(a) == 2 and len(a[-1]) >= 9) |
---|
61 | n/a | candidate_locales = [loc for loc in candidate_locales if accept(loc)] |
---|
62 | n/a | |
---|
63 | n/a | # List known locale values to test against when available. |
---|
64 | n/a | # Dict formatted as ``<locale> : (<decimal_point>, <thousands_sep>)``. If a |
---|
65 | n/a | # value is not known, use '' . |
---|
66 | n/a | known_numerics = { |
---|
67 | n/a | 'en_US': ('.', ','), |
---|
68 | n/a | 'de_DE' : (',', '.'), |
---|
69 | n/a | # The French thousands separator may be a breaking or non-breaking space |
---|
70 | n/a | # depending on the platform, so do not test it |
---|
71 | n/a | 'fr_FR' : (',', ''), |
---|
72 | n/a | 'ps_AF': ('\u066b', '\u066c'), |
---|
73 | n/a | } |
---|
74 | n/a | |
---|
75 | n/a | class _LocaleTests(unittest.TestCase): |
---|
76 | n/a | |
---|
77 | n/a | def setUp(self): |
---|
78 | n/a | self.oldlocale = setlocale(LC_ALL) |
---|
79 | n/a | |
---|
80 | n/a | def tearDown(self): |
---|
81 | n/a | setlocale(LC_ALL, self.oldlocale) |
---|
82 | n/a | |
---|
83 | n/a | # Want to know what value was calculated, what it was compared against, |
---|
84 | n/a | # what function was used for the calculation, what type of data was used, |
---|
85 | n/a | # the locale that was supposedly set, and the actual locale that is set. |
---|
86 | n/a | lc_numeric_err_msg = "%s != %s (%s for %s; set to %s, using %s)" |
---|
87 | n/a | |
---|
88 | n/a | def numeric_tester(self, calc_type, calc_value, data_type, used_locale): |
---|
89 | n/a | """Compare calculation against known value, if available""" |
---|
90 | n/a | try: |
---|
91 | n/a | set_locale = setlocale(LC_NUMERIC) |
---|
92 | n/a | except Error: |
---|
93 | n/a | set_locale = "<not able to determine>" |
---|
94 | n/a | known_value = known_numerics.get(used_locale, |
---|
95 | n/a | ('', ''))[data_type == 'thousands_sep'] |
---|
96 | n/a | if known_value and calc_value: |
---|
97 | n/a | self.assertEqual(calc_value, known_value, |
---|
98 | n/a | self.lc_numeric_err_msg % ( |
---|
99 | n/a | calc_value, known_value, |
---|
100 | n/a | calc_type, data_type, set_locale, |
---|
101 | n/a | used_locale)) |
---|
102 | n/a | return True |
---|
103 | n/a | |
---|
104 | n/a | @unittest.skipUnless(nl_langinfo, "nl_langinfo is not available") |
---|
105 | n/a | def test_lc_numeric_nl_langinfo(self): |
---|
106 | n/a | # Test nl_langinfo against known values |
---|
107 | n/a | tested = False |
---|
108 | n/a | for loc in candidate_locales: |
---|
109 | n/a | try: |
---|
110 | n/a | setlocale(LC_NUMERIC, loc) |
---|
111 | n/a | setlocale(LC_CTYPE, loc) |
---|
112 | n/a | except Error: |
---|
113 | n/a | continue |
---|
114 | n/a | for li, lc in ((RADIXCHAR, "decimal_point"), |
---|
115 | n/a | (THOUSEP, "thousands_sep")): |
---|
116 | n/a | if self.numeric_tester('nl_langinfo', nl_langinfo(li), lc, loc): |
---|
117 | n/a | tested = True |
---|
118 | n/a | if not tested: |
---|
119 | n/a | self.skipTest('no suitable locales') |
---|
120 | n/a | |
---|
121 | n/a | def test_lc_numeric_localeconv(self): |
---|
122 | n/a | # Test localeconv against known values |
---|
123 | n/a | tested = False |
---|
124 | n/a | for loc in candidate_locales: |
---|
125 | n/a | try: |
---|
126 | n/a | setlocale(LC_NUMERIC, loc) |
---|
127 | n/a | setlocale(LC_CTYPE, loc) |
---|
128 | n/a | except Error: |
---|
129 | n/a | continue |
---|
130 | n/a | formatting = localeconv() |
---|
131 | n/a | for lc in ("decimal_point", |
---|
132 | n/a | "thousands_sep"): |
---|
133 | n/a | if self.numeric_tester('localeconv', formatting[lc], lc, loc): |
---|
134 | n/a | tested = True |
---|
135 | n/a | if not tested: |
---|
136 | n/a | self.skipTest('no suitable locales') |
---|
137 | n/a | |
---|
138 | n/a | @unittest.skipUnless(nl_langinfo, "nl_langinfo is not available") |
---|
139 | n/a | def test_lc_numeric_basic(self): |
---|
140 | n/a | # Test nl_langinfo against localeconv |
---|
141 | n/a | tested = False |
---|
142 | n/a | for loc in candidate_locales: |
---|
143 | n/a | try: |
---|
144 | n/a | setlocale(LC_NUMERIC, loc) |
---|
145 | n/a | setlocale(LC_CTYPE, loc) |
---|
146 | n/a | except Error: |
---|
147 | n/a | continue |
---|
148 | n/a | for li, lc in ((RADIXCHAR, "decimal_point"), |
---|
149 | n/a | (THOUSEP, "thousands_sep")): |
---|
150 | n/a | nl_radixchar = nl_langinfo(li) |
---|
151 | n/a | li_radixchar = localeconv()[lc] |
---|
152 | n/a | try: |
---|
153 | n/a | set_locale = setlocale(LC_NUMERIC) |
---|
154 | n/a | except Error: |
---|
155 | n/a | set_locale = "<not able to determine>" |
---|
156 | n/a | self.assertEqual(nl_radixchar, li_radixchar, |
---|
157 | n/a | "%s (nl_langinfo) != %s (localeconv) " |
---|
158 | n/a | "(set to %s, using %s)" % ( |
---|
159 | n/a | nl_radixchar, li_radixchar, |
---|
160 | n/a | loc, set_locale)) |
---|
161 | n/a | tested = True |
---|
162 | n/a | if not tested: |
---|
163 | n/a | self.skipTest('no suitable locales') |
---|
164 | n/a | |
---|
165 | n/a | def test_float_parsing(self): |
---|
166 | n/a | # Bug #1391872: Test whether float parsing is okay on European |
---|
167 | n/a | # locales. |
---|
168 | n/a | tested = False |
---|
169 | n/a | for loc in candidate_locales: |
---|
170 | n/a | try: |
---|
171 | n/a | setlocale(LC_NUMERIC, loc) |
---|
172 | n/a | setlocale(LC_CTYPE, loc) |
---|
173 | n/a | except Error: |
---|
174 | n/a | continue |
---|
175 | n/a | |
---|
176 | n/a | # Ignore buggy locale databases. (Mac OS 10.4 and some other BSDs) |
---|
177 | n/a | if loc == 'eu_ES' and localeconv()['decimal_point'] == "' ": |
---|
178 | n/a | continue |
---|
179 | n/a | |
---|
180 | n/a | self.assertEqual(int(eval('3.14') * 100), 314, |
---|
181 | n/a | "using eval('3.14') failed for %s" % loc) |
---|
182 | n/a | self.assertEqual(int(float('3.14') * 100), 314, |
---|
183 | n/a | "using float('3.14') failed for %s" % loc) |
---|
184 | n/a | if localeconv()['decimal_point'] != '.': |
---|
185 | n/a | self.assertRaises(ValueError, float, |
---|
186 | n/a | localeconv()['decimal_point'].join(['1', '23'])) |
---|
187 | n/a | tested = True |
---|
188 | n/a | if not tested: |
---|
189 | n/a | self.skipTest('no suitable locales') |
---|
190 | n/a | |
---|
191 | n/a | |
---|
192 | n/a | if __name__ == '__main__': |
---|
193 | n/a | unittest.main() |
---|