2002-09-02 10:14:32 -03:00
|
|
|
import test.test_support, unittest
|
2008-05-20 03:08:38 -03:00
|
|
|
import sys, codecs, htmlentitydefs, unicodedata
|
2002-09-02 10:14:32 -03:00
|
|
|
|
2003-01-31 13:19:08 -04:00
|
|
|
class PosReturn:
|
|
|
|
# this can be used for configurable callbacks
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.pos = 0
|
|
|
|
|
|
|
|
def handle(self, exc):
|
|
|
|
oldpos = self.pos
|
|
|
|
realpos = oldpos
|
|
|
|
if realpos<0:
|
2003-02-18 22:35:07 -04:00
|
|
|
realpos = len(exc.object) + realpos
|
2003-01-31 13:19:08 -04:00
|
|
|
# if we don't advance this time, terminate on the next call
|
|
|
|
# otherwise we'd get an endless loop
|
|
|
|
if realpos <= exc.start:
|
|
|
|
self.pos = len(exc.object)
|
|
|
|
return (u"<?>", oldpos)
|
|
|
|
|
2005-11-17 14:51:34 -04:00
|
|
|
# A UnicodeEncodeError object with a bad start attribute
|
|
|
|
class BadStartUnicodeEncodeError(UnicodeEncodeError):
|
|
|
|
def __init__(self):
|
|
|
|
UnicodeEncodeError.__init__(self, "ascii", u"", 0, 1, "bad")
|
|
|
|
self.start = []
|
|
|
|
|
|
|
|
# A UnicodeEncodeError object with a bad object attribute
|
|
|
|
class BadObjectUnicodeEncodeError(UnicodeEncodeError):
|
|
|
|
def __init__(self):
|
|
|
|
UnicodeEncodeError.__init__(self, "ascii", u"", 0, 1, "bad")
|
|
|
|
self.object = []
|
|
|
|
|
|
|
|
# A UnicodeDecodeError object without an end attribute
|
|
|
|
class NoEndUnicodeDecodeError(UnicodeDecodeError):
|
|
|
|
def __init__(self):
|
|
|
|
UnicodeDecodeError.__init__(self, "ascii", "", 0, 1, "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")
|
|
|
|
self.object = []
|
|
|
|
|
|
|
|
# A UnicodeTranslateError object without a start attribute
|
|
|
|
class NoStartUnicodeTranslateError(UnicodeTranslateError):
|
|
|
|
def __init__(self):
|
|
|
|
UnicodeTranslateError.__init__(self, u"", 0, 1, "bad")
|
|
|
|
del self.start
|
|
|
|
|
|
|
|
# A UnicodeTranslateError object without an end attribute
|
|
|
|
class NoEndUnicodeTranslateError(UnicodeTranslateError):
|
|
|
|
def __init__(self):
|
|
|
|
UnicodeTranslateError.__init__(self, u"", 0, 1, "bad")
|
|
|
|
del self.end
|
|
|
|
|
|
|
|
# A UnicodeTranslateError object without an object attribute
|
|
|
|
class NoObjectUnicodeTranslateError(UnicodeTranslateError):
|
|
|
|
def __init__(self):
|
|
|
|
UnicodeTranslateError.__init__(self, u"", 0, 1, "bad")
|
|
|
|
del self.object
|
|
|
|
|
2002-09-02 10:14:32 -03:00
|
|
|
class CodecCallbackTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_xmlcharrefreplace(self):
|
|
|
|
# replace unencodable characters which numeric character entities.
|
|
|
|
# For ascii, latin-1 and charmaps this is completely implemented
|
|
|
|
# in C and should be reasonably fast.
|
|
|
|
s = u"\u30b9\u30d1\u30e2 \xe4nd eggs"
|
|
|
|
self.assertEqual(
|
|
|
|
s.encode("ascii", "xmlcharrefreplace"),
|
|
|
|
"スパモ änd eggs"
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
s.encode("latin-1", "xmlcharrefreplace"),
|
|
|
|
"スパモ \xe4nd eggs"
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_xmlcharnamereplace(self):
|
|
|
|
# This time use a named character entity for unencodable
|
|
|
|
# characters, if one is available.
|
|
|
|
|
|
|
|
def xmlcharnamereplace(exc):
|
|
|
|
if not isinstance(exc, UnicodeEncodeError):
|
|
|
|
raise TypeError("don't know how to handle %r" % exc)
|
|
|
|
l = []
|
|
|
|
for c in exc.object[exc.start:exc.end]:
|
|
|
|
try:
|
2008-05-20 03:08:38 -03:00
|
|
|
l.append(u"&%s;" % htmlentitydefs.codepoint2name[ord(c)])
|
2002-09-02 10:14:32 -03:00
|
|
|
except KeyError:
|
|
|
|
l.append(u"&#%d;" % ord(c))
|
|
|
|
return (u"".join(l), exc.end)
|
|
|
|
|
|
|
|
codecs.register_error(
|
|
|
|
"test.xmlcharnamereplace", xmlcharnamereplace)
|
|
|
|
|
|
|
|
sin = u"\xab\u211c\xbb = \u2329\u1234\u20ac\u232a"
|
|
|
|
sout = "«ℜ» = ⟨ሴ€⟩"
|
|
|
|
self.assertEqual(sin.encode("ascii", "test.xmlcharnamereplace"), sout)
|
|
|
|
sout = "\xabℜ\xbb = ⟨ሴ€⟩"
|
|
|
|
self.assertEqual(sin.encode("latin-1", "test.xmlcharnamereplace"), sout)
|
|
|
|
sout = "\xabℜ\xbb = ⟨ሴ\xa4⟩"
|
|
|
|
self.assertEqual(sin.encode("iso-8859-15", "test.xmlcharnamereplace"), sout)
|
|
|
|
|
|
|
|
def test_uninamereplace(self):
|
|
|
|
# We're using the names from the unicode database this time,
|
2002-11-25 13:58:02 -04:00
|
|
|
# and we're doing "syntax highlighting" here, i.e. we include
|
2002-09-02 10:14:32 -03:00
|
|
|
# the replaced text in ANSI escape sequences. For this it is
|
|
|
|
# useful that the error handler is not called for every single
|
|
|
|
# unencodable character, but for a complete sequence of
|
|
|
|
# unencodable characters, otherwise we would output many
|
2009-02-21 16:27:01 -04:00
|
|
|
# unnecessary escape sequences.
|
2002-09-02 10:14:32 -03:00
|
|
|
|
|
|
|
def uninamereplace(exc):
|
|
|
|
if not isinstance(exc, UnicodeEncodeError):
|
|
|
|
raise TypeError("don't know how to handle %r" % exc)
|
|
|
|
l = []
|
|
|
|
for c in exc.object[exc.start:exc.end]:
|
|
|
|
l.append(unicodedata.name(c, u"0x%x" % ord(c)))
|
|
|
|
return (u"\033[1m%s\033[0m" % u", ".join(l), exc.end)
|
|
|
|
|
|
|
|
codecs.register_error(
|
|
|
|
"test.uninamereplace", uninamereplace)
|
|
|
|
|
|
|
|
sin = u"\xac\u1234\u20ac\u8000"
|
2002-11-23 15:41:01 -04:00
|
|
|
sout = "\033[1mNOT SIGN, ETHIOPIC SYLLABLE SEE, EURO SIGN, CJK UNIFIED IDEOGRAPH-8000\033[0m"
|
2002-09-02 10:14:32 -03:00
|
|
|
self.assertEqual(sin.encode("ascii", "test.uninamereplace"), sout)
|
|
|
|
|
2002-11-23 15:41:01 -04:00
|
|
|
sout = "\xac\033[1mETHIOPIC SYLLABLE SEE, EURO SIGN, CJK UNIFIED IDEOGRAPH-8000\033[0m"
|
2002-09-02 10:14:32 -03:00
|
|
|
self.assertEqual(sin.encode("latin-1", "test.uninamereplace"), sout)
|
|
|
|
|
2002-11-23 15:41:01 -04:00
|
|
|
sout = "\xac\033[1mETHIOPIC SYLLABLE SEE\033[0m\xa4\033[1mCJK UNIFIED IDEOGRAPH-8000\033[0m"
|
2002-09-02 10:14:32 -03:00
|
|
|
self.assertEqual(sin.encode("iso-8859-15", "test.uninamereplace"), sout)
|
|
|
|
|
|
|
|
def test_backslashescape(self):
|
|
|
|
# Does the same as the "unicode-escape" encoding, but with different
|
|
|
|
# base encodings.
|
|
|
|
sin = u"a\xac\u1234\u20ac\u8000"
|
|
|
|
if sys.maxunicode > 0xffff:
|
|
|
|
sin += unichr(sys.maxunicode)
|
|
|
|
sout = "a\\xac\\u1234\\u20ac\\u8000"
|
|
|
|
if sys.maxunicode > 0xffff:
|
|
|
|
sout += "\\U%08x" % sys.maxunicode
|
|
|
|
self.assertEqual(sin.encode("ascii", "backslashreplace"), sout)
|
|
|
|
|
|
|
|
sout = "a\xac\\u1234\\u20ac\\u8000"
|
|
|
|
if sys.maxunicode > 0xffff:
|
|
|
|
sout += "\\U%08x" % sys.maxunicode
|
|
|
|
self.assertEqual(sin.encode("latin-1", "backslashreplace"), sout)
|
|
|
|
|
|
|
|
sout = "a\xac\\u1234\xa4\\u8000"
|
|
|
|
if sys.maxunicode > 0xffff:
|
|
|
|
sout += "\\U%08x" % sys.maxunicode
|
|
|
|
self.assertEqual(sin.encode("iso-8859-15", "backslashreplace"), sout)
|
|
|
|
|
2010-06-05 14:51:07 -03:00
|
|
|
def test_decoding_callbacks(self):
|
|
|
|
# This is a test for a decoding callback handler
|
|
|
|
# that allows the decoding of the invalid sequence
|
|
|
|
# "\xc0\x80" and returns "\x00" instead of raising an error.
|
|
|
|
# All other illegal sequences will be handled strictly.
|
2002-09-02 10:14:32 -03:00
|
|
|
def relaxedutf8(exc):
|
|
|
|
if not isinstance(exc, UnicodeDecodeError):
|
|
|
|
raise TypeError("don't know how to handle %r" % exc)
|
2010-06-05 14:51:07 -03:00
|
|
|
if exc.object[exc.start:exc.start+2] == "\xc0\x80":
|
2002-09-02 10:14:32 -03:00
|
|
|
return (u"\x00", exc.start+2) # retry after two bytes
|
|
|
|
else:
|
|
|
|
raise exc
|
|
|
|
|
2010-06-05 14:51:07 -03:00
|
|
|
codecs.register_error("test.relaxedutf8", relaxedutf8)
|
2002-09-02 10:14:32 -03:00
|
|
|
|
2010-06-05 14:51:07 -03:00
|
|
|
# all the "\xc0\x80" will be decoded to "\x00"
|
2002-09-02 10:14:32 -03:00
|
|
|
sin = "a\x00b\xc0\x80c\xc3\xbc\xc0\x80\xc0\x80"
|
|
|
|
sout = u"a\x00b\x00c\xfc\x00\x00"
|
|
|
|
self.assertEqual(sin.decode("utf-8", "test.relaxedutf8"), sout)
|
2010-06-05 14:51:07 -03:00
|
|
|
|
|
|
|
# "\xc0\x81" is not valid and a UnicodeDecodeError will be raised
|
2002-09-02 10:14:32 -03:00
|
|
|
sin = "\xc0\x80\xc0\x81"
|
2010-06-05 14:51:07 -03:00
|
|
|
self.assertRaises(UnicodeDecodeError, sin.decode,
|
|
|
|
"utf-8", "test.relaxedutf8")
|
2002-09-02 10:14:32 -03:00
|
|
|
|
|
|
|
def test_charmapencode(self):
|
|
|
|
# For charmap encodings the replacement string will be
|
|
|
|
# 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"])
|
|
|
|
sin = u"abc"
|
|
|
|
sout = "AABBCC"
|
|
|
|
self.assertEquals(codecs.charmap_encode(sin, "strict", charmap)[0], sout)
|
|
|
|
|
|
|
|
sin = u"abcA"
|
|
|
|
self.assertRaises(UnicodeError, codecs.charmap_encode, sin, "strict", charmap)
|
|
|
|
|
|
|
|
charmap[ord("?")] = "XYZ"
|
|
|
|
sin = u"abcDEF"
|
|
|
|
sout = "AABBCCXYZXYZXYZ"
|
|
|
|
self.assertEquals(codecs.charmap_encode(sin, "replace", charmap)[0], sout)
|
|
|
|
|
|
|
|
charmap[ord("?")] = u"XYZ"
|
|
|
|
self.assertRaises(TypeError, codecs.charmap_encode, sin, "replace", charmap)
|
|
|
|
|
|
|
|
charmap[ord("?")] = u"XYZ"
|
|
|
|
self.assertRaises(TypeError, codecs.charmap_encode, sin, "replace", charmap)
|
|
|
|
|
2005-08-30 07:23:14 -03:00
|
|
|
def test_decodeunicodeinternal(self):
|
|
|
|
self.assertRaises(
|
|
|
|
UnicodeDecodeError,
|
|
|
|
"\x00\x00\x00\x00\x00".decode,
|
|
|
|
"unicode-internal",
|
|
|
|
)
|
|
|
|
if sys.maxunicode > 0xffff:
|
|
|
|
def handler_unicodeinternal(exc):
|
|
|
|
if not isinstance(exc, UnicodeDecodeError):
|
|
|
|
raise TypeError("don't know how to handle %r" % exc)
|
|
|
|
return (u"\x01", 1)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
"\x00\x00\x00\x00\x00".decode("unicode-internal", "ignore"),
|
|
|
|
u"\u0000"
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
"\x00\x00\x00\x00\x00".decode("unicode-internal", "replace"),
|
|
|
|
u"\u0000\ufffd"
|
|
|
|
)
|
|
|
|
|
|
|
|
codecs.register_error("test.hui", handler_unicodeinternal)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
"\x00\x00\x00\x00\x00".decode("unicode-internal", "test.hui"),
|
|
|
|
u"\u0000\u0001\u0000"
|
|
|
|
)
|
|
|
|
|
2002-09-02 10:14:32 -03:00
|
|
|
def test_callbacks(self):
|
|
|
|
def handler1(exc):
|
|
|
|
if not isinstance(exc, UnicodeEncodeError) \
|
|
|
|
and not isinstance(exc, UnicodeDecodeError):
|
|
|
|
raise TypeError("don't know how to handle %r" % exc)
|
|
|
|
l = [u"<%d>" % ord(exc.object[pos]) for pos in xrange(exc.start, exc.end)]
|
|
|
|
return (u"[%s]" % u"".join(l), exc.end)
|
|
|
|
|
|
|
|
codecs.register_error("test.handler1", handler1)
|
|
|
|
|
|
|
|
def handler2(exc):
|
|
|
|
if not isinstance(exc, UnicodeDecodeError):
|
|
|
|
raise TypeError("don't know how to handle %r" % exc)
|
|
|
|
l = [u"<%d>" % ord(exc.object[pos]) for pos in xrange(exc.start, exc.end)]
|
|
|
|
return (u"[%s]" % u"".join(l), exc.end+1) # skip one character
|
|
|
|
|
|
|
|
codecs.register_error("test.handler2", handler2)
|
|
|
|
|
|
|
|
s = "\x00\x81\x7f\x80\xff"
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
s.decode("ascii", "test.handler1"),
|
|
|
|
u"\x00[<129>]\x7f[<128>][<255>]"
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
s.decode("ascii", "test.handler2"),
|
|
|
|
u"\x00[<129>][<128>]"
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
"\\u3042\u3xxx".decode("unicode-escape", "test.handler1"),
|
|
|
|
u"\u3042[<92><117><51><120>]xx"
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
"\\u3042\u3xx".decode("unicode-escape", "test.handler1"),
|
|
|
|
u"\u3042[<92><117><51><120><120>]"
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
codecs.charmap_decode("abc", "test.handler1", {ord("a"): u"z"})[0],
|
|
|
|
u"z[<98>][<99>]"
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
u"g\xfc\xdfrk".encode("ascii", "test.handler1"),
|
|
|
|
u"g[<252><223>]rk"
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
u"g\xfc\xdf".encode("ascii", "test.handler1"),
|
|
|
|
u"g[<252><223>]"
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_longstrings(self):
|
|
|
|
# test long strings to check for memory overflow problems
|
2007-08-17 13:41:28 -03:00
|
|
|
errors = [ "strict", "ignore", "replace", "xmlcharrefreplace",
|
|
|
|
"backslashreplace"]
|
2002-09-02 10:14:32 -03:00
|
|
|
# register the handlers under different names,
|
|
|
|
# to prevent the codec from recognizing the name
|
|
|
|
for err in errors:
|
|
|
|
codecs.register_error("test." + err, codecs.lookup_error(err))
|
|
|
|
l = 1000
|
|
|
|
errors += [ "test." + err for err in errors ]
|
|
|
|
for uni in [ s*l for s in (u"x", u"\u3042", u"a\xe4") ]:
|
2007-08-17 13:41:28 -03:00
|
|
|
for enc in ("ascii", "latin-1", "iso-8859-1", "iso-8859-15",
|
|
|
|
"utf-8", "utf-7", "utf-16", "utf-32"):
|
2002-09-02 10:14:32 -03:00
|
|
|
for err in errors:
|
2002-11-09 01:26:15 -04:00
|
|
|
try:
|
|
|
|
uni.encode(enc, err)
|
|
|
|
except UnicodeError:
|
|
|
|
pass
|
2002-09-02 10:14:32 -03:00
|
|
|
|
|
|
|
def check_exceptionobjectargs(self, exctype, args, msg):
|
|
|
|
# Test UnicodeError subclasses: construction, attribute assignment and __str__ conversion
|
|
|
|
# check with one missing argument
|
|
|
|
self.assertRaises(TypeError, exctype, *args[:-1])
|
2003-01-19 22:34:07 -04:00
|
|
|
# check with one argument too much
|
2002-09-02 10:14:32 -03:00
|
|
|
self.assertRaises(TypeError, exctype, *(args + ["too much"]))
|
|
|
|
# check with one argument of the wrong type
|
|
|
|
wrongargs = [ "spam", u"eggs", 42, 1.0, None ]
|
|
|
|
for i in xrange(len(args)):
|
|
|
|
for wrongarg in wrongargs:
|
|
|
|
if type(wrongarg) is type(args[i]):
|
2002-11-09 01:26:15 -04:00
|
|
|
continue
|
2002-09-02 10:14:32 -03:00
|
|
|
# build argument array
|
|
|
|
callargs = []
|
|
|
|
for j in xrange(len(args)):
|
|
|
|
if i==j:
|
|
|
|
callargs.append(wrongarg)
|
|
|
|
else:
|
|
|
|
callargs.append(args[i])
|
|
|
|
self.assertRaises(TypeError, exctype, *callargs)
|
2003-01-19 22:34:07 -04:00
|
|
|
|
|
|
|
# check with the correct number and type of arguments
|
2002-09-02 10:14:32 -03:00
|
|
|
exc = exctype(*args)
|
|
|
|
self.assertEquals(str(exc), msg)
|
|
|
|
|
|
|
|
def test_unicodeencodeerror(self):
|
|
|
|
self.check_exceptionobjectargs(
|
|
|
|
UnicodeEncodeError,
|
|
|
|
["ascii", u"g\xfcrk", 1, 2, "ouch"],
|
2003-08-12 14:34:49 -03:00
|
|
|
"'ascii' codec can't encode character u'\\xfc' in position 1: ouch"
|
2002-09-02 10:14:32 -03:00
|
|
|
)
|
|
|
|
self.check_exceptionobjectargs(
|
|
|
|
UnicodeEncodeError,
|
|
|
|
["ascii", u"g\xfcrk", 1, 4, "ouch"],
|
|
|
|
"'ascii' codec can't encode characters in position 1-3: ouch"
|
|
|
|
)
|
|
|
|
self.check_exceptionobjectargs(
|
|
|
|
UnicodeEncodeError,
|
|
|
|
["ascii", u"\xfcx", 0, 1, "ouch"],
|
2003-08-12 14:34:49 -03:00
|
|
|
"'ascii' codec can't encode character u'\\xfc' in position 0: ouch"
|
2002-09-02 10:14:32 -03:00
|
|
|
)
|
2003-08-12 14:32:43 -03:00
|
|
|
self.check_exceptionobjectargs(
|
|
|
|
UnicodeEncodeError,
|
|
|
|
["ascii", u"\u0100x", 0, 1, "ouch"],
|
2003-08-12 14:34:49 -03:00
|
|
|
"'ascii' codec can't encode character u'\\u0100' in position 0: ouch"
|
2003-08-12 14:32:43 -03:00
|
|
|
)
|
|
|
|
self.check_exceptionobjectargs(
|
|
|
|
UnicodeEncodeError,
|
|
|
|
["ascii", u"\uffffx", 0, 1, "ouch"],
|
2003-08-12 14:34:49 -03:00
|
|
|
"'ascii' codec can't encode character u'\\uffff' in position 0: ouch"
|
2003-08-12 14:32:43 -03:00
|
|
|
)
|
|
|
|
if sys.maxunicode > 0xffff:
|
|
|
|
self.check_exceptionobjectargs(
|
|
|
|
UnicodeEncodeError,
|
|
|
|
["ascii", u"\U00010000x", 0, 1, "ouch"],
|
2003-08-12 14:34:49 -03:00
|
|
|
"'ascii' codec can't encode character u'\\U00010000' in position 0: ouch"
|
2003-08-12 14:32:43 -03:00
|
|
|
)
|
2002-09-02 10:14:32 -03:00
|
|
|
|
|
|
|
def test_unicodedecodeerror(self):
|
|
|
|
self.check_exceptionobjectargs(
|
|
|
|
UnicodeDecodeError,
|
|
|
|
["ascii", "g\xfcrk", 1, 2, "ouch"],
|
|
|
|
"'ascii' codec can't decode byte 0xfc in position 1: ouch"
|
|
|
|
)
|
|
|
|
self.check_exceptionobjectargs(
|
|
|
|
UnicodeDecodeError,
|
|
|
|
["ascii", "g\xfcrk", 1, 3, "ouch"],
|
|
|
|
"'ascii' codec can't decode bytes in position 1-2: ouch"
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_unicodetranslateerror(self):
|
|
|
|
self.check_exceptionobjectargs(
|
|
|
|
UnicodeTranslateError,
|
|
|
|
[u"g\xfcrk", 1, 2, "ouch"],
|
2003-08-12 14:34:49 -03:00
|
|
|
"can't translate character u'\\xfc' in position 1: ouch"
|
2002-09-02 10:14:32 -03:00
|
|
|
)
|
|
|
|
self.check_exceptionobjectargs(
|
|
|
|
UnicodeTranslateError,
|
2003-08-12 14:32:43 -03:00
|
|
|
[u"g\u0100rk", 1, 2, "ouch"],
|
2003-08-12 14:34:49 -03:00
|
|
|
"can't translate character u'\\u0100' in position 1: ouch"
|
2003-08-12 14:32:43 -03:00
|
|
|
)
|
|
|
|
self.check_exceptionobjectargs(
|
|
|
|
UnicodeTranslateError,
|
|
|
|
[u"g\uffffrk", 1, 2, "ouch"],
|
2003-08-12 14:34:49 -03:00
|
|
|
"can't translate character u'\\uffff' in position 1: ouch"
|
2003-08-12 14:32:43 -03:00
|
|
|
)
|
|
|
|
if sys.maxunicode > 0xffff:
|
|
|
|
self.check_exceptionobjectargs(
|
|
|
|
UnicodeTranslateError,
|
|
|
|
[u"g\U00010000rk", 1, 2, "ouch"],
|
2003-08-12 14:34:49 -03:00
|
|
|
"can't translate character u'\\U00010000' in position 1: ouch"
|
2003-08-12 14:32:43 -03:00
|
|
|
)
|
|
|
|
self.check_exceptionobjectargs(
|
|
|
|
UnicodeTranslateError,
|
2002-09-02 10:14:32 -03:00
|
|
|
[u"g\xfcrk", 1, 3, "ouch"],
|
|
|
|
"can't translate characters in position 1-2: ouch"
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_badandgoodstrictexceptions(self):
|
2003-01-19 22:34:07 -04:00
|
|
|
# "strict" complains about a non-exception passed in
|
2002-09-02 10:14:32 -03:00
|
|
|
self.assertRaises(
|
|
|
|
TypeError,
|
|
|
|
codecs.strict_errors,
|
|
|
|
42
|
|
|
|
)
|
2003-01-19 22:34:07 -04:00
|
|
|
# "strict" complains about the wrong exception type
|
2002-09-02 10:14:32 -03:00
|
|
|
self.assertRaises(
|
|
|
|
Exception,
|
|
|
|
codecs.strict_errors,
|
|
|
|
Exception("ouch")
|
|
|
|
)
|
|
|
|
|
2003-01-19 22:34:07 -04:00
|
|
|
# If the correct exception is passed in, "strict" raises it
|
2002-09-02 10:14:32 -03:00
|
|
|
self.assertRaises(
|
|
|
|
UnicodeEncodeError,
|
|
|
|
codecs.strict_errors,
|
|
|
|
UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_badandgoodignoreexceptions(self):
|
2003-01-19 22:34:07 -04:00
|
|
|
# "ignore" complains about a non-exception passed in
|
2002-09-02 10:14:32 -03:00
|
|
|
self.assertRaises(
|
|
|
|
TypeError,
|
|
|
|
codecs.ignore_errors,
|
|
|
|
42
|
|
|
|
)
|
2003-01-19 22:34:07 -04:00
|
|
|
# "ignore" complains about the wrong exception type
|
2002-09-02 10:14:32 -03:00
|
|
|
self.assertRaises(
|
|
|
|
TypeError,
|
|
|
|
codecs.ignore_errors,
|
|
|
|
UnicodeError("ouch")
|
|
|
|
)
|
2003-01-19 22:34:07 -04:00
|
|
|
# If the correct exception is passed in, "ignore" returns an empty replacement
|
2002-09-02 10:14:32 -03:00
|
|
|
self.assertEquals(
|
|
|
|
codecs.ignore_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")),
|
|
|
|
(u"", 1)
|
|
|
|
)
|
|
|
|
self.assertEquals(
|
|
|
|
codecs.ignore_errors(UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch")),
|
|
|
|
(u"", 1)
|
|
|
|
)
|
|
|
|
self.assertEquals(
|
|
|
|
codecs.ignore_errors(UnicodeTranslateError(u"\u3042", 0, 1, "ouch")),
|
|
|
|
(u"", 1)
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_badandgoodreplaceexceptions(self):
|
2003-01-19 22:34:07 -04:00
|
|
|
# "replace" complains about a non-exception passed in
|
2002-09-02 10:14:32 -03:00
|
|
|
self.assertRaises(
|
|
|
|
TypeError,
|
|
|
|
codecs.replace_errors,
|
|
|
|
42
|
|
|
|
)
|
2003-01-19 22:34:07 -04:00
|
|
|
# "replace" complains about the wrong exception type
|
2002-09-02 10:14:32 -03:00
|
|
|
self.assertRaises(
|
|
|
|
TypeError,
|
|
|
|
codecs.replace_errors,
|
|
|
|
UnicodeError("ouch")
|
|
|
|
)
|
2005-11-17 14:51:34 -04:00
|
|
|
self.assertRaises(
|
|
|
|
TypeError,
|
|
|
|
codecs.replace_errors,
|
|
|
|
BadObjectUnicodeEncodeError()
|
|
|
|
)
|
|
|
|
self.assertRaises(
|
|
|
|
TypeError,
|
|
|
|
codecs.replace_errors,
|
|
|
|
BadObjectUnicodeDecodeError()
|
|
|
|
)
|
2004-12-14 17:28:07 -04:00
|
|
|
# With the correct exception, "replace" returns an "?" or u"\ufffd" replacement
|
2002-09-02 10:14:32 -03:00
|
|
|
self.assertEquals(
|
|
|
|
codecs.replace_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")),
|
|
|
|
(u"?", 1)
|
|
|
|
)
|
|
|
|
self.assertEquals(
|
|
|
|
codecs.replace_errors(UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch")),
|
|
|
|
(u"\ufffd", 1)
|
|
|
|
)
|
|
|
|
self.assertEquals(
|
|
|
|
codecs.replace_errors(UnicodeTranslateError(u"\u3042", 0, 1, "ouch")),
|
|
|
|
(u"\ufffd", 1)
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_badandgoodxmlcharrefreplaceexceptions(self):
|
2003-01-19 22:34:07 -04:00
|
|
|
# "xmlcharrefreplace" complains about a non-exception passed in
|
2002-09-02 10:14:32 -03:00
|
|
|
self.assertRaises(
|
|
|
|
TypeError,
|
|
|
|
codecs.xmlcharrefreplace_errors,
|
|
|
|
42
|
|
|
|
)
|
2003-01-19 22:34:07 -04:00
|
|
|
# "xmlcharrefreplace" complains about the wrong exception types
|
2002-09-02 10:14:32 -03:00
|
|
|
self.assertRaises(
|
|
|
|
TypeError,
|
|
|
|
codecs.xmlcharrefreplace_errors,
|
|
|
|
UnicodeError("ouch")
|
|
|
|
)
|
2003-01-19 22:34:07 -04:00
|
|
|
# "xmlcharrefreplace" can only be used for encoding
|
2002-09-02 10:14:32 -03:00
|
|
|
self.assertRaises(
|
|
|
|
TypeError,
|
|
|
|
codecs.xmlcharrefreplace_errors,
|
|
|
|
UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch")
|
|
|
|
)
|
|
|
|
self.assertRaises(
|
|
|
|
TypeError,
|
|
|
|
codecs.xmlcharrefreplace_errors,
|
|
|
|
UnicodeTranslateError(u"\u3042", 0, 1, "ouch")
|
|
|
|
)
|
2003-01-19 22:34:07 -04:00
|
|
|
# Use the correct exception
|
2005-11-17 14:51:34 -04:00
|
|
|
cs = (0, 1, 9, 10, 99, 100, 999, 1000, 9999, 10000, 0x3042)
|
|
|
|
s = "".join(unichr(c) for c in cs)
|
2003-01-19 22:34:07 -04:00
|
|
|
self.assertEquals(
|
2005-11-17 14:51:34 -04:00
|
|
|
codecs.xmlcharrefreplace_errors(
|
|
|
|
UnicodeEncodeError("ascii", s, 0, len(s), "ouch")
|
|
|
|
),
|
|
|
|
(u"".join(u"&#%d;" % ord(c) for c in s), len(s))
|
2003-01-19 22:34:07 -04:00
|
|
|
)
|
2002-09-02 10:14:32 -03:00
|
|
|
|
|
|
|
def test_badandgoodbackslashreplaceexceptions(self):
|
2003-01-19 22:34:07 -04:00
|
|
|
# "backslashreplace" complains about a non-exception passed in
|
2002-09-02 10:14:32 -03:00
|
|
|
self.assertRaises(
|
|
|
|
TypeError,
|
|
|
|
codecs.backslashreplace_errors,
|
|
|
|
42
|
|
|
|
)
|
2003-01-19 22:34:07 -04:00
|
|
|
# "backslashreplace" complains about the wrong exception types
|
2002-09-02 10:14:32 -03:00
|
|
|
self.assertRaises(
|
|
|
|
TypeError,
|
|
|
|
codecs.backslashreplace_errors,
|
|
|
|
UnicodeError("ouch")
|
|
|
|
)
|
2003-01-19 22:34:07 -04:00
|
|
|
# "backslashreplace" can only be used for encoding
|
|
|
|
self.assertRaises(
|
|
|
|
TypeError,
|
|
|
|
codecs.backslashreplace_errors,
|
|
|
|
UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch")
|
|
|
|
)
|
|
|
|
self.assertRaises(
|
|
|
|
TypeError,
|
|
|
|
codecs.backslashreplace_errors,
|
|
|
|
UnicodeTranslateError(u"\u3042", 0, 1, "ouch")
|
|
|
|
)
|
|
|
|
# Use the correct exception
|
2002-09-02 10:14:32 -03:00
|
|
|
self.assertEquals(
|
|
|
|
codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")),
|
|
|
|
(u"\\u3042", 1)
|
|
|
|
)
|
|
|
|
self.assertEquals(
|
|
|
|
codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\x00", 0, 1, "ouch")),
|
|
|
|
(u"\\x00", 1)
|
|
|
|
)
|
|
|
|
self.assertEquals(
|
|
|
|
codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\xff", 0, 1, "ouch")),
|
|
|
|
(u"\\xff", 1)
|
|
|
|
)
|
|
|
|
self.assertEquals(
|
|
|
|
codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\u0100", 0, 1, "ouch")),
|
|
|
|
(u"\\u0100", 1)
|
|
|
|
)
|
|
|
|
self.assertEquals(
|
|
|
|
codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\uffff", 0, 1, "ouch")),
|
|
|
|
(u"\\uffff", 1)
|
|
|
|
)
|
|
|
|
if sys.maxunicode>0xffff:
|
|
|
|
self.assertEquals(
|
|
|
|
codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\U00010000", 0, 1, "ouch")),
|
|
|
|
(u"\\U00010000", 1)
|
|
|
|
)
|
|
|
|
self.assertEquals(
|
|
|
|
codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\U0010ffff", 0, 1, "ouch")),
|
|
|
|
(u"\\U0010ffff", 1)
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_badhandlerresults(self):
|
|
|
|
results = ( 42, u"foo", (1,2,3), (u"foo", 1, 3), (u"foo", None), (u"foo",), ("foo", 1, 3), ("foo", None), ("foo",) )
|
|
|
|
encs = ("ascii", "latin-1", "iso-8859-1", "iso-8859-15")
|
|
|
|
|
|
|
|
for res in results:
|
2009-01-18 17:11:38 -04:00
|
|
|
codecs.register_error("test.badhandler", lambda x: res)
|
2002-09-02 10:14:32 -03:00
|
|
|
for enc in encs:
|
|
|
|
self.assertRaises(
|
|
|
|
TypeError,
|
|
|
|
u"\u3042".encode,
|
|
|
|
enc,
|
|
|
|
"test.badhandler"
|
|
|
|
)
|
|
|
|
for (enc, bytes) in (
|
|
|
|
("ascii", "\xff"),
|
|
|
|
("utf-8", "\xff"),
|
2005-08-30 07:23:14 -03:00
|
|
|
("utf-7", "+x-"),
|
|
|
|
("unicode-internal", "\x00"),
|
2002-09-02 10:14:32 -03:00
|
|
|
):
|
|
|
|
self.assertRaises(
|
|
|
|
TypeError,
|
|
|
|
bytes.decode,
|
|
|
|
enc,
|
|
|
|
"test.badhandler"
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_lookup(self):
|
|
|
|
self.assertEquals(codecs.strict_errors, codecs.lookup_error("strict"))
|
|
|
|
self.assertEquals(codecs.ignore_errors, codecs.lookup_error("ignore"))
|
|
|
|
self.assertEquals(codecs.strict_errors, codecs.lookup_error("strict"))
|
|
|
|
self.assertEquals(
|
|
|
|
codecs.xmlcharrefreplace_errors,
|
|
|
|
codecs.lookup_error("xmlcharrefreplace")
|
|
|
|
)
|
|
|
|
self.assertEquals(
|
|
|
|
codecs.backslashreplace_errors,
|
|
|
|
codecs.lookup_error("backslashreplace")
|
|
|
|
)
|
|
|
|
|
2002-09-06 14:21:40 -03:00
|
|
|
def test_unencodablereplacement(self):
|
|
|
|
def unencrepl(exc):
|
|
|
|
if isinstance(exc, UnicodeEncodeError):
|
|
|
|
return (u"\u4242", exc.end)
|
|
|
|
else:
|
|
|
|
raise TypeError("don't know how to handle %r" % exc)
|
|
|
|
codecs.register_error("test.unencreplhandler", unencrepl)
|
|
|
|
for enc in ("ascii", "iso-8859-1", "iso-8859-15"):
|
|
|
|
self.assertRaises(
|
|
|
|
UnicodeEncodeError,
|
|
|
|
u"\u4242".encode,
|
|
|
|
enc,
|
|
|
|
"test.unencreplhandler"
|
|
|
|
)
|
|
|
|
|
2003-01-08 19:22:13 -04:00
|
|
|
def test_badregistercall(self):
|
|
|
|
# enhance coverage of:
|
|
|
|
# Modules/_codecsmodule.c::register_error()
|
|
|
|
# Python/codecs.c::PyCodec_RegisterError()
|
|
|
|
self.assertRaises(TypeError, codecs.register_error, 42)
|
|
|
|
self.assertRaises(TypeError, codecs.register_error, "test.dummy", 42)
|
|
|
|
|
2005-11-17 04:52:34 -04:00
|
|
|
def test_badlookupcall(self):
|
|
|
|
# enhance coverage of:
|
|
|
|
# Modules/_codecsmodule.c::lookup_error()
|
|
|
|
self.assertRaises(TypeError, codecs.lookup_error)
|
|
|
|
|
2003-01-08 19:22:13 -04:00
|
|
|
def test_unknownhandler(self):
|
|
|
|
# enhance coverage of:
|
|
|
|
# Modules/_codecsmodule.c::lookup_error()
|
|
|
|
self.assertRaises(LookupError, codecs.lookup_error, "test.unknown")
|
|
|
|
|
|
|
|
def test_xmlcharrefvalues(self):
|
|
|
|
# enhance coverage of:
|
|
|
|
# Python/codecs.c::PyCodec_XMLCharRefReplaceErrors()
|
|
|
|
# and inline implementations
|
|
|
|
v = (1, 5, 10, 50, 100, 500, 1000, 5000, 10000, 50000)
|
2003-01-09 07:38:50 -04:00
|
|
|
if sys.maxunicode>=100000:
|
2003-02-18 22:35:07 -04:00
|
|
|
v += (100000, 500000, 1000000)
|
2003-01-08 19:22:13 -04:00
|
|
|
s = u"".join([unichr(x) for x in v])
|
|
|
|
codecs.register_error("test.xmlcharrefreplace", codecs.xmlcharrefreplace_errors)
|
|
|
|
for enc in ("ascii", "iso-8859-15"):
|
|
|
|
for err in ("xmlcharrefreplace", "test.xmlcharrefreplace"):
|
|
|
|
s.encode(enc, err)
|
|
|
|
|
|
|
|
def test_decodehelper(self):
|
|
|
|
# enhance coverage of:
|
|
|
|
# Objects/unicodeobject.c::unicode_decode_call_errorhandler()
|
|
|
|
# and callers
|
|
|
|
self.assertRaises(LookupError, "\xff".decode, "ascii", "test.unknown")
|
|
|
|
|
|
|
|
def baddecodereturn1(exc):
|
|
|
|
return 42
|
|
|
|
codecs.register_error("test.baddecodereturn1", baddecodereturn1)
|
|
|
|
self.assertRaises(TypeError, "\xff".decode, "ascii", "test.baddecodereturn1")
|
|
|
|
self.assertRaises(TypeError, "\\".decode, "unicode-escape", "test.baddecodereturn1")
|
|
|
|
self.assertRaises(TypeError, "\\x0".decode, "unicode-escape", "test.baddecodereturn1")
|
|
|
|
self.assertRaises(TypeError, "\\x0y".decode, "unicode-escape", "test.baddecodereturn1")
|
|
|
|
self.assertRaises(TypeError, "\\Uffffeeee".decode, "unicode-escape", "test.baddecodereturn1")
|
|
|
|
self.assertRaises(TypeError, "\\uyyyy".decode, "raw-unicode-escape", "test.baddecodereturn1")
|
|
|
|
|
|
|
|
def baddecodereturn2(exc):
|
|
|
|
return (u"?", None)
|
|
|
|
codecs.register_error("test.baddecodereturn2", baddecodereturn2)
|
|
|
|
self.assertRaises(TypeError, "\xff".decode, "ascii", "test.baddecodereturn2")
|
|
|
|
|
2003-01-31 13:19:08 -04:00
|
|
|
handler = PosReturn()
|
|
|
|
codecs.register_error("test.posreturn", handler.handle)
|
|
|
|
|
|
|
|
# Valid negative position
|
|
|
|
handler.pos = -1
|
|
|
|
self.assertEquals("\xff0".decode("ascii", "test.posreturn"), u"<?>0")
|
|
|
|
|
|
|
|
# Valid negative position
|
|
|
|
handler.pos = -2
|
|
|
|
self.assertEquals("\xff0".decode("ascii", "test.posreturn"), u"<?><?>")
|
|
|
|
|
|
|
|
# Negative position out of bounds
|
|
|
|
handler.pos = -3
|
|
|
|
self.assertRaises(IndexError, "\xff0".decode, "ascii", "test.posreturn")
|
|
|
|
|
|
|
|
# Valid positive position
|
|
|
|
handler.pos = 1
|
|
|
|
self.assertEquals("\xff0".decode("ascii", "test.posreturn"), u"<?>0")
|
|
|
|
|
2004-12-14 17:28:07 -04:00
|
|
|
# Largest valid positive position (one beyond end of input)
|
2003-01-31 13:19:08 -04:00
|
|
|
handler.pos = 2
|
|
|
|
self.assertEquals("\xff0".decode("ascii", "test.posreturn"), u"<?>")
|
|
|
|
|
|
|
|
# Invalid positive position
|
|
|
|
handler.pos = 3
|
|
|
|
self.assertRaises(IndexError, "\xff0".decode, "ascii", "test.posreturn")
|
2003-01-08 19:22:13 -04:00
|
|
|
|
2003-01-31 13:19:08 -04:00
|
|
|
# Restart at the "0"
|
|
|
|
handler.pos = 6
|
|
|
|
self.assertEquals("\\uyyyy0".decode("raw-unicode-escape", "test.posreturn"), u"<?>0")
|
2003-01-08 19:22:13 -04:00
|
|
|
|
|
|
|
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})
|
|
|
|
|
|
|
|
def test_encodehelper(self):
|
|
|
|
# enhance coverage of:
|
|
|
|
# Objects/unicodeobject.c::unicode_encode_call_errorhandler()
|
|
|
|
# and callers
|
|
|
|
self.assertRaises(LookupError, u"\xff".encode, "ascii", "test.unknown")
|
|
|
|
|
|
|
|
def badencodereturn1(exc):
|
|
|
|
return 42
|
|
|
|
codecs.register_error("test.badencodereturn1", badencodereturn1)
|
|
|
|
self.assertRaises(TypeError, u"\xff".encode, "ascii", "test.badencodereturn1")
|
|
|
|
|
|
|
|
def badencodereturn2(exc):
|
|
|
|
return (u"?", None)
|
|
|
|
codecs.register_error("test.badencodereturn2", badencodereturn2)
|
|
|
|
self.assertRaises(TypeError, u"\xff".encode, "ascii", "test.badencodereturn2")
|
|
|
|
|
2003-01-31 13:19:08 -04:00
|
|
|
handler = PosReturn()
|
|
|
|
codecs.register_error("test.posreturn", handler.handle)
|
|
|
|
|
|
|
|
# Valid negative position
|
|
|
|
handler.pos = -1
|
|
|
|
self.assertEquals(u"\xff0".encode("ascii", "test.posreturn"), "<?>0")
|
|
|
|
|
|
|
|
# Valid negative position
|
|
|
|
handler.pos = -2
|
|
|
|
self.assertEquals(u"\xff0".encode("ascii", "test.posreturn"), "<?><?>")
|
|
|
|
|
|
|
|
# Negative position out of bounds
|
|
|
|
handler.pos = -3
|
|
|
|
self.assertRaises(IndexError, u"\xff0".encode, "ascii", "test.posreturn")
|
|
|
|
|
|
|
|
# Valid positive position
|
|
|
|
handler.pos = 1
|
|
|
|
self.assertEquals(u"\xff0".encode("ascii", "test.posreturn"), "<?>0")
|
|
|
|
|
|
|
|
# Largest valid positive position (one beyond end of input
|
|
|
|
handler.pos = 2
|
|
|
|
self.assertEquals(u"\xff0".encode("ascii", "test.posreturn"), "<?>")
|
|
|
|
|
|
|
|
# Invalid positive position
|
|
|
|
handler.pos = 3
|
|
|
|
self.assertRaises(IndexError, u"\xff0".encode, "ascii", "test.posreturn")
|
2003-01-08 19:22:13 -04:00
|
|
|
|
2003-01-31 13:19:08 -04:00
|
|
|
handler.pos = 0
|
2003-01-08 19:22:13 -04:00
|
|
|
|
|
|
|
class D(dict):
|
|
|
|
def __getitem__(self, key):
|
|
|
|
raise ValueError
|
2003-01-31 13:19:08 -04:00
|
|
|
for err in ("strict", "replace", "xmlcharrefreplace", "backslashreplace", "test.posreturn"):
|
2003-01-08 19:22:13 -04:00
|
|
|
self.assertRaises(UnicodeError, codecs.charmap_encode, u"\xff", err, {0xff: None})
|
|
|
|
self.assertRaises(ValueError, codecs.charmap_encode, u"\xff", err, D())
|
|
|
|
self.assertRaises(TypeError, codecs.charmap_encode, u"\xff", err, {0xff: 300})
|
|
|
|
|
|
|
|
def test_translatehelper(self):
|
|
|
|
# enhance coverage of:
|
|
|
|
# Objects/unicodeobject.c::unicode_encode_call_errorhandler()
|
|
|
|
# and callers
|
|
|
|
# (Unfortunately the errors argument is not directly accessible
|
|
|
|
# from Python, so we can't test that much)
|
|
|
|
class D(dict):
|
|
|
|
def __getitem__(self, key):
|
|
|
|
raise ValueError
|
|
|
|
self.assertRaises(ValueError, u"\xff".translate, D())
|
|
|
|
self.assertRaises(TypeError, u"\xff".translate, {0xff: sys.maxunicode+1})
|
|
|
|
self.assertRaises(TypeError, u"\xff".translate, {0xff: ()})
|
|
|
|
|
2003-10-24 11:25:28 -03:00
|
|
|
def test_bug828737(self):
|
|
|
|
charmap = {
|
|
|
|
ord("&"): u"&",
|
|
|
|
ord("<"): u"<",
|
|
|
|
ord(">"): u">",
|
|
|
|
ord('"'): u""",
|
|
|
|
}
|
2004-01-18 16:29:55 -04:00
|
|
|
|
2003-10-24 11:25:28 -03:00
|
|
|
for n in (1, 10, 100, 1000):
|
|
|
|
text = u'abc<def>ghi'*n
|
|
|
|
text.translate(charmap)
|
|
|
|
|
2002-09-02 10:14:32 -03:00
|
|
|
def test_main():
|
2003-05-01 14:45:56 -03:00
|
|
|
test.test_support.run_unittest(CodecCallbackTest)
|
2002-09-02 10:14:32 -03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|