test_log.py 1.8 KB

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