#NEWLINE# def _encode_entity( text , pattern = _escape ) : #NEWLINE# #INDENT# def escape_entities( m , map = _escape_map ) : #NEWLINE# #INDENT# out = [ ] #NEWLINE# append = out . append #NEWLINE# for char in m . group ( ) : #NEWLINE# #INDENT# text = map . get ( char ) #NEWLINE# if ( text is None ) : #NEWLINE# #INDENT# text = ( '&#%d;' % ord ( char ) ) #NEWLINE# #UNINDENT# append ( text ) #NEWLINE# #UNINDENT# return string . join ( out , '' ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# return _encode ( pattern . sub ( escape_entities , pattern ) , 'ascii' ) #NEWLINE# #UNINDENT# except TypeError : #NEWLINE# #INDENT# _raise_serialization_error ( text )