test_codeccallbacks.py passes again.

This commit is contained in:
Walter Dörwald 2007-05-09 10:44:06 +00:00
parent 580ceedd06
commit 00048f0c22

View file

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