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"): |
Anthony Baxter | 451ae18 | 2005-06-03 15:04:15 +0000 | [diff] [blame] | 10 | tlocs = ("en",) |
Hye-Shik Chang | 8d2e08d | 2003-12-19 01:16:03 +0000 | [diff] [blame] | 11 | else: |
Anthony Baxter | 451ae18 | 2005-06-03 15:04:15 +0000 | [diff] [blame] | 12 | tlocs = ("en_US.UTF-8", "en_US.US-ASCII", "en_US") |
Guido van Rossum | fc34986 | 2001-04-15 13:15:56 +0000 | [diff] [blame] | 13 | |
Anthony Baxter | 451ae18 | 2005-06-03 15:04:15 +0000 | [diff] [blame] | 14 | for tloc in tlocs: |
| 15 | try: |
| 16 | locale.setlocale(locale.LC_NUMERIC, tloc) |
| 17 | break |
| 18 | except locale.Error: |
| 19 | continue |
| 20 | else: |
| 21 | raise ImportError, "test locale not supported (tried %s)"%(', '.join(tlocs)) |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 22 | |
| 23 | def testformat(formatstr, value, grouping = 0, output=None): |
| 24 | if verbose: |
| 25 | if output: |
| 26 | print "%s %% %s =? %s ..." %\ |
| 27 | (repr(formatstr), repr(value), repr(output)), |
| 28 | else: |
| 29 | print "%s %% %s works? ..." % (repr(formatstr), repr(value)), |
| 30 | result = locale.format(formatstr, value, grouping = grouping) |
| 31 | if output and result != output: |
| 32 | if verbose: |
| 33 | print 'no' |
| 34 | print "%s %% %s == %s != %s" %\ |
| 35 | (repr(formatstr), repr(value), repr(result), repr(output)) |
| 36 | else: |
| 37 | if verbose: |
| 38 | print "yes" |
| 39 | |
| 40 | try: |
Martin v. Löwis | 4cfa136 | 2005-12-30 12:51:45 +0000 | [diff] [blame^] | 41 | # On Solaris 10, the thousands_sep is the empty string |
| 42 | sep = locale.localeconv()['thousands_sep'] |
| 43 | testformat("%f", 1024, grouping=1, output='1%s024.000000' % sep) |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 44 | testformat("%f", 102, grouping=1, output='102.000000') |
| 45 | testformat("%f", -42, grouping=1, output='-42.000000') |
| 46 | testformat("%+f", -42, grouping=1, output='-42.000000') |
| 47 | testformat("%20.f", -42, grouping=1, output=' -42') |
Martin v. Löwis | 4cfa136 | 2005-12-30 12:51:45 +0000 | [diff] [blame^] | 48 | testformat("%+10.f", -4200, grouping=1, output=' -4%s200' % sep) |
| 49 | testformat("%-10.f", 4200, grouping=1, output='4%s200 ' % sep) |
Martin v. Löwis | f0a4668 | 2002-11-03 17:20:12 +0000 | [diff] [blame] | 50 | # Invoke getpreferredencoding to make sure it does not cause exceptions, |
| 51 | locale.getpreferredencoding() |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 52 | finally: |
| 53 | locale.setlocale(locale.LC_NUMERIC, oldlocale) |
Hye-Shik Chang | b5047fd | 2004-08-04 06:33:51 +0000 | [diff] [blame] | 54 | |
| 55 | |
| 56 | # Test BSD Rune locale's bug for isctype functions. |
| 57 | def teststrop(s, method, output): |
| 58 | if verbose: |
| 59 | print "%s.%s() =? %s ..." % (repr(s), method, repr(output)), |
| 60 | result = getattr(s, method)() |
| 61 | if result != output: |
| 62 | if verbose: |
| 63 | print "no" |
| 64 | print "%s.%s() == %s != %s" % (repr(s), method, repr(result), |
| 65 | repr(output)) |
| 66 | elif verbose: |
| 67 | print "yes" |
| 68 | |
| 69 | try: |
Martin v. Löwis | 4cfa136 | 2005-12-30 12:51:45 +0000 | [diff] [blame^] | 70 | if sys.platform == 'sunos5': |
| 71 | # On Solaris, in en_US.UTF-8, \xa0 is a space |
| 72 | raise locale.Error |
Hye-Shik Chang | b5047fd | 2004-08-04 06:33:51 +0000 | [diff] [blame] | 73 | oldlocale = locale.setlocale(locale.LC_CTYPE) |
| 74 | locale.setlocale(locale.LC_CTYPE, 'en_US.UTF-8') |
| 75 | except locale.Error: |
| 76 | pass |
| 77 | else: |
| 78 | try: |
| 79 | teststrop('\x20', 'isspace', True) |
| 80 | teststrop('\xa0', 'isspace', False) |
| 81 | teststrop('\xa1', 'isspace', False) |
| 82 | teststrop('\xc0', 'isalpha', False) |
| 83 | teststrop('\xc0', 'isalnum', False) |
| 84 | teststrop('\xc0', 'isupper', False) |
| 85 | teststrop('\xc0', 'islower', False) |
| 86 | teststrop('\xec\xa0\xbc', 'split', ['\xec\xa0\xbc']) |
| 87 | teststrop('\xed\x95\xa0', 'strip', '\xed\x95\xa0') |
| 88 | teststrop('\xcc\x85', 'lower', '\xcc\x85') |
| 89 | teststrop('\xed\x95\xa0', 'upper', '\xed\x95\xa0') |
| 90 | finally: |
| 91 | locale.setlocale(locale.LC_CTYPE, oldlocale) |