test__locale.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. import locale
  7. import sys
  8. import unittest
  9. from platform import uname
  10. from test import support
  11. if uname().system == "Darwin":
  12. maj, min, mic = [int(part) for part in uname().release.split(".")]
  13. if (maj, min, mic) < (8, 0, 0):
  14. raise unittest.SkipTest("locale support broken for OS X < 10.4")
  15. candidate_locales = ['es_UY', 'fr_FR', 'fi_FI', 'es_CO', 'pt_PT', 'it_IT',
  16. 'et_EE', 'es_PY', 'no_NO', 'nl_NL', 'lv_LV', 'el_GR', 'be_BY', 'fr_BE',
  17. 'ro_RO', 'ru_UA', 'ru_RU', 'es_VE', 'ca_ES', 'se_NO', 'es_EC', 'id_ID',
  18. 'ka_GE', 'es_CL', 'wa_BE', 'hu_HU', 'lt_LT', 'sl_SI', 'hr_HR', 'es_AR',
  19. 'es_ES', 'oc_FR', 'gl_ES', 'bg_BG', 'is_IS', 'mk_MK', 'de_AT', 'pt_BR',
  20. 'da_DK', 'nn_NO', 'cs_CZ', 'de_LU', 'es_BO', 'sq_AL', 'sk_SK', 'fr_CH',
  21. 'de_DE', 'sr_YU', 'br_FR', 'nl_BE', 'sv_FI', 'pl_PL', 'fr_CA', 'fo_FO',
  22. 'bs_BA', 'fr_LU', 'kl_GL', 'fa_IR', 'de_BE', 'sv_SE', 'it_CH', 'uk_UA',
  23. 'eu_ES', 'vi_VN', 'af_ZA', 'nb_NO', 'en_DK', 'tg_TJ', 'ps_AF', 'en_US',
  24. 'fr_FR.ISO8859-1', 'fr_FR.UTF-8', 'fr_FR.ISO8859-15@euro',
  25. 'ru_RU.KOI8-R', 'ko_KR.eucKR']
  26. def setUpModule():
  27. global candidate_locales
  28. # Issue #13441: Skip some locales (e.g. cs_CZ and hu_HU) on Solaris to
  29. # workaround a mbstowcs() bug. For example, on Solaris, the hu_HU locale uses
  30. # the locale encoding ISO-8859-2, the thousands separator is b'\xA0' and it is
  31. # decoded as U+30000020 (an invalid character) by mbstowcs().
  32. if sys.platform == 'sunos5':
  33. old_locale = locale.setlocale(locale.LC_ALL)
  34. try:
  35. locales = []
  36. for loc in candidate_locales:
  37. try:
  38. locale.setlocale(locale.LC_ALL, loc)
  39. except Error:
  40. continue
  41. encoding = locale.getencoding()
  42. try:
  43. localeconv()
  44. except Exception as err:
  45. print("WARNING: Skip locale %s (encoding %s): [%s] %s"
  46. % (loc, encoding, type(err), err))
  47. else:
  48. locales.append(loc)
  49. candidate_locales = locales
  50. finally:
  51. locale.setlocale(locale.LC_ALL, old_locale)
  52. # Workaround for MSVC6(debug) crash bug
  53. if "MSC v.1200" in sys.version:
  54. def accept(loc):
  55. a = loc.split(".")
  56. return not(len(a) == 2 and len(a[-1]) >= 9)
  57. candidate_locales = [loc for loc in candidate_locales if accept(loc)]
  58. # List known locale values to test against when available.
  59. # Dict formatted as ``<locale> : (<decimal_point>, <thousands_sep>)``. If a
  60. # value is not known, use '' .
  61. known_numerics = {
  62. 'en_US': ('.', ','),
  63. 'de_DE' : (',', '.'),
  64. # The French thousands separator may be a breaking or non-breaking space
  65. # depending on the platform, so do not test it
  66. 'fr_FR' : (',', ''),
  67. 'ps_AF': ('\u066b', '\u066c'),
  68. }
  69. if sys.platform == 'win32':
  70. # ps_AF doesn't work on Windows: see bpo-38324 (msg361830)
  71. del known_numerics['ps_AF']
  72. class _LocaleTests(unittest.TestCase):
  73. def setUp(self):
  74. self.oldlocale = setlocale(LC_ALL)
  75. def tearDown(self):
  76. setlocale(LC_ALL, self.oldlocale)
  77. # Want to know what value was calculated, what it was compared against,
  78. # what function was used for the calculation, what type of data was used,
  79. # the locale that was supposedly set, and the actual locale that is set.
  80. lc_numeric_err_msg = "%s != %s (%s for %s; set to %s, using %s)"
  81. def numeric_tester(self, calc_type, calc_value, data_type, used_locale):
  82. """Compare calculation against known value, if available"""
  83. try:
  84. set_locale = setlocale(LC_NUMERIC)
  85. except Error:
  86. set_locale = "<not able to determine>"
  87. known_value = known_numerics.get(used_locale,
  88. ('', ''))[data_type == 'thousands_sep']
  89. if known_value and calc_value:
  90. self.assertEqual(calc_value, known_value,
  91. self.lc_numeric_err_msg % (
  92. calc_value, known_value,
  93. calc_type, data_type, set_locale,
  94. used_locale))
  95. return True
  96. @unittest.skipUnless(nl_langinfo, "nl_langinfo is not available")
  97. @unittest.skipIf(
  98. support.is_emscripten or support.is_wasi,
  99. "musl libc issue on Emscripten, bpo-46390"
  100. )
  101. def test_lc_numeric_nl_langinfo(self):
  102. # Test nl_langinfo against known values
  103. tested = False
  104. for loc in candidate_locales:
  105. try:
  106. setlocale(LC_NUMERIC, loc)
  107. setlocale(LC_CTYPE, loc)
  108. except Error:
  109. continue
  110. for li, lc in ((RADIXCHAR, "decimal_point"),
  111. (THOUSEP, "thousands_sep")):
  112. if self.numeric_tester('nl_langinfo', nl_langinfo(li), lc, loc):
  113. tested = True
  114. if not tested:
  115. self.skipTest('no suitable locales')
  116. @unittest.skipIf(
  117. support.is_emscripten or support.is_wasi,
  118. "musl libc issue on Emscripten, bpo-46390"
  119. )
  120. def test_lc_numeric_localeconv(self):
  121. # Test localeconv against known values
  122. tested = False
  123. for loc in candidate_locales:
  124. try:
  125. setlocale(LC_NUMERIC, loc)
  126. setlocale(LC_CTYPE, loc)
  127. except Error:
  128. continue
  129. formatting = localeconv()
  130. for lc in ("decimal_point",
  131. "thousands_sep"):
  132. if self.numeric_tester('localeconv', formatting[lc], lc, loc):
  133. tested = True
  134. if not tested:
  135. self.skipTest('no suitable locales')
  136. @unittest.skipUnless(nl_langinfo, "nl_langinfo is not available")
  137. def test_lc_numeric_basic(self):
  138. # Test nl_langinfo against localeconv
  139. tested = False
  140. for loc in candidate_locales:
  141. try:
  142. setlocale(LC_NUMERIC, loc)
  143. setlocale(LC_CTYPE, loc)
  144. except Error:
  145. continue
  146. for li, lc in ((RADIXCHAR, "decimal_point"),
  147. (THOUSEP, "thousands_sep")):
  148. nl_radixchar = nl_langinfo(li)
  149. li_radixchar = localeconv()[lc]
  150. try:
  151. set_locale = setlocale(LC_NUMERIC)
  152. except Error:
  153. set_locale = "<not able to determine>"
  154. self.assertEqual(nl_radixchar, li_radixchar,
  155. "%s (nl_langinfo) != %s (localeconv) "
  156. "(set to %s, using %s)" % (
  157. nl_radixchar, li_radixchar,
  158. loc, set_locale))
  159. tested = True
  160. if not tested:
  161. self.skipTest('no suitable locales')
  162. def test_float_parsing(self):
  163. # Bug #1391872: Test whether float parsing is okay on European
  164. # locales.
  165. tested = False
  166. for loc in candidate_locales:
  167. try:
  168. setlocale(LC_NUMERIC, loc)
  169. setlocale(LC_CTYPE, loc)
  170. except Error:
  171. continue
  172. # Ignore buggy locale databases. (Mac OS 10.4 and some other BSDs)
  173. if loc == 'eu_ES' and localeconv()['decimal_point'] == "' ":
  174. continue
  175. self.assertEqual(int(eval('3.14') * 100), 314,
  176. "using eval('3.14') failed for %s" % loc)
  177. self.assertEqual(int(float('3.14') * 100), 314,
  178. "using float('3.14') failed for %s" % loc)
  179. if localeconv()['decimal_point'] != '.':
  180. self.assertRaises(ValueError, float,
  181. localeconv()['decimal_point'].join(['1', '23']))
  182. tested = True
  183. if not tested:
  184. self.skipTest('no suitable locales')
  185. if __name__ == '__main__':
  186. unittest.main()