- Notifications
You must be signed in to change notification settings - Fork 31.8k
/
Copy pathtest_log.py
46 lines (40 loc) · 1.82 KB
/
test_log.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""Tests for distutils.log"""
importio
importsys
importunittest
fromtest.supportimportswap_attr, run_unittest
fromdistutilsimportlog
classTestLog(unittest.TestCase):
deftest_non_ascii(self):
# Issues #8663, #34421: test that non-encodable text is escaped with
# backslashreplace error handler and encodable non-ASCII text is
# output as is.
forerrorsin ('strict', 'backslashreplace', 'surrogateescape',
'replace', 'ignore'):
withself.subTest(errors=errors):
stdout=io.TextIOWrapper(io.BytesIO(),
encoding='cp437', errors=errors)
stderr=io.TextIOWrapper(io.BytesIO(),
encoding='cp437', errors=errors)
old_threshold=log.set_threshold(log.DEBUG)
try:
withswap_attr(sys, 'stdout', stdout), \
swap_attr(sys, 'stderr', stderr):
log.debug('Dεbug\tMėssãge')
log.fatal('Fαtal\tÈrrōr')
finally:
log.set_threshold(old_threshold)
stdout.seek(0)
self.assertEqual(stdout.read().rstrip(),
'Dεbug\tM?ss?ge'iferrors=='replace'else
'Dεbug\tMssge'iferrors=='ignore'else
'Dεbug\tM\\u0117ss\\xe3ge')
stderr.seek(0)
self.assertEqual(stderr.read().rstrip(),
'Fαtal\t?rr?r'iferrors=='replace'else
'Fαtal\trrr'iferrors=='ignore'else
'Fαtal\t\\xc8rr\\u014dr')
deftest_suite():
returnunittest.makeSuite(TestLog)
if__name__=="__main__":
run_unittest(test_suite())