Georg Brandl | a4f46e1 | 2010-02-07 17:03:15 +0000 | [diff] [blame] | 1 | from test.test_support import run_unittest |
Hirokazu Yamamoto | fbf63a7 | 2009-06-05 05:15:58 +0000 | [diff] [blame] | 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 | |
Brett Cannon | 2ad68e6 | 2004-09-06 23:30:27 +0000 | [diff] [blame] | 8 | import unittest |
Hirokazu Yamamoto | fbf63a7 | 2009-06-05 05:15:58 +0000 | [diff] [blame] | 9 | import sys |
Skip Montanaro | f8948ca | 2005-09-19 03:54:46 +0000 | [diff] [blame] | 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): |
Benjamin Peterson | bec087f | 2009-03-26 21:10:30 +0000 | [diff] [blame] | 15 | raise unittest.SkipTest("locale support broken for OS X < 10.4") |
Martin v. Löwis | f5b9373 | 2003-09-04 18:24:47 +0000 | [diff] [blame] | 16 | |
| 17 | candidate_locales = ['es_UY', 'fr_FR', 'fi_FI', 'es_CO', 'pt_PT', 'it_IT', |
Jeremy Hylton | b7b1db9 | 2003-09-10 20:19:54 +0000 | [diff] [blame] | 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', |
Serhiy Storchaka | ad9a1ba | 2015-02-18 08:04:26 +0200 | [diff] [blame] | 25 | 'eu_ES', 'vi_VN', 'af_ZA', 'nb_NO', 'en_DK', 'tg_TJ', 'ps_AF.UTF-8', 'en_US', |
Hye-Shik Chang | 8d2e08d | 2003-12-19 01:16:03 +0000 | [diff] [blame] | 26 | 'es_ES.ISO8859-1', 'fr_FR.ISO8859-15', 'ru_RU.KOI8-R', 'ko_KR.eucKR'] |
Martin v. Löwis | f5b9373 | 2003-09-04 18:24:47 +0000 | [diff] [blame] | 27 | |
Hirokazu Yamamoto | fbf63a7 | 2009-06-05 05:15:58 +0000 | [diff] [blame] | 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 | |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 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 '' . |
Serhiy Storchaka | ad9a1ba | 2015-02-18 08:04:26 +0200 | [diff] [blame] | 38 | known_numerics = { |
| 39 | 'en_US': ('.', ','), |
| 40 | 'fr_FR' : (',', ' '), |
| 41 | 'de_DE' : (',', '.'), |
| 42 | 'ps_AF.UTF-8' : ('\xd9\xab', '\xd9\xac'), |
| 43 | } |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 44 | |
Brett Cannon | 2ad68e6 | 2004-09-06 23:30:27 +0000 | [diff] [blame] | 45 | class _LocaleTests(unittest.TestCase): |
| 46 | |
| 47 | def setUp(self): |
| 48 | self.oldlocale = setlocale(LC_NUMERIC) |
| 49 | |
| 50 | def tearDown(self): |
| 51 | setlocale(LC_NUMERIC, self.oldlocale) |
| 52 | |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 53 | # Want to know what value was calculated, what it was compared against, |
| 54 | # what function was used for the calculation, what type of data was used, |
| 55 | # the locale that was supposedly set, and the actual locale that is set. |
| 56 | lc_numeric_err_msg = "%s != %s (%s for %s; set to %s, using %s)" |
| 57 | |
| 58 | def numeric_tester(self, calc_type, calc_value, data_type, used_locale): |
| 59 | """Compare calculation against known value, if available""" |
| 60 | try: |
| 61 | set_locale = setlocale(LC_NUMERIC) |
| 62 | except Error: |
| 63 | set_locale = "<not able to determine>" |
| 64 | known_value = known_numerics.get(used_locale, |
Benjamin Peterson | b3af601 | 2009-01-16 02:55:24 +0000 | [diff] [blame] | 65 | ('', ''))[data_type == 'thousands_sep'] |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 66 | if known_value and calc_value: |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 67 | self.assertEqual(calc_value, known_value, |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 68 | self.lc_numeric_err_msg % ( |
| 69 | calc_value, known_value, |
| 70 | calc_type, data_type, set_locale, |
| 71 | used_locale)) |
Serhiy Storchaka | ad9a1ba | 2015-02-18 08:04:26 +0200 | [diff] [blame] | 72 | return True |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 73 | |
Hirokazu Yamamoto | fbf63a7 | 2009-06-05 05:15:58 +0000 | [diff] [blame] | 74 | @unittest.skipUnless(nl_langinfo, "nl_langinfo is not available") |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 75 | def test_lc_numeric_nl_langinfo(self): |
| 76 | # Test nl_langinfo against known values |
Serhiy Storchaka | ad9a1ba | 2015-02-18 08:04:26 +0200 | [diff] [blame] | 77 | tested = False |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 78 | for loc in candidate_locales: |
| 79 | try: |
| 80 | setlocale(LC_NUMERIC, loc) |
| 81 | except Error: |
| 82 | continue |
| 83 | for li, lc in ((RADIXCHAR, "decimal_point"), |
| 84 | (THOUSEP, "thousands_sep")): |
Serhiy Storchaka | ad9a1ba | 2015-02-18 08:04:26 +0200 | [diff] [blame] | 85 | if self.numeric_tester('nl_langinfo', nl_langinfo(li), lc, loc): |
| 86 | tested = True |
| 87 | if not tested: |
| 88 | self.skipTest('no suitable locales') |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 89 | |
| 90 | def test_lc_numeric_localeconv(self): |
| 91 | # Test localeconv against known values |
Serhiy Storchaka | ad9a1ba | 2015-02-18 08:04:26 +0200 | [diff] [blame] | 92 | tested = False |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 93 | for loc in candidate_locales: |
| 94 | try: |
| 95 | setlocale(LC_NUMERIC, loc) |
| 96 | except Error: |
| 97 | continue |
Serhiy Storchaka | ad9a1ba | 2015-02-18 08:04:26 +0200 | [diff] [blame] | 98 | formatting = localeconv() |
Georg Brandl | 9be5998 | 2009-06-06 05:54:34 +0000 | [diff] [blame] | 99 | for lc in ("decimal_point", "thousands_sep"): |
Serhiy Storchaka | ad9a1ba | 2015-02-18 08:04:26 +0200 | [diff] [blame] | 100 | if self.numeric_tester('localeconv', formatting[lc], lc, loc): |
| 101 | tested = True |
| 102 | if not tested: |
| 103 | self.skipTest('no suitable locales') |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 104 | |
Hirokazu Yamamoto | fbf63a7 | 2009-06-05 05:15:58 +0000 | [diff] [blame] | 105 | @unittest.skipUnless(nl_langinfo, "nl_langinfo is not available") |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 106 | def test_lc_numeric_basic(self): |
| 107 | # Test nl_langinfo against localeconv |
Serhiy Storchaka | ad9a1ba | 2015-02-18 08:04:26 +0200 | [diff] [blame] | 108 | tested = False |
Brett Cannon | 2ad68e6 | 2004-09-06 23:30:27 +0000 | [diff] [blame] | 109 | for loc in candidate_locales: |
| 110 | try: |
| 111 | setlocale(LC_NUMERIC, loc) |
| 112 | except Error: |
| 113 | continue |
| 114 | for li, lc in ((RADIXCHAR, "decimal_point"), |
| 115 | (THOUSEP, "thousands_sep")): |
| 116 | nl_radixchar = nl_langinfo(li) |
| 117 | li_radixchar = localeconv()[lc] |
Brett Cannon | 85ae1a6 | 2004-09-08 02:02:41 +0000 | [diff] [blame] | 118 | try: |
| 119 | set_locale = setlocale(LC_NUMERIC) |
| 120 | except Error: |
| 121 | set_locale = "<not able to determine>" |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 122 | self.assertEqual(nl_radixchar, li_radixchar, |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 123 | "%s (nl_langinfo) != %s (localeconv) " |
| 124 | "(set to %s, using %s)" % ( |
| 125 | nl_radixchar, li_radixchar, |
| 126 | loc, set_locale)) |
Serhiy Storchaka | ad9a1ba | 2015-02-18 08:04:26 +0200 | [diff] [blame] | 127 | tested = True |
| 128 | if not tested: |
| 129 | self.skipTest('no suitable locales') |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 130 | |
Fredrik Lundh | 24f0fa9 | 2005-12-29 20:35:52 +0000 | [diff] [blame] | 131 | def test_float_parsing(self): |
| 132 | # Bug #1391872: Test whether float parsing is okay on European |
| 133 | # locales. |
Serhiy Storchaka | ad9a1ba | 2015-02-18 08:04:26 +0200 | [diff] [blame] | 134 | tested = False |
Fredrik Lundh | 24f0fa9 | 2005-12-29 20:35:52 +0000 | [diff] [blame] | 135 | for loc in candidate_locales: |
| 136 | try: |
| 137 | setlocale(LC_NUMERIC, loc) |
| 138 | except Error: |
| 139 | continue |
Neal Norwitz | bb45973 | 2006-02-19 00:13:15 +0000 | [diff] [blame] | 140 | |
| 141 | # Ignore buggy locale databases. (Mac OS 10.4 and some other BSDs) |
| 142 | if loc == 'eu_ES' and localeconv()['decimal_point'] == "' ": |
| 143 | continue |
| 144 | |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 145 | self.assertEqual(int(eval('3.14') * 100), 314, |
Brett Cannon | 2dbf2a9 | 2006-01-19 07:09:09 +0000 | [diff] [blame] | 146 | "using eval('3.14') failed for %s" % loc) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 147 | self.assertEqual(int(float('3.14') * 100), 314, |
Brett Cannon | 2dbf2a9 | 2006-01-19 07:09:09 +0000 | [diff] [blame] | 148 | "using float('3.14') failed for %s" % loc) |
Martin v. Löwis | fcfff0a | 2006-07-03 12:19:50 +0000 | [diff] [blame] | 149 | if localeconv()['decimal_point'] != '.': |
| 150 | self.assertRaises(ValueError, float, |
| 151 | localeconv()['decimal_point'].join(['1', '23'])) |
Serhiy Storchaka | ad9a1ba | 2015-02-18 08:04:26 +0200 | [diff] [blame] | 152 | tested = True |
| 153 | if not tested: |
| 154 | self.skipTest('no suitable locales') |
| 155 | |
Fredrik Lundh | 24f0fa9 | 2005-12-29 20:35:52 +0000 | [diff] [blame] | 156 | |
Brett Cannon | 2ad68e6 | 2004-09-06 23:30:27 +0000 | [diff] [blame] | 157 | def test_main(): |
| 158 | run_unittest(_LocaleTests) |
| 159 | |
| 160 | if __name__ == '__main__': |
| 161 | test_main() |