with self.subTest(str=s):
self.assertEqual(
codecs.backslashreplace_errors(
- UnicodeEncodeError("ascii", s, 0, len(s), "ouch")),
- (r, len(s))
+ UnicodeEncodeError("ascii", "a" + s + "b",
+ 1, 1 + len(s), "ouch")),
+ (r, 1 + len(s))
)
- UnicodeTranslateError(s, 0, len(s), "ouch")),
- (r, len(s))
+ self.assertEqual(
+ codecs.backslashreplace_errors(
- UnicodeDecodeError("ascii", bytearray(b), 0, 1, "ouch")),
- (r, 1)
++ UnicodeTranslateError("a" + s + "b",
++ 1, 1 + len(s), "ouch")),
++ (r, 1 + len(s))
+ )
+ tests = [
+ (b"a", "\\x61"),
+ (b"\n", "\\x0a"),
+ (b"\x00", "\\x00"),
+ (b"\xff", "\\xff"),
+ ]
+ for b, r in tests:
+ with self.subTest(bytes=b):
+ self.assertEqual(
+ codecs.backslashreplace_errors(
- UnicodeEncodeError("ascii", s, 0, len(s), "ouch")),
- (r, len(s))
++ UnicodeDecodeError("ascii", bytearray(b"a" + b + b"b"),
++ 1, 2, "ouch")),
++ (r, 2)
+ )
+
+ def test_badandgoodnamereplaceexceptions(self):
+ # "namereplace" complains about a non-exception passed in
+ self.assertRaises(
+ TypeError,
+ codecs.namereplace_errors,
+ 42
+ )
+ # "namereplace" complains about the wrong exception types
+ self.assertRaises(
+ TypeError,
+ codecs.namereplace_errors,
+ UnicodeError("ouch")
+ )
+ # "namereplace" can only be used for encoding
+ self.assertRaises(
+ TypeError,
+ codecs.namereplace_errors,
+ UnicodeDecodeError("ascii", bytearray(b"\xff"), 0, 1, "ouch")
+ )
+ self.assertRaises(
+ TypeError,
+ codecs.namereplace_errors,
+ UnicodeTranslateError("\u3042", 0, 1, "ouch")
+ )
+ # Use the correct exception
+ tests = [
+ ("\u3042", "\\N{HIRAGANA LETTER A}"),
+ ("\x00", "\\x00"),
+ ("\ufbf9", "\\N{ARABIC LIGATURE UIGHUR KIRGHIZ YEH WITH "
+ "HAMZA ABOVE WITH ALEF MAKSURA ISOLATED FORM}"),
+ ("\U000e007f", "\\N{CANCEL TAG}"),
+ ("\U0010ffff", "\\U0010ffff"),
+ # Lone surrogates
+ ("\ud800", "\\ud800"),
+ ("\udfff", "\\udfff"),
+ ("\ud800\udfff", "\\ud800\\udfff"),
+ ]
+ for s, r in tests:
+ with self.subTest(str=s):
+ self.assertEqual(
+ codecs.namereplace_errors(
++ UnicodeEncodeError("ascii", "a" + s + "b",
++ 1, 1 + len(s), "ouch")),
++ (r, 1 + len(s))
+ )
def test_badandgoodsurrogateescapeexceptions(self):
surrogateescape_errors = codecs.lookup_error('surrogateescape')
)
self.assertEqual(
surrogatepass_errors(
- UnicodeDecodeError(enc, bytearray(b[:n]), 0, n, "ouch")),
- (s[:1], n)
+ UnicodeDecodeError(enc, bytearray(b"a" + b[:n] + b"b"),
- 1, n, "ouch")),
++ 1, 1 + n, "ouch")),
+ (s[:1], 1 + n)
)
def test_badhandlerresults(self):