def htmlentityreplace_errors(ex):
 if isinstance(ex, UnicodeEncodeError):
  bad_text = ex.object[ex.start:ex.end]
  text = _html_entities_escaper.escape(bad_text)
  return (unicode(text), ex.end)
 raise ex

def htmlentityreplace_errors ( ex ) : if isinstance ( ex , UnicodeEncodeError ) : bad_text = ex . object [ ex . start : ex . end ] text = _html_entities_escaper . escape ( bad_text ) return ( unicode ( text ) , ex . end ) raise ex
an encoding error handler .