|
1 | 1 | """Tests for distutils.log"""
|
2 | 2 |
|
| 3 | +import io |
3 | 4 | import sys
|
4 | 5 | import unittest
|
5 |
| -from tempfile import NamedTemporaryFile |
6 |
| -from test.support import run_unittest |
| 6 | +from test.support import swap_attr, run_unittest |
7 | 7 |
|
8 | 8 | from distutils import log
|
9 | 9 |
|
10 | 10 | class TestLog(unittest.TestCase):
|
11 | 11 | def test_non_ascii(self):
|
12 |
| - # Issue #8663: test that non-ASCII text is escaped with |
13 |
| - # backslashreplace error handler (stream use ASCII encoding and strict |
14 |
| - # error handler) |
15 |
| - old_stdout = sys.stdout |
16 |
| - old_stderr = sys.stderr |
17 |
| - old_threshold = log.set_threshold(log.DEBUG) |
18 |
| - try: |
19 |
| - with NamedTemporaryFile(mode="w+", encoding='ascii') as stdout, \ |
20 |
| - NamedTemporaryFile(mode="w+", encoding='ascii') as stderr: |
21 |
| - sys.stdout = stdout |
22 |
| - sys.stderr = stderr |
23 |
| - log.debug("debug:\xe9") |
24 |
| - log.fatal("fatal:\xe9") |
| 12 | + # Issues #8663, #34421: test that non-encodable text is escaped with |
| 13 | + # backslashreplace error handler and encodable non-ASCII text is |
| 14 | + # output as is. |
| 15 | + for errors in ('strict', 'backslashreplace', 'surrogateescape', |
| 16 | + 'replace', 'ignore'): |
| 17 | + with self.subTest(errors=errors): |
| 18 | + stdout = io.TextIOWrapper(io.BytesIO(), |
| 19 | + encoding='cp437', errors=errors) |
| 20 | + stderr = io.TextIOWrapper(io.BytesIO(), |
| 21 | + encoding='cp437', errors=errors) |
| 22 | + old_threshold = log.set_threshold(log.DEBUG) |
| 23 | + try: |
| 24 | + with swap_attr(sys, 'stdout', stdout), \ |
| 25 | + swap_attr(sys, 'stderr', stderr): |
| 26 | + log.debug('Dεbug\tMėssãge') |
| 27 | + log.fatal('Fαtal\tÈrrōr') |
| 28 | +
8000
finally: |
| 29 | + log.set_threshold(old_threshold) |
| 30 | + |
25 | 31 | stdout.seek(0)
|
26 |
| - self.assertEqual(stdout.read().rstrip(), "debug:\\xe9") |
| 32 | + self.assertEqual(stdout.read().rstrip(), |
| 33 | + 'Dεbug\tM?ss?ge' if errors == 'replace' else |
| 34 | + 'Dεbug\tMssge' if errors == 'ignore' else |
| 35 | + 'Dεbug\tM\\u0117ss\\xe3ge') |
27 | 36 | stderr.seek(0)
|
28 |
| - self.assertEqual(stderr.read().rstrip(), "fatal:\\xe9") |
29 |
| - finally: |
30 |
| - log.set_threshold(old_threshold) |
31 |
| - sys.stdout = old_stdout |
32 |
| - sys.stderr = old_stderr |
| 37 | + self.assertEqual(stderr.read().rstrip(), |
| 38 | + 'Fαtal\t?rr?r' if errors == 'replace' else |
| 39 | + 'Fαtal\trrr' if errors == 'ignore' else |
| 40 | + 'Fαtal\t\\xc8rr\\u014dr') |
33 | 41 |
|
34 | 42 | def test_suite():
|
35 | 43 | return unittest.makeSuite(TestLog)
|
|
0 commit comments