diff --git a/Lib/test/test_locale.py b/Lib/test/test_locale.py --- a/Lib/test/test_locale.py +++ b/Lib/test/test_locale.py @@ -4,10 +4,11 @@ import sys import codecs -enUS_locale = None - def get_enUS_locale(): - global enUS_locale + """ + Return a tuple of enUS_locale and a reason for skipping certain tests. + One or the other should be None. + """ if sys.platform == 'darwin': import os tlocs = ("en_US.UTF-8", "en_US.ISO8859-1", "en_US") @@ -15,23 +16,27 @@ # The locale test work fine on OSX 10.6, I (ronaldoussoren) # haven't had time yet to verify if tests work on OSX 10.5 # (10.4 is known to be bad) - raise unittest.SkipTest("Locale support on MacOSX is minimal") + return None, "Locale support on MacOSX is minimal" elif sys.platform.startswith("win"): tlocs = ("En", "English") else: tlocs = ("en_US.UTF-8", "en_US.ISO8859-1", "en_US.US-ASCII", "en_US") oldlocale = locale.setlocale(locale.LC_NUMERIC) - for tloc in tlocs: - try: - locale.setlocale(locale.LC_NUMERIC, tloc) - except locale.Error: - continue - break - else: - raise unittest.SkipTest( - "Test locale not supported (tried %s)" % (', '.join(tlocs))) - enUS_locale = tloc - locale.setlocale(locale.LC_NUMERIC, oldlocale) + try: + for tloc in tlocs: + try: + locale.setlocale(locale.LC_NUMERIC, tloc) + except locale.Error: + continue + break + else: + return (None, + "Test locale not supported (tried %s)" % (', '.join(tlocs))) + return tloc, None + finally: + locale.setlocale(locale.LC_NUMERIC, oldlocale) + +enUS_locale, enUS_locale_skip_reason = get_enUS_locale() class BaseLocalizedTest(unittest.TestCase): @@ -257,6 +262,7 @@ +@unittest.skipUnless(enUS_locale, enUS_locale_skip_reason) class TestNumberFormatting(BaseLocalizedTest, EnUSNumberFormatting): # Test number formatting with a real English locale. @@ -346,6 +352,7 @@ self.assertLess(locale.strxfrm('a'), locale.strxfrm('b')) +@unittest.skipUnless(enUS_locale, enUS_locale_skip_reason) class TestEnUSCollation(BaseLocalizedTest, TestCollation): # Test string collation functions with a real English locale @@ -415,25 +422,5 @@ locale.setlocale(locale.LC_ALL, (b'not', b'valid')) -def test_main(): - tests = [ - TestMiscellaneous, - TestFormatPatternArg, - TestLocaleFormatString, - TestEnUSNumberFormatting, - TestCNumberFormatting, - TestFrFRNumberFormatting, - TestCollation - ] - # SkipTest can't be raised inside unittests, handle it manually instead - try: - get_enUS_locale() - except unittest.SkipTest as e: - if verbose: - print("Some tests will be disabled: %s" % e) - else: - tests += [TestNumberFormatting, TestEnUSCollation] - run_unittest(*tests) - if __name__ == '__main__': - test_main() + unittest.main()