test_locale.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. from decimal import Decimal
  2. from test.support import verbose, is_android, is_emscripten, is_wasi
  3. from test.support.warnings_helper import check_warnings
  4. import unittest
  5. import locale
  6. import sys
  7. import codecs
  8. class BaseLocalizedTest(unittest.TestCase):
  9. #
  10. # Base class for tests using a real locale
  11. #
  12. @classmethod
  13. def setUpClass(cls):
  14. if sys.platform == 'darwin':
  15. import os
  16. tlocs = ("en_US.UTF-8", "en_US.ISO8859-1", "en_US")
  17. if int(os.uname().release.split('.')[0]) < 10:
  18. # The locale test work fine on OSX 10.6, I (ronaldoussoren)
  19. # haven't had time yet to verify if tests work on OSX 10.5
  20. # (10.4 is known to be bad)
  21. raise unittest.SkipTest("Locale support on MacOSX is minimal")
  22. elif sys.platform.startswith("win"):
  23. tlocs = ("En", "English")
  24. else:
  25. tlocs = ("en_US.UTF-8", "en_US.ISO8859-1",
  26. "en_US.US-ASCII", "en_US")
  27. try:
  28. oldlocale = locale.setlocale(locale.LC_NUMERIC)
  29. for tloc in tlocs:
  30. try:
  31. locale.setlocale(locale.LC_NUMERIC, tloc)
  32. except locale.Error:
  33. continue
  34. break
  35. else:
  36. raise unittest.SkipTest("Test locale not supported "
  37. "(tried %s)" % (', '.join(tlocs)))
  38. cls.enUS_locale = tloc
  39. finally:
  40. locale.setlocale(locale.LC_NUMERIC, oldlocale)
  41. def setUp(self):
  42. oldlocale = locale.setlocale(self.locale_type)
  43. self.addCleanup(locale.setlocale, self.locale_type, oldlocale)
  44. locale.setlocale(self.locale_type, self.enUS_locale)
  45. if verbose:
  46. print("testing with %r..." % self.enUS_locale, end=' ', flush=True)
  47. class BaseCookedTest(unittest.TestCase):
  48. #
  49. # Base class for tests using cooked localeconv() values
  50. #
  51. def setUp(self):
  52. locale._override_localeconv = self.cooked_values
  53. def tearDown(self):
  54. locale._override_localeconv = {}
  55. class CCookedTest(BaseCookedTest):
  56. # A cooked "C" locale
  57. cooked_values = {
  58. 'currency_symbol': '',
  59. 'decimal_point': '.',
  60. 'frac_digits': 127,
  61. 'grouping': [],
  62. 'int_curr_symbol': '',
  63. 'int_frac_digits': 127,
  64. 'mon_decimal_point': '',
  65. 'mon_grouping': [],
  66. 'mon_thousands_sep': '',
  67. 'n_cs_precedes': 127,
  68. 'n_sep_by_space': 127,
  69. 'n_sign_posn': 127,
  70. 'negative_sign': '',
  71. 'p_cs_precedes': 127,
  72. 'p_sep_by_space': 127,
  73. 'p_sign_posn': 127,
  74. 'positive_sign': '',
  75. 'thousands_sep': ''
  76. }
  77. class EnUSCookedTest(BaseCookedTest):
  78. # A cooked "en_US" locale
  79. cooked_values = {
  80. 'currency_symbol': '$',
  81. 'decimal_point': '.',
  82. 'frac_digits': 2,
  83. 'grouping': [3, 3, 0],
  84. 'int_curr_symbol': 'USD ',
  85. 'int_frac_digits': 2,
  86. 'mon_decimal_point': '.',
  87. 'mon_grouping': [3, 3, 0],
  88. 'mon_thousands_sep': ',',
  89. 'n_cs_precedes': 1,
  90. 'n_sep_by_space': 0,
  91. 'n_sign_posn': 1,
  92. 'negative_sign': '-',
  93. 'p_cs_precedes': 1,
  94. 'p_sep_by_space': 0,
  95. 'p_sign_posn': 1,
  96. 'positive_sign': '',
  97. 'thousands_sep': ','
  98. }
  99. class FrFRCookedTest(BaseCookedTest):
  100. # A cooked "fr_FR" locale with a space character as decimal separator
  101. # and a non-ASCII currency symbol.
  102. cooked_values = {
  103. 'currency_symbol': '\u20ac',
  104. 'decimal_point': ',',
  105. 'frac_digits': 2,
  106. 'grouping': [3, 3, 0],
  107. 'int_curr_symbol': 'EUR ',
  108. 'int_frac_digits': 2,
  109. 'mon_decimal_point': ',',
  110. 'mon_grouping': [3, 3, 0],
  111. 'mon_thousands_sep': ' ',
  112. 'n_cs_precedes': 0,
  113. 'n_sep_by_space': 1,
  114. 'n_sign_posn': 1,
  115. 'negative_sign': '-',
  116. 'p_cs_precedes': 0,
  117. 'p_sep_by_space': 1,
  118. 'p_sign_posn': 1,
  119. 'positive_sign': '',
  120. 'thousands_sep': ' '
  121. }
  122. class BaseFormattingTest(object):
  123. #
  124. # Utility functions for formatting tests
  125. #
  126. def _test_formatfunc(self, format, value, out, func, **format_opts):
  127. self.assertEqual(
  128. func(format, value, **format_opts), out)
  129. def _test_format(self, format, value, out, **format_opts):
  130. with check_warnings(('', DeprecationWarning)):
  131. self._test_formatfunc(format, value, out,
  132. func=locale.format, **format_opts)
  133. def _test_format_string(self, format, value, out, **format_opts):
  134. self._test_formatfunc(format, value, out,
  135. func=locale.format_string, **format_opts)
  136. def _test_currency(self, value, out, **format_opts):
  137. self.assertEqual(locale.currency(value, **format_opts), out)
  138. class EnUSNumberFormatting(BaseFormattingTest):
  139. # XXX there is a grouping + padding bug when the thousands separator
  140. # is empty but the grouping array contains values (e.g. Solaris 10)
  141. def setUp(self):
  142. self.sep = locale.localeconv()['thousands_sep']
  143. def test_grouping(self):
  144. self._test_format("%f", 1024, grouping=1, out='1%s024.000000' % self.sep)
  145. self._test_format("%f", 102, grouping=1, out='102.000000')
  146. self._test_format("%f", -42, grouping=1, out='-42.000000')
  147. self._test_format("%+f", -42, grouping=1, out='-42.000000')
  148. def test_grouping_and_padding(self):
  149. self._test_format("%20.f", -42, grouping=1, out='-42'.rjust(20))
  150. if self.sep:
  151. self._test_format("%+10.f", -4200, grouping=1,
  152. out=('-4%s200' % self.sep).rjust(10))
  153. self._test_format("%-10.f", -4200, grouping=1,
  154. out=('-4%s200' % self.sep).ljust(10))
  155. def test_integer_grouping(self):
  156. self._test_format("%d", 4200, grouping=True, out='4%s200' % self.sep)
  157. self._test_format("%+d", 4200, grouping=True, out='+4%s200' % self.sep)
  158. self._test_format("%+d", -4200, grouping=True, out='-4%s200' % self.sep)
  159. def test_integer_grouping_and_padding(self):
  160. self._test_format("%10d", 4200, grouping=True,
  161. out=('4%s200' % self.sep).rjust(10))
  162. self._test_format("%-10d", -4200, grouping=True,
  163. out=('-4%s200' % self.sep).ljust(10))
  164. def test_simple(self):
  165. self._test_format("%f", 1024, grouping=0, out='1024.000000')
  166. self._test_format("%f", 102, grouping=0, out='102.000000')
  167. self._test_format("%f", -42, grouping=0, out='-42.000000')
  168. self._test_format("%+f", -42, grouping=0, out='-42.000000')
  169. def test_padding(self):
  170. self._test_format("%20.f", -42, grouping=0, out='-42'.rjust(20))
  171. self._test_format("%+10.f", -4200, grouping=0, out='-4200'.rjust(10))
  172. self._test_format("%-10.f", 4200, grouping=0, out='4200'.ljust(10))
  173. def test_format_deprecation(self):
  174. with self.assertWarns(DeprecationWarning):
  175. locale.format("%-10.f", 4200, grouping=True)
  176. def test_complex_formatting(self):
  177. # Spaces in formatting string
  178. self._test_format_string("One million is %i", 1000000, grouping=1,
  179. out='One million is 1%s000%s000' % (self.sep, self.sep))
  180. self._test_format_string("One million is %i", 1000000, grouping=1,
  181. out='One million is 1%s000%s000' % (self.sep, self.sep))
  182. # Dots in formatting string
  183. self._test_format_string(".%f.", 1000.0, out='.1000.000000.')
  184. # Padding
  185. if self.sep:
  186. self._test_format_string("--> %10.2f", 4200, grouping=1,
  187. out='--> ' + ('4%s200.00' % self.sep).rjust(10))
  188. # Asterisk formats
  189. self._test_format_string("%10.*f", (2, 1000), grouping=0,
  190. out='1000.00'.rjust(10))
  191. if self.sep:
  192. self._test_format_string("%*.*f", (10, 2, 1000), grouping=1,
  193. out=('1%s000.00' % self.sep).rjust(10))
  194. # Test more-in-one
  195. if self.sep:
  196. self._test_format_string("int %i float %.2f str %s",
  197. (1000, 1000.0, 'str'), grouping=1,
  198. out='int 1%s000 float 1%s000.00 str str' %
  199. (self.sep, self.sep))
  200. class TestFormatPatternArg(unittest.TestCase):
  201. # Test handling of pattern argument of format
  202. def test_onlyOnePattern(self):
  203. with check_warnings(('', DeprecationWarning)):
  204. # Issue 2522: accept exactly one % pattern, and no extra chars.
  205. self.assertRaises(ValueError, locale.format, "%f\n", 'foo')
  206. self.assertRaises(ValueError, locale.format, "%f\r", 'foo')
  207. self.assertRaises(ValueError, locale.format, "%f\r\n", 'foo')
  208. self.assertRaises(ValueError, locale.format, " %f", 'foo')
  209. self.assertRaises(ValueError, locale.format, "%fg", 'foo')
  210. self.assertRaises(ValueError, locale.format, "%^g", 'foo')
  211. self.assertRaises(ValueError, locale.format, "%f%%", 'foo')
  212. class TestLocaleFormatString(unittest.TestCase):
  213. """General tests on locale.format_string"""
  214. def test_percent_escape(self):
  215. self.assertEqual(locale.format_string('%f%%', 1.0), '%f%%' % 1.0)
  216. self.assertEqual(locale.format_string('%d %f%%d', (1, 1.0)),
  217. '%d %f%%d' % (1, 1.0))
  218. self.assertEqual(locale.format_string('%(foo)s %%d', {'foo': 'bar'}),
  219. ('%(foo)s %%d' % {'foo': 'bar'}))
  220. def test_mapping(self):
  221. self.assertEqual(locale.format_string('%(foo)s bing.', {'foo': 'bar'}),
  222. ('%(foo)s bing.' % {'foo': 'bar'}))
  223. self.assertEqual(locale.format_string('%(foo)s', {'foo': 'bar'}),
  224. ('%(foo)s' % {'foo': 'bar'}))
  225. class TestNumberFormatting(BaseLocalizedTest, EnUSNumberFormatting):
  226. # Test number formatting with a real English locale.
  227. locale_type = locale.LC_NUMERIC
  228. def setUp(self):
  229. BaseLocalizedTest.setUp(self)
  230. EnUSNumberFormatting.setUp(self)
  231. class TestEnUSNumberFormatting(EnUSCookedTest, EnUSNumberFormatting):
  232. # Test number formatting with a cooked "en_US" locale.
  233. def setUp(self):
  234. EnUSCookedTest.setUp(self)
  235. EnUSNumberFormatting.setUp(self)
  236. def test_currency(self):
  237. self._test_currency(50000, "$50000.00")
  238. self._test_currency(50000, "$50,000.00", grouping=True)
  239. self._test_currency(50000, "USD 50,000.00",
  240. grouping=True, international=True)
  241. class TestCNumberFormatting(CCookedTest, BaseFormattingTest):
  242. # Test number formatting with a cooked "C" locale.
  243. def test_grouping(self):
  244. self._test_format("%.2f", 12345.67, grouping=True, out='12345.67')
  245. def test_grouping_and_padding(self):
  246. self._test_format("%9.2f", 12345.67, grouping=True, out=' 12345.67')
  247. class TestFrFRNumberFormatting(FrFRCookedTest, BaseFormattingTest):
  248. # Test number formatting with a cooked "fr_FR" locale.
  249. def test_decimal_point(self):
  250. self._test_format("%.2f", 12345.67, out='12345,67')
  251. def test_grouping(self):
  252. self._test_format("%.2f", 345.67, grouping=True, out='345,67')
  253. self._test_format("%.2f", 12345.67, grouping=True, out='12 345,67')
  254. def test_grouping_and_padding(self):
  255. self._test_format("%6.2f", 345.67, grouping=True, out='345,67')
  256. self._test_format("%7.2f", 345.67, grouping=True, out=' 345,67')
  257. self._test_format("%8.2f", 12345.67, grouping=True, out='12 345,67')
  258. self._test_format("%9.2f", 12345.67, grouping=True, out='12 345,67')
  259. self._test_format("%10.2f", 12345.67, grouping=True, out=' 12 345,67')
  260. self._test_format("%-6.2f", 345.67, grouping=True, out='345,67')
  261. self._test_format("%-7.2f", 345.67, grouping=True, out='345,67 ')
  262. self._test_format("%-8.2f", 12345.67, grouping=True, out='12 345,67')
  263. self._test_format("%-9.2f", 12345.67, grouping=True, out='12 345,67')
  264. self._test_format("%-10.2f", 12345.67, grouping=True, out='12 345,67 ')
  265. def test_integer_grouping(self):
  266. self._test_format("%d", 200, grouping=True, out='200')
  267. self._test_format("%d", 4200, grouping=True, out='4 200')
  268. def test_integer_grouping_and_padding(self):
  269. self._test_format("%4d", 4200, grouping=True, out='4 200')
  270. self._test_format("%5d", 4200, grouping=True, out='4 200')
  271. self._test_format("%10d", 4200, grouping=True, out='4 200'.rjust(10))
  272. self._test_format("%-4d", 4200, grouping=True, out='4 200')
  273. self._test_format("%-5d", 4200, grouping=True, out='4 200')
  274. self._test_format("%-10d", 4200, grouping=True, out='4 200'.ljust(10))
  275. def test_currency(self):
  276. euro = '\u20ac'
  277. self._test_currency(50000, "50000,00 " + euro)
  278. self._test_currency(50000, "50 000,00 " + euro, grouping=True)
  279. self._test_currency(50000, "50 000,00 EUR",
  280. grouping=True, international=True)
  281. class TestCollation(unittest.TestCase):
  282. # Test string collation functions
  283. def test_strcoll(self):
  284. self.assertLess(locale.strcoll('a', 'b'), 0)
  285. self.assertEqual(locale.strcoll('a', 'a'), 0)
  286. self.assertGreater(locale.strcoll('b', 'a'), 0)
  287. # embedded null character
  288. self.assertRaises(ValueError, locale.strcoll, 'a\0', 'a')
  289. self.assertRaises(ValueError, locale.strcoll, 'a', 'a\0')
  290. def test_strxfrm(self):
  291. self.assertLess(locale.strxfrm('a'), locale.strxfrm('b'))
  292. # embedded null character
  293. self.assertRaises(ValueError, locale.strxfrm, 'a\0')
  294. class TestEnUSCollation(BaseLocalizedTest, TestCollation):
  295. # Test string collation functions with a real English locale
  296. locale_type = locale.LC_ALL
  297. def setUp(self):
  298. enc = codecs.lookup(locale.getencoding() or 'ascii').name
  299. if enc not in ('utf-8', 'iso8859-1', 'cp1252'):
  300. raise unittest.SkipTest('encoding not suitable')
  301. if enc != 'iso8859-1' and (sys.platform == 'darwin' or is_android or
  302. sys.platform.startswith('freebsd')):
  303. raise unittest.SkipTest('wcscoll/wcsxfrm have known bugs')
  304. BaseLocalizedTest.setUp(self)
  305. @unittest.skipIf(sys.platform.startswith('aix'),
  306. 'bpo-29972: broken test on AIX')
  307. @unittest.skipIf(
  308. is_emscripten or is_wasi,
  309. "musl libc issue on Emscripten/WASI, bpo-46390"
  310. )
  311. def test_strcoll_with_diacritic(self):
  312. self.assertLess(locale.strcoll('à', 'b'), 0)
  313. @unittest.skipIf(sys.platform.startswith('aix'),
  314. 'bpo-29972: broken test on AIX')
  315. @unittest.skipIf(
  316. is_emscripten or is_wasi,
  317. "musl libc issue on Emscripten/WASI, bpo-46390"
  318. )
  319. def test_strxfrm_with_diacritic(self):
  320. self.assertLess(locale.strxfrm('à'), locale.strxfrm('b'))
  321. class NormalizeTest(unittest.TestCase):
  322. def check(self, localename, expected):
  323. self.assertEqual(locale.normalize(localename), expected, msg=localename)
  324. def test_locale_alias(self):
  325. for localename, alias in locale.locale_alias.items():
  326. with self.subTest(locale=(localename, alias)):
  327. self.check(localename, alias)
  328. def test_empty(self):
  329. self.check('', '')
  330. def test_c(self):
  331. self.check('c', 'C')
  332. self.check('posix', 'C')
  333. def test_english(self):
  334. self.check('en', 'en_US.ISO8859-1')
  335. self.check('EN', 'en_US.ISO8859-1')
  336. self.check('en.iso88591', 'en_US.ISO8859-1')
  337. self.check('en_US', 'en_US.ISO8859-1')
  338. self.check('en_us', 'en_US.ISO8859-1')
  339. self.check('en_GB', 'en_GB.ISO8859-1')
  340. self.check('en_US.UTF-8', 'en_US.UTF-8')
  341. self.check('en_US.utf8', 'en_US.UTF-8')
  342. self.check('en_US:UTF-8', 'en_US.UTF-8')
  343. self.check('en_US.ISO8859-1', 'en_US.ISO8859-1')
  344. self.check('en_US.US-ASCII', 'en_US.ISO8859-1')
  345. self.check('en_US.88591', 'en_US.ISO8859-1')
  346. self.check('en_US.885915', 'en_US.ISO8859-15')
  347. self.check('english', 'en_EN.ISO8859-1')
  348. self.check('english_uk.ascii', 'en_GB.ISO8859-1')
  349. def test_hyphenated_encoding(self):
  350. self.check('az_AZ.iso88599e', 'az_AZ.ISO8859-9E')
  351. self.check('az_AZ.ISO8859-9E', 'az_AZ.ISO8859-9E')
  352. self.check('tt_RU.koi8c', 'tt_RU.KOI8-C')
  353. self.check('tt_RU.KOI8-C', 'tt_RU.KOI8-C')
  354. self.check('lo_LA.cp1133', 'lo_LA.IBM-CP1133')
  355. self.check('lo_LA.ibmcp1133', 'lo_LA.IBM-CP1133')
  356. self.check('lo_LA.IBM-CP1133', 'lo_LA.IBM-CP1133')
  357. self.check('uk_ua.microsoftcp1251', 'uk_UA.CP1251')
  358. self.check('uk_ua.microsoft-cp1251', 'uk_UA.CP1251')
  359. self.check('ka_ge.georgianacademy', 'ka_GE.GEORGIAN-ACADEMY')
  360. self.check('ka_GE.GEORGIAN-ACADEMY', 'ka_GE.GEORGIAN-ACADEMY')
  361. self.check('cs_CZ.iso88592', 'cs_CZ.ISO8859-2')
  362. self.check('cs_CZ.ISO8859-2', 'cs_CZ.ISO8859-2')
  363. def test_euro_modifier(self):
  364. self.check('de_DE@euro', 'de_DE.ISO8859-15')
  365. self.check('en_US.ISO8859-15@euro', 'en_US.ISO8859-15')
  366. self.check('de_DE.utf8@euro', 'de_DE.UTF-8')
  367. def test_latin_modifier(self):
  368. self.check('be_BY.UTF-8@latin', 'be_BY.UTF-8@latin')
  369. self.check('sr_RS.UTF-8@latin', 'sr_RS.UTF-8@latin')
  370. self.check('sr_RS.UTF-8@latn', 'sr_RS.UTF-8@latin')
  371. def test_valencia_modifier(self):
  372. self.check('ca_ES.UTF-8@valencia', 'ca_ES.UTF-8@valencia')
  373. self.check('ca_ES@valencia', 'ca_ES.UTF-8@valencia')
  374. self.check('ca@valencia', 'ca_ES.ISO8859-1@valencia')
  375. def test_devanagari_modifier(self):
  376. self.check('ks_IN.UTF-8@devanagari', 'ks_IN.UTF-8@devanagari')
  377. self.check('ks_IN@devanagari', 'ks_IN.UTF-8@devanagari')
  378. self.check('ks@devanagari', 'ks_IN.UTF-8@devanagari')
  379. self.check('ks_IN.UTF-8', 'ks_IN.UTF-8')
  380. self.check('ks_IN', 'ks_IN.UTF-8')
  381. self.check('ks', 'ks_IN.UTF-8')
  382. self.check('sd_IN.UTF-8@devanagari', 'sd_IN.UTF-8@devanagari')
  383. self.check('sd_IN@devanagari', 'sd_IN.UTF-8@devanagari')
  384. self.check('sd@devanagari', 'sd_IN.UTF-8@devanagari')
  385. self.check('sd_IN.UTF-8', 'sd_IN.UTF-8')
  386. self.check('sd_IN', 'sd_IN.UTF-8')
  387. self.check('sd', 'sd_IN.UTF-8')
  388. def test_euc_encoding(self):
  389. self.check('ja_jp.euc', 'ja_JP.eucJP')
  390. self.check('ja_jp.eucjp', 'ja_JP.eucJP')
  391. self.check('ko_kr.euc', 'ko_KR.eucKR')
  392. self.check('ko_kr.euckr', 'ko_KR.eucKR')
  393. self.check('zh_cn.euc', 'zh_CN.eucCN')
  394. self.check('zh_tw.euc', 'zh_TW.eucTW')
  395. self.check('zh_tw.euctw', 'zh_TW.eucTW')
  396. def test_japanese(self):
  397. self.check('ja', 'ja_JP.eucJP')
  398. self.check('ja.jis', 'ja_JP.JIS7')
  399. self.check('ja.sjis', 'ja_JP.SJIS')
  400. self.check('ja_jp', 'ja_JP.eucJP')
  401. self.check('ja_jp.ajec', 'ja_JP.eucJP')
  402. self.check('ja_jp.euc', 'ja_JP.eucJP')
  403. self.check('ja_jp.eucjp', 'ja_JP.eucJP')
  404. self.check('ja_jp.iso-2022-jp', 'ja_JP.JIS7')
  405. self.check('ja_jp.iso2022jp', 'ja_JP.JIS7')
  406. self.check('ja_jp.jis', 'ja_JP.JIS7')
  407. self.check('ja_jp.jis7', 'ja_JP.JIS7')
  408. self.check('ja_jp.mscode', 'ja_JP.SJIS')
  409. self.check('ja_jp.pck', 'ja_JP.SJIS')
  410. self.check('ja_jp.sjis', 'ja_JP.SJIS')
  411. self.check('ja_jp.ujis', 'ja_JP.eucJP')
  412. self.check('ja_jp.utf8', 'ja_JP.UTF-8')
  413. self.check('japan', 'ja_JP.eucJP')
  414. self.check('japanese', 'ja_JP.eucJP')
  415. self.check('japanese-euc', 'ja_JP.eucJP')
  416. self.check('japanese.euc', 'ja_JP.eucJP')
  417. self.check('japanese.sjis', 'ja_JP.SJIS')
  418. self.check('jp_jp', 'ja_JP.eucJP')
  419. class TestMiscellaneous(unittest.TestCase):
  420. def test_defaults_UTF8(self):
  421. # Issue #18378: on (at least) macOS setting LC_CTYPE to "UTF-8" is
  422. # valid. Furthermore LC_CTYPE=UTF is used by the UTF-8 locale coercing
  423. # during interpreter startup (on macOS).
  424. import _locale
  425. import os
  426. self.assertEqual(locale._parse_localename('UTF-8'), (None, 'UTF-8'))
  427. if hasattr(_locale, '_getdefaultlocale'):
  428. orig_getlocale = _locale._getdefaultlocale
  429. del _locale._getdefaultlocale
  430. else:
  431. orig_getlocale = None
  432. orig_env = {}
  433. try:
  434. for key in ('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE'):
  435. if key in os.environ:
  436. orig_env[key] = os.environ[key]
  437. del os.environ[key]
  438. os.environ['LC_CTYPE'] = 'UTF-8'
  439. with check_warnings(('', DeprecationWarning)):
  440. self.assertEqual(locale.getdefaultlocale(), (None, 'UTF-8'))
  441. finally:
  442. for k in orig_env:
  443. os.environ[k] = orig_env[k]
  444. if 'LC_CTYPE' not in orig_env:
  445. del os.environ['LC_CTYPE']
  446. if orig_getlocale is not None:
  447. _locale._getdefaultlocale = orig_getlocale
  448. def test_getencoding(self):
  449. # Invoke getencoding to make sure it does not cause exceptions.
  450. enc = locale.getencoding()
  451. self.assertIsInstance(enc, str)
  452. self.assertNotEqual(enc, "")
  453. # make sure it is valid
  454. codecs.lookup(enc)
  455. def test_getpreferredencoding(self):
  456. # Invoke getpreferredencoding to make sure it does not cause exceptions.
  457. enc = locale.getpreferredencoding()
  458. if enc:
  459. # If encoding non-empty, make sure it is valid
  460. codecs.lookup(enc)
  461. def test_strcoll_3303(self):
  462. # test crasher from bug #3303
  463. self.assertRaises(TypeError, locale.strcoll, "a", None)
  464. self.assertRaises(TypeError, locale.strcoll, b"a", None)
  465. def test_setlocale_category(self):
  466. locale.setlocale(locale.LC_ALL)
  467. locale.setlocale(locale.LC_TIME)
  468. locale.setlocale(locale.LC_CTYPE)
  469. locale.setlocale(locale.LC_COLLATE)
  470. locale.setlocale(locale.LC_MONETARY)
  471. locale.setlocale(locale.LC_NUMERIC)
  472. # crasher from bug #7419
  473. self.assertRaises(locale.Error, locale.setlocale, 12345)
  474. def test_getsetlocale_issue1813(self):
  475. # Issue #1813: setting and getting the locale under a Turkish locale
  476. oldlocale = locale.setlocale(locale.LC_CTYPE)
  477. self.addCleanup(locale.setlocale, locale.LC_CTYPE, oldlocale)
  478. try:
  479. locale.setlocale(locale.LC_CTYPE, 'tr_TR')
  480. except locale.Error:
  481. # Unsupported locale on this system
  482. self.skipTest('test needs Turkish locale')
  483. loc = locale.getlocale(locale.LC_CTYPE)
  484. if verbose:
  485. print('testing with %a' % (loc,), end=' ', flush=True)
  486. try:
  487. locale.setlocale(locale.LC_CTYPE, loc)
  488. except locale.Error as exc:
  489. # bpo-37945: setlocale(LC_CTYPE) fails with getlocale(LC_CTYPE)
  490. # and the tr_TR locale on Windows. getlocale() builds a locale
  491. # which is not recognize by setlocale().
  492. self.skipTest(f"setlocale(LC_CTYPE, {loc!r}) failed: {exc!r}")
  493. self.assertEqual(loc, locale.getlocale(locale.LC_CTYPE))
  494. def test_invalid_locale_format_in_localetuple(self):
  495. with self.assertRaises(TypeError):
  496. locale.setlocale(locale.LC_ALL, b'fi_FI')
  497. def test_invalid_iterable_in_localetuple(self):
  498. with self.assertRaises(TypeError):
  499. locale.setlocale(locale.LC_ALL, (b'not', b'valid'))
  500. class BaseDelocalizeTest(BaseLocalizedTest):
  501. def _test_delocalize(self, value, out):
  502. self.assertEqual(locale.delocalize(value), out)
  503. def _test_atof(self, value, out):
  504. self.assertEqual(locale.atof(value), out)
  505. def _test_atoi(self, value, out):
  506. self.assertEqual(locale.atoi(value), out)
  507. class TestEnUSDelocalize(EnUSCookedTest, BaseDelocalizeTest):
  508. def test_delocalize(self):
  509. self._test_delocalize('50000.00', '50000.00')
  510. self._test_delocalize('50,000.00', '50000.00')
  511. def test_atof(self):
  512. self._test_atof('50000.00', 50000.)
  513. self._test_atof('50,000.00', 50000.)
  514. def test_atoi(self):
  515. self._test_atoi('50000', 50000)
  516. self._test_atoi('50,000', 50000)
  517. class TestCDelocalizeTest(CCookedTest, BaseDelocalizeTest):
  518. def test_delocalize(self):
  519. self._test_delocalize('50000.00', '50000.00')
  520. def test_atof(self):
  521. self._test_atof('50000.00', 50000.)
  522. def test_atoi(self):
  523. self._test_atoi('50000', 50000)
  524. class TestfrFRDelocalizeTest(FrFRCookedTest, BaseDelocalizeTest):
  525. def test_delocalize(self):
  526. self._test_delocalize('50000,00', '50000.00')
  527. self._test_delocalize('50 000,00', '50000.00')
  528. def test_atof(self):
  529. self._test_atof('50000,00', 50000.)
  530. self._test_atof('50 000,00', 50000.)
  531. def test_atoi(self):
  532. self._test_atoi('50000', 50000)
  533. self._test_atoi('50 000', 50000)
  534. class BaseLocalizeTest(BaseLocalizedTest):
  535. def _test_localize(self, value, out, grouping=False):
  536. self.assertEqual(locale.localize(value, grouping=grouping), out)
  537. class TestEnUSLocalize(EnUSCookedTest, BaseLocalizeTest):
  538. def test_localize(self):
  539. self._test_localize('50000.00', '50000.00')
  540. self._test_localize(
  541. '{0:.16f}'.format(Decimal('1.15')), '1.1500000000000000')
  542. class TestCLocalize(CCookedTest, BaseLocalizeTest):
  543. def test_localize(self):
  544. self._test_localize('50000.00', '50000.00')
  545. class TestfrFRLocalize(FrFRCookedTest, BaseLocalizeTest):
  546. def test_localize(self):
  547. self._test_localize('50000.00', '50000,00')
  548. self._test_localize('50000.00', '50 000,00', grouping=True)
  549. if __name__ == '__main__':
  550. unittest.main()