@contextmanager
def captured_output(stream_name):
    orig_stdout = getattr(sys, stream_name)
    setattr(sys, stream_name, six.StringIO())
    try:
        yield getattr(sys, stream_name)
    finally:
        setattr(sys, stream_name, orig_stdout)


# Copied from Django 1.8
Return a context manager used by captured_stdout/stdin/stderr
dataset/ETHPy150Open adamchainz/django-mysql/tests/testapp/utils.py/captured_output