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