def METHOD_NAME(stream, encoding=None):
    if not encoding:
        encoding = select_best_encoding(stream)
    if six.PY3:
        return stream
    elif hasattr(stream, "stream"):
        return stream    # Already wrapped with a codecs.StreamWriter
    else:
        assert six.PY2
        # py2 does, however, sometimes declare an encoding on sys.stdout,
        # even if it doesn't use it (or it might be explicitly None)
        stream = codecs.getwriter(encoding)(stream)
        return stream