diff options
Diffstat (limited to 'Lib/test/test_support.py')
-rw-r--r-- | Lib/test/test_support.py | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py index 2d9612e..99f57e6 100644 --- a/Lib/test/test_support.py +++ b/Lib/test/test_support.py @@ -362,6 +362,22 @@ def transient_internet(): return contextlib.nested(time_out, socket_peer_reset, ioerror_peer_reset) +@contextlib.contextmanager +def captured_stdout(): + """Run the with statement body using a StringIO object as sys.stdout. + Example use:: + + with captured_stdout() as s: + print "hello" + assert s.getvalue() == "hello" + """ + import io + orig_stdout = sys.stdout + sys.stdout = io.StringIO() + yield sys.stdout + sys.stdout = orig_stdout + + #======================================================================= # Decorator for running a function in a different locale, correctly resetting # it afterwards. |