blob: 3217f7114b77f3af55ba22ac183f7bf722585b93 [file] [log] [blame]
Georg Brandl1b37e872010-03-14 10:45:50 +00001from test.support import run_unittest
Benjamin Peterson7847cd62009-05-05 22:49:38 +00002from _locale import (setlocale, LC_ALL, LC_CTYPE, LC_NUMERIC, localeconv, Error)
3try:
4 from _locale import (RADIXCHAR, THOUSEP, nl_langinfo)
5except ImportError:
6 nl_langinfo = None
7
Brett Cannon2ad68e62004-09-06 23:30:27 +00008import unittest
Benjamin Peterson7847cd62009-05-05 22:49:38 +00009import sys
Skip Montanarof8948ca2005-09-19 03:54:46 +000010from platform import uname
11
12if uname()[0] == "Darwin":
13 maj, min, mic = [int(part) for part in uname()[2].split(".")]
14 if (maj, min, mic) < (8, 0, 0):
Benjamin Petersone549ead2009-03-28 21:42:05 +000015 raise unittest.SkipTest("locale support broken for OS X < 10.4")
Martin v. Löwisf5b93732003-09-04 18:24:47 +000016
17candidate_locales = ['es_UY', 'fr_FR', 'fi_FI', 'es_CO', 'pt_PT', 'it_IT',
Jeremy Hyltonb7b1db92003-09-10 20:19:54 +000018 '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',
Victor Stinner5446bba2011-12-09 01:20:03 +010020 'ka_GE', 'es_CL', 'wa_BE', 'lt_LT', 'sl_SI', 'hr_HR', 'es_AR',
Jeremy Hyltonb7b1db92003-09-10 20:19:54 +000021 '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 Cannone94e74a2005-03-01 03:15:50 +000025 'eu_ES', 'vi_VN', 'af_ZA', 'nb_NO', 'en_DK', 'tg_TJ', 'en_US',
Hye-Shik Chang8d2e08d2003-12-19 01:16:03 +000026 'es_ES.ISO8859-1', 'fr_FR.ISO8859-15', 'ru_RU.KOI8-R', 'ko_KR.eucKR']
Martin v. Löwisf5b93732003-09-04 18:24:47 +000027
Victor Stinner5446bba2011-12-09 01:20:03 +010028# Issue #13441: Don't test the hu_HU locale on Solaris to workaround a
29# mbstowcs() bug. On Solaris, if the locale is hu_HU (and if the locale
30# encoding is not UTF-8), the thousauds separator is b'\xA0' which is decoded
31# as U+30000020 instead of U+0020 by mbstowcs().
32if sys.platform != 'sunos5':
33 candidate_locales.append('hu_HU')
34
Benjamin Peterson7847cd62009-05-05 22:49:38 +000035# Workaround for MSVC6(debug) crash bug
36if "MSC v.1200" in sys.version:
37 def accept(loc):
38 a = loc.split(".")
39 return not(len(a) == 2 and len(a[-1]) >= 9)
40 candidate_locales = [loc for loc in candidate_locales if accept(loc)]
41
Brett Cannone94e74a2005-03-01 03:15:50 +000042# List known locale values to test against when available.
43# Dict formatted as ``<locale> : (<decimal_point>, <thousands_sep>)``. If a
44# value is not known, use '' .
45known_numerics = {'fr_FR' : (',', ''), 'en_US':('.', ',')}
46
Brett Cannon2ad68e62004-09-06 23:30:27 +000047class _LocaleTests(unittest.TestCase):
48
49 def setUp(self):
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +000050 self.oldlocale = setlocale(LC_ALL)
Brett Cannon2ad68e62004-09-06 23:30:27 +000051
52 def tearDown(self):
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +000053 setlocale(LC_ALL, self.oldlocale)
Brett Cannon2ad68e62004-09-06 23:30:27 +000054
Brett Cannone94e74a2005-03-01 03:15:50 +000055 # Want to know what value was calculated, what it was compared against,
56 # what function was used for the calculation, what type of data was used,
57 # the locale that was supposedly set, and the actual locale that is set.
58 lc_numeric_err_msg = "%s != %s (%s for %s; set to %s, using %s)"
59
60 def numeric_tester(self, calc_type, calc_value, data_type, used_locale):
61 """Compare calculation against known value, if available"""
62 try:
63 set_locale = setlocale(LC_NUMERIC)
64 except Error:
65 set_locale = "<not able to determine>"
66 known_value = known_numerics.get(used_locale,
Guido van Rossumf40e5762007-05-17 20:58:33 +000067 ('', ''))[data_type == 'thousands_sep']
Brett Cannone94e74a2005-03-01 03:15:50 +000068 if known_value and calc_value:
Ezio Melottib3aedd42010-11-20 19:04:17 +000069 self.assertEqual(calc_value, known_value,
Brett Cannone94e74a2005-03-01 03:15:50 +000070 self.lc_numeric_err_msg % (
71 calc_value, known_value,
72 calc_type, data_type, set_locale,
73 used_locale))
74
Benjamin Peterson7847cd62009-05-05 22:49:38 +000075 @unittest.skipUnless(nl_langinfo, "nl_langinfo is not available")
Brett Cannone94e74a2005-03-01 03:15:50 +000076 def test_lc_numeric_nl_langinfo(self):
77 # Test nl_langinfo against known values
78 for loc in candidate_locales:
79 try:
80 setlocale(LC_NUMERIC, loc)
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +000081 setlocale(LC_CTYPE, loc)
Brett Cannone94e74a2005-03-01 03:15:50 +000082 except Error:
83 continue
Benjamin Peterson7847cd62009-05-05 22:49:38 +000084 for li, lc in ((RADIXCHAR, "decimal_point"),
85 (THOUSEP, "thousands_sep")):
Brett Cannone94e74a2005-03-01 03:15:50 +000086 self.numeric_tester('nl_langinfo', nl_langinfo(li), lc, loc)
87
88 def test_lc_numeric_localeconv(self):
89 # Test localeconv against known values
90 for loc in candidate_locales:
91 try:
92 setlocale(LC_NUMERIC, loc)
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +000093 setlocale(LC_CTYPE, loc)
Brett Cannone94e74a2005-03-01 03:15:50 +000094 except Error:
95 continue
Victor Stinner70614132011-12-08 23:42:52 +010096 try:
97 formatting = localeconv()
98 except Exception as err:
99 self.fail("localeconv() failed with %s locale: %s" % (loc, err))
Benjamin Peterson7847cd62009-05-05 22:49:38 +0000100 for lc in ("decimal_point",
101 "thousands_sep"):
Victor Stinner70614132011-12-08 23:42:52 +0100102 self.numeric_tester('localeconv', formatting[lc], lc, loc)
Brett Cannone94e74a2005-03-01 03:15:50 +0000103
Benjamin Peterson7847cd62009-05-05 22:49:38 +0000104 @unittest.skipUnless(nl_langinfo, "nl_langinfo is not available")
Brett Cannone94e74a2005-03-01 03:15:50 +0000105 def test_lc_numeric_basic(self):
106 # Test nl_langinfo against localeconv
Brett Cannon2ad68e62004-09-06 23:30:27 +0000107 for loc in candidate_locales:
108 try:
109 setlocale(LC_NUMERIC, loc)
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +0000110 setlocale(LC_CTYPE, loc)
Brett Cannon2ad68e62004-09-06 23:30:27 +0000111 except Error:
112 continue
Benjamin Peterson7847cd62009-05-05 22:49:38 +0000113 for li, lc in ((RADIXCHAR, "decimal_point"),
114 (THOUSEP, "thousands_sep")):
Brett Cannon2ad68e62004-09-06 23:30:27 +0000115 nl_radixchar = nl_langinfo(li)
116 li_radixchar = localeconv()[lc]
Brett Cannon85ae1a62004-09-08 02:02:41 +0000117 try:
118 set_locale = setlocale(LC_NUMERIC)
119 except Error:
120 set_locale = "<not able to determine>"
Ezio Melottib3aedd42010-11-20 19:04:17 +0000121 self.assertEqual(nl_radixchar, li_radixchar,
Brett Cannone94e74a2005-03-01 03:15:50 +0000122 "%s (nl_langinfo) != %s (localeconv) "
123 "(set to %s, using %s)" % (
124 nl_radixchar, li_radixchar,
125 loc, set_locale))
126
Fredrik Lundh24f0fa92005-12-29 20:35:52 +0000127 def test_float_parsing(self):
128 # Bug #1391872: Test whether float parsing is okay on European
129 # locales.
130 for loc in candidate_locales:
131 try:
132 setlocale(LC_NUMERIC, loc)
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +0000133 setlocale(LC_CTYPE, loc)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +0000134 except Error:
135 continue
Neal Norwitzbb459732006-02-19 00:13:15 +0000136
137 # Ignore buggy locale databases. (Mac OS 10.4 and some other BSDs)
138 if loc == 'eu_ES' and localeconv()['decimal_point'] == "' ":
139 continue
140
Ezio Melottib3aedd42010-11-20 19:04:17 +0000141 self.assertEqual(int(eval('3.14') * 100), 314,
Brett Cannon2dbf2a92006-01-19 07:09:09 +0000142 "using eval('3.14') failed for %s" % loc)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000143 self.assertEqual(int(float('3.14') * 100), 314,
Brett Cannon2dbf2a92006-01-19 07:09:09 +0000144 "using float('3.14') failed for %s" % loc)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000145 if localeconv()['decimal_point'] != '.':
146 self.assertRaises(ValueError, float,
147 localeconv()['decimal_point'].join(['1', '23']))
Fredrik Lundh24f0fa92005-12-29 20:35:52 +0000148
Brett Cannon2ad68e62004-09-06 23:30:27 +0000149def test_main():
150 run_unittest(_LocaleTests)
151
152if __name__ == '__main__':
153 test_main()