| 1 | from test.test_support import verbose, TestSkipped
|
|---|
| 2 | import locale
|
|---|
| 3 | import sys
|
|---|
| 4 |
|
|---|
| 5 | if sys.platform == 'darwin':
|
|---|
| 6 | raise TestSkipped("Locale support on MacOSX is minimal and cannot be tested")
|
|---|
| 7 | oldlocale = locale.setlocale(locale.LC_NUMERIC)
|
|---|
| 8 |
|
|---|
| 9 | if sys.platform.startswith("win"):
|
|---|
| 10 | tlocs = ("en",)
|
|---|
| 11 | else:
|
|---|
| 12 | tlocs = ("en_US.UTF-8", "en_US.US-ASCII", "en_US")
|
|---|
| 13 |
|
|---|
| 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))
|
|---|
| 22 |
|
|---|
| 23 | def testformat(formatstr, value, grouping = 0, output=None, func=locale.format):
|
|---|
| 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 = func(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:
|
|---|
| 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)
|
|---|
| 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')
|
|---|
| 48 | testformat("%+10.f", -4200, grouping=1, output=' -4%s200' % sep)
|
|---|
| 49 | testformat("%-10.f", 4200, grouping=1, output='4%s200 ' % sep)
|
|---|
| 50 | # Invoke getpreferredencoding to make sure it does not cause exceptions,
|
|---|
| 51 | locale.getpreferredencoding()
|
|---|
| 52 |
|
|---|
| 53 | # === Test format() with more complex formatting strings
|
|---|
| 54 | # test if grouping is independent from other characters in formatting string
|
|---|
| 55 | testformat("One million is %i", 1000000, grouping=1,
|
|---|
| 56 | output='One million is 1%s000%s000' % (sep, sep),
|
|---|
| 57 | func=locale.format_string)
|
|---|
| 58 | testformat("One million is %i", 1000000, grouping=1,
|
|---|
| 59 | output='One million is 1%s000%s000' % (sep, sep),
|
|---|
| 60 | func=locale.format_string)
|
|---|
| 61 | # test dots in formatting string
|
|---|
| 62 | testformat(".%f.", 1000.0, output='.1000.000000.', func=locale.format_string)
|
|---|
| 63 | # test floats
|
|---|
| 64 | testformat("--> %10.2f", 1000.0, grouping=1, output='--> 1%s000.00' % sep,
|
|---|
| 65 | func=locale.format_string)
|
|---|
| 66 | # test asterisk formats
|
|---|
| 67 | testformat("%10.*f", (2, 1000.0), grouping=0, output=' 1000.00',
|
|---|
| 68 | func=locale.format_string)
|
|---|
| 69 | testformat("%*.*f", (10, 2, 1000.0), grouping=1, output=' 1%s000.00' % sep,
|
|---|
| 70 | func=locale.format_string)
|
|---|
| 71 | # test more-in-one
|
|---|
| 72 | testformat("int %i float %.2f str %s", (1000, 1000.0, 'str'), grouping=1,
|
|---|
| 73 | output='int 1%s000 float 1%s000.00 str str' % (sep, sep),
|
|---|
| 74 | func=locale.format_string)
|
|---|
| 75 |
|
|---|
| 76 | finally:
|
|---|
| 77 | locale.setlocale(locale.LC_NUMERIC, oldlocale)
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 | # Test BSD Rune locale's bug for isctype functions.
|
|---|
| 81 | def teststrop(s, method, output):
|
|---|
| 82 | if verbose:
|
|---|
| 83 | print "%s.%s() =? %s ..." % (repr(s), method, repr(output)),
|
|---|
| 84 | result = getattr(s, method)()
|
|---|
| 85 | if result != output:
|
|---|
| 86 | if verbose:
|
|---|
| 87 | print "no"
|
|---|
| 88 | print "%s.%s() == %s != %s" % (repr(s), method, repr(result),
|
|---|
| 89 | repr(output))
|
|---|
| 90 | elif verbose:
|
|---|
| 91 | print "yes"
|
|---|
| 92 |
|
|---|
| 93 | try:
|
|---|
| 94 | if sys.platform == 'sunos5':
|
|---|
| 95 | # On Solaris, in en_US.UTF-8, \xa0 is a space
|
|---|
| 96 | raise locale.Error
|
|---|
| 97 | oldlocale = locale.setlocale(locale.LC_CTYPE)
|
|---|
| 98 | locale.setlocale(locale.LC_CTYPE, 'en_US.UTF-8')
|
|---|
| 99 | except locale.Error:
|
|---|
| 100 | pass
|
|---|
| 101 | else:
|
|---|
| 102 | try:
|
|---|
| 103 | teststrop('\x20', 'isspace', True)
|
|---|
| 104 | teststrop('\xa0', 'isspace', False)
|
|---|
| 105 | teststrop('\xa1', 'isspace', False)
|
|---|
| 106 | teststrop('\xc0', 'isalpha', False)
|
|---|
| 107 | teststrop('\xc0', 'isalnum', False)
|
|---|
| 108 | teststrop('\xc0', 'isupper', False)
|
|---|
| 109 | teststrop('\xc0', 'islower', False)
|
|---|
| 110 | teststrop('\xec\xa0\xbc', 'split', ['\xec\xa0\xbc'])
|
|---|
| 111 | teststrop('\xed\x95\xa0', 'strip', '\xed\x95\xa0')
|
|---|
| 112 | teststrop('\xcc\x85', 'lower', '\xcc\x85')
|
|---|
| 113 | teststrop('\xed\x95\xa0', 'upper', '\xed\x95\xa0')
|
|---|
| 114 | finally:
|
|---|
| 115 | locale.setlocale(locale.LC_CTYPE, oldlocale)
|
|---|