Show More
@@ -100,36 +100,6 b' def skip_without(*names):' | |||
|
100 | 100 | # Classes |
|
101 | 101 | #------------------------------------------------------------------------------- |
|
102 | 102 | |
|
103 | class CapturedIO(object): | |
|
104 | """Simple object for containing captured stdout/err StringIO objects""" | |
|
105 | ||
|
106 | def __init__(self, stdout, stderr): | |
|
107 | self.stdout_io = stdout | |
|
108 | self.stderr_io = stderr | |
|
109 | ||
|
110 | @property | |
|
111 | def stdout(self): | |
|
112 | return self.stdout_io.getvalue() | |
|
113 | ||
|
114 | @property | |
|
115 | def stderr(self): | |
|
116 | return self.stderr_io.getvalue() | |
|
117 | ||
|
118 | ||
|
119 | class capture_output(object): | |
|
120 | """context manager for capturing stdout/err""" | |
|
121 | ||
|
122 | def __enter__(self): | |
|
123 | self.sys_stdout = sys.stdout | |
|
124 | self.sys_stderr = sys.stderr | |
|
125 | stdout = sys.stdout = StringIO() | |
|
126 | stderr = sys.stderr = StringIO() | |
|
127 | return CapturedIO(stdout, stderr) | |
|
128 | ||
|
129 | def __exit__(self, exc_type, exc_value, traceback): | |
|
130 | sys.stdout = self.sys_stdout | |
|
131 | sys.stderr = self.sys_stderr | |
|
132 | ||
|
133 | 103 | |
|
134 | 104 | class ClusterTestCase(BaseZMQTestCase): |
|
135 | 105 |
@@ -18,11 +18,12 b' Authors:' | |||
|
18 | 18 | |
|
19 | 19 | import time |
|
20 | 20 | |
|
21 |
from IPython. |
|
|
21 | from IPython.utils.io import capture_output | |
|
22 | 22 | |
|
23 | from IPython.parallel.error import TimeoutError | |
|
23 | 24 | from IPython.parallel import error, Client |
|
24 | 25 | from IPython.parallel.tests import add_engines |
|
25 |
from .clienttest import ClusterTestCase |
|
|
26 | from .clienttest import ClusterTestCase | |
|
26 | 27 | |
|
27 | 28 | def setup(): |
|
28 | 29 | add_engines(2, total=True) |
@@ -25,6 +25,7 b' from nose import SkipTest' | |||
|
25 | 25 | |
|
26 | 26 | from IPython.testing import decorators as dec |
|
27 | 27 | from IPython.testing.ipunittest import ParametricTestCase |
|
28 | from IPython.utils.io import capture_output | |
|
28 | 29 | |
|
29 | 30 | from IPython import parallel as pmod |
|
30 | 31 | from IPython.parallel import error |
@@ -33,7 +34,7 b' from IPython.parallel.util import interactive' | |||
|
33 | 34 | |
|
34 | 35 | from IPython.parallel.tests import add_engines |
|
35 | 36 | |
|
36 |
from .clienttest import ClusterTestCase, |
|
|
37 | from .clienttest import ClusterTestCase, generate_output | |
|
37 | 38 | |
|
38 | 39 | def setup(): |
|
39 | 40 | add_engines(3, total=True) |
@@ -17,6 +17,7 b' from __future__ import print_function' | |||
|
17 | 17 | import os |
|
18 | 18 | import sys |
|
19 | 19 | import tempfile |
|
20 | from StringIO import StringIO | |
|
20 | 21 | |
|
21 | 22 | #----------------------------------------------------------------------------- |
|
22 | 23 | # Code |
@@ -321,3 +322,36 b' def raw_print_err(*args, **kw):' | |||
|
321 | 322 | # Short aliases for quick debugging, do NOT use these in production code. |
|
322 | 323 | rprint = raw_print |
|
323 | 324 | rprinte = raw_print_err |
|
325 | ||
|
326 | ||
|
327 | class CapturedIO(object): | |
|
328 | """Simple object for containing captured stdout/err StringIO objects""" | |
|
329 | ||
|
330 | def __init__(self, stdout, stderr): | |
|
331 | self.stdout_io = stdout | |
|
332 | self.stderr_io = stderr | |
|
333 | ||
|
334 | @property | |
|
335 | def stdout(self): | |
|
336 | return self.stdout_io.getvalue() | |
|
337 | ||
|
338 | @property | |
|
339 | def stderr(self): | |
|
340 | return self.stderr_io.getvalue() | |
|
341 | ||
|
342 | ||
|
343 | class capture_output(object): | |
|
344 | """context manager for capturing stdout/err""" | |
|
345 | ||
|
346 | def __enter__(self): | |
|
347 | self.sys_stdout = sys.stdout | |
|
348 | self.sys_stderr = sys.stderr | |
|
349 | stdout = sys.stdout = StringIO() | |
|
350 | stderr = sys.stderr = StringIO() | |
|
351 | return CapturedIO(stdout, stderr) | |
|
352 | ||
|
353 | def __exit__(self, exc_type, exc_value, traceback): | |
|
354 | sys.stdout = self.sys_stdout | |
|
355 | sys.stderr = self.sys_stderr | |
|
356 | ||
|
357 |
@@ -20,7 +20,7 b' from subprocess import Popen, PIPE' | |||
|
20 | 20 | import nose.tools as nt |
|
21 | 21 | |
|
22 | 22 | from IPython.testing import decorators as dec |
|
23 | from IPython.utils.io import Tee | |
|
23 | from IPython.utils.io import Tee, capture_output | |
|
24 | 24 | from IPython.utils.py3compat import doctest_refactor_print |
|
25 | 25 | |
|
26 | 26 | #----------------------------------------------------------------------------- |
@@ -73,3 +73,13 b' def test_io_init():' | |||
|
73 | 73 | # __class__ is a reference to the class object in Python 3, so we can't |
|
74 | 74 | # just test for string equality. |
|
75 | 75 | assert 'IPython.utils.io.IOStream' in classname, classname |
|
76 | ||
|
77 | def test_capture_output(): | |
|
78 | """capture_output() context works""" | |
|
79 | ||
|
80 | with capture_output() as io: | |
|
81 | print 'hi, stdout' | |
|
82 | print >> sys.stderr, 'hi, stderr' | |
|
83 | ||
|
84 | nt.assert_equals(io.stdout, 'hi, stdout\n') | |
|
85 | nt.assert_equals(io.stderr, 'hi, stderr\n') |
General Comments 0
You need to be logged in to leave comments.
Login now