|
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 |
|
@@ -486,6 +487,54 @@ def test_japanese(self):
|
486 | 487 | self.check('jp_jp', 'ja_JP.eucJP')
|
487 | 488 |
|
488 | 489 |
|
| 490 | +class TestRealLocales(unittest.TestCase): |
| 491 | + def setUp(self): |
| 492 | + oldlocale = locale.setlocale(locale.LC_CTYPE) |
| 493 | + self.addCleanup(locale.setlocale, locale.LC_CTYPE, oldlocale) |
| 494 | + |
| 495 | + def test_getsetlocale_issue1813(self): |
| 496 | + # Issue #1813: setting and getting the locale under a Turkish locale |
| 497 | + try: |
| 498 | + locale.setlocale(locale.LC_CTYPE, 'tr_TR') |
| 499 | + except locale.Error: |
| 500 | + # Unsupported locale on this system |
| 501 | + self.skipTest('test needs Turkish locale') |
| 502 | + loc = locale.getlocale(locale.LC_CTYPE) |
| 503 | + if verbose: |
| 504 | + print('testing with %a' % (loc,), end=' ', flush=True) |
| 505 | + try: |
| 506 | + locale.setlocale(locale.LC_CTYPE, loc) |
| 507 | + except locale.Error as exc: |
| 508 | + # bpo-37945: setlocale(LC_CTYPE) fails with getlocale(LC_CTYPE) |
| 509 | + # and the tr_TR locale on Windows. getlocale() builds a locale |
| 510 | + # which is not recognize by setlocale(). |
| 511 | + self.skipTest(f"setlocale(LC_CTYPE, {loc!r}) failed: {exc!r}") |
| 512 | + self.assertEqual(loc, locale.getlocale(locale.LC_CTYPE)) |
| 513 | + |
| 514 | + @unittest.skipUnless(os.name == 'nt', 'requires Windows') |
| 515 | + def test_setlocale_long_encoding(self): |
| 516 | + with self.assertRaises(locale.Error): |
| 517 | + locale.setlocale(locale.LC_CTYPE, 'English.%016d' % 1252) |
| 518 | + locale.setlocale(locale.LC_CTYPE, 'English.%015d' % 1252) |
| 519 | + loc = locale.setlocale(locale.LC_ALL) |
| 520 | + self.assertIn('.1252', loc) |
| 521 | + loc2 = loc.replace('.1252', '.%016d' % 1252, 1) |
| 522 | + with self.assertRaises(locale.Error): |
| 523 | + locale.setlocale(locale.LC_ALL, loc2) |
| 524 | + loc2 = loc.replace('.1252', '.%015d' % 1252, 1) |
| 525 | + locale.setlocale(locale.LC_ALL, loc2) |
| 526 | + |
| 527 | + # gh-137273: Debug assertion failure on Windows for long encoding. |
| 528 | + with self.assertRaises(locale.Error): |
| 529 | + locale.setlocale(locale.LC_CTYPE, 'en_US.' + 'x'*16) |
| 530 | + locale.setlocale(locale.LC_CTYPE, 'en_US.UTF-8') |
| 531 | + loc = locale.setlocale(locale.LC_ALL) |
| 532 | + self.assertIn('.UTF-8', loc) |
| 533 | + loc2 = loc.replace('.UTF-8', '.' + 'x'*16, 1) |
| 534 | + with self.assertRaises(locale.Error): |
| 535 | + locale.setlocale(locale.LC_ALL, loc2) |
| 536 | + |
| 537 | + |
489 | 538 | class TestMiscellaneous(unittest.TestCase):
|
490 | 539 | def test_defaults_UTF8(self):
|
491 | 540 | # Issue #18378: on (at least) macOS setting LC_CTYPE to "UTF-8" is
|
@@ -552,27 +601,6 @@ def test_setlocale_category(self):
|
552 | 601 | # crasher from bug #7419
|
553 | 602 | self.assertRaises(locale.Error, locale.setlocale, 12345)
|
554 | 603 |
|
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 | 604 | def test_invalid_locale_format_in_localetuple(self):
|
577 | 605 | with self.assertRaises(TypeError):
|
578 | 606 | locale.setlocale(locale.LC_ALL, b'fi_FI')
|
|
0 commit comments