def safe_truncate(value, length):
 b_value = encodeutils.safe_encode(value)[:length]
 decode_ok = False
 while (not decode_ok):
  try:
   u_value = encodeutils.safe_decode(b_value)
   decode_ok = True
  except UnicodeDecodeError:
   b_value = b_value[:(-1)]
 return u_value

def safe_truncate ( value , length ) : b_value = encodeutils . safe_encode ( value ) [ : length ] decode_ok = False while ( not decode_ok ) : try : u_value = encodeutils . safe_decode ( b_value ) decode_ok = True except UnicodeDecodeError : b_value = b_value [ : ( - 1 ) ] return u_value
safely truncates unicode strings such that their encoded length is no greater than the length provided .