all Python 3codecsfunction
codecs.register_error()
error handlers
Registers a custom encoding error handler under a name that you then pass as errors=... to encode, decode and open.
Syntax
codecs.register_error(name, error_handler)
The handler receives the exception (UnicodeEncodeError or UnicodeDecodeError) and must return a pair (replacement, position to resume at), usually err.end, or raise. Returning a bare string is a mistake that yields TypeError. One handler serves both encoding and decoding; tell them apart with isinstance. Registration is process-wide, so do not reuse a built-in name like 'replace'.
Examples
import codecs
def mark(err):
return ('<?>', err.end)
codecs.register_error('mark', mark)
print('aéb'.encode('ascii', 'mark')) # → b'a<?>b'
print(b'a\xffb'.decode('utf-8', 'mark')) # → a<?>b
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy