Return a context manager used by captured_stdout/stdin/stderr that temporarily replaces the sys stream *stream_name* with a StringIO.
(stream_name)
| 893 | |
| 894 | @contextlib.contextmanager |
| 895 | def captured_output(stream_name): |
| 896 | """Return a context manager used by captured_stdout/stdin/stderr |
| 897 | that temporarily replaces the sys stream *stream_name* with a StringIO.""" |
| 898 | import io |
| 899 | orig_stdout = getattr(sys, stream_name) |
| 900 | setattr(sys, stream_name, io.StringIO()) |
| 901 | try: |
| 902 | yield getattr(sys, stream_name) |
| 903 | finally: |
| 904 | setattr(sys, stream_name, orig_stdout) |
| 905 | |
| 906 | def captured_stdout(): |
| 907 | """Capture the output of sys.stdout: |
no outgoing calls
searching dependent graphs…