mirror of
https://github.com/python/cpython.git
synced 2024-12-14 12:26:47 +08:00
test_codeccallbacks.py passes again.
This commit is contained in:
parent
580ceedd06
commit
00048f0c22
@ -21,43 +21,43 @@ class PosReturn:
|
||||
# A UnicodeEncodeError object with a bad start attribute
|
||||
class BadStartUnicodeEncodeError(UnicodeEncodeError):
|
||||
def __init__(self):
|
||||
UnicodeEncodeError.__init__(self, "ascii", "", 0, 1, "bad")
|
||||
UnicodeEncodeError.__init__(self, str8("ascii"), "", 0, 1, str8("bad"))
|
||||
self.start = []
|
||||
|
||||
# A UnicodeEncodeError object with a bad object attribute
|
||||
class BadObjectUnicodeEncodeError(UnicodeEncodeError):
|
||||
def __init__(self):
|
||||
UnicodeEncodeError.__init__(self, "ascii", "", 0, 1, "bad")
|
||||
UnicodeEncodeError.__init__(self, str8("ascii"), "", 0, 1, str8("bad"))
|
||||
self.object = []
|
||||
|
||||
# A UnicodeDecodeError object without an end attribute
|
||||
class NoEndUnicodeDecodeError(UnicodeDecodeError):
|
||||
def __init__(self):
|
||||
UnicodeDecodeError.__init__(self, "ascii", "", 0, 1, "bad")
|
||||
UnicodeDecodeError.__init__(self, str8("ascii"), b"", 0, 1, str8("bad"))
|
||||
del self.end
|
||||
|
||||
# A UnicodeDecodeError object with a bad object attribute
|
||||
class BadObjectUnicodeDecodeError(UnicodeDecodeError):
|
||||
def __init__(self):
|
||||
UnicodeDecodeError.__init__(self, "ascii", "", 0, 1, "bad")
|
||||
UnicodeDecodeError.__init__(self, str8("ascii"), b"", 0, 1, str8("bad"))
|
||||
self.object = []
|
||||
|
||||
# A UnicodeTranslateError object without a start attribute
|
||||
class NoStartUnicodeTranslateError(UnicodeTranslateError):
|
||||
def __init__(self):
|
||||
UnicodeTranslateError.__init__(self, "", 0, 1, "bad")
|
||||
UnicodeTranslateError.__init__(self, "", 0, 1, str8("bad"))
|
||||
del self.start
|
||||
|
||||
# A UnicodeTranslateError object without an end attribute
|
||||
class NoEndUnicodeTranslateError(UnicodeTranslateError):
|
||||
def __init__(self):
|
||||
UnicodeTranslateError.__init__(self, "", 0, 1, "bad")
|
||||
UnicodeTranslateError.__init__(self, "", 0, 1, str8("bad"))
|
||||
del self.end
|
||||
|
||||
# A UnicodeTranslateError object without an object attribute
|
||||
class NoObjectUnicodeTranslateError(UnicodeTranslateError):
|
||||
def __init__(self):
|
||||
UnicodeTranslateError.__init__(self, "", 0, 1, "bad")
|
||||
UnicodeTranslateError.__init__(self, "", 0, 1, str8("bad"))
|
||||
del self.object
|
||||
|
||||
class CodecCallbackTest(unittest.TestCase):
|
||||
@ -69,11 +69,11 @@ class CodecCallbackTest(unittest.TestCase):
|
||||
s = "\u30b9\u30d1\u30e2 \xe4nd eggs"
|
||||
self.assertEqual(
|
||||
s.encode("ascii", "xmlcharrefreplace"),
|
||||
"スパモ änd eggs"
|
||||
b"スパモ änd eggs"
|
||||
)
|
||||
self.assertEqual(
|
||||
s.encode("latin-1", "xmlcharrefreplace"),
|
||||
"スパモ \xe4nd eggs"
|
||||
b"スパモ \xe4nd eggs"
|
||||
)
|
||||
|
||||
def test_xmlcharnamereplace(self):
|
||||
@ -95,11 +95,11 @@ class CodecCallbackTest(unittest.TestCase):
|
||||
"test.xmlcharnamereplace", xmlcharnamereplace)
|
||||
|
||||
sin = "\xab\u211c\xbb = \u2329\u1234\u20ac\u232a"
|
||||
sout = "«ℜ» = ⟨ሴ€⟩"
|
||||
sout = b"«ℜ» = ⟨ሴ€⟩"
|
||||
self.assertEqual(sin.encode("ascii", "test.xmlcharnamereplace"), sout)
|
||||
sout = "\xabℜ\xbb = ⟨ሴ€⟩"
|
||||
sout = b"\xabℜ\xbb = ⟨ሴ€⟩"
|
||||
self.assertEqual(sin.encode("latin-1", "test.xmlcharnamereplace"), sout)
|
||||
sout = "\xabℜ\xbb = ⟨ሴ\xa4⟩"
|
||||
sout = b"\xabℜ\xbb = ⟨ሴ\xa4⟩"
|
||||
self.assertEqual(sin.encode("iso-8859-15", "test.xmlcharnamereplace"), sout)
|
||||
|
||||
def test_uninamereplace(self):
|
||||
@ -123,13 +123,13 @@ class CodecCallbackTest(unittest.TestCase):
|
||||
"test.uninamereplace", uninamereplace)
|
||||
|
||||
sin = "\xac\u1234\u20ac\u8000"
|
||||
sout = "\033[1mNOT SIGN, ETHIOPIC SYLLABLE SEE, EURO SIGN, CJK UNIFIED IDEOGRAPH-8000\033[0m"
|
||||
sout = b"\033[1mNOT SIGN, ETHIOPIC SYLLABLE SEE, EURO SIGN, CJK UNIFIED IDEOGRAPH-8000\033[0m"
|
||||
self.assertEqual(sin.encode("ascii", "test.uninamereplace"), sout)
|
||||
|
||||
sout = "\xac\033[1mETHIOPIC SYLLABLE SEE, EURO SIGN, CJK UNIFIED IDEOGRAPH-8000\033[0m"
|
||||
sout = b"\xac\033[1mETHIOPIC SYLLABLE SEE, EURO SIGN, CJK UNIFIED IDEOGRAPH-8000\033[0m"
|
||||
self.assertEqual(sin.encode("latin-1", "test.uninamereplace"), sout)
|
||||
|
||||
sout = "\xac\033[1mETHIOPIC SYLLABLE SEE\033[0m\xa4\033[1mCJK UNIFIED IDEOGRAPH-8000\033[0m"
|
||||
sout = b"\xac\033[1mETHIOPIC SYLLABLE SEE\033[0m\xa4\033[1mCJK UNIFIED IDEOGRAPH-8000\033[0m"
|
||||
self.assertEqual(sin.encode("iso-8859-15", "test.uninamereplace"), sout)
|
||||
|
||||
def test_backslashescape(self):
|
||||
@ -138,19 +138,19 @@ class CodecCallbackTest(unittest.TestCase):
|
||||
sin = "a\xac\u1234\u20ac\u8000"
|
||||
if sys.maxunicode > 0xffff:
|
||||
sin += chr(sys.maxunicode)
|
||||
sout = "a\\xac\\u1234\\u20ac\\u8000"
|
||||
sout = b"a\\xac\\u1234\\u20ac\\u8000"
|
||||
if sys.maxunicode > 0xffff:
|
||||
sout += "\\U%08x" % sys.maxunicode
|
||||
sout += b"\\U%08x" % sys.maxunicode
|
||||
self.assertEqual(sin.encode("ascii", "backslashreplace"), sout)
|
||||
|
||||
sout = "a\xac\\u1234\\u20ac\\u8000"
|
||||
sout = b"a\xac\\u1234\\u20ac\\u8000"
|
||||
if sys.maxunicode > 0xffff:
|
||||
sout += "\\U%08x" % sys.maxunicode
|
||||
sout += b"\\U%08x" % sys.maxunicode
|
||||
self.assertEqual(sin.encode("latin-1", "backslashreplace"), sout)
|
||||
|
||||
sout = "a\xac\\u1234\xa4\\u8000"
|
||||
sout = b"a\xac\\u1234\xa4\\u8000"
|
||||
if sys.maxunicode > 0xffff:
|
||||
sout += "\\U%08x" % sys.maxunicode
|
||||
sout += b"\\U%08x" % sys.maxunicode
|
||||
self.assertEqual(sin.encode("iso-8859-15", "backslashreplace"), sout)
|
||||
|
||||
def test_decoderelaxedutf8(self):
|
||||
@ -162,7 +162,7 @@ class CodecCallbackTest(unittest.TestCase):
|
||||
def relaxedutf8(exc):
|
||||
if not isinstance(exc, UnicodeDecodeError):
|
||||
raise TypeError("don't know how to handle %r" % exc)
|
||||
if exc.object[exc.start:exc.end].startswith("\xc0\x80"):
|
||||
if exc.object[exc.start:exc.end].startswith(b"\xc0\x80"):
|
||||
return ("\x00", exc.start+2) # retry after two bytes
|
||||
else:
|
||||
raise exc
|
||||
@ -170,10 +170,10 @@ class CodecCallbackTest(unittest.TestCase):
|
||||
codecs.register_error(
|
||||
"test.relaxedutf8", relaxedutf8)
|
||||
|
||||
sin = "a\x00b\xc0\x80c\xc3\xbc\xc0\x80\xc0\x80"
|
||||
sin = b"a\x00b\xc0\x80c\xc3\xbc\xc0\x80\xc0\x80"
|
||||
sout = "a\x00b\x00c\xfc\x00\x00"
|
||||
self.assertEqual(sin.decode("utf-8", "test.relaxedutf8"), sout)
|
||||
sin = "\xc0\x80\xc0\x81"
|
||||
sin = b"\xc0\x80\xc0\x81"
|
||||
self.assertRaises(UnicodeError, sin.decode, "utf-8", "test.relaxedutf8")
|
||||
|
||||
def test_charmapencode(self):
|
||||
@ -181,29 +181,26 @@ class CodecCallbackTest(unittest.TestCase):
|
||||
# mapped through the encoding again. This means, that
|
||||
# to be able to use e.g. the "replace" handler, the
|
||||
# charmap has to have a mapping for "?".
|
||||
charmap = dict([ (ord(c), 2*c.upper()) for c in "abcdefgh"])
|
||||
charmap = dict((ord(c), str8(2*c.upper())) for c in "abcdefgh")
|
||||
sin = "abc"
|
||||
sout = "AABBCC"
|
||||
sout = b"AABBCC"
|
||||
self.assertEquals(codecs.charmap_encode(sin, "strict", charmap)[0], sout)
|
||||
|
||||
sin = "abcA"
|
||||
self.assertRaises(UnicodeError, codecs.charmap_encode, sin, "strict", charmap)
|
||||
|
||||
charmap[ord("?")] = "XYZ"
|
||||
charmap[ord("?")] = str8("XYZ")
|
||||
sin = "abcDEF"
|
||||
sout = "AABBCCXYZXYZXYZ"
|
||||
sout = b"AABBCCXYZXYZXYZ"
|
||||
self.assertEquals(codecs.charmap_encode(sin, "replace", charmap)[0], sout)
|
||||
|
||||
charmap[ord("?")] = "XYZ"
|
||||
self.assertRaises(TypeError, codecs.charmap_encode, sin, "replace", charmap)
|
||||
|
||||
charmap[ord("?")] = "XYZ"
|
||||
charmap[ord("?")] = "XYZ" # wrong type in mapping
|
||||
self.assertRaises(TypeError, codecs.charmap_encode, sin, "replace", charmap)
|
||||
|
||||
def test_decodeunicodeinternal(self):
|
||||
self.assertRaises(
|
||||
UnicodeDecodeError,
|
||||
"\x00\x00\x00\x00\x00".decode,
|
||||
b"\x00\x00\x00\x00\x00".decode,
|
||||
"unicode-internal",
|
||||
)
|
||||
if sys.maxunicode > 0xffff:
|
||||
@ -213,28 +210,31 @@ class CodecCallbackTest(unittest.TestCase):
|
||||
return ("\x01", 1)
|
||||
|
||||
self.assertEqual(
|
||||
"\x00\x00\x00\x00\x00".decode("unicode-internal", "ignore"),
|
||||
b"\x00\x00\x00\x00\x00".decode("unicode-internal", "ignore"),
|
||||
"\u0000"
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
"\x00\x00\x00\x00\x00".decode("unicode-internal", "replace"),
|
||||
b"\x00\x00\x00\x00\x00".decode("unicode-internal", "replace"),
|
||||
"\u0000\ufffd"
|
||||
)
|
||||
|
||||
codecs.register_error("test.hui", handler_unicodeinternal)
|
||||
|
||||
self.assertEqual(
|
||||
"\x00\x00\x00\x00\x00".decode("unicode-internal", "test.hui"),
|
||||
b"\x00\x00\x00\x00\x00".decode("unicode-internal", "test.hui"),
|
||||
"\u0000\u0001\u0000"
|
||||
)
|
||||
|
||||
def test_callbacks(self):
|
||||
def handler1(exc):
|
||||
if not isinstance(exc, UnicodeEncodeError) \
|
||||
and not isinstance(exc, UnicodeDecodeError):
|
||||
r = range(exc.start, exc.end)
|
||||
if isinstance(exc, UnicodeEncodeError):
|
||||
l = ["<%d>" % ord(exc.object[pos]) for pos in r]
|
||||
elif isinstance(exc, UnicodeDecodeError):
|
||||
l = ["<%d>" % exc.object[pos] for pos in r]
|
||||
else:
|
||||
raise TypeError("don't know how to handle %r" % exc)
|
||||
l = ["<%d>" % ord(exc.object[pos]) for pos in range(exc.start, exc.end)]
|
||||
return ("[%s]" % "".join(l), exc.end)
|
||||
|
||||
codecs.register_error("test.handler1", handler1)
|
||||
@ -242,12 +242,12 @@ class CodecCallbackTest(unittest.TestCase):
|
||||
def handler2(exc):
|
||||
if not isinstance(exc, UnicodeDecodeError):
|
||||
raise TypeError("don't know how to handle %r" % exc)
|
||||
l = ["<%d>" % ord(exc.object[pos]) for pos in range(exc.start, exc.end)]
|
||||
l = ["<%d>" % exc.object[pos] for pos in range(exc.start, exc.end)]
|
||||
return ("[%s]" % "".join(l), exc.end+1) # skip one character
|
||||
|
||||
codecs.register_error("test.handler2", handler2)
|
||||
|
||||
s = "\x00\x81\x7f\x80\xff"
|
||||
s = b"\x00\x81\x7f\x80\xff"
|
||||
|
||||
self.assertEqual(
|
||||
s.decode("ascii", "test.handler1"),
|
||||
@ -259,28 +259,28 @@ class CodecCallbackTest(unittest.TestCase):
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
"\\u3042\u3xxx".decode("unicode-escape", "test.handler1"),
|
||||
b"\\u3042\u3xxx".decode("unicode-escape", "test.handler1"),
|
||||
"\u3042[<92><117><51><120>]xx"
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
"\\u3042\u3xx".decode("unicode-escape", "test.handler1"),
|
||||
b"\\u3042\u3xx".decode("unicode-escape", "test.handler1"),
|
||||
"\u3042[<92><117><51><120><120>]"
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
codecs.charmap_decode("abc", "test.handler1", {ord("a"): "z"})[0],
|
||||
codecs.charmap_decode(b"abc", "test.handler1", {ord("a"): "z"})[0],
|
||||
"z[<98>][<99>]"
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
"g\xfc\xdfrk".encode("ascii", "test.handler1"),
|
||||
"g[<252><223>]rk"
|
||||
b"g[<252><223>]rk"
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
"g\xfc\xdf".encode("ascii", "test.handler1"),
|
||||
"g[<252><223>]"
|
||||
b"g[<252><223>]"
|
||||
)
|
||||
|
||||
def test_longstrings(self):
|
||||
@ -307,7 +307,7 @@ class CodecCallbackTest(unittest.TestCase):
|
||||
# check with one argument too much
|
||||
self.assertRaises(TypeError, exctype, *(args + ["too much"]))
|
||||
# check with one argument of the wrong type
|
||||
wrongargs = [ "spam", "eggs", 42, 1.0, None ]
|
||||
wrongargs = [ "spam", str8("eggs"), b"spam", 42, 1.0, None ]
|
||||
for i in range(len(args)):
|
||||
for wrongarg in wrongargs:
|
||||
if type(wrongarg) is type(args[i]):
|
||||
@ -328,73 +328,73 @@ class CodecCallbackTest(unittest.TestCase):
|
||||
def test_unicodeencodeerror(self):
|
||||
self.check_exceptionobjectargs(
|
||||
UnicodeEncodeError,
|
||||
["ascii", "g\xfcrk", 1, 2, "ouch"],
|
||||
[str8("ascii"), "g\xfcrk", 1, 2, str8("ouch")],
|
||||
"'ascii' codec can't encode character u'\\xfc' in position 1: ouch"
|
||||
)
|
||||
self.check_exceptionobjectargs(
|
||||
UnicodeEncodeError,
|
||||
["ascii", "g\xfcrk", 1, 4, "ouch"],
|
||||
[str8("ascii"), "g\xfcrk", 1, 4, str8("ouch")],
|
||||
"'ascii' codec can't encode characters in position 1-3: ouch"
|
||||
)
|
||||
self.check_exceptionobjectargs(
|
||||
UnicodeEncodeError,
|
||||
["ascii", "\xfcx", 0, 1, "ouch"],
|
||||
[str8("ascii"), "\xfcx", 0, 1, str8("ouch")],
|
||||
"'ascii' codec can't encode character u'\\xfc' in position 0: ouch"
|
||||
)
|
||||
self.check_exceptionobjectargs(
|
||||
UnicodeEncodeError,
|
||||
["ascii", "\u0100x", 0, 1, "ouch"],
|
||||
[str8("ascii"), "\u0100x", 0, 1, str8("ouch")],
|
||||
"'ascii' codec can't encode character u'\\u0100' in position 0: ouch"
|
||||
)
|
||||
self.check_exceptionobjectargs(
|
||||
UnicodeEncodeError,
|
||||
["ascii", "\uffffx", 0, 1, "ouch"],
|
||||
[str8("ascii"), "\uffffx", 0, 1, str8("ouch")],
|
||||
"'ascii' codec can't encode character u'\\uffff' in position 0: ouch"
|
||||
)
|
||||
if sys.maxunicode > 0xffff:
|
||||
self.check_exceptionobjectargs(
|
||||
UnicodeEncodeError,
|
||||
["ascii", "\U00010000x", 0, 1, "ouch"],
|
||||
[str8("ascii"), "\U00010000x", 0, 1, str8("ouch")],
|
||||
"'ascii' codec can't encode character u'\\U00010000' in position 0: ouch"
|
||||
)
|
||||
|
||||
def test_unicodedecodeerror(self):
|
||||
self.check_exceptionobjectargs(
|
||||
UnicodeDecodeError,
|
||||
["ascii", "g\xfcrk", 1, 2, "ouch"],
|
||||
[str8("ascii"), b"g\xfcrk", 1, 2, str8("ouch")],
|
||||
"'ascii' codec can't decode byte 0xfc in position 1: ouch"
|
||||
)
|
||||
self.check_exceptionobjectargs(
|
||||
UnicodeDecodeError,
|
||||
["ascii", "g\xfcrk", 1, 3, "ouch"],
|
||||
[str8("ascii"), b"g\xfcrk", 1, 3, str8("ouch")],
|
||||
"'ascii' codec can't decode bytes in position 1-2: ouch"
|
||||
)
|
||||
|
||||
def test_unicodetranslateerror(self):
|
||||
self.check_exceptionobjectargs(
|
||||
UnicodeTranslateError,
|
||||
["g\xfcrk", 1, 2, "ouch"],
|
||||
["g\xfcrk", 1, 2, str8("ouch")],
|
||||
"can't translate character u'\\xfc' in position 1: ouch"
|
||||
)
|
||||
self.check_exceptionobjectargs(
|
||||
UnicodeTranslateError,
|
||||
["g\u0100rk", 1, 2, "ouch"],
|
||||
["g\u0100rk", 1, 2, str8("ouch")],
|
||||
"can't translate character u'\\u0100' in position 1: ouch"
|
||||
)
|
||||
self.check_exceptionobjectargs(
|
||||
UnicodeTranslateError,
|
||||
["g\uffffrk", 1, 2, "ouch"],
|
||||
["g\uffffrk", 1, 2, str8("ouch")],
|
||||
"can't translate character u'\\uffff' in position 1: ouch"
|
||||
)
|
||||
if sys.maxunicode > 0xffff:
|
||||
self.check_exceptionobjectargs(
|
||||
UnicodeTranslateError,
|
||||
["g\U00010000rk", 1, 2, "ouch"],
|
||||
["g\U00010000rk", 1, 2, str8("ouch")],
|
||||
"can't translate character u'\\U00010000' in position 1: ouch"
|
||||
)
|
||||
self.check_exceptionobjectargs(
|
||||
UnicodeTranslateError,
|
||||
["g\xfcrk", 1, 3, "ouch"],
|
||||
["g\xfcrk", 1, 3, str8("ouch")],
|
||||
"can't translate characters in position 1-2: ouch"
|
||||
)
|
||||
|
||||
@ -416,7 +416,7 @@ class CodecCallbackTest(unittest.TestCase):
|
||||
self.assertRaises(
|
||||
UnicodeEncodeError,
|
||||
codecs.strict_errors,
|
||||
UnicodeEncodeError("ascii", "\u3042", 0, 1, "ouch")
|
||||
UnicodeEncodeError(str8("ascii"), "\u3042", 0, 1, str8("ouch"))
|
||||
)
|
||||
|
||||
def test_badandgoodignoreexceptions(self):
|
||||
@ -434,15 +434,18 @@ class CodecCallbackTest(unittest.TestCase):
|
||||
)
|
||||
# If the correct exception is passed in, "ignore" returns an empty replacement
|
||||
self.assertEquals(
|
||||
codecs.ignore_errors(UnicodeEncodeError("ascii", "\u3042", 0, 1, "ouch")),
|
||||
codecs.ignore_errors(
|
||||
UnicodeEncodeError(str8("ascii"), "\u3042", 0, 1, str8("ouch"))),
|
||||
("", 1)
|
||||
)
|
||||
self.assertEquals(
|
||||
codecs.ignore_errors(UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch")),
|
||||
codecs.ignore_errors(
|
||||
UnicodeDecodeError(str8("ascii"), b"\xff", 0, 1, str8("ouch"))),
|
||||
("", 1)
|
||||
)
|
||||
self.assertEquals(
|
||||
codecs.ignore_errors(UnicodeTranslateError("\u3042", 0, 1, "ouch")),
|
||||
codecs.ignore_errors(
|
||||
UnicodeTranslateError("\u3042", 0, 1, str8("ouch"))),
|
||||
("", 1)
|
||||
)
|
||||
|
||||
@ -471,15 +474,18 @@ class CodecCallbackTest(unittest.TestCase):
|
||||
)
|
||||
# With the correct exception, "replace" returns an "?" or "\ufffd" replacement
|
||||
self.assertEquals(
|
||||
codecs.replace_errors(UnicodeEncodeError("ascii", "\u3042", 0, 1, "ouch")),
|
||||
codecs.replace_errors(
|
||||
UnicodeEncodeError(str8("ascii"), "\u3042", 0, 1, str8("ouch"))),
|
||||
("?", 1)
|
||||
)
|
||||
self.assertEquals(
|
||||
codecs.replace_errors(UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch")),
|
||||
codecs.replace_errors(
|
||||
UnicodeDecodeError(str8("ascii"), b"\xff", 0, 1, str8("ouch"))),
|
||||
("\ufffd", 1)
|
||||
)
|
||||
self.assertEquals(
|
||||
codecs.replace_errors(UnicodeTranslateError("\u3042", 0, 1, "ouch")),
|
||||
codecs.replace_errors(
|
||||
UnicodeTranslateError("\u3042", 0, 1, str8("ouch"))),
|
||||
("\ufffd", 1)
|
||||
)
|
||||
|
||||
@ -500,19 +506,19 @@ class CodecCallbackTest(unittest.TestCase):
|
||||
self.assertRaises(
|
||||
TypeError,
|
||||
codecs.xmlcharrefreplace_errors,
|
||||
UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch")
|
||||
UnicodeDecodeError(str8("ascii"), b"\xff", 0, 1, str8("ouch"))
|
||||
)
|
||||
self.assertRaises(
|
||||
TypeError,
|
||||
codecs.xmlcharrefreplace_errors,
|
||||
UnicodeTranslateError("\u3042", 0, 1, "ouch")
|
||||
UnicodeTranslateError("\u3042", 0, 1, str8("ouch"))
|
||||
)
|
||||
# Use the correct exception
|
||||
cs = (0, 1, 9, 10, 99, 100, 999, 1000, 9999, 10000, 0x3042)
|
||||
s = "".join(chr(c) for c in cs)
|
||||
self.assertEquals(
|
||||
codecs.xmlcharrefreplace_errors(
|
||||
UnicodeEncodeError("ascii", s, 0, len(s), "ouch")
|
||||
UnicodeEncodeError(str8("ascii"), s, 0, len(s), str8("ouch"))
|
||||
),
|
||||
("".join("&#%d;" % ord(c) for c in s), len(s))
|
||||
)
|
||||
@ -534,41 +540,48 @@ class CodecCallbackTest(unittest.TestCase):
|
||||
self.assertRaises(
|
||||
TypeError,
|
||||
codecs.backslashreplace_errors,
|
||||
UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch")
|
||||
UnicodeDecodeError(str8("ascii"), b"\xff", 0, 1, str8("ouch"))
|
||||
)
|
||||
self.assertRaises(
|
||||
TypeError,
|
||||
codecs.backslashreplace_errors,
|
||||
UnicodeTranslateError("\u3042", 0, 1, "ouch")
|
||||
UnicodeTranslateError("\u3042", 0, 1, str8("ouch"))
|
||||
)
|
||||
# Use the correct exception
|
||||
self.assertEquals(
|
||||
codecs.backslashreplace_errors(UnicodeEncodeError("ascii", "\u3042", 0, 1, "ouch")),
|
||||
codecs.backslashreplace_errors(
|
||||
UnicodeEncodeError(str8("ascii"), "\u3042", 0, 1, str8("ouch"))),
|
||||
("\\u3042", 1)
|
||||
)
|
||||
self.assertEquals(
|
||||
codecs.backslashreplace_errors(UnicodeEncodeError("ascii", "\x00", 0, 1, "ouch")),
|
||||
codecs.backslashreplace_errors(
|
||||
UnicodeEncodeError(str8("ascii"), "\x00", 0, 1, str8("ouch"))),
|
||||
("\\x00", 1)
|
||||
)
|
||||
self.assertEquals(
|
||||
codecs.backslashreplace_errors(UnicodeEncodeError("ascii", "\xff", 0, 1, "ouch")),
|
||||
codecs.backslashreplace_errors(
|
||||
UnicodeEncodeError(str8("ascii"), "\xff", 0, 1, str8("ouch"))),
|
||||
("\\xff", 1)
|
||||
)
|
||||
self.assertEquals(
|
||||
codecs.backslashreplace_errors(UnicodeEncodeError("ascii", "\u0100", 0, 1, "ouch")),
|
||||
codecs.backslashreplace_errors(
|
||||
UnicodeEncodeError(str8("ascii"), "\u0100", 0, 1, str8("ouch"))),
|
||||
("\\u0100", 1)
|
||||
)
|
||||
self.assertEquals(
|
||||
codecs.backslashreplace_errors(UnicodeEncodeError("ascii", "\uffff", 0, 1, "ouch")),
|
||||
codecs.backslashreplace_errors(
|
||||
UnicodeEncodeError(str8("ascii"), "\uffff", 0, 1, str8("ouch"))),
|
||||
("\\uffff", 1)
|
||||
)
|
||||
if sys.maxunicode>0xffff:
|
||||
self.assertEquals(
|
||||
codecs.backslashreplace_errors(UnicodeEncodeError("ascii", "\U00010000", 0, 1, "ouch")),
|
||||
codecs.backslashreplace_errors(
|
||||
UnicodeEncodeError(str8("ascii"), "\U00010000", 0, 1, str8("ouch"))),
|
||||
("\\U00010000", 1)
|
||||
)
|
||||
self.assertEquals(
|
||||
codecs.backslashreplace_errors(UnicodeEncodeError("ascii", "\U0010ffff", 0, 1, "ouch")),
|
||||
codecs.backslashreplace_errors(
|
||||
UnicodeEncodeError(str8("ascii"), "\U0010ffff", 0, 1, str8("ouch"))),
|
||||
("\\U0010ffff", 1)
|
||||
)
|
||||
|
||||
@ -586,10 +599,10 @@ class CodecCallbackTest(unittest.TestCase):
|
||||
"test.badhandler"
|
||||
)
|
||||
for (enc, bytes) in (
|
||||
("ascii", "\xff"),
|
||||
("utf-8", "\xff"),
|
||||
("utf-7", "+x-"),
|
||||
("unicode-internal", "\x00"),
|
||||
("ascii", b"\xff"),
|
||||
("utf-8", b"\xff"),
|
||||
("utf-7", b"+x-"),
|
||||
("unicode-internal", b"\x00"),
|
||||
):
|
||||
self.assertRaises(
|
||||
TypeError,
|
||||
@ -682,38 +695,38 @@ class CodecCallbackTest(unittest.TestCase):
|
||||
|
||||
# Valid negative position
|
||||
handler.pos = -1
|
||||
self.assertEquals("\xff0".decode("ascii", "test.posreturn"), "<?>0")
|
||||
self.assertEquals(b"\xff0".decode("ascii", "test.posreturn"), "<?>0")
|
||||
|
||||
# Valid negative position
|
||||
handler.pos = -2
|
||||
self.assertEquals("\xff0".decode("ascii", "test.posreturn"), "<?><?>")
|
||||
self.assertEquals(b"\xff0".decode("ascii", "test.posreturn"), "<?><?>")
|
||||
|
||||
# Negative position out of bounds
|
||||
handler.pos = -3
|
||||
self.assertRaises(IndexError, "\xff0".decode, "ascii", "test.posreturn")
|
||||
self.assertRaises(IndexError, b"\xff0".decode, "ascii", "test.posreturn")
|
||||
|
||||
# Valid positive position
|
||||
handler.pos = 1
|
||||
self.assertEquals("\xff0".decode("ascii", "test.posreturn"), "<?>0")
|
||||
self.assertEquals(b"\xff0".decode("ascii", "test.posreturn"), "<?>0")
|
||||
|
||||
# Largest valid positive position (one beyond end of input)
|
||||
handler.pos = 2
|
||||
self.assertEquals("\xff0".decode("ascii", "test.posreturn"), "<?>")
|
||||
self.assertEquals(b"\xff0".decode("ascii", "test.posreturn"), "<?>")
|
||||
|
||||
# Invalid positive position
|
||||
handler.pos = 3
|
||||
self.assertRaises(IndexError, "\xff0".decode, "ascii", "test.posreturn")
|
||||
self.assertRaises(IndexError, b"\xff0".decode, "ascii", "test.posreturn")
|
||||
|
||||
# Restart at the "0"
|
||||
handler.pos = 6
|
||||
self.assertEquals("\\uyyyy0".decode("raw-unicode-escape", "test.posreturn"), "<?>0")
|
||||
self.assertEquals(b"\\uyyyy0".decode("raw-unicode-escape", "test.posreturn"), "<?>0")
|
||||
|
||||
class D(dict):
|
||||
def __getitem__(self, key):
|
||||
raise ValueError
|
||||
self.assertRaises(UnicodeError, codecs.charmap_decode, "\xff", "strict", {0xff: None})
|
||||
self.assertRaises(ValueError, codecs.charmap_decode, "\xff", "strict", D())
|
||||
self.assertRaises(TypeError, codecs.charmap_decode, "\xff", "strict", {0xff: sys.maxunicode+1})
|
||||
self.assertRaises(UnicodeError, codecs.charmap_decode, b"\xff", "strict", {0xff: None})
|
||||
self.assertRaises(ValueError, codecs.charmap_decode, b"\xff", "strict", D())
|
||||
self.assertRaises(TypeError, codecs.charmap_decode, b"\xff", "strict", {0xff: sys.maxunicode+1})
|
||||
|
||||
def test_encodehelper(self):
|
||||
# enhance coverage of:
|
||||
@ -736,11 +749,11 @@ class CodecCallbackTest(unittest.TestCase):
|
||||
|
||||
# Valid negative position
|
||||
handler.pos = -1
|
||||
self.assertEquals("\xff0".encode("ascii", "test.posreturn"), "<?>0")
|
||||
self.assertEquals("\xff0".encode("ascii", "test.posreturn"), b"<?>0")
|
||||
|
||||
# Valid negative position
|
||||
handler.pos = -2
|
||||
self.assertEquals("\xff0".encode("ascii", "test.posreturn"), "<?><?>")
|
||||
self.assertEquals("\xff0".encode("ascii", "test.posreturn"), b"<?><?>")
|
||||
|
||||
# Negative position out of bounds
|
||||
handler.pos = -3
|
||||
@ -748,11 +761,11 @@ class CodecCallbackTest(unittest.TestCase):
|
||||
|
||||
# Valid positive position
|
||||
handler.pos = 1
|
||||
self.assertEquals("\xff0".encode("ascii", "test.posreturn"), "<?>0")
|
||||
self.assertEquals("\xff0".encode("ascii", "test.posreturn"), b"<?>0")
|
||||
|
||||
# Largest valid positive position (one beyond end of input
|
||||
handler.pos = 2
|
||||
self.assertEquals("\xff0".encode("ascii", "test.posreturn"), "<?>")
|
||||
self.assertEquals("\xff0".encode("ascii", "test.posreturn"), b"<?>")
|
||||
|
||||
# Invalid positive position
|
||||
handler.pos = 3
|
||||
|
Loading…
Reference in New Issue
Block a user