Jack Jansen | 06f0cef | 2002-12-30 23:02:55 +0000 | [diff] [blame] | 1 | from test.test_support import verbose, TestSkipped |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 2 | import locale |
Guido van Rossum | fc34986 | 2001-04-15 13:15:56 +0000 | [diff] [blame] | 3 | import sys |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 4 | |
Jack Jansen | 06f0cef | 2002-12-30 23:02:55 +0000 | [diff] [blame] | 5 | if sys.platform == 'darwin': |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 6 | raise TestSkipped("Locale support on MacOSX is minimal and cannot be tested") |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 7 | oldlocale = locale.setlocale(locale.LC_NUMERIC) |
| 8 | |
Hye-Shik Chang | 8d2e08d | 2003-12-19 01:16:03 +0000 | [diff] [blame] | 9 | if sys.platform.startswith("win"): |
Guido van Rossum | fc34986 | 2001-04-15 13:15:56 +0000 | [diff] [blame] | 10 | tloc = "en" |
Hye-Shik Chang | 8d2e08d | 2003-12-19 01:16:03 +0000 | [diff] [blame] | 11 | elif sys.platform.startswith("freebsd"): |
| 12 | tloc = "en_US.US-ASCII" |
| 13 | else: |
| 14 | tloc = "en_US" |
Guido van Rossum | fc34986 | 2001-04-15 13:15:56 +0000 | [diff] [blame] | 15 | |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 16 | try: |
Guido van Rossum | fc34986 | 2001-04-15 13:15:56 +0000 | [diff] [blame] | 17 | locale.setlocale(locale.LC_NUMERIC, tloc) |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 18 | except locale.Error: |
Guido van Rossum | fc34986 | 2001-04-15 13:15:56 +0000 | [diff] [blame] | 19 | raise ImportError, "test locale %s not supported" % tloc |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 20 | |
| 21 | def testformat(formatstr, value, grouping = 0, output=None): |
| 22 | if verbose: |
| 23 | if output: |
| 24 | print "%s %% %s =? %s ..." %\ |
| 25 | (repr(formatstr), repr(value), repr(output)), |
| 26 | else: |
| 27 | print "%s %% %s works? ..." % (repr(formatstr), repr(value)), |
| 28 | result = locale.format(formatstr, value, grouping = grouping) |
| 29 | if output and result != output: |
| 30 | if verbose: |
| 31 | print 'no' |
| 32 | print "%s %% %s == %s != %s" %\ |
| 33 | (repr(formatstr), repr(value), repr(result), repr(output)) |
| 34 | else: |
| 35 | if verbose: |
| 36 | print "yes" |
| 37 | |
| 38 | try: |
| 39 | testformat("%f", 1024, grouping=1, output='1,024.000000') |
| 40 | testformat("%f", 102, grouping=1, output='102.000000') |
| 41 | testformat("%f", -42, grouping=1, output='-42.000000') |
| 42 | testformat("%+f", -42, grouping=1, output='-42.000000') |
| 43 | testformat("%20.f", -42, grouping=1, output=' -42') |
Tim Peters | 8ae2df4 | 2001-05-02 05:54:44 +0000 | [diff] [blame] | 44 | testformat("%+10.f", -4200, grouping=1, output=' -4,200') |
| 45 | testformat("%-10.f", 4200, grouping=1, output='4,200 ') |
Martin v. Löwis | f0a4668 | 2002-11-03 17:20:12 +0000 | [diff] [blame] | 46 | # Invoke getpreferredencoding to make sure it does not cause exceptions, |
| 47 | locale.getpreferredencoding() |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 48 | finally: |
| 49 | locale.setlocale(locale.LC_NUMERIC, oldlocale) |
Hye-Shik Chang | b5047fd | 2004-08-04 06:33:51 +0000 | [diff] [blame] | 50 | |
| 51 | |
| 52 | # Test BSD Rune locale's bug for isctype functions. |
| 53 | def teststrop(s, method, output): |
| 54 | if verbose: |
| 55 | print "%s.%s() =? %s ..." % (repr(s), method, repr(output)), |
| 56 | result = getattr(s, method)() |
| 57 | if result != output: |
| 58 | if verbose: |
| 59 | print "no" |
| 60 | print "%s.%s() == %s != %s" % (repr(s), method, repr(result), |
| 61 | repr(output)) |
| 62 | elif verbose: |
| 63 | print "yes" |
| 64 | |
| 65 | try: |
| 66 | oldlocale = locale.setlocale(locale.LC_CTYPE) |
| 67 | locale.setlocale(locale.LC_CTYPE, 'en_US.UTF-8') |
| 68 | except locale.Error: |
| 69 | pass |
| 70 | else: |
| 71 | try: |
| 72 | teststrop('\x20', 'isspace', True) |
| 73 | teststrop('\xa0', 'isspace', False) |
| 74 | teststrop('\xa1', 'isspace', False) |
| 75 | teststrop('\xc0', 'isalpha', False) |
| 76 | teststrop('\xc0', 'isalnum', False) |
| 77 | teststrop('\xc0', 'isupper', False) |
| 78 | teststrop('\xc0', 'islower', False) |
| 79 | teststrop('\xec\xa0\xbc', 'split', ['\xec\xa0\xbc']) |
| 80 | teststrop('\xed\x95\xa0', 'strip', '\xed\x95\xa0') |
| 81 | teststrop('\xcc\x85', 'lower', '\xcc\x85') |
| 82 | teststrop('\xed\x95\xa0', 'upper', '\xed\x95\xa0') |
| 83 | finally: |
| 84 | locale.setlocale(locale.LC_CTYPE, oldlocale) |