code: def SafeText(data, encoding=None, escape=True):
      rb"""Converts the data to a text string compatible with the given encoding.
  
    This works the same way as Decode() below except it guarantees that any
    characters in the resulting text string can be re-encoded using the given
    encoding (or GetConsoleAttr().GetEncoding() if None is given). This means
    that the string will be safe to print to sys.stdout (for example) without
    getting codec exceptions if the user's terminal doesn't support the encoding
    used by the source of the text.
  
    Args:
      data: Any bytes, string, or object that has str() or unicode() methods.
      encoding: The encoding name to ensure compatibility with. Defaults to
        GetConsoleAttr().GetEncoding().
      escape: Replace unencodable characters with a \uXXXX or \xXX equivalent if
        True. Otherwise replace unencodable characters with an appropriate unknown
        character, '?' for ASCII, and the unicode unknown replacement character
        \uFFFE for unicode.
  
    Returns:
      A text string representation of the data, but modified to remove any
      characters that would result in an encoding exception with the target
      encoding. In the worst case, with escape=False, it will contain only ?
      characters.
    """
      if data is None:
          return "None"
      encoding = encoding or GetConsoleAttr().GetEncoding()
      string = encoding_util.Decode(data, encoding=encoding)
  
      try:
          # No change needed if the string encodes to the output encoding.
          string.encode(encoding)
          return string
      except UnicodeError:
          # The string does not encode to the output encoding. Encode it with error
          # handling then convert it back into a text string (which will be
          # guaranteed to only contain characters that can be encoded later.
          return string.encode(
              encoding, "backslashreplace" if escape else "replace"
          ).decode(encoding)
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import unicode_literals
  import os
  import sys
  import unicodedata
  from fire.console import console_attr_os
  from fire.console import encoding
  from fire.console import text
  import six
label_desc: Use image node_id 4 for calling a global function with example usage: SafeText(data, encoding, escape) and returns: str, string, string