def _TruncateAlert(alert, max_bytes):
 alert_json = escape.utf8(json.dumps(escape.recursive_unicode(alert), ensure_ascii=False))
 alert_json = alert_json[1:(-1)]
 if (len(alert_json) <= max_bytes):
  return escape.utf8(alert)
 assert (max_bytes >= len(_ELLIPSIS_BYTES)), ('max_bytes must be at least %d' % len(_ELLIPSIS_BYTES))
 max_bytes -= len(_ELLIPSIS_BYTES)
 truncated = alert_json[:max_bytes].decode('utf-8', errors='ignore')
 while True:
  try:
   alert = json.loads((u'"%s"' % truncated))
   break
  except Exception:
   truncated = truncated[:(-1)]
 return (escape.utf8(alert) + _ELLIPSIS_BYTES)

def _TruncateAlert ( alert , max_bytes ) : alert_json = escape . utf8 ( json . dumps ( escape . recursive_unicode ( alert ) , ensure_ascii = False ) ) alert_json = alert_json [ 1 : ( - 1 ) ] if ( len ( alert_json ) <= max_bytes ) : return escape . utf8 ( alert ) assert ( max_bytes >= len ( _ELLIPSIS_BYTES ) ) , ( 'max_bytes must be at least %d' % len ( _ELLIPSIS_BYTES ) ) max_bytes -= len ( _ELLIPSIS_BYTES ) truncated = alert_json [ : max_bytes ] . decode ( 'utf-8' , errors = 'ignore' ) while True : try : alert = json . loads ( ( u'"%s"' % truncated ) ) break except Exception : truncated = truncated [ : ( - 1 ) ] return ( escape . utf8 ( alert ) + _ELLIPSIS_BYTES )
converts the alert text to utf-8 encoded json format .