1from decimal import Decimal
2from test.support import verbose, is_android
3from test.support.warnings_helper import check_warnings
4import unittest
5import locale
6import sys
7import codecs
8
9
10class BaseLocalizedTest(unittest.TestCase):
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
53class BaseCookedTest(unittest.TestCase):
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
64class CCookedTest(BaseCookedTest):
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
88class EnUSCookedTest(BaseCookedTest):
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
113class FrFRCookedTest(BaseCookedTest):
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
139class BaseFormattingTest(object):
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
161class EnUSNumberFormatting(BaseFormattingTest):
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
234class TestFormatPatternArg(unittest.TestCase):
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
249class TestLocaleFormatString(unittest.TestCase):
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
267class TestNumberFormatting(BaseLocalizedTest, EnUSNumberFormatting):
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
277class TestEnUSNumberFormatting(EnUSCookedTest, EnUSNumberFormatting):
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
291class TestCNumberFormatting(CCookedTest, BaseFormattingTest):
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
301class TestFrFRNumberFormatting(FrFRCookedTest, BaseFormattingTest):
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
343class TestCollation(unittest.TestCase):
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
360class TestEnUSCollation(BaseLocalizedTest, TestCollation):
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.getpreferredencoding(False) 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    def test_strcoll_with_diacritic(self):
377        self.assertLess(locale.strcoll('à', 'b'), 0)
378
379    @unittest.skipIf(sys.platform.startswith('aix'),
380                     'bpo-29972: broken test on AIX')
381    def test_strxfrm_with_diacritic(self):
382        self.assertLess(locale.strxfrm('à'), locale.strxfrm('b'))
383
384
385class NormalizeTest(unittest.TestCase):
386    def check(self, localename, expected):
387        self.assertEqual(locale.normalize(localename), expected, msg=localename)
388
389    def test_locale_alias(self):
390        for localename, alias in locale.locale_alias.items():
391            with self.subTest(locale=(localename, alias)):
392                self.check(localename, alias)
393
394    def test_empty(self):
395        self.check('', '')
396
397    def test_c(self):
398        self.check('c', 'C')
399        self.check('posix', 'C')
400
401    def test_english(self):
402        self.check('en', 'en_US.ISO8859-1')
403        self.check('EN', 'en_US.ISO8859-1')
404        self.check('en.iso88591', 'en_US.ISO8859-1')
405        self.check('en_US', 'en_US.ISO8859-1')
406        self.check('en_us', 'en_US.ISO8859-1')
407        self.check('en_GB', 'en_GB.ISO8859-1')
408        self.check('en_US.UTF-8', 'en_US.UTF-8')
409        self.check('en_US.utf8', 'en_US.UTF-8')
410        self.check('en_US:UTF-8', 'en_US.UTF-8')
411        self.check('en_US.ISO8859-1', 'en_US.ISO8859-1')
412        self.check('en_US.US-ASCII', 'en_US.ISO8859-1')
413        self.check('en_US.88591', 'en_US.ISO8859-1')
414        self.check('en_US.885915', 'en_US.ISO8859-15')
415        self.check('english', 'en_EN.ISO8859-1')
416        self.check('english_uk.ascii', 'en_GB.ISO8859-1')
417
418    def test_hyphenated_encoding(self):
419        self.check('az_AZ.iso88599e', 'az_AZ.ISO8859-9E')
420        self.check('az_AZ.ISO8859-9E', 'az_AZ.ISO8859-9E')
421        self.check('tt_RU.koi8c', 'tt_RU.KOI8-C')
422        self.check('tt_RU.KOI8-C', 'tt_RU.KOI8-C')
423        self.check('lo_LA.cp1133', 'lo_LA.IBM-CP1133')
424        self.check('lo_LA.ibmcp1133', 'lo_LA.IBM-CP1133')
425        self.check('lo_LA.IBM-CP1133', 'lo_LA.IBM-CP1133')
426        self.check('uk_ua.microsoftcp1251', 'uk_UA.CP1251')
427        self.check('uk_ua.microsoft-cp1251', 'uk_UA.CP1251')
428        self.check('ka_ge.georgianacademy', 'ka_GE.GEORGIAN-ACADEMY')
429        self.check('ka_GE.GEORGIAN-ACADEMY', 'ka_GE.GEORGIAN-ACADEMY')
430        self.check('cs_CZ.iso88592', 'cs_CZ.ISO8859-2')
431        self.check('cs_CZ.ISO8859-2', 'cs_CZ.ISO8859-2')
432
433    def test_euro_modifier(self):
434        self.check('de_DE@euro', 'de_DE.ISO8859-15')
435        self.check('en_US.ISO8859-15@euro', 'en_US.ISO8859-15')
436        self.check('de_DE.utf8@euro', 'de_DE.UTF-8')
437
438    def test_latin_modifier(self):
439        self.check('be_BY.UTF-8@latin', 'be_BY.UTF-8@latin')
440        self.check('sr_RS.UTF-8@latin', 'sr_RS.UTF-8@latin')
441        self.check('sr_RS.UTF-8@latn', 'sr_RS.UTF-8@latin')
442
443    def test_valencia_modifier(self):
444        self.check('ca_ES.UTF-8@valencia', 'ca_ES.UTF-8@valencia')
445        self.check('ca_ES@valencia', 'ca_ES.UTF-8@valencia')
446        self.check('ca@valencia', 'ca_ES.ISO8859-1@valencia')
447
448    def test_devanagari_modifier(self):
449        self.check('ks_IN.UTF-8@devanagari', 'ks_IN.UTF-8@devanagari')
450        self.check('ks_IN@devanagari', 'ks_IN.UTF-8@devanagari')
451        self.check('ks@devanagari', 'ks_IN.UTF-8@devanagari')
452        self.check('ks_IN.UTF-8', 'ks_IN.UTF-8')
453        self.check('ks_IN', 'ks_IN.UTF-8')
454        self.check('ks', 'ks_IN.UTF-8')
455        self.check('sd_IN.UTF-8@devanagari', 'sd_IN.UTF-8@devanagari')
456        self.check('sd_IN@devanagari', 'sd_IN.UTF-8@devanagari')
457        self.check('sd@devanagari', 'sd_IN.UTF-8@devanagari')
458        self.check('sd_IN.UTF-8', 'sd_IN.UTF-8')
459        self.check('sd_IN', 'sd_IN.UTF-8')
460        self.check('sd', 'sd_IN.UTF-8')
461
462    def test_euc_encoding(self):
463        self.check('ja_jp.euc', 'ja_JP.eucJP')
464        self.check('ja_jp.eucjp', 'ja_JP.eucJP')
465        self.check('ko_kr.euc', 'ko_KR.eucKR')
466        self.check('ko_kr.euckr', 'ko_KR.eucKR')
467        self.check('zh_cn.euc', 'zh_CN.eucCN')
468        self.check('zh_tw.euc', 'zh_TW.eucTW')
469        self.check('zh_tw.euctw', 'zh_TW.eucTW')
470
471    def test_japanese(self):
472        self.check('ja', 'ja_JP.eucJP')
473        self.check('ja.jis', 'ja_JP.JIS7')
474        self.check('ja.sjis', 'ja_JP.SJIS')
475        self.check('ja_jp', 'ja_JP.eucJP')
476        self.check('ja_jp.ajec', 'ja_JP.eucJP')
477        self.check('ja_jp.euc', 'ja_JP.eucJP')
478        self.check('ja_jp.eucjp', 'ja_JP.eucJP')
479        self.check('ja_jp.iso-2022-jp', 'ja_JP.JIS7')
480        self.check('ja_jp.iso2022jp', 'ja_JP.JIS7')
481        self.check('ja_jp.jis', 'ja_JP.JIS7')
482        self.check('ja_jp.jis7', 'ja_JP.JIS7')
483        self.check('ja_jp.mscode', 'ja_JP.SJIS')
484        self.check('ja_jp.pck', 'ja_JP.SJIS')
485        self.check('ja_jp.sjis', 'ja_JP.SJIS')
486        self.check('ja_jp.ujis', 'ja_JP.eucJP')
487        self.check('ja_jp.utf8', 'ja_JP.UTF-8')
488        self.check('japan', 'ja_JP.eucJP')
489        self.check('japanese', 'ja_JP.eucJP')
490        self.check('japanese-euc', 'ja_JP.eucJP')
491        self.check('japanese.euc', 'ja_JP.eucJP')
492        self.check('japanese.sjis', 'ja_JP.SJIS')
493        self.check('jp_jp', 'ja_JP.eucJP')
494
495
496class TestMiscellaneous(unittest.TestCase):
497    def test_defaults_UTF8(self):
498        # Issue #18378: on (at least) macOS setting LC_CTYPE to "UTF-8" is
499        # valid. Furthermore LC_CTYPE=UTF is used by the UTF-8 locale coercing
500        # during interpreter startup (on macOS).
501        import _locale
502        import os
503
504        self.assertEqual(locale._parse_localename('UTF-8'), (None, 'UTF-8'))
505
506        if hasattr(_locale, '_getdefaultlocale'):
507            orig_getlocale = _locale._getdefaultlocale
508            del _locale._getdefaultlocale
509        else:
510            orig_getlocale = None
511
512        orig_env = {}
513        try:
514            for key in ('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE'):
515                if key in os.environ:
516                    orig_env[key] = os.environ[key]
517                    del os.environ[key]
518
519            os.environ['LC_CTYPE'] = 'UTF-8'
520
521            self.assertEqual(locale.getdefaultlocale(), (None, 'UTF-8'))
522
523        finally:
524            for k in orig_env:
525                os.environ[k] = orig_env[k]
526
527            if 'LC_CTYPE' not in orig_env:
528                del os.environ['LC_CTYPE']
529
530            if orig_getlocale is not None:
531                _locale._getdefaultlocale = orig_getlocale
532
533    def test_getpreferredencoding(self):
534        # Invoke getpreferredencoding to make sure it does not cause exceptions.
535        enc = locale.getpreferredencoding()
536        if enc:
537            # If encoding non-empty, make sure it is valid
538            codecs.lookup(enc)
539
540    def test_strcoll_3303(self):
541        # test crasher from bug #3303
542        self.assertRaises(TypeError, locale.strcoll, "a", None)
543        self.assertRaises(TypeError, locale.strcoll, b"a", None)
544
545    def test_setlocale_category(self):
546        locale.setlocale(locale.LC_ALL)
547        locale.setlocale(locale.LC_TIME)
548        locale.setlocale(locale.LC_CTYPE)
549        locale.setlocale(locale.LC_COLLATE)
550        locale.setlocale(locale.LC_MONETARY)
551        locale.setlocale(locale.LC_NUMERIC)
552
553        # crasher from bug #7419
554        self.assertRaises(locale.Error, locale.setlocale, 12345)
555
556    def test_getsetlocale_issue1813(self):
557        # Issue #1813: setting and getting the locale under a Turkish locale
558        oldlocale = locale.setlocale(locale.LC_CTYPE)
559        self.addCleanup(locale.setlocale, locale.LC_CTYPE, oldlocale)
560        try:
561            locale.setlocale(locale.LC_CTYPE, 'tr_TR')
562        except locale.Error:
563            # Unsupported locale on this system
564            self.skipTest('test needs Turkish locale')
565        loc = locale.getlocale(locale.LC_CTYPE)
566        if verbose:
567            print('testing with %a' % (loc,), end=' ', flush=True)
568        try:
569            locale.setlocale(locale.LC_CTYPE, loc)
570        except locale.Error as exc:
571            # bpo-37945: setlocale(LC_CTYPE) fails with getlocale(LC_CTYPE)
572            # and the tr_TR locale on Windows. getlocale() builds a locale
573            # which is not recognize by setlocale().
574            self.skipTest(f"setlocale(LC_CTYPE, {loc!r}) failed: {exc!r}")
575        self.assertEqual(loc, locale.getlocale(locale.LC_CTYPE))
576
577    def test_invalid_locale_format_in_localetuple(self):
578        with self.assertRaises(TypeError):
579            locale.setlocale(locale.LC_ALL, b'fi_FI')
580
581    def test_invalid_iterable_in_localetuple(self):
582        with self.assertRaises(TypeError):
583            locale.setlocale(locale.LC_ALL, (b'not', b'valid'))
584
585
586class BaseDelocalizeTest(BaseLocalizedTest):
587
588    def _test_delocalize(self, value, out):
589        self.assertEqual(locale.delocalize(value), out)
590
591    def _test_atof(self, value, out):
592        self.assertEqual(locale.atof(value), out)
593
594    def _test_atoi(self, value, out):
595        self.assertEqual(locale.atoi(value), out)
596
597
598class TestEnUSDelocalize(EnUSCookedTest, BaseDelocalizeTest):
599
600    def test_delocalize(self):
601        self._test_delocalize('50000.00', '50000.00')
602        self._test_delocalize('50,000.00', '50000.00')
603
604    def test_atof(self):
605        self._test_atof('50000.00', 50000.)
606        self._test_atof('50,000.00', 50000.)
607
608    def test_atoi(self):
609        self._test_atoi('50000', 50000)
610        self._test_atoi('50,000', 50000)
611
612
613class TestCDelocalizeTest(CCookedTest, BaseDelocalizeTest):
614
615    def test_delocalize(self):
616        self._test_delocalize('50000.00', '50000.00')
617
618    def test_atof(self):
619        self._test_atof('50000.00', 50000.)
620
621    def test_atoi(self):
622        self._test_atoi('50000', 50000)
623
624
625class TestfrFRDelocalizeTest(FrFRCookedTest, BaseDelocalizeTest):
626
627    def test_delocalize(self):
628        self._test_delocalize('50000,00', '50000.00')
629        self._test_delocalize('50 000,00', '50000.00')
630
631    def test_atof(self):
632        self._test_atof('50000,00', 50000.)
633        self._test_atof('50 000,00', 50000.)
634
635    def test_atoi(self):
636        self._test_atoi('50000', 50000)
637        self._test_atoi('50 000', 50000)
638
639
640class BaseLocalizeTest(BaseLocalizedTest):
641
642    def _test_localize(self, value, out, grouping=False):
643        self.assertEqual(locale.localize(value, grouping=grouping), out)
644
645
646class TestEnUSLocalize(EnUSCookedTest, BaseLocalizeTest):
647
648    def test_localize(self):
649        self._test_localize('50000.00', '50000.00')
650        self._test_localize(
651            '{0:.16f}'.format(Decimal('1.15')), '1.1500000000000000')
652
653
654class TestCLocalize(CCookedTest, BaseLocalizeTest):
655
656    def test_localize(self):
657        self._test_localize('50000.00', '50000.00')
658
659
660class TestfrFRLocalize(FrFRCookedTest, BaseLocalizeTest):
661
662    def test_localize(self):
663        self._test_localize('50000.00', '50000,00')
664        self._test_localize('50000.00', '50 000,00', grouping=True)
665
666
667if __name__ == '__main__':
668    unittest.main()
669