code: def safe_decode(text, incoming=None, errors='strict'):
      
      if not isinstance(text, (six.string_types, six.binary_type)):
          raise TypeError("%s can't be decoded" % type(text))
  
      if isinstance(text, six.text_type):
          return text
  
      if not incoming:
          incoming = (sys.stdin.encoding or
                      sys.getdefaultencoding())
  
      try:
          return text.decode(incoming, errors)
      except UnicodeDecodeError:
          # Note(flaper87) If we get here, it means that
          # sys.stdin.encoding / sys.getdefaultencoding
          # didn't return a suitable encoding to decode
          # text. This happens mostly when global LANG
          # var is not set correctly and there's no
          # default encoding. In this case, most likely
          # python will use ASCII or ANSI encoders as
          # default encodings but they won't be capable
          # of decoding non-ASCII characters.
          #
          # Also, UTF-8 is being used since it's an ASCII
          # extension.
          return text.decode('utf-8', errors)
docs: Decodes incoming text/bytes string using `incoming` if they're not
         already unicode.
  
      This function was copied from novaclient.openstack.strutils
  
      :param incoming: Text's current encoding
      :param errors: Errors handling policy. See here for valid
          values http://docs.python.org/2/library/codecs.html
      :returns: text or a unicode `incoming` encoded
                  representation of it.
      :raises TypeError: If text is not an instance of str