Victor Stinner | 5b08b4d | 2010-05-19 17:15:50 +0000 | [diff] [blame] | 1 | """Tests for distutils.log""" |
| 2 | |
| 3 | import sys |
| 4 | import unittest |
| 5 | from tempfile import NamedTemporaryFile |
Éric Araujo | 70ec44a | 2010-11-06 02:44:43 +0000 | [diff] [blame] | 6 | from test.support import run_unittest |
Victor Stinner | 5b08b4d | 2010-05-19 17:15:50 +0000 | [diff] [blame] | 7 | |
| 8 | from distutils import log |
| 9 | |
| 10 | class TestLog(unittest.TestCase): |
| 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 | try: |
| 18 | log.set_threshold(log.DEBUG) |
| 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") |
| 25 | stdout.seek(0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 26 | self.assertEqual(stdout.read().rstrip(), "debug:\\xe9") |
Victor Stinner | 5b08b4d | 2010-05-19 17:15:50 +0000 | [diff] [blame] | 27 | stderr.seek(0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 28 | self.assertEqual(stderr.read().rstrip(), "fatal:\\xe9") |
Victor Stinner | 5b08b4d | 2010-05-19 17:15:50 +0000 | [diff] [blame] | 29 | finally: |
| 30 | sys.stdout = old_stdout |
| 31 | sys.stderr = old_stderr |
| 32 | |
| 33 | def test_suite(): |
| 34 | return unittest.makeSuite(TestLog) |
| 35 | |
| 36 | if __name__ == "__main__": |
Éric Araujo | 70ec44a | 2010-11-06 02:44:43 +0000 | [diff] [blame] | 37 | run_unittest(test_suite()) |