Return a string, safe for output, of subprocess output.

    We assume the data is in the locale preferred encoding.
    If it won't decode properly, we warn the user but decode as
    best we can.

    We also ensure that the output can be safely written to
    standard output without encoding errors.

def console_to_str(data):
    # type: (bytes) -> Text
    """Return a string, safe for output, of subprocess output.

    We assume the data is in the locale preferred encoding.
    If it won't decode properly, we warn the user but decode as
    best we can.

    We also ensure that the output can be safely written to
    standard output without encoding errors.
    """

    # First, get the encoding we assume. This is the preferred
    # encoding for the locale, unless that is not found, or
    # it is ASCII, in which case assume UTF-8
    encoding = locale.getpreferredencoding()
    if (not encoding) or codecs.lookup(encoding).name == "ascii":
        encoding = "utf-8"

    # Now try to decode the data - if we fail, warn the user and
    # decode with replacement.
    try:
        decoded_data = data.decode(encoding)
    except UnicodeDecodeError:
        logger.warning(
            "Subprocess output does not appear to be encoded as %s",
            encoding,
        )
        decoded_data = data.decode(encoding, errors=backslashreplace_decode)

    # Make sure we can print the output, by encoding it to the output
    # encoding with replacement of unencodable characters, and then
    # decoding again.
    # We use stderr's encoding because it's less likely to be
    # redirected and if we don't find an encoding we skip this
    # step (on the assumption that output is wrapped by something
    # that won't fail).
    # The double getattr is to deal with the possibility that we're
    # being called in a situation where sys.__stderr__ doesn't exist,
    # or doesn't have an encoding attribute. Neither of these cases
    # should occur in normal pip use, but there's no harm in checking
    # in case people use pip in (unsupported) unusual situations.
    output_encoding = getattr(getattr(sys, "__stderr__", None),
                              "encoding", None)

    if output_encoding:
        output_encoded = decoded_data.encode(
            output_encoding,
            errors="backslashreplace"
        )
        decoded_data = output_encoded.decode(output_encoding)

    return decoded_data