diff --git a/IPython/utils/io.py b/IPython/utils/io.py index 3d236eb..8d15f11 100644 --- a/IPython/utils/io.py +++ b/IPython/utils/io.py @@ -3,33 +3,24 @@ IO related utilities. """ -#----------------------------------------------------------------------------- -# Copyright (C) 2008-2011 The IPython Development Team -# -# Distributed under the terms of the BSD License. The full license is in -# the file COPYING, distributed as part of this software. -#----------------------------------------------------------------------------- +# Copyright (c) IPython Development Team. +# Distributed under the terms of the Modified BSD License. + from __future__ import print_function from __future__ import absolute_import -#----------------------------------------------------------------------------- -# Imports -#----------------------------------------------------------------------------- + import codecs from contextlib import contextmanager import io import os import shutil -import stat import sys import tempfile +import warnings from .capture import CapturedIO, capture_output from .py3compat import string_types, input, PY3 -#----------------------------------------------------------------------------- -# Code -#----------------------------------------------------------------------------- - class IOStream: @@ -323,25 +314,9 @@ def raw_print_err(*args, **kw): rprint = raw_print rprinte = raw_print_err -def unicode_std_stream(stream='stdout'): - u"""Get a wrapper to write unicode to stdout/stderr as UTF-8. - - This ignores environment variables and default encodings, to reliably write - unicode to stdout or stderr. - - :: - unicode_std_stream().write(u'ł@e¶ŧ←') - """ - assert stream in ('stdout', 'stderr') - stream = getattr(sys, stream) - if PY3: - try: - stream_b = stream.buffer - except AttributeError: - # sys.stdout has been replaced - use it directly - return stream - else: - stream_b = stream - - return codecs.getwriter('utf-8')(stream_b) +def unicode_std_stream(stream='stdout'): + """DEPRECATED, moved to jupyter_nbconvert.utils.io""" + warn("IPython.utils.io.unicode_std_stream has moved to jupyter_nbconvert.utils.io") + from jupyter_nbconvert.utils.io import unicode_std_stream + return unicode_std_stream(stream) diff --git a/IPython/utils/tests/test_io.py b/IPython/utils/tests/test_io.py index aa00a88..5e75d7d 100644 --- a/IPython/utils/tests/test_io.py +++ b/IPython/utils/tests/test_io.py @@ -86,36 +86,6 @@ def test_capture_output(): nt.assert_equal(io.stdout, 'hi, stdout\n') nt.assert_equal(io.stderr, 'hi, stderr\n') -def test_UnicodeStdStream(): - # Test wrapping a bytes-level stdout - if PY3: - stdoutb = stdlib_io.BytesIO() - stdout = stdlib_io.TextIOWrapper(stdoutb, encoding='ascii') - else: - stdout = stdoutb = stdlib_io.BytesIO() - - orig_stdout = sys.stdout - sys.stdout = stdout - try: - sample = u"@łe¶ŧ←" - unicode_std_stream().write(sample) - - output = stdoutb.getvalue().decode('utf-8') - nt.assert_equal(output, sample) - assert not stdout.closed - finally: - sys.stdout = orig_stdout - -@skipif(not PY3, "Not applicable on Python 2") -def test_UnicodeStdStream_nowrap(): - # If we replace stdout with a StringIO, it shouldn't get wrapped. - orig_stdout = sys.stdout - sys.stdout = StringIO() - try: - nt.assert_is(unicode_std_stream(), sys.stdout) - assert not sys.stdout.closed - finally: - sys.stdout = orig_stdout def test_atomic_writing(): class CustomExc(Exception): pass diff --git a/jupyter_nbconvert/utils/io.py b/jupyter_nbconvert/utils/io.py new file mode 100644 index 0000000..c03ec7b --- /dev/null +++ b/jupyter_nbconvert/utils/io.py @@ -0,0 +1,33 @@ +# coding: utf-8 +"""io-related utilities""" + +# Copyright (c) Jupyter Development Team. +# Distributed under the terms of the Modified BSD License. + +import codecs +import sys +from IPython.utils.py3compat import PY3 + + +def unicode_std_stream(stream='stdout'): + u"""Get a wrapper to write unicode to stdout/stderr as UTF-8. + + This ignores environment variables and default encodings, to reliably write + unicode to stdout or stderr. + + :: + + unicode_std_stream().write(u'ł@e¶ŧ←') + """ + assert stream in ('stdout', 'stderr') + stream = getattr(sys, stream) + if PY3: + try: + stream_b = stream.buffer + except AttributeError: + # sys.stdout has been replaced - use it directly + return stream + else: + stream_b = stream + + return codecs.getwriter('utf-8')(stream_b) diff --git a/jupyter_nbconvert/utils/tests/test_io.py b/jupyter_nbconvert/utils/tests/test_io.py new file mode 100644 index 0000000..6c4af00 --- /dev/null +++ b/jupyter_nbconvert/utils/tests/test_io.py @@ -0,0 +1,50 @@ +# encoding: utf-8 +"""Tests for utils.io""" + +# Copyright (c) Jupyter Development Team. +# Distributed under the terms of the Modified BSD License. + +import io as stdlib_io +import sys + +import nose.tools as nt + +from IPython.testing.decorators import skipif +from ..io import unicode_std_stream +from IPython.utils.py3compat import PY3 + +if PY3: + from io import StringIO +else: + from StringIO import StringIO + +def test_UnicodeStdStream(): + # Test wrapping a bytes-level stdout + if PY3: + stdoutb = stdlib_io.BytesIO() + stdout = stdlib_io.TextIOWrapper(stdoutb, encoding='ascii') + else: + stdout = stdoutb = stdlib_io.BytesIO() + + orig_stdout = sys.stdout + sys.stdout = stdout + try: + sample = u"@łe¶ŧ←" + unicode_std_stream().write(sample) + + output = stdoutb.getvalue().decode('utf-8') + nt.assert_equal(output, sample) + assert not stdout.closed + finally: + sys.stdout = orig_stdout + +@skipif(not PY3, "Not applicable on Python 2") +def test_UnicodeStdStream_nowrap(): + # If we replace stdout with a StringIO, it shouldn't get wrapped. + orig_stdout = sys.stdout + sys.stdout = StringIO() + try: + nt.assert_is(unicode_std_stream(), sys.stdout) + assert not sys.stdout.closed + finally: + sys.stdout = orig_stdout diff --git a/jupyter_nbconvert/writers/stdout.py b/jupyter_nbconvert/writers/stdout.py index b816425..6970219 100644 --- a/jupyter_nbconvert/writers/stdout.py +++ b/jupyter_nbconvert/writers/stdout.py @@ -1,24 +1,13 @@ """ Contains Stdout writer """ -#----------------------------------------------------------------------------- -#Copyright (c) 2013, the IPython Development Team. -# -#Distributed under the terms of the Modified BSD License. -# -#The full license is in the file COPYING.txt, distributed with this software. -#----------------------------------------------------------------------------- -#----------------------------------------------------------------------------- -# Imports -#----------------------------------------------------------------------------- +# Copyright (c) Jupyter Development Team. +# Distributed under the terms of the Modified BSD License. -from IPython.utils import io +from jupyter_nbconvert.utils import io from .base import WriterBase -#----------------------------------------------------------------------------- -# Classes -#----------------------------------------------------------------------------- class StdoutWriter(WriterBase): """Consumes output from nbconvert export...() methods and writes to the