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