ยปCore Development>Code coverage>Lib/test/test__locale.py

Python code coverage for Lib/test/test__locale.py

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