test__locale.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. from test.test_support import run_unittest
  2. from _locale import (setlocale, LC_NUMERIC, localeconv, Error)
  3. try:
  4. from _locale import (RADIXCHAR, THOUSEP, nl_langinfo)
  5. except ImportError:
  6. nl_langinfo = None
  7. import unittest
  8. import sys
  9. from platform import uname
  10. if uname()[0] == "Darwin":
  11. maj, min, mic = [int(part) for part in uname()[2].split(".")]
  12. if (maj, min, mic) < (8, 0, 0):
  13. raise unittest.SkipTest("locale support broken for OS X < 10.4")
  14. candidate_locales = ['es_UY', 'fr_FR', 'fi_FI', 'es_CO', 'pt_PT', 'it_IT',
  15. 'et_EE', 'es_PY', 'no_NO', 'nl_NL', 'lv_LV', 'el_GR', 'be_BY', 'fr_BE',
  16. 'ro_RO', 'ru_UA', 'ru_RU', 'es_VE', 'ca_ES', 'se_NO', 'es_EC', 'id_ID',
  17. 'ka_GE', 'es_CL', 'hu_HU', 'wa_BE', 'lt_LT', 'sl_SI', 'hr_HR', 'es_AR',
  18. 'es_ES', 'oc_FR', 'gl_ES', 'bg_BG', 'is_IS', 'mk_MK', 'de_AT', 'pt_BR',
  19. 'da_DK', 'nn_NO', 'cs_CZ', 'de_LU', 'es_BO', 'sq_AL', 'sk_SK', 'fr_CH',
  20. 'de_DE', 'sr_YU', 'br_FR', 'nl_BE', 'sv_FI', 'pl_PL', 'fr_CA', 'fo_FO',
  21. 'bs_BA', 'fr_LU', 'kl_GL', 'fa_IR', 'de_BE', 'sv_SE', 'it_CH', 'uk_UA',
  22. 'eu_ES', 'vi_VN', 'af_ZA', 'nb_NO', 'en_DK', 'tg_TJ', 'ps_AF.UTF-8', 'en_US',
  23. 'fr_FR.ISO8859-1', 'fr_FR.UTF-8', 'fr_FR.ISO8859-15@euro',
  24. 'ru_RU.KOI8-R', 'ko_KR.eucKR']
  25. # Workaround for MSVC6(debug) crash bug
  26. if "MSC v.1200" in sys.version:
  27. def accept(loc):
  28. a = loc.split(".")
  29. return not(len(a) == 2 and len(a[-1]) >= 9)
  30. candidate_locales = [loc for loc in candidate_locales if accept(loc)]
  31. # List known locale values to test against when available.
  32. # Dict formatted as ``<locale> : (<decimal_point>, <thousands_sep>)``. If a
  33. # value is not known, use '' .
  34. known_numerics = {
  35. 'en_US': ('.', ','),
  36. 'de_DE' : (',', '.'),
  37. # The French thousands separator may be a breaking or non-breaking space
  38. # depending on the platform, so do not test it
  39. 'fr_FR' : (',', ''),
  40. 'ps_AF.UTF-8' : ('\xd9\xab', '\xd9\xac'),
  41. }
  42. class _LocaleTests(unittest.TestCase):
  43. def setUp(self):
  44. self.oldlocale = setlocale(LC_NUMERIC)
  45. def tearDown(self):
  46. setlocale(LC_NUMERIC, self.oldlocale)
  47. # Want to know what value was calculated, what it was compared against,
  48. # what function was used for the calculation, what type of data was used,
  49. # the locale that was supposedly set, and the actual locale that is set.
  50. lc_numeric_err_msg = "%s != %s (%s for %s; set to %s, using %s)"
  51. def numeric_tester(self, calc_type, calc_value, data_type, used_locale):
  52. """Compare calculation against known value, if available"""
  53. try:
  54. set_locale = setlocale(LC_NUMERIC)
  55. except Error:
  56. set_locale = "<not able to determine>"
  57. known_value = known_numerics.get(used_locale,
  58. ('', ''))[data_type == 'thousands_sep']
  59. if known_value and calc_value:
  60. self.assertEqual(calc_value, known_value,
  61. self.lc_numeric_err_msg % (
  62. calc_value, known_value,
  63. calc_type, data_type, set_locale,
  64. used_locale))
  65. return True
  66. @unittest.skipUnless(nl_langinfo, "nl_langinfo is not available")
  67. def test_lc_numeric_nl_langinfo(self):
  68. # Test nl_langinfo against known values
  69. tested = False
  70. for loc in candidate_locales:
  71. try:
  72. setlocale(LC_NUMERIC, loc)
  73. except Error:
  74. continue
  75. for li, lc in ((RADIXCHAR, "decimal_point"),
  76. (THOUSEP, "thousands_sep")):
  77. if self.numeric_tester('nl_langinfo', nl_langinfo(li), lc, loc):
  78. tested = True
  79. if not tested:
  80. self.skipTest('no suitable locales')
  81. def test_lc_numeric_localeconv(self):
  82. # Test localeconv against known values
  83. tested = False
  84. for loc in candidate_locales:
  85. try:
  86. setlocale(LC_NUMERIC, loc)
  87. except Error:
  88. continue
  89. formatting = localeconv()
  90. for lc in ("decimal_point", "thousands_sep"):
  91. if self.numeric_tester('localeconv', formatting[lc], lc, loc):
  92. tested = True
  93. if not tested:
  94. self.skipTest('no suitable locales')
  95. @unittest.skipUnless(nl_langinfo, "nl_langinfo is not available")
  96. def test_lc_numeric_basic(self):
  97. # Test nl_langinfo against localeconv
  98. tested = False
  99. for loc in candidate_locales:
  100. try:
  101. setlocale(LC_NUMERIC, loc)
  102. except Error:
  103. continue
  104. for li, lc in ((RADIXCHAR, "decimal_point"),
  105. (THOUSEP, "thousands_sep")):
  106. nl_radixchar = nl_langinfo(li)
  107. li_radixchar = localeconv()[lc]
  108. try:
  109. set_locale = setlocale(LC_NUMERIC)
  110. except Error:
  111. set_locale = "<not able to determine>"
  112. self.assertEqual(nl_radixchar, li_radixchar,
  113. "%s (nl_langinfo) != %s (localeconv) "
  114. "(set to %s, using %s)" % (
  115. nl_radixchar, li_radixchar,
  116. loc, set_locale))
  117. tested = True
  118. if not tested:
  119. self.skipTest('no suitable locales')
  120. def test_float_parsing(self):
  121. # Bug #1391872: Test whether float parsing is okay on European
  122. # locales.
  123. tested = False
  124. for loc in candidate_locales:
  125. try:
  126. setlocale(LC_NUMERIC, loc)
  127. except Error:
  128. continue
  129. # Ignore buggy locale databases. (Mac OS 10.4 and some other BSDs)
  130. if loc == 'eu_ES' and localeconv()['decimal_point'] == "' ":
  131. continue
  132. self.assertEqual(int(eval('3.14') * 100), 314,
  133. "using eval('3.14') failed for %s" % loc)
  134. self.assertEqual(int(float('3.14') * 100), 314,
  135. "using float('3.14') failed for %s" % loc)
  136. if localeconv()['decimal_point'] != '.':
  137. self.assertRaises(ValueError, float,
  138. localeconv()['decimal_point'].join(['1', '23']))
  139. tested = True
  140. if not tested:
  141. self.skipTest('no suitable locales')
  142. def test_main():
  143. run_unittest(_LocaleTests)
  144. if __name__ == '__main__':
  145. test_main()