Show More
@@ -0,0 +1,33 b'' | |||
|
1 | # coding: utf-8 | |
|
2 | """io-related utilities""" | |
|
3 | ||
|
4 | # Copyright (c) Jupyter Development Team. | |
|
5 | # Distributed under the terms of the Modified BSD License. | |
|
6 | ||
|
7 | import codecs | |
|
8 | import sys | |
|
9 | from IPython.utils.py3compat import PY3 | |
|
10 | ||
|
11 | ||
|
12 | def unicode_std_stream(stream='stdout'): | |
|
13 | u"""Get a wrapper to write unicode to stdout/stderr as UTF-8. | |
|
14 | ||
|
15 | This ignores environment variables and default encodings, to reliably write | |
|
16 | unicode to stdout or stderr. | |
|
17 | ||
|
18 | :: | |
|
19 | ||
|
20 | unicode_std_stream().write(u'ł@e¶ŧ←') | |
|
21 | """ | |
|
22 | assert stream in ('stdout', 'stderr') | |
|
23 | stream = getattr(sys, stream) | |
|
24 | if PY3: | |
|
25 | try: | |
|
26 | stream_b = stream.buffer | |
|
27 | except AttributeError: | |
|
28 | # sys.stdout has been replaced - use it directly | |
|
29 | return stream | |
|
30 | else: | |
|
31 | stream_b = stream | |
|
32 | ||
|
33 | return codecs.getwriter('utf-8')(stream_b) |
@@ -0,0 +1,50 b'' | |||
|
1 | # encoding: utf-8 | |
|
2 | """Tests for utils.io""" | |
|
3 | ||
|
4 | # Copyright (c) Jupyter Development Team. | |
|
5 | # Distributed under the terms of the Modified BSD License. | |
|
6 | ||
|
7 | import io as stdlib_io | |
|
8 | import sys | |
|
9 | ||
|
10 | import nose.tools as nt | |
|
11 | ||
|
12 | from IPython.testing.decorators import skipif | |
|
13 | from ..io import unicode_std_stream | |
|
14 | from IPython.utils.py3compat import PY3 | |
|
15 | ||
|
16 | if PY3: | |
|
17 | from io import StringIO | |
|
18 | else: | |
|
19 | from StringIO import StringIO | |
|
20 | ||
|
21 | def test_UnicodeStdStream(): | |
|
22 | # Test wrapping a bytes-level stdout | |
|
23 | if PY3: | |
|
24 | stdoutb = stdlib_io.BytesIO() | |
|
25 | stdout = stdlib_io.TextIOWrapper(stdoutb, encoding='ascii') | |
|
26 | else: | |
|
27 | stdout = stdoutb = stdlib_io.BytesIO() | |
|
28 | ||
|
29 | orig_stdout = sys.stdout | |
|
30 | sys.stdout = stdout | |
|
31 | try: | |
|
32 | sample = u"@łe¶ŧ←" | |
|
33 | unicode_std_stream().write(sample) | |
|
34 | ||
|
35 | output = stdoutb.getvalue().decode('utf-8') | |
|
36 | nt.assert_equal(output, sample) | |
|
37 | assert not stdout.closed | |
|
38 | finally: | |
|
39 | sys.stdout = orig_stdout | |
|
40 | ||
|
41 | @skipif(not PY3, "Not applicable on Python 2") | |
|
42 | def test_UnicodeStdStream_nowrap(): | |
|
43 | # If we replace stdout with a StringIO, it shouldn't get wrapped. | |
|
44 | orig_stdout = sys.stdout | |
|
45 | sys.stdout = StringIO() | |
|
46 | try: | |
|
47 | nt.assert_is(unicode_std_stream(), sys.stdout) | |
|
48 | assert not sys.stdout.closed | |
|
49 | finally: | |
|
50 | sys.stdout = orig_stdout |
@@ -3,33 +3,24 b'' | |||
|
3 | 3 | IO related utilities. |
|
4 | 4 | """ |
|
5 | 5 | |
|
6 | #----------------------------------------------------------------------------- | |
|
7 | # Copyright (C) 2008-2011 The IPython Development Team | |
|
8 | # | |
|
9 | # Distributed under the terms of the BSD License. The full license is in | |
|
10 | # the file COPYING, distributed as part of this software. | |
|
11 | #----------------------------------------------------------------------------- | |
|
6 | # Copyright (c) IPython Development Team. | |
|
7 | # Distributed under the terms of the Modified BSD License. | |
|
8 | ||
|
12 | 9 | from __future__ import print_function |
|
13 | 10 | from __future__ import absolute_import |
|
14 | 11 | |
|
15 | #----------------------------------------------------------------------------- | |
|
16 | # Imports | |
|
17 | #----------------------------------------------------------------------------- | |
|
12 | ||
|
18 | 13 | import codecs |
|
19 | 14 | from contextlib import contextmanager |
|
20 | 15 | import io |
|
21 | 16 | import os |
|
22 | 17 | import shutil |
|
23 | import stat | |
|
24 | 18 | import sys |
|
25 | 19 | import tempfile |
|
20 | import warnings | |
|
26 | 21 | from .capture import CapturedIO, capture_output |
|
27 | 22 | from .py3compat import string_types, input, PY3 |
|
28 | 23 | |
|
29 | #----------------------------------------------------------------------------- | |
|
30 | # Code | |
|
31 | #----------------------------------------------------------------------------- | |
|
32 | ||
|
33 | 24 | |
|
34 | 25 | class IOStream: |
|
35 | 26 | |
@@ -323,25 +314,9 b' def raw_print_err(*args, **kw):' | |||
|
323 | 314 | rprint = raw_print |
|
324 | 315 | rprinte = raw_print_err |
|
325 | 316 | |
|
326 | def unicode_std_stream(stream='stdout'): | |
|
327 | u"""Get a wrapper to write unicode to stdout/stderr as UTF-8. | |
|
328 | ||
|
329 | This ignores environment variables and default encodings, to reliably write | |
|
330 | unicode to stdout or stderr. | |
|
331 | ||
|
332 | :: | |
|
333 | 317 | |
|
334 | unicode_std_stream().write(u'ł@e¶ŧ←') | |
|
335 | """ | |
|
336 | assert stream in ('stdout', 'stderr') | |
|
337 | stream = getattr(sys, stream) | |
|
338 | if PY3: | |
|
339 | try: | |
|
340 | stream_b = stream.buffer | |
|
341 | except AttributeError: | |
|
342 | # sys.stdout has been replaced - use it directly | |
|
343 | return stream | |
|
344 | else: | |
|
345 | stream_b = stream | |
|
346 | ||
|
347 | return codecs.getwriter('utf-8')(stream_b) | |
|
318 | def unicode_std_stream(stream='stdout'): | |
|
319 | """DEPRECATED, moved to jupyter_nbconvert.utils.io""" | |
|
320 | warn("IPython.utils.io.unicode_std_stream has moved to jupyter_nbconvert.utils.io") | |
|
321 | from jupyter_nbconvert.utils.io import unicode_std_stream | |
|
322 | return unicode_std_stream(stream) |
@@ -86,36 +86,6 b' def test_capture_output():' | |||
|
86 | 86 | nt.assert_equal(io.stdout, 'hi, stdout\n') |
|
87 | 87 | nt.assert_equal(io.stderr, 'hi, stderr\n') |
|
88 | 88 | |
|
89 | def test_UnicodeStdStream(): | |
|
90 | # Test wrapping a bytes-level stdout | |
|
91 | if PY3: | |
|
92 | stdoutb = stdlib_io.BytesIO() | |
|
93 | stdout = stdlib_io.TextIOWrapper(stdoutb, encoding='ascii') | |
|
94 | else: | |
|
95 | stdout = stdoutb = stdlib_io.BytesIO() | |
|
96 | ||
|
97 | orig_stdout = sys.stdout | |
|
98 | sys.stdout = stdout | |
|
99 | try: | |
|
100 | sample = u"@łe¶ŧ←" | |
|
101 | unicode_std_stream().write(sample) | |
|
102 | ||
|
103 | output = stdoutb.getvalue().decode('utf-8') | |
|
104 | nt.assert_equal(output, sample) | |
|
105 | assert not stdout.closed | |
|
106 | finally: | |
|
107 | sys.stdout = orig_stdout | |
|
108 | ||
|
109 | @skipif(not PY3, "Not applicable on Python 2") | |
|
110 | def test_UnicodeStdStream_nowrap(): | |
|
111 | # If we replace stdout with a StringIO, it shouldn't get wrapped. | |
|
112 | orig_stdout = sys.stdout | |
|
113 | sys.stdout = StringIO() | |
|
114 | try: | |
|
115 | nt.assert_is(unicode_std_stream(), sys.stdout) | |
|
116 | assert not sys.stdout.closed | |
|
117 | finally: | |
|
118 | sys.stdout = orig_stdout | |
|
119 | 89 | |
|
120 | 90 | def test_atomic_writing(): |
|
121 | 91 | class CustomExc(Exception): pass |
@@ -1,24 +1,13 b'' | |||
|
1 | 1 | """ |
|
2 | 2 | Contains Stdout writer |
|
3 | 3 | """ |
|
4 | #----------------------------------------------------------------------------- | |
|
5 | #Copyright (c) 2013, the IPython Development Team. | |
|
6 | # | |
|
7 | #Distributed under the terms of the Modified BSD License. | |
|
8 | # | |
|
9 | #The full license is in the file COPYING.txt, distributed with this software. | |
|
10 | #----------------------------------------------------------------------------- | |
|
11 | 4 | |
|
12 | #----------------------------------------------------------------------------- | |
|
13 | # Imports | |
|
14 | #----------------------------------------------------------------------------- | |
|
5 | # Copyright (c) Jupyter Development Team. | |
|
6 | # Distributed under the terms of the Modified BSD License. | |
|
15 | 7 | |
|
16 |
from |
|
|
8 | from jupyter_nbconvert.utils import io | |
|
17 | 9 | from .base import WriterBase |
|
18 | 10 | |
|
19 | #----------------------------------------------------------------------------- | |
|
20 | # Classes | |
|
21 | #----------------------------------------------------------------------------- | |
|
22 | 11 | |
|
23 | 12 | class StdoutWriter(WriterBase): |
|
24 | 13 | """Consumes output from nbconvert export...() methods and writes to the |
General Comments 0
You need to be logged in to leave comments.
Login now