Get the OS appropriate handle for the corresponding output stream.

    :param str stream: The the stream to get the handle for
    :return: A handle to the appropriate stream, either a ctypes buffer
             or **sys.stdout** or **sys.stderr**.

def get_stream_handle(stream=sys.stdout):
    """
    Get the OS appropriate handle for the corresponding output stream.

    :param str stream: The the stream to get the handle for
    :return: A handle to the appropriate stream, either a ctypes buffer
             or **sys.stdout** or **sys.stderr**.
    """
    handle = stream
    if os.name == "nt":
        from ctypes import windll

        handle_id = WIN_STDOUT_HANDLE_ID
        handle = windll.kernel32.GetStdHandle(handle_id)
    return handle