Commit e8cfe521 authored by Serhiy Storchaka's avatar Serhiy Storchaka

Increased coverage of standard codec error handlers.

parent 74108dda
...@@ -441,6 +441,16 @@ class CodecCallbackTest(unittest.TestCase): ...@@ -441,6 +441,16 @@ class CodecCallbackTest(unittest.TestCase):
codecs.strict_errors, codecs.strict_errors,
UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch") UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")
) )
self.assertRaises(
UnicodeDecodeError,
codecs.strict_errors,
UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch")
)
self.assertRaises(
UnicodeTranslateError,
codecs.strict_errors,
UnicodeTranslateError(u"\u3042", 0, 1, "ouch")
)
def test_badandgoodignoreexceptions(self): def test_badandgoodignoreexceptions(self):
# "ignore" complains about a non-exception passed in # "ignore" complains about a non-exception passed in
...@@ -531,13 +541,16 @@ class CodecCallbackTest(unittest.TestCase): ...@@ -531,13 +541,16 @@ class CodecCallbackTest(unittest.TestCase):
UnicodeTranslateError(u"\u3042", 0, 1, "ouch") UnicodeTranslateError(u"\u3042", 0, 1, "ouch")
) )
# Use the correct exception # Use the correct exception
cs = (0, 1, 9, 10, 99, 100, 999, 1000, 9999, 10000, 0x3042) cs = (0, 1, 9, 10, 99, 100, 999, 1000, 9999, 10000)
s = "".join(unichr(c) for c in cs) cs += (0xdfff, 0xd800)
s = u"".join(unichr(c) for c in cs)
s += u"\U0001869f\U000186a0\U000f423f\U000f4240"
cs += (99999, 100000, 999999, 1000000)
self.assertEqual( self.assertEqual(
codecs.xmlcharrefreplace_errors( codecs.xmlcharrefreplace_errors(
UnicodeEncodeError("ascii", s, 0, len(s), "ouch") UnicodeEncodeError("ascii", s, 0, len(s), "ouch")
), ),
(u"".join(u"&#%d;" % ord(c) for c in s), len(s)) (u"".join(u"&#%d;" % c for c in cs), len(s))
) )
def test_badandgoodbackslashreplaceexceptions(self): def test_badandgoodbackslashreplaceexceptions(self):
...@@ -565,34 +578,33 @@ class CodecCallbackTest(unittest.TestCase): ...@@ -565,34 +578,33 @@ class CodecCallbackTest(unittest.TestCase):
UnicodeTranslateError(u"\u3042", 0, 1, "ouch") UnicodeTranslateError(u"\u3042", 0, 1, "ouch")
) )
# Use the correct exception # Use the correct exception
self.assertEqual( tests = [
codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")), (u"\u3042", u"\\u3042"),
(u"\\u3042", 1) (u"\n", u"\\x0a"),
) (u"a", u"\\x61"),
self.assertEqual( (u"\x00", u"\\x00"),
codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\x00", 0, 1, "ouch")), (u"\xff", u"\\xff"),
(u"\\x00", 1) (u"\u0100", u"\\u0100"),
) (u"\uffff", u"\\uffff"),
self.assertEqual( # Lone surrogates
codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\xff", 0, 1, "ouch")), (u"\ud800", u"\\ud800"),
(u"\\xff", 1) (u"\udfff", u"\\udfff"),
) ]
self.assertEqual( if sys.maxunicode > 0xffff:
codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\u0100", 0, 1, "ouch")), tests += [
(u"\\u0100", 1) (u"\U00010000", u"\\U00010000"),
) (u"\U0010ffff", u"\\U0010ffff"),
self.assertEqual( ]
codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\uffff", 0, 1, "ouch")), else:
(u"\\uffff", 1) tests += [
) (u"\U00010000", u"\\ud800\\udc00"),
if sys.maxunicode>0xffff: (u"\U0010ffff", u"\\udbff\\udfff"),
self.assertEqual( ]
codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\U00010000", 0, 1, "ouch")), for s, r in tests:
(u"\\U00010000", 1)
)
self.assertEqual( self.assertEqual(
codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\U0010ffff", 0, 1, "ouch")), codecs.backslashreplace_errors(
(u"\\U0010ffff", 1) UnicodeEncodeError("ascii", s, 0, len(s), "ouch")),
(r, len(s))
) )
def test_badhandlerresults(self): def test_badhandlerresults(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment