|
5 | 5 | from unittest import mock
|
6 | 6 | import unittest
|
7 | 7 | import locale
|
| 8 | +import os |
8 | 9 | import sys
|
9 | 10 | import codecs
|
10 | 11 |
|
@@ -487,6 +488,54 @@ def test_japanese(self):
|
487 | 488 | self.check('jp_jp', 'ja_JP.eucJP')
|
488 | 489 |
|
489 | 490 |
|
| 491 | +class TestRealLocales(unittest.TestCase): |
| 492 | + def setUp(self): |
| 493 | + oldlocale = locale.setlocale(locale.LC_CTYPE) |
| 494 | + self.addCleanup(locale.setlocale, locale.LC_CTYPE, oldlocale) |
| 495 | + |
| 496 | + def test_getsetlocale_issue1813(self): |
| 497 | + # Issue #1813: setting and getting the locale under a Turkish locale |
| 498 | + try: |
| 499 | + locale.setlocale(locale.LC_CTYPE, 'tr_TR') |
| 500 | + except locale.Error: |
| 501 | + # Unsupported locale on this system |
| 502 | + self.skipTest('test needs Turkish locale') |
| 503 | + loc = locale.getlocale(locale.LC_CTYPE) |
| 504 | + if verbose: |
| 505 | + print('testing with %a' % (loc,), end=' ', flush=True) |
| 506 | + try: |
| 507 | + locale.setlocale(locale.LC_CTYPE, loc) |
| 508 | + except locale.Error as exc: |
| 509 | + # bpo-37945: setlocale(LC_CTYPE) fails with getlocale(LC_CTYPE) |
| 510 | + # and the tr_TR locale on Windows. getlocale() builds a locale |
| 511 | + # which is not recognize by setlocale(). |
| 512 | + self.skipTest(f"setlocale(LC_CTYPE, {loc!r}) failed: {exc!r}") |
| 513 | + self.assertEqual(loc, locale.getlocale(locale.LC_CTYPE)) |
| 514 | + |
| 515 | + @unittest.skipUnless(os.name == 'nt', 'requires Windows') |
| 516 | + def test_setlocale_long_encoding(self): |
| 517 | + with self.assertRaises(locale.Error): |
| 518 | + locale.setlocale(locale.LC_CTYPE, 'English.%016d' % 1252) |
| 519 | + locale.setlocale(locale.LC_CTYPE, 'English.%015d' % 1252) |
| 520 | + loc = locale.setlocale(locale.LC_ALL) |
| 521 | + self.assertIn('.1252', loc) |
| 522 | + loc2 = loc.replace('.1252', '.%016d' % 1252, 1) |
| 523 | + with self.assertRaises(locale.Error): |
| 524 | + locale.setlocale(locale.LC_ALL, loc2) |
| 525 | + loc2 = loc.replace('.1252', '.%015d' % 1252, 1) |
| 526 | + locale.setlocale(locale.LC_ALL, loc2) |
| 527 | + |
| 528 | + # gh-137273: Debug assertion failure on Windows for long encoding. |
| 529 | + with self.assertRaises(locale.Error): |
| 530 | + locale.setlocale(locale.LC_CTYPE, 'en_US.' + 'x'*16) |
| 531 | + locale.setlocale(locale.LC_CTYPE, 'en_US.UTF-8') |
| 532 | + loc = locale.setlocale(locale.LC_ALL) |
| 533 | + self.assertIn('.UTF-8', loc) |
| 534 | + loc2 = loc.replace('.UTF-8', '.' + 'x'*16, 1) |
| 535 | + with self.assertRaises(locale.Error): |
| 536 | + locale.setlocale(locale.LC_ALL, loc2) |
| 537 | + |
| 538 | + |
490 | 539 | class TestMiscellaneous(unittest.TestCase):
|
491 | 540 | def test_defaults_UTF8(self):
|
492 | 541 | # Issue #18378: on (at least) macOS setting LC_CTYPE to "UTF-8" is
|
@@ -565,27 +614,6 @@ def test_setlocale_category(self):
|
565 | 614 | # crasher from bug #7419
|
566 | 615 | self.assertRaises(locale.Error, locale.setlocale, 12345)
|
567 | 616 |
|
568 |
| - def test_getsetlocale_issue1813(self): |
569 |
| - # Issue #1813: setting and getting the locale under a Turkish locale |
570 |
| - oldlocale = locale.setlocale(locale.LC_CTYPE) |
571 |
| - self.addCleanup(locale.setlocale, locale.LC_CTYPE, oldlocale) |
572 |
| - try: |
573 |
| - locale.setlocale(locale.LC_CTYPE, 'tr_TR') |
574 |
| - except locale.Error: |
575 |
| - # Unsupported locale on this system |
576 |
| - self.skipTest('test needs Turkish locale') |
577 |
| - loc = locale.getlocale(locale.LC_CTYPE) |
578 |
| - if verbose: |
579 |
| - print('testing with %a' % (loc,), end=' ', flush=True) |
580 |
| - try: |
581 |
| - locale.setlocale(locale.LC_CTYPE, loc) |
582 |
| - except locale.Error as exc: |
583 |
| - # bpo-37945: setlocale(LC_CTYPE) fails with getlocale(LC_CTYPE) |
584 |
| - # and the tr_TR locale on Windows. getlocale() builds a locale |
585 |
| - # which is not recognize by setlocale(). |
586 |
| - self.skipTest(f"setlocale(LC_CTYPE, {loc!r}) failed: {exc!r}") |
587 |
| - self.assertEqual(loc, locale.getlocale(locale.LC_CTYPE)) |
588 |
| - |
589 | 617 | def test_invalid_locale_format_in_localetuple(self):
|
590 | 618 | with self.assertRaises(TypeError):
|
591 | 619 | locale.setlocale(locale.LC_ALL, b'fi_FI')
|
|
0 commit comments