|
1 | | -from test.support import run_unittest, verbose |
| 1 | +from test.support import verbose |
2 | 2 | import unittest |
3 | 3 | import locale |
4 | 4 | import sys |
5 | 5 | import codecs |
6 | 6 |
|
7 | | -enUS_locale = None |
8 | | - |
9 | | -def get_enUS_locale(): |
10 | | - global enUS_locale |
11 | | - if sys.platform == 'darwin': |
12 | | - import os |
13 | | - tlocs = ("en_US.UTF-8", "en_US.ISO8859-1", "en_US") |
14 | | - if int(os.uname().release.split('.')[0]) < 10: |
15 | | - # The locale test work fine on OSX 10.6, I (ronaldoussoren) |
16 | | - # haven't had time yet to verify if tests work on OSX 10.5 |
17 | | - # (10.4 is known to be bad) |
18 | | - raise unittest.SkipTest("Locale support on MacOSX is minimal") |
19 | | - elif sys.platform.startswith("win"): |
20 | | - tlocs = ("En", "English") |
21 | | - else: |
22 | | - tlocs = ("en_US.UTF-8", "en_US.ISO8859-1", "en_US.US-ASCII", "en_US") |
23 | | - oldlocale = locale.setlocale(locale.LC_NUMERIC) |
24 | | - for tloc in tlocs: |
25 | | - try: |
26 | | - locale.setlocale(locale.LC_NUMERIC, tloc) |
27 | | - except locale.Error: |
28 | | - continue |
29 | | - break |
30 | | - else: |
31 | | - raise unittest.SkipTest( |
32 | | - "Test locale not supported (tried %s)" % (', '.join(tlocs))) |
33 | | - enUS_locale = tloc |
34 | | - locale.setlocale(locale.LC_NUMERIC, oldlocale) |
35 | | - |
36 | | - |
37 | 7 | class BaseLocalizedTest(unittest.TestCase): |
38 | 8 | # |
39 | 9 | # Base class for tests using a real locale |
40 | 10 | # |
41 | 11 |
|
| 12 | + @classmethod |
| 13 | + def setUpClass(cls): |
| 14 | + if sys.platform == 'darwin': |
| 15 | + import os |
| 16 | + tlocs = ("en_US.UTF-8", "en_US.ISO8859-1", "en_US") |
| 17 | + if int(os.uname().release.split('.')[0]) < 10: |
| 18 | + # The locale test work fine on OSX 10.6, I (ronaldoussoren) |
| 19 | + # haven't had time yet to verify if tests work on OSX 10.5 |
| 20 | + # (10.4 is known to be bad) |
| 21 | + raise unittest.SkipTest("Locale support on MacOSX is minimal") |
| 22 | + elif sys.platform.startswith("win"): |
| 23 | + tlocs = ("En", "English") |
| 24 | + else: |
| 25 | + tlocs = ("en_US.UTF-8", "en_US.ISO8859-1", |
| 26 | + "en_US.US-ASCII", "en_US") |
| 27 | + try: |
| 28 | + oldlocale = locale.setlocale(locale.LC_NUMERIC) |
| 29 | + for tloc in tlocs: |
| 30 | + try: |
| 31 | + locale.setlocale(locale.LC_NUMERIC, tloc) |
| 32 | + except locale.Error: |
| 33 | + continue |
| 34 | + break |
| 35 | + else: |
| 36 | + raise unittest.SkipTest("Test locale not supported " |
| 37 | + "(tried %s)" % (', '.join(tlocs))) |
| 38 | + cls.enUS_locale = tloc |
| 39 | + finally: |
| 40 | + locale.setlocale(locale.LC_NUMERIC, oldlocale) |
| 41 | + |
42 | 42 | def setUp(self): |
43 | | - self.oldlocale = locale.setlocale(self.locale_type) |
44 | | - locale.setlocale(self.locale_type, enUS_locale) |
| 43 | + oldlocale = locale.setlocale(self.locale_type) |
| 44 | + self.addCleanup(locale.setlocale, self.locale_type, oldlocale) |
| 45 | + locale.setlocale(self.locale_type, self.enUS_locale) |
45 | 46 | if verbose: |
46 | | - print("testing with \"%s\"..." % enUS_locale, end=' ') |
47 | | - |
48 | | - def tearDown(self): |
49 | | - locale.setlocale(self.locale_type, self.oldlocale) |
| 47 | + print("testing with %r..." % self.enUS_locale, end=' ', flush=True) |
50 | 48 |
|
51 | 49 |
|
52 | 50 | class BaseCookedTest(unittest.TestCase): |
@@ -415,25 +413,5 @@ def test_invalid_iterable_in_localetuple(self): |
415 | 413 | locale.setlocale(locale.LC_ALL, (b'not', b'valid')) |
416 | 414 |
|
417 | 415 |
|
418 | | -def test_main(): |
419 | | - tests = [ |
420 | | - TestMiscellaneous, |
421 | | - TestFormatPatternArg, |
422 | | - TestLocaleFormatString, |
423 | | - TestEnUSNumberFormatting, |
424 | | - TestCNumberFormatting, |
425 | | - TestFrFRNumberFormatting, |
426 | | - TestCollation |
427 | | - ] |
428 | | - # SkipTest can't be raised inside unittests, handle it manually instead |
429 | | - try: |
430 | | - get_enUS_locale() |
431 | | - except unittest.SkipTest as e: |
432 | | - if verbose: |
433 | | - print("Some tests will be disabled: %s" % e) |
434 | | - else: |
435 | | - tests += [TestNumberFormatting, TestEnUSCollation] |
436 | | - run_unittest(*tests) |
437 | | - |
438 | 416 | if __name__ == '__main__': |
439 | | - test_main() |
| 417 | + unittest.main() |
0 commit comments