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