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