1 from test
.test_support
import run_unittest
2 from _locale
import (setlocale
, LC_NUMERIC
, localeconv
, Error
)
4 from _locale
import (RADIXCHAR
, THOUSEP
, nl_langinfo
)
10 from platform
import uname
12 if uname()[0] == "Darwin":
13 maj
, min, mic
= [int(part
) for part
in uname()[2].split(".")]
14 if (maj
, min, mic
) < (8, 0, 0):
15 raise unittest
.SkipTest("locale support broken for OS X < 10.4")
17 candidate_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', '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',
25 'eu_ES', 'vi_VN', 'af_ZA', 'nb_NO', 'en_DK', 'tg_TJ', 'en_US',
26 'es_ES.ISO8859-1', 'fr_FR.ISO8859-15', 'ru_RU.KOI8-R', 'ko_KR.eucKR']
28 # Workaround for MSVC6(debug) crash bug
29 if "MSC v.1200" in sys
.version
:
32 return not(len(a
) == 2 and len(a
[-1]) >= 9)
33 candidate_locales
= [loc
for loc
in candidate_locales
if accept(loc
)]
35 # List known locale values to test against when available.
36 # Dict formatted as ``<locale> : (<decimal_point>, <thousands_sep>)``. If a
37 # value is not known, use '' .
38 known_numerics
= {'fr_FR' : (',', ''), 'en_US':('.', ',')}
40 class _LocaleTests(unittest
.TestCase
):
43 self
.oldlocale
= setlocale(LC_NUMERIC
)
46 setlocale(LC_NUMERIC
, self
.oldlocale
)
48 # Want to know what value was calculated, what it was compared against,
49 # what function was used for the calculation, what type of data was used,
50 # the locale that was supposedly set, and the actual locale that is set.
51 lc_numeric_err_msg
= "%s != %s (%s for %s; set to %s, using %s)"
53 def numeric_tester(self
, calc_type
, calc_value
, data_type
, used_locale
):
54 """Compare calculation against known value, if available"""
56 set_locale
= setlocale(LC_NUMERIC
)
58 set_locale
= "<not able to determine>"
59 known_value
= known_numerics
.get(used_locale
,
60 ('', ''))[data_type
== 'thousands_sep']
61 if known_value
and calc_value
:
62 self
.assertEquals(calc_value
, known_value
,
63 self
.lc_numeric_err_msg
% (
64 calc_value
, known_value
,
65 calc_type
, data_type
, set_locale
,
68 @unittest.skipUnless(nl_langinfo
, "nl_langinfo is not available")
69 def test_lc_numeric_nl_langinfo(self
):
70 # Test nl_langinfo against known values
71 for loc
in candidate_locales
:
73 setlocale(LC_NUMERIC
, loc
)
76 for li
, lc
in ((RADIXCHAR
, "decimal_point"),
77 (THOUSEP
, "thousands_sep")):
78 self
.numeric_tester('nl_langinfo', nl_langinfo(li
), lc
, loc
)
80 def test_lc_numeric_localeconv(self
):
81 # Test localeconv against known values
82 for loc
in candidate_locales
:
84 setlocale(LC_NUMERIC
, loc
)
87 for lc
in ("decimal_point", "thousands_sep"):
88 self
.numeric_tester('localeconv', localeconv()[lc
], lc
, loc
)
90 @unittest.skipUnless(nl_langinfo
, "nl_langinfo is not available")
91 def test_lc_numeric_basic(self
):
92 # Test nl_langinfo against localeconv
93 for loc
in candidate_locales
:
95 setlocale(LC_NUMERIC
, loc
)
98 for li
, lc
in ((RADIXCHAR
, "decimal_point"),
99 (THOUSEP
, "thousands_sep")):
100 nl_radixchar
= nl_langinfo(li
)
101 li_radixchar
= localeconv()[lc
]
103 set_locale
= setlocale(LC_NUMERIC
)
105 set_locale
= "<not able to determine>"
106 self
.assertEquals(nl_radixchar
, li_radixchar
,
107 "%s (nl_langinfo) != %s (localeconv) "
108 "(set to %s, using %s)" % (
109 nl_radixchar
, li_radixchar
,
112 def test_float_parsing(self
):
113 # Bug #1391872: Test whether float parsing is okay on European
115 for loc
in candidate_locales
:
117 setlocale(LC_NUMERIC
, loc
)
121 # Ignore buggy locale databases. (Mac OS 10.4 and some other BSDs)
122 if loc
== 'eu_ES' and localeconv()['decimal_point'] == "' ":
125 self
.assertEquals(int(eval('3.14') * 100), 314,
126 "using eval('3.14') failed for %s" % loc
)
127 self
.assertEquals(int(float('3.14') * 100), 314,
128 "using float('3.14') failed for %s" % loc
)
129 if localeconv()['decimal_point'] != '.':
130 self
.assertRaises(ValueError, float,
131 localeconv()['decimal_point'].join(['1', '23']))
134 run_unittest(_LocaleTests
)
136 if __name__
== '__main__':