Return a context manager used by captured_stdout/stdin/stderr that temporarily replaces the sys stream *stream_name* with a StringIO.
(stream_name)
| 805 | |
| 806 | @contextlib.contextmanager |
| 807 | def captured_output(stream_name): |
| 808 | """Return a context manager used by captured_stdout/stdin/stderr |
| 809 | that temporarily replaces the sys stream *stream_name* with a StringIO.""" |
| 810 | import io |
| 811 | orig_stdout = getattr(sys, stream_name) |
| 812 | setattr(sys, stream_name, io.StringIO()) |
| 813 | try: |
| 814 | yield getattr(sys, stream_name) |
| 815 | finally: |
| 816 | setattr(sys, stream_name, orig_stdout) |
| 817 | |
| 818 | def captured_stdout(): |
| 819 | """Capture the output of sys.stdout: |