def test_unicode_error():
# test with a standard error handler
res = u"\xac\u1234\u20ac\u8000".encode("rot_13", "backslashreplace")
assert res == "\xac\\h1234\\h20np\\h8000"
# test with a custom error handler
from codecs import register_error
def handler(ex):
return (u"", ex.end)
register_error("test_unicode_error", handler)
res = u"\xac\u1234\u20ac\u8000".encode("rot_13", "test_unicode_error")
assert res == "\xac"
test_unicode_error()
Here's the repro code: