def surrogateescape_handler(exc):
 mystring = exc.object[exc.start:exc.end]
 try:
  if isinstance(exc, UnicodeDecodeError):
   decoded = replace_surrogate_decode(mystring)
  elif isinstance(exc, UnicodeEncodeError):
   decoded = replace_surrogate_encode(mystring)
  else:
   raise exc
 except NotASurrogateError:
  raise exc
 return (decoded, exc.end)

def surrogateescape_handler ( exc ) : mystring = exc . object [ exc . start : exc . end ] try : if isinstance ( exc , UnicodeDecodeError ) : decoded = replace_surrogate_decode ( mystring ) elif isinstance ( exc , UnicodeEncodeError ) : decoded = replace_surrogate_encode ( mystring ) else : raise exc except NotASurrogateError : raise exc return ( decoded , exc . end )
pure python implementation of the pep 383: the "surrogateescape" error handler of python 3 .