def replaced_stream(stream_name):
    """
    Context manager to temporarily swap out *stream_name* with a stream wrapper.

    :param str stream_name: The name of a sys stream to wrap
    :returns: A ``StreamWrapper`` replacement, temporarily

    >>> orig_stdout = sys.stdout
    >>> with replaced_stream("stdout") as stdout:
    ...     sys.stdout.write("hello")
    ...     assert stdout.getvalue() == "hello"

    >>> sys.stdout.write("hello")
    'hello'
    """
    orig_stream = getattr(sys, stream_name)
    new_stream = six.StringIO()
    try:
        setattr(sys, stream_name, new_stream)
        yield getattr(sys, stream_name)
    finally:
        setattr(sys, stream_name, orig_stream)