From: Serhiy Storchaka Date: Mon, 16 Mar 2015 06:31:38 +0000 (+0200) Subject: Use non-zero and non-last positions in error handler tests. X-Git-Tag: v3.5.0a3~169 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b8a78d3d857573ec21162e004b48288f7ad43d89;p=thirdparty%2FPython%2Fcpython.git Use non-zero and non-last positions in error handler tests. --- b8a78d3d857573ec21162e004b48288f7ad43d89 diff --cc Lib/test/test_codeccallbacks.py index b52e1f6d1cfb,cacdfae7d291..4cfb88e99b67 --- a/Lib/test/test_codeccallbacks.py +++ b/Lib/test/test_codeccallbacks.py @@@ -590,72 -581,10 +591,76 @@@ class CodecCallbackTest(unittest.TestCa 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)) ) + self.assertEqual( + codecs.backslashreplace_errors( - UnicodeTranslateError(s, 0, len(s), "ouch")), - (r, len(s)) ++ 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( - UnicodeDecodeError("ascii", bytearray(b), 0, 1, "ouch")), - (r, 1) ++ 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", s, 0, len(s), "ouch")), - (r, len(s)) ++ UnicodeEncodeError("ascii", "a" + s + "b", ++ 1, 1 + len(s), "ouch")), ++ (r, 1 + len(s)) + ) def test_badandgoodsurrogateescapeexceptions(self): surrogateescape_errors = codecs.lookup_error('surrogateescape') @@@ -767,8 -693,9 +773,9 @@@ ) 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):