1from _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
7import locale
8import sys
9import unittest
10from platform import uname
11
12if uname().system == "Darwin":
13    maj, min, mic = [int(part) for part in uname().release.split(".")]
14    if (maj, min, mic) < (8, 0, 0):
15        raise unittest.SkipTest("locale support broken for OS X < 10.4")
16
17candidate_locales = ['es_UY', 'fr_FR', 'fi_FI', 'es_CO', 'pt_PT', 'it_IT',
18    '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', 'wa_BE', 'hu_HU', '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',
25    'eu_ES', 'vi_VN', 'af_ZA', 'nb_NO', 'en_DK', 'tg_TJ', 'ps_AF', 'en_US',
26    'fr_FR.ISO8859-1', 'fr_FR.UTF-8', 'fr_FR.ISO8859-15@euro',
27    'ru_RU.KOI8-R', 'ko_KR.eucKR']
28
29def 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
33    # the locale encoding ISO-8859-2, the thousauds separator is b'\xA0' and it is
34    # 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)
55
56    # 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)]
62
63# 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 '' .
66known_numerics = {
67    'en_US': ('.', ','),
68    'de_DE' : (',', '.'),
69    # 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' : (',', ''),
72    'ps_AF': ('\u066b', '\u066c'),
73}
74
75class _LocaleTests(unittest.TestCase):
76
77    def setUp(self):
78        self.oldlocale = setlocale(LC_ALL)
79
80    def tearDown(self):
81        setlocale(LC_ALL, self.oldlocale)
82
83    # Want to know what value was calculated, what it was compared against,
84    # what function was used for the calculation, what type of data was used,
85    # the locale that was supposedly set, and the actual locale that is set.
86    lc_numeric_err_msg = "%s != %s (%s for %s; set to %s, using %s)"
87
88    def numeric_tester(self, calc_type, calc_value, data_type, used_locale):
89        """Compare calculation against known value, if available"""
90        try:
91            set_locale = setlocale(LC_NUMERIC)
92        except Error:
93            set_locale = "<not able to determine>"
94        known_value = known_numerics.get(used_locale,
95                                    ('', ''))[data_type == 'thousands_sep']
96        if known_value and calc_value:
97            self.assertEqual(calc_value, known_value,
98                                self.lc_numeric_err_msg % (
99                                    calc_value, known_value,
100                                    calc_type, data_type, set_locale,
101                                    used_locale))
102            return True
103
104    @unittest.skipUnless(nl_langinfo, "nl_langinfo is not available")
105    def test_lc_numeric_nl_langinfo(self):
106        # Test nl_langinfo against known values
107        tested = False
108        for loc in candidate_locales:
109            try:
110                setlocale(LC_NUMERIC, loc)
111                setlocale(LC_CTYPE, loc)
112            except Error:
113                continue
114            for li, lc in ((RADIXCHAR, "decimal_point"),
115                            (THOUSEP, "thousands_sep")):
116                if self.numeric_tester('nl_langinfo', nl_langinfo(li), lc, loc):
117                    tested = True
118        if not tested:
119            self.skipTest('no suitable locales')
120
121    def test_lc_numeric_localeconv(self):
122        # Test localeconv against known values
123        tested = False
124        for loc in candidate_locales:
125            try:
126                setlocale(LC_NUMERIC, loc)
127                setlocale(LC_CTYPE, loc)
128            except Error:
129                continue
130            formatting = localeconv()
131            for lc in ("decimal_point",
132                        "thousands_sep"):
133                if self.numeric_tester('localeconv', formatting[lc], lc, loc):
134                    tested = True
135        if not tested:
136            self.skipTest('no suitable locales')
137
138    @unittest.skipUnless(nl_langinfo, "nl_langinfo is not available")
139    def test_lc_numeric_basic(self):
140        # Test nl_langinfo against localeconv
141        tested = False
142        for loc in candidate_locales:
143            try:
144                setlocale(LC_NUMERIC, loc)
145                setlocale(LC_CTYPE, loc)
146            except Error:
147                continue
148            for li, lc in ((RADIXCHAR, "decimal_point"),
149                            (THOUSEP, "thousands_sep")):
150                nl_radixchar = nl_langinfo(li)
151                li_radixchar = localeconv()[lc]
152                try:
153                    set_locale = setlocale(LC_NUMERIC)
154                except Error:
155                    set_locale = "<not able to determine>"
156                self.assertEqual(nl_radixchar, li_radixchar,
157                                "%s (nl_langinfo) != %s (localeconv) "
158                                "(set to %s, using %s)" % (
159                                                nl_radixchar, li_radixchar,
160                                                loc, set_locale))
161                tested = True
162        if not tested:
163            self.skipTest('no suitable locales')
164
165    def test_float_parsing(self):
166        # Bug #1391872: Test whether float parsing is okay on European
167        # locales.
168        tested = False
169        for loc in candidate_locales:
170            try:
171                setlocale(LC_NUMERIC, loc)
172                setlocale(LC_CTYPE, loc)
173            except Error:
174                continue
175
176            # Ignore buggy locale databases. (Mac OS 10.4 and some other BSDs)
177            if loc == 'eu_ES' and localeconv()['decimal_point'] == "' ":
178                continue
179
180            self.assertEqual(int(eval('3.14') * 100), 314,
181                                "using eval('3.14') failed for %s" % loc)
182            self.assertEqual(int(float('3.14') * 100), 314,
183                                "using float('3.14') failed for %s" % loc)
184            if localeconv()['decimal_point'] != '.':
185                self.assertRaises(ValueError, float,
186                                  localeconv()['decimal_point'].join(['1', '23']))
187            tested = True
188        if not tested:
189            self.skipTest('no suitable locales')
190
191
192if __name__ == '__main__':
193    unittest.main()
194