src/pip/_internal/compat.py
def console_to_str(data):
    """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:
        s = data.decode(encoding)
    except UnicodeDecodeError:
        logger.warning(
            "Subprocess output does not appear to be encoded as %s",
            encoding,
        )
        s = 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:
        s = s.encode(output_encoding, errors="backslashreplace")
        s = s.decode(output_encoding)

    return s
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.
    
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.
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):
    encoding = locale.getpreferredencoding()
    if (not encoding) or codecs.lookup(encoding).name == "ascii":
        encoding = "utf-8"
    try:
        s = data.decode(encoding)
    except UnicodeDecodeError:
        logger.warning(
            "Subprocess output does not appear to be encoded as %s",
            encoding,
        )
        s = data.decode(encoding, errors=backslashreplace_decode)
    output_encoding = getattr(getattr(sys, "__stderr__", None),
                              "encoding", None)
    if output_encoding:
        s = s.encode(output_encoding, errors="backslashreplace")
        s = s.decode(output_encoding)
    return s
def console_to_str ( data ) : encoding = locale . getpreferredencoding ( ) if ( not encoding ) or codecs . lookup ( encoding ) . name == "ascii" : encoding = "utf-8" try : s = data . decode ( encoding ) except UnicodeDecodeError : logger . warning ( "Subprocess output does not appear to be encoded as %s" , encoding , ) s = data . decode ( encoding , errors = backslashreplace_decode ) output_encoding = getattr ( getattr ( sys , "__stderr__" , None ) , "encoding" , None ) if output_encoding : s = s . encode ( output_encoding , errors = "backslashreplace" ) s = s . decode ( output_encoding ) return s
Return a string, safe for output, of subprocess output.
Return a string safe for output of subprocess output .
"""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 # Now try to decode the data - if we fail, warn the user and # decode with replacement. # 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.
data