def _EncodeString(self, string):
    """Encodes a string in the preferred encoding.

    Returns:
      bytes: encoded string.
    """
    try:
      # Note that encode() will first convert string into a Unicode string
      # if necessary.
      encoded_string = string.encode(
          self._preferred_encoding, errors=self._encode_errors)
    except UnicodeEncodeError:
      if self._encode_errors == 'strict':
        logging.error(
            'Unable to properly write output due to encoding error. '
            'Switching to error tolerant encoding which can result in '
            'non Basic Latin (C0) characters to be replaced with "?" or '
            '"\\ufffd".')
        self._encode_errors = 'replace'

      encoded_string = string.encode(
          self._preferred_encoding, errors=self._encode_errors)

    return encoded_string
def _EncodeString ( self , string ) : try : encoded_string = string . encode ( self . _preferred_encoding , errors = self . _encode_errors ) except UnicodeEncodeError : if self . _encode_errors == 'strict' : logging . error ( 'Unable to properly write output due to encoding error. ' 'Switching to error tolerant encoding which can result in ' 'non Basic Latin (C0) characters to be replaced with "?" or ' '"\\ufffd".' ) self . _encode_errors = 'replace' encoded_string = string . encode ( self . _preferred_encoding , errors = self . _encode_errors ) return encoded_string
Encodes a string in the preferred encoding.

    Returns:
      bytes: encoded string.