Skip to content

Commit 5bbfbd9

Browse files
Issue #17767: test_locale now works with unittest test discovery.
Original patch by Zachary Ware.
1 parent 392cbcd commit 5bbfbd9

2 files changed

Lines changed: 39 additions & 58 deletions

File tree

Lib/test/test_locale.py

Lines changed: 36 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,50 @@
1-
from test.support import run_unittest, verbose
1+
from test.support import verbose
22
import unittest
33
import locale
44
import sys
55
import codecs
66

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-
377
class BaseLocalizedTest(unittest.TestCase):
388
#
399
# Base class for tests using a real locale
4010
#
4111

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+
4242
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)
4546
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)
5048

5149

5250
class BaseCookedTest(unittest.TestCase):
@@ -415,25 +413,5 @@ def test_invalid_iterable_in_localetuple(self):
415413
locale.setlocale(locale.LC_ALL, (b'not', b'valid'))
416414

417415

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-
438416
if __name__ == '__main__':
439-
test_main()
417+
unittest.main()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ IDLE
198198
Tests
199199
-----
200200

201+
- Issue #17767: test_locale now works with unittest test discovery.
202+
Original patch by Zachary Ware.
203+
201204
- Issue #18375: Assume --randomize when --randseed is used for running the
202205
testsuite.
203206

0 commit comments

Comments
 (0)