blob: 3fadb575f295f362fb5b7e5fe7965a140faf8410 [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',
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 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
Benjamin Peterson7847cd62009-05-05 22:49:38 +000028# Workaround for MSVC6(debug) crash bug
29if "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 Cannone94e74a2005-03-01 03:15:50 +000035# 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 '' .
38known_numerics = {'fr_FR' : (',', ''), 'en_US':('.', ',')}
39
Brett Cannon2ad68e62004-09-06 23:30:27 +000040class _LocaleTests(unittest.TestCase):
41
42 def setUp(self):
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +000043 self.oldlocale = setlocale(LC_ALL)
Brett Cannon2ad68e62004-09-06 23:30:27 +000044
45 def tearDown(self):
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +000046 setlocale(LC_ALL, self.oldlocale)
Brett Cannon2ad68e62004-09-06 23:30:27 +000047
Brett Cannone94e74a2005-03-01 03:15:50 +000048 # 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 Rossumf40e5762007-05-17 20:58:33 +000060 ('', ''))[data_type == 'thousands_sep']
Brett Cannone94e74a2005-03-01 03:15:50 +000061 if known_value and calc_value:
Ezio Melottib3aedd42010-11-20 19:04:17 +000062 self.assertEqual(calc_value, known_value,
Brett Cannone94e74a2005-03-01 03:15:50 +000063 self.lc_numeric_err_msg % (
64 calc_value, known_value,
65 calc_type, data_type, set_locale,
66 used_locale))
67
Benjamin Peterson7847cd62009-05-05 22:49:38 +000068 @unittest.skipUnless(nl_langinfo, "nl_langinfo is not available")
Brett Cannone94e74a2005-03-01 03:15:50 +000069 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öwisfe92d0b2008-03-10 10:18:53 +000074 setlocale(LC_CTYPE, loc)
Brett Cannone94e74a2005-03-01 03:15:50 +000075 except Error:
76 continue
Benjamin Peterson7847cd62009-05-05 22:49:38 +000077 for li, lc in ((RADIXCHAR, "decimal_point"),
78 (THOUSEP, "thousands_sep")):
Brett Cannone94e74a2005-03-01 03:15:50 +000079 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öwisfe92d0b2008-03-10 10:18:53 +000086 setlocale(LC_CTYPE, loc)
Brett Cannone94e74a2005-03-01 03:15:50 +000087 except Error:
88 continue
Benjamin Peterson7847cd62009-05-05 22:49:38 +000089 for lc in ("decimal_point",
90 "thousands_sep"):
Brett Cannone94e74a2005-03-01 03:15:50 +000091 self.numeric_tester('localeconv', localeconv()[lc], lc, loc)
92
Benjamin Peterson7847cd62009-05-05 22:49:38 +000093 @unittest.skipUnless(nl_langinfo, "nl_langinfo is not available")
Brett Cannone94e74a2005-03-01 03:15:50 +000094 def test_lc_numeric_basic(self):
95 # Test nl_langinfo against localeconv
Brett Cannon2ad68e62004-09-06 23:30:27 +000096 for loc in candidate_locales:
97 try:
98 setlocale(LC_NUMERIC, loc)
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +000099 setlocale(LC_CTYPE, loc)
Brett Cannon2ad68e62004-09-06 23:30:27 +0000100 except Error:
101 continue
Benjamin Peterson7847cd62009-05-05 22:49:38 +0000102 for li, lc in ((RADIXCHAR, "decimal_point"),
103 (THOUSEP, "thousands_sep")):
Brett Cannon2ad68e62004-09-06 23:30:27 +0000104 nl_radixchar = nl_langinfo(li)
105 li_radixchar = localeconv()[lc]
Brett Cannon85ae1a62004-09-08 02:02:41 +0000106 try:
107 set_locale = setlocale(LC_NUMERIC)
108 except Error:
109 set_locale = "<not able to determine>"
Ezio Melottib3aedd42010-11-20 19:04:17 +0000110 self.assertEqual(nl_radixchar, li_radixchar,
Brett Cannone94e74a2005-03-01 03:15:50 +0000111 "%s (nl_langinfo) != %s (localeconv) "
112 "(set to %s, using %s)" % (
113 nl_radixchar, li_radixchar,
114 loc, set_locale))
115
Fredrik Lundh24f0fa92005-12-29 20:35:52 +0000116 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öwisfe92d0b2008-03-10 10:18:53 +0000122 setlocale(LC_CTYPE, loc)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +0000123 except Error:
124 continue
Neal Norwitzbb459732006-02-19 00:13:15 +0000125
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 Melottib3aedd42010-11-20 19:04:17 +0000130 self.assertEqual(int(eval('3.14') * 100), 314,
Brett Cannon2dbf2a92006-01-19 07:09:09 +0000131 "using eval('3.14') failed for %s" % loc)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000132 self.assertEqual(int(float('3.14') * 100), 314,
Brett Cannon2dbf2a92006-01-19 07:09:09 +0000133 "using float('3.14') failed for %s" % loc)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000134 if localeconv()['decimal_point'] != '.':
135 self.assertRaises(ValueError, float,
136 localeconv()['decimal_point'].join(['1', '23']))
Fredrik Lundh24f0fa92005-12-29 20:35:52 +0000137
Brett Cannon2ad68e62004-09-06 23:30:27 +0000138def test_main():
139 run_unittest(_LocaleTests)
140
141if __name__ == '__main__':
142 test_main()