blob: 09b05afdb3112303d2488e46577008ad22eaca3f [file] [log] [blame]
Benjamin Petersone549ead2009-03-28 21:42:05 +00001from test.support import verbose, run_unittest
Benjamin Petersonec753122009-05-04 21:28:12 +00002import _locale
3from _locale import (setlocale, LC_ALL, LC_CTYPE, LC_NUMERIC, nl_langinfo,
4 localeconv, Error)
Brett Cannon2ad68e62004-09-06 23:30:27 +00005import unittest
Skip Montanarof8948ca2005-09-19 03:54:46 +00006from platform import uname
7
8if uname()[0] == "Darwin":
9 maj, min, mic = [int(part) for part in uname()[2].split(".")]
10 if (maj, min, mic) < (8, 0, 0):
Benjamin Petersone549ead2009-03-28 21:42:05 +000011 raise unittest.SkipTest("locale support broken for OS X < 10.4")
Martin v. Löwisf5b93732003-09-04 18:24:47 +000012
13candidate_locales = ['es_UY', 'fr_FR', 'fi_FI', 'es_CO', 'pt_PT', 'it_IT',
Jeremy Hyltonb7b1db92003-09-10 20:19:54 +000014 'et_EE', 'es_PY', 'no_NO', 'nl_NL', 'lv_LV', 'el_GR', 'be_BY', 'fr_BE',
15 'ro_RO', 'ru_UA', 'ru_RU', 'es_VE', 'ca_ES', 'se_NO', 'es_EC', 'id_ID',
16 'ka_GE', 'es_CL', 'hu_HU', 'wa_BE', 'lt_LT', 'sl_SI', 'hr_HR', 'es_AR',
17 'es_ES', 'oc_FR', 'gl_ES', 'bg_BG', 'is_IS', 'mk_MK', 'de_AT', 'pt_BR',
18 'da_DK', 'nn_NO', 'cs_CZ', 'de_LU', 'es_BO', 'sq_AL', 'sk_SK', 'fr_CH',
19 'de_DE', 'sr_YU', 'br_FR', 'nl_BE', 'sv_FI', 'pl_PL', 'fr_CA', 'fo_FO',
20 'bs_BA', 'fr_LU', 'kl_GL', 'fa_IR', 'de_BE', 'sv_SE', 'it_CH', 'uk_UA',
Brett Cannone94e74a2005-03-01 03:15:50 +000021 'eu_ES', 'vi_VN', 'af_ZA', 'nb_NO', 'en_DK', 'tg_TJ', 'en_US',
Hye-Shik Chang8d2e08d2003-12-19 01:16:03 +000022 'es_ES.ISO8859-1', 'fr_FR.ISO8859-15', 'ru_RU.KOI8-R', 'ko_KR.eucKR']
Martin v. Löwisf5b93732003-09-04 18:24:47 +000023
Brett Cannone94e74a2005-03-01 03:15:50 +000024# List known locale values to test against when available.
25# Dict formatted as ``<locale> : (<decimal_point>, <thousands_sep>)``. If a
26# value is not known, use '' .
27known_numerics = {'fr_FR' : (',', ''), 'en_US':('.', ',')}
28
Benjamin Petersonec753122009-05-04 21:28:12 +000029def needs_radix_and_thousands(func):
30 return unittest.skipUnless(hasattr(_locale, "RADIXCHAR"),
31 "needs RADIXCHAR and THOUSEP")(func)
32
Brett Cannon2ad68e62004-09-06 23:30:27 +000033class _LocaleTests(unittest.TestCase):
34
35 def setUp(self):
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +000036 self.oldlocale = setlocale(LC_ALL)
Brett Cannon2ad68e62004-09-06 23:30:27 +000037
38 def tearDown(self):
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +000039 setlocale(LC_ALL, self.oldlocale)
Brett Cannon2ad68e62004-09-06 23:30:27 +000040
Brett Cannone94e74a2005-03-01 03:15:50 +000041 # Want to know what value was calculated, what it was compared against,
42 # what function was used for the calculation, what type of data was used,
43 # the locale that was supposedly set, and the actual locale that is set.
44 lc_numeric_err_msg = "%s != %s (%s for %s; set to %s, using %s)"
45
46 def numeric_tester(self, calc_type, calc_value, data_type, used_locale):
47 """Compare calculation against known value, if available"""
48 try:
49 set_locale = setlocale(LC_NUMERIC)
50 except Error:
51 set_locale = "<not able to determine>"
52 known_value = known_numerics.get(used_locale,
Guido van Rossumf40e5762007-05-17 20:58:33 +000053 ('', ''))[data_type == 'thousands_sep']
Brett Cannone94e74a2005-03-01 03:15:50 +000054 if known_value and calc_value:
55 self.assertEquals(calc_value, known_value,
56 self.lc_numeric_err_msg % (
57 calc_value, known_value,
58 calc_type, data_type, set_locale,
59 used_locale))
60
Benjamin Petersonec753122009-05-04 21:28:12 +000061 @needs_radix_and_thousands
Brett Cannone94e74a2005-03-01 03:15:50 +000062 def test_lc_numeric_nl_langinfo(self):
63 # Test nl_langinfo against known values
64 for loc in candidate_locales:
65 try:
66 setlocale(LC_NUMERIC, loc)
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +000067 setlocale(LC_CTYPE, loc)
Brett Cannone94e74a2005-03-01 03:15:50 +000068 except Error:
69 continue
Benjamin Petersonec753122009-05-04 21:28:12 +000070 for li, lc in ((_locale.RADIXCHAR, "decimal_point"),
71 (_locale.THOUSEP, "thousands_sep")):
Brett Cannone94e74a2005-03-01 03:15:50 +000072 self.numeric_tester('nl_langinfo', nl_langinfo(li), lc, loc)
73
Benjamin Petersonec753122009-05-04 21:28:12 +000074 @needs_radix_and_thousands
Brett Cannone94e74a2005-03-01 03:15:50 +000075 def test_lc_numeric_localeconv(self):
76 # Test localeconv against known values
77 for loc in candidate_locales:
78 try:
79 setlocale(LC_NUMERIC, loc)
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +000080 setlocale(LC_CTYPE, loc)
Brett Cannone94e74a2005-03-01 03:15:50 +000081 except Error:
82 continue
Benjamin Petersonec753122009-05-04 21:28:12 +000083 for li, lc in ((_locale.RADIXCHAR, "decimal_point"),
84 (_locale.THOUSEP, "thousands_sep")):
Brett Cannone94e74a2005-03-01 03:15:50 +000085 self.numeric_tester('localeconv', localeconv()[lc], lc, loc)
86
Benjamin Petersonec753122009-05-04 21:28:12 +000087 @needs_radix_and_thousands
Brett Cannone94e74a2005-03-01 03:15:50 +000088 def test_lc_numeric_basic(self):
89 # Test nl_langinfo against localeconv
Brett Cannon2ad68e62004-09-06 23:30:27 +000090 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 Cannon2ad68e62004-09-06 23:30:27 +000094 except Error:
95 continue
Benjamin Petersonec753122009-05-04 21:28:12 +000096 for li, lc in ((_locale.RADIXCHAR, "decimal_point"),
97 (_locale.THOUSEP, "thousands_sep")):
Brett Cannon2ad68e62004-09-06 23:30:27 +000098 nl_radixchar = nl_langinfo(li)
99 li_radixchar = localeconv()[lc]
Brett Cannon85ae1a62004-09-08 02:02:41 +0000100 try:
101 set_locale = setlocale(LC_NUMERIC)
102 except Error:
103 set_locale = "<not able to determine>"
Brett Cannon2ad68e62004-09-06 23:30:27 +0000104 self.assertEquals(nl_radixchar, li_radixchar,
Brett Cannone94e74a2005-03-01 03:15:50 +0000105 "%s (nl_langinfo) != %s (localeconv) "
106 "(set to %s, using %s)" % (
107 nl_radixchar, li_radixchar,
108 loc, set_locale))
109
Fredrik Lundh24f0fa92005-12-29 20:35:52 +0000110 def test_float_parsing(self):
111 # Bug #1391872: Test whether float parsing is okay on European
112 # locales.
113 for loc in candidate_locales:
114 try:
115 setlocale(LC_NUMERIC, loc)
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +0000116 setlocale(LC_CTYPE, loc)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +0000117 except Error:
118 continue
Neal Norwitzbb459732006-02-19 00:13:15 +0000119
120 # Ignore buggy locale databases. (Mac OS 10.4 and some other BSDs)
121 if loc == 'eu_ES' and localeconv()['decimal_point'] == "' ":
122 continue
123
Brett Cannon2dbf2a92006-01-19 07:09:09 +0000124 self.assertEquals(int(eval('3.14') * 100), 314,
125 "using eval('3.14') failed for %s" % loc)
126 self.assertEquals(int(float('3.14') * 100), 314,
127 "using float('3.14') failed for %s" % loc)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000128 if localeconv()['decimal_point'] != '.':
129 self.assertRaises(ValueError, float,
130 localeconv()['decimal_point'].join(['1', '23']))
Fredrik Lundh24f0fa92005-12-29 20:35:52 +0000131
Brett Cannon2ad68e62004-09-06 23:30:27 +0000132def test_main():
133 run_unittest(_LocaleTests)
134
135if __name__ == '__main__':
136 test_main()