parameters: (data, desc=None)
return_statement: return decoded_data
docstring: For display or logging purposes, convert a bytes object (or text) to
      text (e.g. unicode in Python 2) safe for output.
  
      :param desc: An optional phrase describing the input data, for use in
          the log message if a warning is logged. Defaults to "Bytes object".
  
      This function should never error out and so can take a best effort
      approach. It is okay to be lossy if needed since the return value is
      just for display.
  
      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.
docstring_summary: For display or logging purposes, convert a bytes object (or text) to
      text (e.g. unicode in Python 2) safe for output.
docstring_tokens: For display or logging purposes convert a bytes object ( or text ) to text ( e . g . unicode in Python 2 ) safe for output .
function: def str_to_display(data, desc=None):
      # type: (Union[bytes, Text], Optional[str]) -> Text
      """
      For display or logging purposes, convert a bytes object (or text) to
      text (e.g. unicode in Python 2) safe for output.
  
      :param desc: An optional phrase describing the input data, for use in
          the log message if a warning is logged. Defaults to "Bytes object".
  
      This function should never error out and so can take a best effort
      approach. It is okay to be lossy if needed since the return value is
      just for display.
  
      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.
      """
      if isinstance(data, text_type):
          return data
  
      # Otherwise, data is a bytes object (str in Python 2).
      # 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:
          if desc is None:
              desc = 'Bytes object'
          msg_format = '{} does not appear to be encoded as %s'.format(desc)
          logger.warning(msg_format, 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
function_tokens: def str_to_display ( data , desc = None ) : # type: (Union[bytes, Text], Optional[str]) -> Text if isinstance ( data , text_type ) : return data # Otherwise, data is a bytes object (str in Python 2). # 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 : if desc is None : desc = 'Bytes object' msg_format = '{} does not appear to be encoded as %s' . format ( desc ) logger . warning ( msg_format , 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