Georg Brandl | 1b37e87 | 2010-03-14 10:45:50 +0000 | [diff] [blame] | 1 | from test.support import run_unittest |
Benjamin Peterson | 7847cd6 | 2009-05-05 22:49:38 +0000 | [diff] [blame] | 2 | from _locale import (setlocale, LC_ALL, LC_CTYPE, 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 |
Benjamin Peterson | 7847cd6 | 2009-05-05 22:49:38 +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 | e549ead | 2009-03-28 21:42:05 +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', |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 25 | 'eu_ES', 'vi_VN', 'af_ZA', 'nb_NO', 'en_DK', 'tg_TJ', '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 | |
Benjamin Peterson | 7847cd6 | 2009-05-05 22:49:38 +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 '' . |
| 38 | known_numerics = {'fr_FR' : (',', ''), 'en_US':('.', ',')} |
| 39 | |
Brett Cannon | 2ad68e6 | 2004-09-06 23:30:27 +0000 | [diff] [blame] | 40 | class _LocaleTests(unittest.TestCase): |
| 41 | |
| 42 | def setUp(self): |
Martin v. Löwis | fe92d0b | 2008-03-10 10:18:53 +0000 | [diff] [blame] | 43 | self.oldlocale = setlocale(LC_ALL) |
Brett Cannon | 2ad68e6 | 2004-09-06 23:30:27 +0000 | [diff] [blame] | 44 | |
| 45 | def tearDown(self): |
Martin v. Löwis | fe92d0b | 2008-03-10 10:18:53 +0000 | [diff] [blame] | 46 | setlocale(LC_ALL, self.oldlocale) |
Brett Cannon | 2ad68e6 | 2004-09-06 23:30:27 +0000 | [diff] [blame] | 47 | |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 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, |
Guido van Rossum | f40e576 | 2007-05-17 20:58:33 +0000 | [diff] [blame] | 60 | ('', ''))[data_type == 'thousands_sep'] |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 61 | if known_value and calc_value: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 62 | self.assertEqual(calc_value, known_value, |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 63 | self.lc_numeric_err_msg % ( |
| 64 | calc_value, known_value, |
| 65 | calc_type, data_type, set_locale, |
| 66 | used_locale)) |
| 67 | |
Benjamin Peterson | 7847cd6 | 2009-05-05 22:49:38 +0000 | [diff] [blame] | 68 | @unittest.skipUnless(nl_langinfo, "nl_langinfo is not available") |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 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) |
Martin v. Löwis | fe92d0b | 2008-03-10 10:18:53 +0000 | [diff] [blame] | 74 | setlocale(LC_CTYPE, loc) |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 75 | except Error: |
| 76 | continue |
Benjamin Peterson | 7847cd6 | 2009-05-05 22:49:38 +0000 | [diff] [blame] | 77 | for li, lc in ((RADIXCHAR, "decimal_point"), |
| 78 | (THOUSEP, "thousands_sep")): |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 79 | self.numeric_tester('nl_langinfo', nl_langinfo(li), lc, loc) |
| 80 | |
| 81 | def test_lc_numeric_localeconv(self): |
| 82 | # Test localeconv against known values |
| 83 | for loc in candidate_locales: |
| 84 | try: |
| 85 | setlocale(LC_NUMERIC, loc) |
Martin v. Löwis | fe92d0b | 2008-03-10 10:18:53 +0000 | [diff] [blame] | 86 | setlocale(LC_CTYPE, loc) |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 87 | except Error: |
| 88 | continue |
Benjamin Peterson | 7847cd6 | 2009-05-05 22:49:38 +0000 | [diff] [blame] | 89 | for lc in ("decimal_point", |
| 90 | "thousands_sep"): |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 91 | self.numeric_tester('localeconv', localeconv()[lc], lc, loc) |
| 92 | |
Benjamin Peterson | 7847cd6 | 2009-05-05 22:49:38 +0000 | [diff] [blame] | 93 | @unittest.skipUnless(nl_langinfo, "nl_langinfo is not available") |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 94 | def test_lc_numeric_basic(self): |
| 95 | # Test nl_langinfo against localeconv |
Brett Cannon | 2ad68e6 | 2004-09-06 23:30:27 +0000 | [diff] [blame] | 96 | for loc in candidate_locales: |
| 97 | try: |
| 98 | setlocale(LC_NUMERIC, loc) |
Martin v. Löwis | fe92d0b | 2008-03-10 10:18:53 +0000 | [diff] [blame] | 99 | setlocale(LC_CTYPE, loc) |
Brett Cannon | 2ad68e6 | 2004-09-06 23:30:27 +0000 | [diff] [blame] | 100 | except Error: |
| 101 | continue |
Benjamin Peterson | 7847cd6 | 2009-05-05 22:49:38 +0000 | [diff] [blame] | 102 | for li, lc in ((RADIXCHAR, "decimal_point"), |
| 103 | (THOUSEP, "thousands_sep")): |
Brett Cannon | 2ad68e6 | 2004-09-06 23:30:27 +0000 | [diff] [blame] | 104 | nl_radixchar = nl_langinfo(li) |
| 105 | li_radixchar = localeconv()[lc] |
Brett Cannon | 85ae1a6 | 2004-09-08 02:02:41 +0000 | [diff] [blame] | 106 | try: |
| 107 | set_locale = setlocale(LC_NUMERIC) |
| 108 | except Error: |
| 109 | set_locale = "<not able to determine>" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 110 | self.assertEqual(nl_radixchar, li_radixchar, |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 111 | "%s (nl_langinfo) != %s (localeconv) " |
| 112 | "(set to %s, using %s)" % ( |
| 113 | nl_radixchar, li_radixchar, |
| 114 | loc, set_locale)) |
| 115 | |
Fredrik Lundh | 24f0fa9 | 2005-12-29 20:35:52 +0000 | [diff] [blame] | 116 | def test_float_parsing(self): |
| 117 | # Bug #1391872: Test whether float parsing is okay on European |
| 118 | # locales. |
| 119 | for loc in candidate_locales: |
| 120 | try: |
| 121 | setlocale(LC_NUMERIC, loc) |
Martin v. Löwis | fe92d0b | 2008-03-10 10:18:53 +0000 | [diff] [blame] | 122 | setlocale(LC_CTYPE, loc) |
Fredrik Lundh | 24f0fa9 | 2005-12-29 20:35:52 +0000 | [diff] [blame] | 123 | except Error: |
| 124 | continue |
Neal Norwitz | bb45973 | 2006-02-19 00:13:15 +0000 | [diff] [blame] | 125 | |
| 126 | # Ignore buggy locale databases. (Mac OS 10.4 and some other BSDs) |
| 127 | if loc == 'eu_ES' and localeconv()['decimal_point'] == "' ": |
| 128 | continue |
| 129 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 130 | self.assertEqual(int(eval('3.14') * 100), 314, |
Brett Cannon | 2dbf2a9 | 2006-01-19 07:09:09 +0000 | [diff] [blame] | 131 | "using eval('3.14') failed for %s" % loc) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 132 | self.assertEqual(int(float('3.14') * 100), 314, |
Brett Cannon | 2dbf2a9 | 2006-01-19 07:09:09 +0000 | [diff] [blame] | 133 | "using float('3.14') failed for %s" % loc) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 134 | if localeconv()['decimal_point'] != '.': |
| 135 | self.assertRaises(ValueError, float, |
| 136 | localeconv()['decimal_point'].join(['1', '23'])) |
Fredrik Lundh | 24f0fa9 | 2005-12-29 20:35:52 +0000 | [diff] [blame] | 137 | |
Brett Cannon | 2ad68e6 | 2004-09-06 23:30:27 +0000 | [diff] [blame] | 138 | def test_main(): |
| 139 | run_unittest(_LocaleTests) |
| 140 | |
| 141 | if __name__ == '__main__': |
| 142 | test_main() |