test_io.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # encoding: utf-8
  2. """Tests for utils.io"""
  3. # Copyright (c) Jupyter Development Team.
  4. # Distributed under the terms of the Modified BSD License.
  5. import io as stdlib_io
  6. import sys
  7. import pytest
  8. from ..io import unicode_std_stream
  9. from ipython_genutils.py3compat import PY3
  10. if PY3:
  11. from io import StringIO
  12. else:
  13. from StringIO import StringIO
  14. def test_UnicodeStdStream():
  15. # Test wrapping a bytes-level stdout
  16. if PY3:
  17. stdoutb = stdlib_io.BytesIO()
  18. stdout = stdlib_io.TextIOWrapper(stdoutb, encoding='ascii')
  19. else:
  20. stdout = stdoutb = stdlib_io.BytesIO()
  21. orig_stdout = sys.stdout
  22. sys.stdout = stdout
  23. try:
  24. sample = u"@łe¶ŧ←"
  25. unicode_std_stream().write(sample)
  26. output = stdoutb.getvalue().decode('utf-8')
  27. assert output == sample
  28. assert not stdout.closed
  29. finally:
  30. sys.stdout = orig_stdout
  31. @pytest.mark.skipif(not PY3,
  32. reason = "Not applicable on Python 2")
  33. def test_UnicodeStdStream_nowrap():
  34. # If we replace stdout with a StringIO, it shouldn't get wrapped.
  35. orig_stdout = sys.stdout
  36. sys.stdout = StringIO()
  37. try:
  38. assert unicode_std_stream() is sys.stdout
  39. assert not sys.stdout.closed
  40. finally:
  41. sys.stdout = orig_stdout