| 1 |
from test.test_support import run_unittest |
| 2 |
from _locale import (setlocale, LC_NUMERIC, localeconv, Error) |
| 3 |
try: |
| 4 |
from _locale import (RADIXCHAR, THOUSEP, nl_langinfo) |
| 5 |
except ImportError: |
| 6 |
nl_langinfo = None |
| 7 |
|
| 8 |
import unittest |
| 9 |
import sys |
| 10 |
from platform import uname |
| 11 |
|
| 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") |
| 16 |
|
| 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', 'ps_AF.UTF-8', '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 |
|
| 29 |
|
| 30 |
if "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 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
known_numerics = { |
| 40 |
'en_US': ('.', ','), |
| 41 |
'de_DE' : (',', '.'), |
| 42 |
|
| 43 |
|
| 44 |
'fr_FR' : (',', ''), |
| 45 |
'ps_AF.UTF-8' : ('\xd9\xab', '\xd9\xac'), |
| 46 |
} |
| 47 |
|
| 48 |
class _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 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 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, |
| 68 |
('', ''))[data_type == 'thousands_sep'] |
| 69 |
if known_value and calc_value: |
| 70 |
self.assertEqual(calc_value, known_value, |
| 71 |
self.lc_numeric_err_msg % ( |
| 72 |
calc_value, known_value, |
| 73 |
calc_type, data_type, set_locale, |
| 74 |
used_locale)) |
| 75 |
return True |
| 76 |
|
| 77 |
@unittest.skipUnless(nl_langinfo, "nl_langinfo is not available") |
| 78 |
def test_lc_numeric_nl_langinfo(self): |
| 79 |
|
| 80 |
tested = False |
| 81 |
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")): |
| 88 |
if self.numeric_tester('nl_langinfo', nl_langinfo(li), lc, loc): |
| 89 |
tested = True |
| 90 |
if not tested: |
| 91 |
self.skipTest('no suitable locales') |
| 92 |
|
| 93 |
def test_lc_numeric_localeconv(self): |
| 94 |
|
| 95 |
tested = False |
| 96 |
for loc in candidate_locales: |
| 97 |
try: |
| 98 |
setlocale(LC_NUMERIC, loc) |
| 99 |
except Error: |
| 100 |
continue |
| 101 |
formatting = localeconv() |
| 102 |
for lc in ("decimal_point", "thousands_sep"): |
| 103 |
if self.numeric_tester('localeconv', formatting[lc], lc, loc): |
| 104 |
tested = True |
| 105 |
if not tested: |
| 106 |
self.skipTest('no suitable locales') |
| 107 |
|
| 108 |
@unittest.skipUnless(nl_langinfo, "nl_langinfo is not available") |
| 109 |
def test_lc_numeric_basic(self): |
| 110 |
|
| 111 |
tested = False |
| 112 |
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] |
| 121 |
try: |
| 122 |
set_locale = setlocale(LC_NUMERIC) |
| 123 |
except Error: |
| 124 |
set_locale = "<not able to determine>" |
| 125 |
self.assertEqual(nl_radixchar, li_radixchar, |
| 126 |
"%s (nl_langinfo) != %s (localeconv) " |
| 127 |
"(set to %s, using %s)" % ( |
| 128 |
nl_radixchar, li_radixchar, |
| 129 |
loc, set_locale)) |
| 130 |
tested = True |
| 131 |
if not tested: |
| 132 |
self.skipTest('no suitable locales') |
| 133 |
|
| 134 |
def test_float_parsing(self): |
| 135 |
|
| 136 |
|
| 137 |
tested = False |
| 138 |
for loc in candidate_locales: |
| 139 |
try: |
| 140 |
setlocale(LC_NUMERIC, loc) |
| 141 |
except Error: |
| 142 |
continue |
| 143 |
|
| 144 |
|
| 145 |
if loc == 'eu_ES' and localeconv()['decimal_point'] == "' ": |
| 146 |
continue |
| 147 |
|
| 148 |
self.assertEqual(int(eval('3.14') * 100), 314, |
| 149 |
"using eval('3.14') failed for %s" % loc) |
| 150 |
self.assertEqual(int(float('3.14') * 100), 314, |
| 151 |
"using float('3.14') failed for %s" % loc) |
| 152 |
if localeconv()['decimal_point'] != '.': |
| 153 |
self.assertRaises(ValueError, float, |
| 154 |
localeconv()['decimal_point'].join(['1', '23'])) |
| 155 |
tested = True |
| 156 |
if not tested: |
| 157 |
self.skipTest('no suitable locales') |
| 158 |
|
| 159 |
|
| 160 |
def test_main(): |
| 161 |
run_unittest(_LocaleTests) |
| 162 |
|
| 163 |
if __name__ == '__main__': |
| 164 |
test_main() |