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