code: def get_best_encoding(stream):
      
      rv = getattr(stream, 'encoding', None) or sys.getdefaultencoding()
      if is_ascii_encoding(rv):
          return 'utf-8'
      return rv
docs: Returns the default stream encoding if not found.