[Python-ideas] Add "namereplace" error handler
Serhiy Storchaka
storchaka at gmail.com
Tue Jun 11 16:21:15 CEST 2013
11.06.13 14:47, Amaury Forgeot d'Arc написав(ла):
> It's easy to implement in Python code:
>
> import codecs, unicodedata
>
> def namereplace_errors(exc):
> replace = "\\N{{{}}}".format(
> unicodedata.name <http://unicodedata.name>(exc.object[exc.start]))
> return replace, exc.start + 1
>
> codecs.register_error('namereplace', namereplace_errors)
Indead. But namereplace_errors() should be a little more verbose:
def namereplace_errors(exc):
if not isinstance(exc, UnicodeEncodeError):
raise exc
try:
replace = r'\N{%s}' % unicodedata.name(exc.object[exc.start])
except ValueError:
return codecs.backslashreplace_errors(exc)
return replace, exc.start + 1
Even if do not register this handler from the start, it may be worth to
provide namereplace_errors() in the unicodedata module.
More information about the Python-ideas
mailing list