Benjamin Peterson | 7847cd6 | 2009-05-05 22:49:38 +0000 | [diff] [blame] | 1 | from _locale import (setlocale, LC_ALL, LC_CTYPE, LC_NUMERIC, localeconv, Error) |
| 2 | try: |
| 3 | from _locale import (RADIXCHAR, THOUSEP, nl_langinfo) |
| 4 | except ImportError: |
| 5 | nl_langinfo = None |
| 6 | |
Victor Stinner | 69291c4 | 2011-12-09 10:28:45 +0100 | [diff] [blame] | 7 | import codecs |
| 8 | import locale |
Benjamin Peterson | 7847cd6 | 2009-05-05 22:49:38 +0000 | [diff] [blame] | 9 | import sys |
Victor Stinner | 69291c4 | 2011-12-09 10:28:45 +0100 | [diff] [blame] | 10 | import unittest |
Skip Montanaro | f8948ca | 2005-09-19 03:54:46 +0000 | [diff] [blame] | 11 | from platform import uname |
Victor Stinner | 69291c4 | 2011-12-09 10:28:45 +0100 | [diff] [blame] | 12 | from test.support import run_unittest |
Skip Montanaro | f8948ca | 2005-09-19 03:54:46 +0000 | [diff] [blame] | 13 | |
Larry Hastings | 68386bc | 2012-06-24 14:30:41 -0700 | [diff] [blame] | 14 | if uname().system == "Darwin": |
| 15 | maj, min, mic = [int(part) for part in uname().release.split(".")] |
Skip Montanaro | f8948ca | 2005-09-19 03:54:46 +0000 | [diff] [blame] | 16 | if (maj, min, mic) < (8, 0, 0): |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 17 | 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] | 18 | |
| 19 | 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] | 20 | 'et_EE', 'es_PY', 'no_NO', 'nl_NL', 'lv_LV', 'el_GR', 'be_BY', 'fr_BE', |
| 21 | 'ro_RO', 'ru_UA', 'ru_RU', 'es_VE', 'ca_ES', 'se_NO', 'es_EC', 'id_ID', |
Victor Stinner | 69291c4 | 2011-12-09 10:28:45 +0100 | [diff] [blame] | 22 | 'ka_GE', 'es_CL', 'wa_BE', 'hu_HU', 'lt_LT', 'sl_SI', 'hr_HR', 'es_AR', |
Jeremy Hylton | b7b1db9 | 2003-09-10 20:19:54 +0000 | [diff] [blame] | 23 | 'es_ES', 'oc_FR', 'gl_ES', 'bg_BG', 'is_IS', 'mk_MK', 'de_AT', 'pt_BR', |
| 24 | 'da_DK', 'nn_NO', 'cs_CZ', 'de_LU', 'es_BO', 'sq_AL', 'sk_SK', 'fr_CH', |
| 25 | 'de_DE', 'sr_YU', 'br_FR', 'nl_BE', 'sv_FI', 'pl_PL', 'fr_CA', 'fo_FO', |
| 26 | '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] | 27 | '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] | 28 | '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] | 29 | |
Victor Stinner | 69291c4 | 2011-12-09 10:28:45 +0100 | [diff] [blame] | 30 | # Issue #13441: Skip some locales (e.g. cs_CZ and hu_HU) on Solaris to |
| 31 | # workaround a mbstowcs() bug. For example, on Solaris, the hu_HU locale uses |
| 32 | # the locale encoding ISO-8859-2, the thousauds separator is b'\xA0' and it is |
| 33 | # decoded as U+30000020 (an invalid character) by mbstowcs(). |
| 34 | if sys.platform == 'sunos5': |
| 35 | old_locale = locale.setlocale(locale.LC_ALL) |
| 36 | try: |
| 37 | locales = [] |
| 38 | for loc in candidate_locales: |
| 39 | try: |
| 40 | locale.setlocale(locale.LC_ALL, loc) |
| 41 | except Error: |
| 42 | continue |
| 43 | encoding = locale.getpreferredencoding(False) |
| 44 | try: |
| 45 | localeconv() |
| 46 | except Exception as err: |
| 47 | print("WARNING: Skip locale %s (encoding %s): [%s] %s" |
| 48 | % (loc, encoding, type(err), err)) |
| 49 | else: |
| 50 | locales.append(loc) |
Victor Stinner | 7f54f75 | 2011-12-09 11:29:44 +0100 | [diff] [blame] | 51 | candidate_locales = locales |
Victor Stinner | 69291c4 | 2011-12-09 10:28:45 +0100 | [diff] [blame] | 52 | finally: |
| 53 | locale.setlocale(locale.LC_ALL, old_locale) |
Victor Stinner | 5446bba | 2011-12-09 01:20:03 +0100 | [diff] [blame] | 54 | |
Benjamin Peterson | 7847cd6 | 2009-05-05 22:49:38 +0000 | [diff] [blame] | 55 | # Workaround for MSVC6(debug) crash bug |
| 56 | if "MSC v.1200" in sys.version: |
| 57 | def accept(loc): |
| 58 | a = loc.split(".") |
| 59 | return not(len(a) == 2 and len(a[-1]) >= 9) |
| 60 | candidate_locales = [loc for loc in candidate_locales if accept(loc)] |
| 61 | |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 62 | # List known locale values to test against when available. |
| 63 | # Dict formatted as ``<locale> : (<decimal_point>, <thousands_sep>)``. If a |
| 64 | # value is not known, use '' . |
| 65 | known_numerics = {'fr_FR' : (',', ''), 'en_US':('.', ',')} |
| 66 | |
Brett Cannon | 2ad68e6 | 2004-09-06 23:30:27 +0000 | [diff] [blame] | 67 | class _LocaleTests(unittest.TestCase): |
| 68 | |
| 69 | def setUp(self): |
Martin v. Löwis | fe92d0b | 2008-03-10 10:18:53 +0000 | [diff] [blame] | 70 | self.oldlocale = setlocale(LC_ALL) |
Brett Cannon | 2ad68e6 | 2004-09-06 23:30:27 +0000 | [diff] [blame] | 71 | |
| 72 | def tearDown(self): |
Martin v. Löwis | fe92d0b | 2008-03-10 10:18:53 +0000 | [diff] [blame] | 73 | setlocale(LC_ALL, self.oldlocale) |
Brett Cannon | 2ad68e6 | 2004-09-06 23:30:27 +0000 | [diff] [blame] | 74 | |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 75 | # Want to know what value was calculated, what it was compared against, |
| 76 | # what function was used for the calculation, what type of data was used, |
| 77 | # the locale that was supposedly set, and the actual locale that is set. |
| 78 | lc_numeric_err_msg = "%s != %s (%s for %s; set to %s, using %s)" |
| 79 | |
| 80 | def numeric_tester(self, calc_type, calc_value, data_type, used_locale): |
| 81 | """Compare calculation against known value, if available""" |
| 82 | try: |
| 83 | set_locale = setlocale(LC_NUMERIC) |
| 84 | except Error: |
| 85 | set_locale = "<not able to determine>" |
| 86 | known_value = known_numerics.get(used_locale, |
Guido van Rossum | f40e576 | 2007-05-17 20:58:33 +0000 | [diff] [blame] | 87 | ('', ''))[data_type == 'thousands_sep'] |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 88 | if known_value and calc_value: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 89 | self.assertEqual(calc_value, known_value, |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 90 | self.lc_numeric_err_msg % ( |
| 91 | calc_value, known_value, |
| 92 | calc_type, data_type, set_locale, |
| 93 | used_locale)) |
| 94 | |
Benjamin Peterson | 7847cd6 | 2009-05-05 22:49:38 +0000 | [diff] [blame] | 95 | @unittest.skipUnless(nl_langinfo, "nl_langinfo is not available") |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 96 | def test_lc_numeric_nl_langinfo(self): |
| 97 | # Test nl_langinfo against known values |
| 98 | for loc in candidate_locales: |
| 99 | try: |
| 100 | setlocale(LC_NUMERIC, loc) |
Martin v. Löwis | fe92d0b | 2008-03-10 10:18:53 +0000 | [diff] [blame] | 101 | setlocale(LC_CTYPE, loc) |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 102 | except Error: |
| 103 | continue |
Benjamin Peterson | 7847cd6 | 2009-05-05 22:49:38 +0000 | [diff] [blame] | 104 | for li, lc in ((RADIXCHAR, "decimal_point"), |
| 105 | (THOUSEP, "thousands_sep")): |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 106 | self.numeric_tester('nl_langinfo', nl_langinfo(li), lc, loc) |
| 107 | |
| 108 | def test_lc_numeric_localeconv(self): |
| 109 | # Test localeconv against known values |
| 110 | for loc in candidate_locales: |
| 111 | try: |
| 112 | setlocale(LC_NUMERIC, loc) |
Martin v. Löwis | fe92d0b | 2008-03-10 10:18:53 +0000 | [diff] [blame] | 113 | setlocale(LC_CTYPE, loc) |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 114 | except Error: |
| 115 | continue |
Victor Stinner | 69291c4 | 2011-12-09 10:28:45 +0100 | [diff] [blame] | 116 | formatting = localeconv() |
Benjamin Peterson | 7847cd6 | 2009-05-05 22:49:38 +0000 | [diff] [blame] | 117 | for lc in ("decimal_point", |
| 118 | "thousands_sep"): |
Victor Stinner | 7061413 | 2011-12-08 23:42:52 +0100 | [diff] [blame] | 119 | self.numeric_tester('localeconv', formatting[lc], lc, loc) |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 120 | |
Benjamin Peterson | 7847cd6 | 2009-05-05 22:49:38 +0000 | [diff] [blame] | 121 | @unittest.skipUnless(nl_langinfo, "nl_langinfo is not available") |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 122 | def test_lc_numeric_basic(self): |
| 123 | # Test nl_langinfo against localeconv |
Brett Cannon | 2ad68e6 | 2004-09-06 23:30:27 +0000 | [diff] [blame] | 124 | for loc in candidate_locales: |
| 125 | try: |
| 126 | setlocale(LC_NUMERIC, loc) |
Martin v. Löwis | fe92d0b | 2008-03-10 10:18:53 +0000 | [diff] [blame] | 127 | setlocale(LC_CTYPE, loc) |
Brett Cannon | 2ad68e6 | 2004-09-06 23:30:27 +0000 | [diff] [blame] | 128 | except Error: |
| 129 | continue |
Benjamin Peterson | 7847cd6 | 2009-05-05 22:49:38 +0000 | [diff] [blame] | 130 | for li, lc in ((RADIXCHAR, "decimal_point"), |
| 131 | (THOUSEP, "thousands_sep")): |
Brett Cannon | 2ad68e6 | 2004-09-06 23:30:27 +0000 | [diff] [blame] | 132 | nl_radixchar = nl_langinfo(li) |
| 133 | li_radixchar = localeconv()[lc] |
Brett Cannon | 85ae1a6 | 2004-09-08 02:02:41 +0000 | [diff] [blame] | 134 | try: |
| 135 | set_locale = setlocale(LC_NUMERIC) |
| 136 | except Error: |
| 137 | set_locale = "<not able to determine>" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 138 | self.assertEqual(nl_radixchar, li_radixchar, |
Brett Cannon | e94e74a | 2005-03-01 03:15:50 +0000 | [diff] [blame] | 139 | "%s (nl_langinfo) != %s (localeconv) " |
| 140 | "(set to %s, using %s)" % ( |
| 141 | nl_radixchar, li_radixchar, |
| 142 | loc, set_locale)) |
| 143 | |
Fredrik Lundh | 24f0fa9 | 2005-12-29 20:35:52 +0000 | [diff] [blame] | 144 | def test_float_parsing(self): |
| 145 | # Bug #1391872: Test whether float parsing is okay on European |
| 146 | # locales. |
| 147 | for loc in candidate_locales: |
| 148 | try: |
| 149 | setlocale(LC_NUMERIC, loc) |
Martin v. Löwis | fe92d0b | 2008-03-10 10:18:53 +0000 | [diff] [blame] | 150 | setlocale(LC_CTYPE, loc) |
Fredrik Lundh | 24f0fa9 | 2005-12-29 20:35:52 +0000 | [diff] [blame] | 151 | except Error: |
| 152 | continue |
Neal Norwitz | bb45973 | 2006-02-19 00:13:15 +0000 | [diff] [blame] | 153 | |
| 154 | # Ignore buggy locale databases. (Mac OS 10.4 and some other BSDs) |
| 155 | if loc == 'eu_ES' and localeconv()['decimal_point'] == "' ": |
| 156 | continue |
| 157 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 158 | self.assertEqual(int(eval('3.14') * 100), 314, |
Brett Cannon | 2dbf2a9 | 2006-01-19 07:09:09 +0000 | [diff] [blame] | 159 | "using eval('3.14') failed for %s" % loc) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 160 | self.assertEqual(int(float('3.14') * 100), 314, |
Brett Cannon | 2dbf2a9 | 2006-01-19 07:09:09 +0000 | [diff] [blame] | 161 | "using float('3.14') failed for %s" % loc) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 162 | if localeconv()['decimal_point'] != '.': |
| 163 | self.assertRaises(ValueError, float, |
| 164 | localeconv()['decimal_point'].join(['1', '23'])) |
Fredrik Lundh | 24f0fa9 | 2005-12-29 20:35:52 +0000 | [diff] [blame] | 165 | |
Brett Cannon | 2ad68e6 | 2004-09-06 23:30:27 +0000 | [diff] [blame] | 166 | def test_main(): |
| 167 | run_unittest(_LocaleTests) |
| 168 | |
| 169 | if __name__ == '__main__': |
| 170 | test_main() |