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