py-cvs-rel2_1 (Rev 1.2) merge
[python/dscho.git] / Lib / test / test_locale.py
blob0040a20899af109902356871f73118bb917fed0b
1 from test_support import verbose
2 import locale
3 import sys
5 oldlocale = locale.setlocale(locale.LC_NUMERIC)
7 tloc = "en_US"
8 if sys.platform[:3] == "win":
9 tloc = "en"
11 try:
12 locale.setlocale(locale.LC_NUMERIC, tloc)
13 except locale.Error:
14 raise ImportError, "test locale %s not supported" % tloc
16 def testformat(formatstr, value, grouping = 0, output=None):
17 if verbose:
18 if output:
19 print "%s %% %s =? %s ..." %\
20 (repr(formatstr), repr(value), repr(output)),
21 else:
22 print "%s %% %s works? ..." % (repr(formatstr), repr(value)),
23 result = locale.format(formatstr, value, grouping = grouping)
24 if output and result != output:
25 if verbose:
26 print 'no'
27 print "%s %% %s == %s != %s" %\
28 (repr(formatstr), repr(value), repr(result), repr(output))
29 else:
30 if verbose:
31 print "yes"
33 try:
34 testformat("%f", 1024, grouping=1, output='1,024.000000')
35 testformat("%f", 102, grouping=1, output='102.000000')
36 testformat("%f", -42, grouping=1, output='-42.000000')
37 testformat("%+f", -42, grouping=1, output='-42.000000')
38 testformat("%20.f", -42, grouping=1, output=' -42')
39 testformat("%+10.f", -4200, grouping=1, output=' -4,200')
40 testformat("%-10.f", 4200, grouping=1, output='4,200 ')
41 finally:
42 locale.setlocale(locale.LC_NUMERIC, oldlocale)