| 
									
										
										
										
											2002-12-30 23:02:55 +00:00
										 |  |  | from test.test_support import verbose, TestSkipped | 
					
						
							| 
									
										
										
										
											2001-04-13 08:09:50 +00:00
										 |  |  | import locale | 
					
						
							| 
									
										
										
										
											2001-04-15 13:15:56 +00:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											2001-04-13 08:09:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-12-30 23:02:55 +00:00
										 |  |  | if sys.platform == 'darwin': | 
					
						
							| 
									
										
										
										
											2003-02-19 02:35:07 +00:00
										 |  |  |     raise TestSkipped("Locale support on MacOSX is minimal and cannot be tested") | 
					
						
							| 
									
										
										
										
											2001-04-13 08:09:50 +00:00
										 |  |  | oldlocale = locale.setlocale(locale.LC_NUMERIC) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-12-19 01:16:03 +00:00
										 |  |  | if sys.platform.startswith("win"): | 
					
						
							| 
									
										
										
										
											2005-06-03 15:04:15 +00:00
										 |  |  |     tlocs = ("en",) | 
					
						
							| 
									
										
										
										
											2003-12-19 01:16:03 +00:00
										 |  |  | else: | 
					
						
							| 
									
										
										
										
											2005-06-03 15:04:15 +00:00
										 |  |  |     tlocs = ("en_US.UTF-8", "en_US.US-ASCII", "en_US") | 
					
						
							| 
									
										
										
										
											2001-04-15 13:15:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-06-03 15:04:15 +00:00
										 |  |  | for tloc in tlocs: | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         locale.setlocale(locale.LC_NUMERIC, tloc) | 
					
						
							|  |  |  |         break | 
					
						
							|  |  |  |     except locale.Error: | 
					
						
							|  |  |  |         continue | 
					
						
							|  |  |  | else: | 
					
						
							|  |  |  |     raise ImportError, "test locale not supported (tried %s)"%(', '.join(tlocs)) | 
					
						
							| 
									
										
										
										
											2001-04-13 08:09:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-17 15:51:16 +00:00
										 |  |  | def testformat(formatstr, value, grouping = 0, output=None, func=locale.format): | 
					
						
							| 
									
										
										
										
											2001-04-13 08:09:50 +00:00
										 |  |  |     if verbose: | 
					
						
							|  |  |  |         if output: | 
					
						
							|  |  |  |             print "%s %% %s =? %s ..." %\ | 
					
						
							|  |  |  |                 (repr(formatstr), repr(value), repr(output)), | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             print "%s %% %s works? ..." % (repr(formatstr), repr(value)), | 
					
						
							| 
									
										
										
										
											2006-05-17 15:51:16 +00:00
										 |  |  |     result = func(formatstr, value, grouping = grouping) | 
					
						
							| 
									
										
										
										
											2001-04-13 08:09:50 +00:00
										 |  |  |     if output and result != output: | 
					
						
							|  |  |  |         if verbose: | 
					
						
							|  |  |  |             print 'no' | 
					
						
							|  |  |  |         print "%s %% %s == %s != %s" %\ | 
					
						
							|  |  |  |               (repr(formatstr), repr(value), repr(result), repr(output)) | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         if verbose: | 
					
						
							|  |  |  |             print "yes" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | try: | 
					
						
							| 
									
										
										
										
											2005-12-30 12:51:45 +00:00
										 |  |  |     # On Solaris 10, the thousands_sep is the empty string | 
					
						
							|  |  |  |     sep = locale.localeconv()['thousands_sep'] | 
					
						
							|  |  |  |     testformat("%f", 1024, grouping=1, output='1%s024.000000' % sep) | 
					
						
							| 
									
										
										
										
											2001-04-13 08:09:50 +00:00
										 |  |  |     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') | 
					
						
							| 
									
										
										
										
											2005-12-30 12:51:45 +00:00
										 |  |  |     testformat("%+10.f", -4200, grouping=1, output='    -4%s200' % sep) | 
					
						
							|  |  |  |     testformat("%-10.f", 4200, grouping=1, output='4%s200     ' % sep) | 
					
						
							| 
									
										
										
										
											2002-11-03 17:20:12 +00:00
										 |  |  |     # Invoke getpreferredencoding to make sure it does not cause exceptions, | 
					
						
							|  |  |  |     locale.getpreferredencoding() | 
					
						
							| 
									
										
										
										
											2006-05-17 15:51:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # === Test format() with more complex formatting strings | 
					
						
							|  |  |  |     # test if grouping is independent from other characters in formatting string | 
					
						
							| 
									
										
										
										
											2006-05-18 06:33:27 +00:00
										 |  |  |     testformat("One million is %i", 1000000, grouping=1, | 
					
						
							|  |  |  |                output='One million is 1%s000%s000' % (sep, sep), | 
					
						
							| 
									
										
										
										
											2006-05-17 15:51:16 +00:00
										 |  |  |                func=locale.format_string) | 
					
						
							| 
									
										
										
										
											2006-05-18 06:33:27 +00:00
										 |  |  |     testformat("One  million is %i", 1000000, grouping=1, | 
					
						
							|  |  |  |                output='One  million is 1%s000%s000' % (sep, sep), | 
					
						
							| 
									
										
										
										
											2006-05-17 15:51:16 +00:00
										 |  |  |                func=locale.format_string) | 
					
						
							|  |  |  |     # test dots in formatting string | 
					
						
							|  |  |  |     testformat(".%f.", 1000.0, output='.1000.000000.', func=locale.format_string) | 
					
						
							|  |  |  |     # test floats | 
					
						
							| 
									
										
										
										
											2006-05-18 06:33:27 +00:00
										 |  |  |     testformat("--> %10.2f", 1000.0, grouping=1, output='-->   1%s000.00' % sep, | 
					
						
							| 
									
										
										
										
											2006-05-17 15:51:16 +00:00
										 |  |  |                func=locale.format_string) | 
					
						
							|  |  |  |     # test asterisk formats | 
					
						
							|  |  |  |     testformat("%10.*f", (2, 1000.0), grouping=0, output='   1000.00', | 
					
						
							|  |  |  |                func=locale.format_string) | 
					
						
							| 
									
										
										
										
											2006-05-18 06:33:27 +00:00
										 |  |  |     testformat("%*.*f", (10, 2, 1000.0), grouping=1, output='  1%s000.00' % sep, | 
					
						
							| 
									
										
										
										
											2006-05-17 15:51:16 +00:00
										 |  |  |                func=locale.format_string) | 
					
						
							|  |  |  |     # test more-in-one | 
					
						
							|  |  |  |     testformat("int %i float %.2f str %s", (1000, 1000.0, 'str'), grouping=1, | 
					
						
							| 
									
										
										
										
											2006-05-18 06:33:27 +00:00
										 |  |  |                output='int 1%s000 float 1%s000.00 str str' % (sep, sep), | 
					
						
							|  |  |  |                func=locale.format_string) | 
					
						
							| 
									
										
										
										
											2006-05-17 15:51:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-04-13 08:09:50 +00:00
										 |  |  | finally: | 
					
						
							|  |  |  |     locale.setlocale(locale.LC_NUMERIC, oldlocale) | 
					
						
							| 
									
										
										
										
											2004-08-04 06:33:51 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Test BSD Rune locale's bug for isctype functions. | 
					
						
							|  |  |  | def teststrop(s, method, output): | 
					
						
							|  |  |  |     if verbose: | 
					
						
							|  |  |  |         print "%s.%s() =? %s ..." % (repr(s), method, repr(output)), | 
					
						
							|  |  |  |     result = getattr(s, method)() | 
					
						
							|  |  |  |     if result != output: | 
					
						
							|  |  |  |         if verbose: | 
					
						
							|  |  |  |             print "no" | 
					
						
							|  |  |  |         print "%s.%s() == %s != %s" % (repr(s), method, repr(result), | 
					
						
							|  |  |  |                                        repr(output)) | 
					
						
							|  |  |  |     elif verbose: | 
					
						
							|  |  |  |         print "yes" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | try: | 
					
						
							| 
									
										
										
										
											2005-12-30 12:51:45 +00:00
										 |  |  |     if sys.platform == 'sunos5': | 
					
						
							|  |  |  |         # On Solaris, in en_US.UTF-8, \xa0 is a space | 
					
						
							|  |  |  |         raise locale.Error | 
					
						
							| 
									
										
										
										
											2004-08-04 06:33:51 +00:00
										 |  |  |     oldlocale = locale.setlocale(locale.LC_CTYPE) | 
					
						
							|  |  |  |     locale.setlocale(locale.LC_CTYPE, 'en_US.UTF-8') | 
					
						
							|  |  |  | except locale.Error: | 
					
						
							|  |  |  |     pass | 
					
						
							|  |  |  | else: | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         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') | 
					
						
							|  |  |  |     finally: | 
					
						
							|  |  |  |         locale.setlocale(locale.LC_CTYPE, oldlocale) |