Update for release.
[python/dscho.git] / Lib / test / test_locale.py
blobd15bd283bf9e31fea0fd801f4784e0850825e2a4
1 from test.test_support import verbose, TestSkipped
2 import locale
3 import sys
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)
9 tloc = "en_US"
10 if sys.platform[:3] == "win":
11 tloc = "en"
13 try:
14 locale.setlocale(locale.LC_NUMERIC, tloc)
15 except locale.Error:
16 raise ImportError, "test locale %s not supported" % tloc
18 def testformat(formatstr, value, grouping = 0, output=None):
19 if verbose:
20 if output:
21 print "%s %% %s =? %s ..." %\
22 (repr(formatstr), repr(value), repr(output)),
23 else:
24 print "%s %% %s works? ..." % (repr(formatstr), repr(value)),
25 result = locale.format(formatstr, value, grouping = grouping)
26 if output and result != output:
27 if verbose:
28 print 'no'
29 print "%s %% %s == %s != %s" %\
30 (repr(formatstr), repr(value), repr(result), repr(output))
31 else:
32 if verbose:
33 print "yes"
35 try:
36 testformat("%f", 1024, grouping=1, output='1,024.000000')
37 testformat("%f", 102, grouping=1, output='102.000000')
38 testformat("%f", -42, grouping=1, output='-42.000000')
39 testformat("%+f", -42, grouping=1, output='-42.000000')
40 testformat("%20.f", -42, grouping=1, output=' -42')
41 testformat("%+10.f", -4200, grouping=1, output=' -4,200')
42 testformat("%-10.f", 4200, grouping=1, output='4,200 ')
43 # Invoke getpreferredencoding to make sure it does not cause exceptions,
44 locale.getpreferredencoding()
45 finally:
46 locale.setlocale(locale.LC_NUMERIC, oldlocale)