from test
.test_support
import verbose
, TestSkipped
if sys
.platform
== 'darwin':
raise TestSkipped("Locale support on MacOSX is minimal and cannot be tested")
oldlocale
= locale
.setlocale(locale
.LC_NUMERIC
)
if sys
.platform
.startswith("win"):
tlocs
= ("en_US.UTF-8", "en_US.US-ASCII", "en_US")
locale
.setlocale(locale
.LC_NUMERIC
, tloc
)
raise ImportError, "test locale not supported (tried %s)"%(', '.join(tlocs
))
def testformat(formatstr
, value
, grouping
= 0, output
=None):
print "%s %% %s =? %s ..." %\
(repr(formatstr
), repr(value
), repr(output
)),
print "%s %% %s works? ..." % (repr(formatstr
), repr(value
)),
result
= locale
.format(formatstr
, value
, grouping
= grouping
)
if output
and result
!= output
:
print "%s %% %s == %s != %s" %\
(repr(formatstr
), repr(value
), repr(result
), repr(output
))
testformat("%f", 1024, grouping
=1, output
='1,024.000000')
testformat("%f", 102, grouping
=1, output
='102.000000')
testformat("%f", -42, grouping
=1, output
='-42.000000')
testformat("%+f", -42, grouping
=1, output
='-42.000000')
testformat("%20.f", -42, grouping
=1, output
=' -42')
testformat("%+10.f", -4200, grouping
=1, output
=' -4,200')
testformat("%-10.f", 4200, grouping
=1, output
='4,200 ')
# Invoke getpreferredencoding to make sure it does not cause exceptions,
locale
.getpreferredencoding()
locale
.setlocale(locale
.LC_NUMERIC
, oldlocale
)
# Test BSD Rune locale's bug for isctype functions.
def teststrop(s
, method
, output
):
print "%s.%s() =? %s ..." % (repr(s
), method
, repr(output
)),
result
= getattr(s
, method
)()
print "%s.%s() == %s != %s" % (repr(s
), method
, repr(result
),
oldlocale
= locale
.setlocale(locale
.LC_CTYPE
)
locale
.setlocale(locale
.LC_CTYPE
, 'en_US.UTF-8')
teststrop('\x20', 'isspace', True)
teststrop('\xa0', 'isspace', False)
teststrop('\xa1', 'isspace', False)
teststrop('\xc0', 'isalpha', False)
teststrop('\xc0', 'isalnum', False)
teststrop('\xc0', 'isupper', False)
teststrop('\xc0', 'islower', False)
teststrop('\xec\xa0\xbc', 'split', ['\xec\xa0\xbc'])
teststrop('\xed\x95\xa0', 'strip', '\xed\x95\xa0')
teststrop('\xcc\x85', 'lower', '\xcc\x85')
teststrop('\xed\x95\xa0', 'upper', '\xed\x95\xa0')
locale
.setlocale(locale
.LC_CTYPE
, oldlocale
)