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', 'en_US', 26 'es_ES.ISO8859-1', 'fr_FR.ISO8859-15', 'ru_RU.KOI8-R', 'ko_KR.eucKR'] 27 28 # Workaround for MSVC6(debug) crash bug 29 if "MSC v.1200" in sys.version: 30 def accept(loc): 31 a = loc.split(".") 32 return not(len(a) == 2 and len(a[-1]) >= 9) 33 candidate_locales = [loc for loc in candidate_locales if accept(loc)] 34 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':('.', ',')} 39 40 class _LocaleTests(unittest.TestCase): 41 42 def setUp(self): 43 self.oldlocale = setlocale(LC_NUMERIC) 44 45 def tearDown(self): 46 setlocale(LC_NUMERIC, self.oldlocale) 47 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)" 52 53 def numeric_tester(self, calc_type, calc_value, data_type, used_locale): 54 """Compare calculation against known value, if available""" 55 try: 56 set_locale = setlocale(LC_NUMERIC) 57 except Error: 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.assertEqual(calc_value, known_value, 63 self.lc_numeric_err_msg % ( 64 calc_value, known_value, 65 calc_type, data_type, set_locale, 66 used_locale)) 67 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: 72 try: 73 setlocale(LC_NUMERIC, loc) 74 except Error: 75 continue 76 for li, lc in ((RADIXCHAR, "decimal_point"), 77 (THOUSEP, "thousands_sep")): 78 self.numeric_tester('nl_langinfo', nl_langinfo(li), lc, loc) 79 80 def test_lc_numeric_localeconv(self): 81 # Test localeconv against known values 82 for loc in candidate_locales: 83 try: 84 setlocale(LC_NUMERIC, loc) 85 except Error: 86 continue 87 for lc in ("decimal_point", "thousands_sep"): 88 self.numeric_tester('localeconv', localeconv()[lc], lc, loc) 89 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: 94 try: 95 setlocale(LC_NUMERIC, loc) 96 except Error: 97 continue 98 for li, lc in ((RADIXCHAR, "decimal_point"), 99 (THOUSEP, "thousands_sep")): 100 nl_radixchar = nl_langinfo(li) 101 li_radixchar = localeconv()[lc] 102 try: 103 set_locale = setlocale(LC_NUMERIC) 104 except Error: 105 set_locale = "<not able to determine>" 106 self.assertEqual(nl_radixchar, li_radixchar, 107 "%s (nl_langinfo) != %s (localeconv) " 108 "(set to %s, using %s)" % ( 109 nl_radixchar, li_radixchar, 110 loc, set_locale)) 111 112 def test_float_parsing(self): 113 # Bug #1391872: Test whether float parsing is okay on European 114 # locales. 115 for loc in candidate_locales: 116 try: 117 setlocale(LC_NUMERIC, loc) 118 except Error: 119 continue 120 121 # Ignore buggy locale databases. (Mac OS 10.4 and some other BSDs) 122 if loc == 'eu_ES' and localeconv()['decimal_point'] == "' ": 123 continue 124 125 self.assertEqual(int(eval('3.14') * 100), 314, 126 "using eval('3.14') failed for %s" % loc) 127 self.assertEqual(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'])) 132 133 def test_main(): 134 run_unittest(_LocaleTests) 135 136 if __name__ == '__main__': 137 test_main() 138