Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1 | import test.test_support, unittest |
Fred Drake | d995e11 | 2008-05-20 06:08:38 +0000 | [diff] [blame] | 2 | import sys, codecs, htmlentitydefs, unicodedata |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3 | |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 4 | class PosReturn: |
| 5 | # this can be used for configurable callbacks |
| 6 | |
| 7 | def __init__(self): |
| 8 | self.pos = 0 |
| 9 | |
| 10 | def handle(self, exc): |
| 11 | oldpos = self.pos |
| 12 | realpos = oldpos |
| 13 | if realpos<0: |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 14 | realpos = len(exc.object) + realpos |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 15 | # if we don't advance this time, terminate on the next call |
| 16 | # otherwise we'd get an endless loop |
| 17 | if realpos <= exc.start: |
| 18 | self.pos = len(exc.object) |
| 19 | return (u"<?>", oldpos) |
| 20 | |
Walter Dörwald | 690402f | 2005-11-17 18:51:34 +0000 | [diff] [blame] | 21 | # A UnicodeEncodeError object with a bad start attribute |
| 22 | class BadStartUnicodeEncodeError(UnicodeEncodeError): |
| 23 | def __init__(self): |
| 24 | UnicodeEncodeError.__init__(self, "ascii", u"", 0, 1, "bad") |
| 25 | self.start = [] |
| 26 | |
Walter Dörwald | 690402f | 2005-11-17 18:51:34 +0000 | [diff] [blame] | 27 | # A UnicodeEncodeError object with a bad object attribute |
| 28 | class BadObjectUnicodeEncodeError(UnicodeEncodeError): |
| 29 | def __init__(self): |
| 30 | UnicodeEncodeError.__init__(self, "ascii", u"", 0, 1, "bad") |
| 31 | self.object = [] |
| 32 | |
| 33 | # A UnicodeDecodeError object without an end attribute |
| 34 | class NoEndUnicodeDecodeError(UnicodeDecodeError): |
| 35 | def __init__(self): |
| 36 | UnicodeDecodeError.__init__(self, "ascii", "", 0, 1, "bad") |
| 37 | del self.end |
| 38 | |
| 39 | # A UnicodeDecodeError object with a bad object attribute |
| 40 | class BadObjectUnicodeDecodeError(UnicodeDecodeError): |
| 41 | def __init__(self): |
| 42 | UnicodeDecodeError.__init__(self, "ascii", "", 0, 1, "bad") |
| 43 | self.object = [] |
| 44 | |
| 45 | # A UnicodeTranslateError object without a start attribute |
| 46 | class NoStartUnicodeTranslateError(UnicodeTranslateError): |
| 47 | def __init__(self): |
| 48 | UnicodeTranslateError.__init__(self, u"", 0, 1, "bad") |
| 49 | del self.start |
| 50 | |
| 51 | # A UnicodeTranslateError object without an end attribute |
| 52 | class NoEndUnicodeTranslateError(UnicodeTranslateError): |
| 53 | def __init__(self): |
| 54 | UnicodeTranslateError.__init__(self, u"", 0, 1, "bad") |
| 55 | del self.end |
| 56 | |
| 57 | # A UnicodeTranslateError object without an object attribute |
| 58 | class NoObjectUnicodeTranslateError(UnicodeTranslateError): |
| 59 | def __init__(self): |
| 60 | UnicodeTranslateError.__init__(self, u"", 0, 1, "bad") |
| 61 | del self.object |
| 62 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 63 | class CodecCallbackTest(unittest.TestCase): |
| 64 | |
| 65 | def test_xmlcharrefreplace(self): |
| 66 | # replace unencodable characters which numeric character entities. |
| 67 | # For ascii, latin-1 and charmaps this is completely implemented |
| 68 | # in C and should be reasonably fast. |
| 69 | s = u"\u30b9\u30d1\u30e2 \xe4nd eggs" |
| 70 | self.assertEqual( |
| 71 | s.encode("ascii", "xmlcharrefreplace"), |
| 72 | "スパモ änd eggs" |
| 73 | ) |
| 74 | self.assertEqual( |
| 75 | s.encode("latin-1", "xmlcharrefreplace"), |
| 76 | "スパモ \xe4nd eggs" |
| 77 | ) |
| 78 | |
| 79 | def test_xmlcharnamereplace(self): |
| 80 | # This time use a named character entity for unencodable |
| 81 | # characters, if one is available. |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 82 | |
| 83 | def xmlcharnamereplace(exc): |
| 84 | if not isinstance(exc, UnicodeEncodeError): |
| 85 | raise TypeError("don't know how to handle %r" % exc) |
| 86 | l = [] |
| 87 | for c in exc.object[exc.start:exc.end]: |
| 88 | try: |
Fred Drake | d995e11 | 2008-05-20 06:08:38 +0000 | [diff] [blame] | 89 | l.append(u"&%s;" % htmlentitydefs.codepoint2name[ord(c)]) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 90 | except KeyError: |
| 91 | l.append(u"&#%d;" % ord(c)) |
| 92 | return (u"".join(l), exc.end) |
| 93 | |
| 94 | codecs.register_error( |
| 95 | "test.xmlcharnamereplace", xmlcharnamereplace) |
| 96 | |
| 97 | sin = u"\xab\u211c\xbb = \u2329\u1234\u20ac\u232a" |
| 98 | sout = "«ℜ» = ⟨ሴ€⟩" |
| 99 | self.assertEqual(sin.encode("ascii", "test.xmlcharnamereplace"), sout) |
| 100 | sout = "\xabℜ\xbb = ⟨ሴ€⟩" |
| 101 | self.assertEqual(sin.encode("latin-1", "test.xmlcharnamereplace"), sout) |
| 102 | sout = "\xabℜ\xbb = ⟨ሴ\xa4⟩" |
| 103 | self.assertEqual(sin.encode("iso-8859-15", "test.xmlcharnamereplace"), sout) |
| 104 | |
| 105 | def test_uninamereplace(self): |
| 106 | # We're using the names from the unicode database this time, |
Walter Dörwald | 00445d2 | 2002-11-25 17:58:02 +0000 | [diff] [blame] | 107 | # and we're doing "syntax highlighting" here, i.e. we include |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 108 | # the replaced text in ANSI escape sequences. For this it is |
| 109 | # useful that the error handler is not called for every single |
| 110 | # unencodable character, but for a complete sequence of |
| 111 | # unencodable characters, otherwise we would output many |
Jesus Cea | 585ad8a | 2009-07-02 15:37:21 +0000 | [diff] [blame] | 112 | # unnecessary escape sequences. |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 113 | |
| 114 | def uninamereplace(exc): |
| 115 | if not isinstance(exc, UnicodeEncodeError): |
| 116 | raise TypeError("don't know how to handle %r" % exc) |
| 117 | l = [] |
| 118 | for c in exc.object[exc.start:exc.end]: |
| 119 | l.append(unicodedata.name(c, u"0x%x" % ord(c))) |
| 120 | return (u"\033[1m%s\033[0m" % u", ".join(l), exc.end) |
| 121 | |
| 122 | codecs.register_error( |
| 123 | "test.uninamereplace", uninamereplace) |
| 124 | |
| 125 | sin = u"\xac\u1234\u20ac\u8000" |
Martin v. Löwis | 74a530d | 2002-11-23 19:41:01 +0000 | [diff] [blame] | 126 | sout = "\033[1mNOT SIGN, ETHIOPIC SYLLABLE SEE, EURO SIGN, CJK UNIFIED IDEOGRAPH-8000\033[0m" |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 127 | self.assertEqual(sin.encode("ascii", "test.uninamereplace"), sout) |
| 128 | |
Martin v. Löwis | 74a530d | 2002-11-23 19:41:01 +0000 | [diff] [blame] | 129 | sout = "\xac\033[1mETHIOPIC SYLLABLE SEE, EURO SIGN, CJK UNIFIED IDEOGRAPH-8000\033[0m" |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 130 | self.assertEqual(sin.encode("latin-1", "test.uninamereplace"), sout) |
| 131 | |
Martin v. Löwis | 74a530d | 2002-11-23 19:41:01 +0000 | [diff] [blame] | 132 | sout = "\xac\033[1mETHIOPIC SYLLABLE SEE\033[0m\xa4\033[1mCJK UNIFIED IDEOGRAPH-8000\033[0m" |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 133 | self.assertEqual(sin.encode("iso-8859-15", "test.uninamereplace"), sout) |
| 134 | |
| 135 | def test_backslashescape(self): |
| 136 | # Does the same as the "unicode-escape" encoding, but with different |
| 137 | # base encodings. |
| 138 | sin = u"a\xac\u1234\u20ac\u8000" |
| 139 | if sys.maxunicode > 0xffff: |
| 140 | sin += unichr(sys.maxunicode) |
| 141 | sout = "a\\xac\\u1234\\u20ac\\u8000" |
| 142 | if sys.maxunicode > 0xffff: |
| 143 | sout += "\\U%08x" % sys.maxunicode |
| 144 | self.assertEqual(sin.encode("ascii", "backslashreplace"), sout) |
| 145 | |
| 146 | sout = "a\xac\\u1234\\u20ac\\u8000" |
| 147 | if sys.maxunicode > 0xffff: |
| 148 | sout += "\\U%08x" % sys.maxunicode |
| 149 | self.assertEqual(sin.encode("latin-1", "backslashreplace"), sout) |
| 150 | |
| 151 | sout = "a\xac\\u1234\xa4\\u8000" |
| 152 | if sys.maxunicode > 0xffff: |
| 153 | sout += "\\U%08x" % sys.maxunicode |
| 154 | self.assertEqual(sin.encode("iso-8859-15", "backslashreplace"), sout) |
| 155 | |
Ezio Melotti | 86e5e17 | 2010-07-03 05:34:39 +0000 | [diff] [blame] | 156 | def test_decoding_callbacks(self): |
| 157 | # This is a test for a decoding callback handler |
| 158 | # that allows the decoding of the invalid sequence |
| 159 | # "\xc0\x80" and returns "\x00" instead of raising an error. |
| 160 | # All other illegal sequences will be handled strictly. |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 161 | def relaxedutf8(exc): |
| 162 | if not isinstance(exc, UnicodeDecodeError): |
| 163 | raise TypeError("don't know how to handle %r" % exc) |
Ezio Melotti | 86e5e17 | 2010-07-03 05:34:39 +0000 | [diff] [blame] | 164 | if exc.object[exc.start:exc.start+2] == "\xc0\x80": |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 165 | return (u"\x00", exc.start+2) # retry after two bytes |
| 166 | else: |
| 167 | raise exc |
| 168 | |
Ezio Melotti | 86e5e17 | 2010-07-03 05:34:39 +0000 | [diff] [blame] | 169 | codecs.register_error("test.relaxedutf8", relaxedutf8) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 170 | |
Ezio Melotti | 86e5e17 | 2010-07-03 05:34:39 +0000 | [diff] [blame] | 171 | # all the "\xc0\x80" will be decoded to "\x00" |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 172 | sin = "a\x00b\xc0\x80c\xc3\xbc\xc0\x80\xc0\x80" |
| 173 | sout = u"a\x00b\x00c\xfc\x00\x00" |
| 174 | self.assertEqual(sin.decode("utf-8", "test.relaxedutf8"), sout) |
Ezio Melotti | 86e5e17 | 2010-07-03 05:34:39 +0000 | [diff] [blame] | 175 | |
| 176 | # "\xc0\x81" is not valid and a UnicodeDecodeError will be raised |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 177 | sin = "\xc0\x80\xc0\x81" |
Ezio Melotti | 86e5e17 | 2010-07-03 05:34:39 +0000 | [diff] [blame] | 178 | self.assertRaises(UnicodeDecodeError, sin.decode, |
| 179 | "utf-8", "test.relaxedutf8") |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 180 | |
| 181 | def test_charmapencode(self): |
| 182 | # For charmap encodings the replacement string will be |
| 183 | # mapped through the encoding again. This means, that |
| 184 | # to be able to use e.g. the "replace" handler, the |
| 185 | # charmap has to have a mapping for "?". |
| 186 | charmap = dict([ (ord(c), 2*c.upper()) for c in "abcdefgh"]) |
| 187 | sin = u"abc" |
| 188 | sout = "AABBCC" |
| 189 | self.assertEquals(codecs.charmap_encode(sin, "strict", charmap)[0], sout) |
| 190 | |
| 191 | sin = u"abcA" |
| 192 | self.assertRaises(UnicodeError, codecs.charmap_encode, sin, "strict", charmap) |
| 193 | |
| 194 | charmap[ord("?")] = "XYZ" |
| 195 | sin = u"abcDEF" |
| 196 | sout = "AABBCCXYZXYZXYZ" |
| 197 | self.assertEquals(codecs.charmap_encode(sin, "replace", charmap)[0], sout) |
| 198 | |
| 199 | charmap[ord("?")] = u"XYZ" |
| 200 | self.assertRaises(TypeError, codecs.charmap_encode, sin, "replace", charmap) |
| 201 | |
| 202 | charmap[ord("?")] = u"XYZ" |
| 203 | self.assertRaises(TypeError, codecs.charmap_encode, sin, "replace", charmap) |
| 204 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 205 | def test_decodeunicodeinternal(self): |
| 206 | self.assertRaises( |
| 207 | UnicodeDecodeError, |
| 208 | "\x00\x00\x00\x00\x00".decode, |
| 209 | "unicode-internal", |
| 210 | ) |
| 211 | if sys.maxunicode > 0xffff: |
| 212 | def handler_unicodeinternal(exc): |
| 213 | if not isinstance(exc, UnicodeDecodeError): |
| 214 | raise TypeError("don't know how to handle %r" % exc) |
| 215 | return (u"\x01", 1) |
| 216 | |
| 217 | self.assertEqual( |
| 218 | "\x00\x00\x00\x00\x00".decode("unicode-internal", "ignore"), |
| 219 | u"\u0000" |
| 220 | ) |
| 221 | |
| 222 | self.assertEqual( |
| 223 | "\x00\x00\x00\x00\x00".decode("unicode-internal", "replace"), |
| 224 | u"\u0000\ufffd" |
| 225 | ) |
| 226 | |
| 227 | codecs.register_error("test.hui", handler_unicodeinternal) |
| 228 | |
| 229 | self.assertEqual( |
| 230 | "\x00\x00\x00\x00\x00".decode("unicode-internal", "test.hui"), |
| 231 | u"\u0000\u0001\u0000" |
| 232 | ) |
| 233 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 234 | def test_callbacks(self): |
| 235 | def handler1(exc): |
| 236 | if not isinstance(exc, UnicodeEncodeError) \ |
| 237 | and not isinstance(exc, UnicodeDecodeError): |
| 238 | raise TypeError("don't know how to handle %r" % exc) |
| 239 | l = [u"<%d>" % ord(exc.object[pos]) for pos in xrange(exc.start, exc.end)] |
| 240 | return (u"[%s]" % u"".join(l), exc.end) |
| 241 | |
| 242 | codecs.register_error("test.handler1", handler1) |
| 243 | |
| 244 | def handler2(exc): |
| 245 | if not isinstance(exc, UnicodeDecodeError): |
| 246 | raise TypeError("don't know how to handle %r" % exc) |
| 247 | l = [u"<%d>" % ord(exc.object[pos]) for pos in xrange(exc.start, exc.end)] |
| 248 | return (u"[%s]" % u"".join(l), exc.end+1) # skip one character |
| 249 | |
| 250 | codecs.register_error("test.handler2", handler2) |
| 251 | |
| 252 | s = "\x00\x81\x7f\x80\xff" |
| 253 | |
| 254 | self.assertEqual( |
| 255 | s.decode("ascii", "test.handler1"), |
| 256 | u"\x00[<129>]\x7f[<128>][<255>]" |
| 257 | ) |
| 258 | self.assertEqual( |
| 259 | s.decode("ascii", "test.handler2"), |
| 260 | u"\x00[<129>][<128>]" |
| 261 | ) |
| 262 | |
| 263 | self.assertEqual( |
| 264 | "\\u3042\u3xxx".decode("unicode-escape", "test.handler1"), |
| 265 | u"\u3042[<92><117><51><120>]xx" |
| 266 | ) |
| 267 | |
| 268 | self.assertEqual( |
| 269 | "\\u3042\u3xx".decode("unicode-escape", "test.handler1"), |
| 270 | u"\u3042[<92><117><51><120><120>]" |
| 271 | ) |
| 272 | |
| 273 | self.assertEqual( |
| 274 | codecs.charmap_decode("abc", "test.handler1", {ord("a"): u"z"})[0], |
| 275 | u"z[<98>][<99>]" |
| 276 | ) |
| 277 | |
| 278 | self.assertEqual( |
| 279 | u"g\xfc\xdfrk".encode("ascii", "test.handler1"), |
| 280 | u"g[<252><223>]rk" |
| 281 | ) |
| 282 | |
| 283 | self.assertEqual( |
| 284 | u"g\xfc\xdf".encode("ascii", "test.handler1"), |
| 285 | u"g[<252><223>]" |
| 286 | ) |
| 287 | |
| 288 | def test_longstrings(self): |
| 289 | # test long strings to check for memory overflow problems |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 290 | errors = [ "strict", "ignore", "replace", "xmlcharrefreplace", |
| 291 | "backslashreplace"] |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 292 | # register the handlers under different names, |
| 293 | # to prevent the codec from recognizing the name |
| 294 | for err in errors: |
| 295 | codecs.register_error("test." + err, codecs.lookup_error(err)) |
| 296 | l = 1000 |
| 297 | errors += [ "test." + err for err in errors ] |
| 298 | for uni in [ s*l for s in (u"x", u"\u3042", u"a\xe4") ]: |
Walter Dörwald | 6e39080 | 2007-08-17 16:41:28 +0000 | [diff] [blame] | 299 | for enc in ("ascii", "latin-1", "iso-8859-1", "iso-8859-15", |
| 300 | "utf-8", "utf-7", "utf-16", "utf-32"): |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 301 | for err in errors: |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 302 | try: |
| 303 | uni.encode(enc, err) |
| 304 | except UnicodeError: |
| 305 | pass |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 306 | |
| 307 | def check_exceptionobjectargs(self, exctype, args, msg): |
| 308 | # Test UnicodeError subclasses: construction, attribute assignment and __str__ conversion |
| 309 | # check with one missing argument |
| 310 | self.assertRaises(TypeError, exctype, *args[:-1]) |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 311 | # check with one argument too much |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 312 | self.assertRaises(TypeError, exctype, *(args + ["too much"])) |
| 313 | # check with one argument of the wrong type |
| 314 | wrongargs = [ "spam", u"eggs", 42, 1.0, None ] |
| 315 | for i in xrange(len(args)): |
| 316 | for wrongarg in wrongargs: |
| 317 | if type(wrongarg) is type(args[i]): |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 318 | continue |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 319 | # build argument array |
| 320 | callargs = [] |
| 321 | for j in xrange(len(args)): |
| 322 | if i==j: |
| 323 | callargs.append(wrongarg) |
| 324 | else: |
| 325 | callargs.append(args[i]) |
| 326 | self.assertRaises(TypeError, exctype, *callargs) |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 327 | |
| 328 | # check with the correct number and type of arguments |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 329 | exc = exctype(*args) |
| 330 | self.assertEquals(str(exc), msg) |
| 331 | |
| 332 | def test_unicodeencodeerror(self): |
| 333 | self.check_exceptionobjectargs( |
| 334 | UnicodeEncodeError, |
| 335 | ["ascii", u"g\xfcrk", 1, 2, "ouch"], |
Walter Dörwald | a54b92b | 2003-08-12 17:34:49 +0000 | [diff] [blame] | 336 | "'ascii' codec can't encode character u'\\xfc' in position 1: ouch" |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 337 | ) |
| 338 | self.check_exceptionobjectargs( |
| 339 | UnicodeEncodeError, |
| 340 | ["ascii", u"g\xfcrk", 1, 4, "ouch"], |
| 341 | "'ascii' codec can't encode characters in position 1-3: ouch" |
| 342 | ) |
| 343 | self.check_exceptionobjectargs( |
| 344 | UnicodeEncodeError, |
| 345 | ["ascii", u"\xfcx", 0, 1, "ouch"], |
Walter Dörwald | a54b92b | 2003-08-12 17:34:49 +0000 | [diff] [blame] | 346 | "'ascii' codec can't encode character u'\\xfc' in position 0: ouch" |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 347 | ) |
Walter Dörwald | fd196bd | 2003-08-12 17:32:43 +0000 | [diff] [blame] | 348 | self.check_exceptionobjectargs( |
| 349 | UnicodeEncodeError, |
| 350 | ["ascii", u"\u0100x", 0, 1, "ouch"], |
Walter Dörwald | a54b92b | 2003-08-12 17:34:49 +0000 | [diff] [blame] | 351 | "'ascii' codec can't encode character u'\\u0100' in position 0: ouch" |
Walter Dörwald | fd196bd | 2003-08-12 17:32:43 +0000 | [diff] [blame] | 352 | ) |
| 353 | self.check_exceptionobjectargs( |
| 354 | UnicodeEncodeError, |
| 355 | ["ascii", u"\uffffx", 0, 1, "ouch"], |
Walter Dörwald | a54b92b | 2003-08-12 17:34:49 +0000 | [diff] [blame] | 356 | "'ascii' codec can't encode character u'\\uffff' in position 0: ouch" |
Walter Dörwald | fd196bd | 2003-08-12 17:32:43 +0000 | [diff] [blame] | 357 | ) |
| 358 | if sys.maxunicode > 0xffff: |
| 359 | self.check_exceptionobjectargs( |
| 360 | UnicodeEncodeError, |
| 361 | ["ascii", u"\U00010000x", 0, 1, "ouch"], |
Walter Dörwald | a54b92b | 2003-08-12 17:34:49 +0000 | [diff] [blame] | 362 | "'ascii' codec can't encode character u'\\U00010000' in position 0: ouch" |
Walter Dörwald | fd196bd | 2003-08-12 17:32:43 +0000 | [diff] [blame] | 363 | ) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 364 | |
| 365 | def test_unicodedecodeerror(self): |
| 366 | self.check_exceptionobjectargs( |
| 367 | UnicodeDecodeError, |
| 368 | ["ascii", "g\xfcrk", 1, 2, "ouch"], |
| 369 | "'ascii' codec can't decode byte 0xfc in position 1: ouch" |
| 370 | ) |
| 371 | self.check_exceptionobjectargs( |
| 372 | UnicodeDecodeError, |
| 373 | ["ascii", "g\xfcrk", 1, 3, "ouch"], |
| 374 | "'ascii' codec can't decode bytes in position 1-2: ouch" |
| 375 | ) |
| 376 | |
| 377 | def test_unicodetranslateerror(self): |
| 378 | self.check_exceptionobjectargs( |
| 379 | UnicodeTranslateError, |
| 380 | [u"g\xfcrk", 1, 2, "ouch"], |
Walter Dörwald | a54b92b | 2003-08-12 17:34:49 +0000 | [diff] [blame] | 381 | "can't translate character u'\\xfc' in position 1: ouch" |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 382 | ) |
| 383 | self.check_exceptionobjectargs( |
| 384 | UnicodeTranslateError, |
Walter Dörwald | fd196bd | 2003-08-12 17:32:43 +0000 | [diff] [blame] | 385 | [u"g\u0100rk", 1, 2, "ouch"], |
Walter Dörwald | a54b92b | 2003-08-12 17:34:49 +0000 | [diff] [blame] | 386 | "can't translate character u'\\u0100' in position 1: ouch" |
Walter Dörwald | fd196bd | 2003-08-12 17:32:43 +0000 | [diff] [blame] | 387 | ) |
| 388 | self.check_exceptionobjectargs( |
| 389 | UnicodeTranslateError, |
| 390 | [u"g\uffffrk", 1, 2, "ouch"], |
Walter Dörwald | a54b92b | 2003-08-12 17:34:49 +0000 | [diff] [blame] | 391 | "can't translate character u'\\uffff' in position 1: ouch" |
Walter Dörwald | fd196bd | 2003-08-12 17:32:43 +0000 | [diff] [blame] | 392 | ) |
| 393 | if sys.maxunicode > 0xffff: |
| 394 | self.check_exceptionobjectargs( |
| 395 | UnicodeTranslateError, |
| 396 | [u"g\U00010000rk", 1, 2, "ouch"], |
Walter Dörwald | a54b92b | 2003-08-12 17:34:49 +0000 | [diff] [blame] | 397 | "can't translate character u'\\U00010000' in position 1: ouch" |
Walter Dörwald | fd196bd | 2003-08-12 17:32:43 +0000 | [diff] [blame] | 398 | ) |
| 399 | self.check_exceptionobjectargs( |
| 400 | UnicodeTranslateError, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 401 | [u"g\xfcrk", 1, 3, "ouch"], |
| 402 | "can't translate characters in position 1-2: ouch" |
| 403 | ) |
| 404 | |
| 405 | def test_badandgoodstrictexceptions(self): |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 406 | # "strict" complains about a non-exception passed in |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 407 | self.assertRaises( |
| 408 | TypeError, |
| 409 | codecs.strict_errors, |
| 410 | 42 |
| 411 | ) |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 412 | # "strict" complains about the wrong exception type |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 413 | self.assertRaises( |
| 414 | Exception, |
| 415 | codecs.strict_errors, |
| 416 | Exception("ouch") |
| 417 | ) |
| 418 | |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 419 | # If the correct exception is passed in, "strict" raises it |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 420 | self.assertRaises( |
| 421 | UnicodeEncodeError, |
| 422 | codecs.strict_errors, |
| 423 | UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch") |
| 424 | ) |
| 425 | |
| 426 | def test_badandgoodignoreexceptions(self): |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 427 | # "ignore" complains about a non-exception passed in |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 428 | self.assertRaises( |
| 429 | TypeError, |
| 430 | codecs.ignore_errors, |
| 431 | 42 |
| 432 | ) |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 433 | # "ignore" complains about the wrong exception type |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 434 | self.assertRaises( |
| 435 | TypeError, |
| 436 | codecs.ignore_errors, |
| 437 | UnicodeError("ouch") |
| 438 | ) |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 439 | # If the correct exception is passed in, "ignore" returns an empty replacement |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 440 | self.assertEquals( |
| 441 | codecs.ignore_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")), |
| 442 | (u"", 1) |
| 443 | ) |
| 444 | self.assertEquals( |
| 445 | codecs.ignore_errors(UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch")), |
| 446 | (u"", 1) |
| 447 | ) |
| 448 | self.assertEquals( |
| 449 | codecs.ignore_errors(UnicodeTranslateError(u"\u3042", 0, 1, "ouch")), |
| 450 | (u"", 1) |
| 451 | ) |
| 452 | |
| 453 | def test_badandgoodreplaceexceptions(self): |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 454 | # "replace" complains about a non-exception passed in |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 455 | self.assertRaises( |
| 456 | TypeError, |
| 457 | codecs.replace_errors, |
| 458 | 42 |
| 459 | ) |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 460 | # "replace" complains about the wrong exception type |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 461 | self.assertRaises( |
| 462 | TypeError, |
| 463 | codecs.replace_errors, |
| 464 | UnicodeError("ouch") |
| 465 | ) |
Walter Dörwald | 690402f | 2005-11-17 18:51:34 +0000 | [diff] [blame] | 466 | self.assertRaises( |
Walter Dörwald | 690402f | 2005-11-17 18:51:34 +0000 | [diff] [blame] | 467 | TypeError, |
| 468 | codecs.replace_errors, |
| 469 | BadObjectUnicodeEncodeError() |
| 470 | ) |
| 471 | self.assertRaises( |
Walter Dörwald | 690402f | 2005-11-17 18:51:34 +0000 | [diff] [blame] | 472 | TypeError, |
| 473 | codecs.replace_errors, |
| 474 | BadObjectUnicodeDecodeError() |
| 475 | ) |
Walter Dörwald | 29ddfba | 2004-12-14 21:28:07 +0000 | [diff] [blame] | 476 | # With the correct exception, "replace" returns an "?" or u"\ufffd" replacement |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 477 | self.assertEquals( |
| 478 | codecs.replace_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")), |
| 479 | (u"?", 1) |
| 480 | ) |
| 481 | self.assertEquals( |
| 482 | codecs.replace_errors(UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch")), |
| 483 | (u"\ufffd", 1) |
| 484 | ) |
| 485 | self.assertEquals( |
| 486 | codecs.replace_errors(UnicodeTranslateError(u"\u3042", 0, 1, "ouch")), |
| 487 | (u"\ufffd", 1) |
| 488 | ) |
| 489 | |
| 490 | def test_badandgoodxmlcharrefreplaceexceptions(self): |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 491 | # "xmlcharrefreplace" complains about a non-exception passed in |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 492 | self.assertRaises( |
| 493 | TypeError, |
| 494 | codecs.xmlcharrefreplace_errors, |
| 495 | 42 |
| 496 | ) |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 497 | # "xmlcharrefreplace" complains about the wrong exception types |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 498 | self.assertRaises( |
| 499 | TypeError, |
| 500 | codecs.xmlcharrefreplace_errors, |
| 501 | UnicodeError("ouch") |
| 502 | ) |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 503 | # "xmlcharrefreplace" can only be used for encoding |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 504 | self.assertRaises( |
| 505 | TypeError, |
| 506 | codecs.xmlcharrefreplace_errors, |
| 507 | UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch") |
| 508 | ) |
| 509 | self.assertRaises( |
| 510 | TypeError, |
| 511 | codecs.xmlcharrefreplace_errors, |
| 512 | UnicodeTranslateError(u"\u3042", 0, 1, "ouch") |
| 513 | ) |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 514 | # Use the correct exception |
Walter Dörwald | 690402f | 2005-11-17 18:51:34 +0000 | [diff] [blame] | 515 | cs = (0, 1, 9, 10, 99, 100, 999, 1000, 9999, 10000, 0x3042) |
| 516 | s = "".join(unichr(c) for c in cs) |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 517 | self.assertEquals( |
Walter Dörwald | 690402f | 2005-11-17 18:51:34 +0000 | [diff] [blame] | 518 | codecs.xmlcharrefreplace_errors( |
| 519 | UnicodeEncodeError("ascii", s, 0, len(s), "ouch") |
| 520 | ), |
| 521 | (u"".join(u"&#%d;" % ord(c) for c in s), len(s)) |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 522 | ) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 523 | |
| 524 | def test_badandgoodbackslashreplaceexceptions(self): |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 525 | # "backslashreplace" complains about a non-exception passed in |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 526 | self.assertRaises( |
| 527 | TypeError, |
| 528 | codecs.backslashreplace_errors, |
| 529 | 42 |
| 530 | ) |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 531 | # "backslashreplace" complains about the wrong exception types |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 532 | self.assertRaises( |
| 533 | TypeError, |
| 534 | codecs.backslashreplace_errors, |
| 535 | UnicodeError("ouch") |
| 536 | ) |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 537 | # "backslashreplace" can only be used for encoding |
| 538 | self.assertRaises( |
| 539 | TypeError, |
| 540 | codecs.backslashreplace_errors, |
| 541 | UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch") |
| 542 | ) |
| 543 | self.assertRaises( |
| 544 | TypeError, |
| 545 | codecs.backslashreplace_errors, |
| 546 | UnicodeTranslateError(u"\u3042", 0, 1, "ouch") |
| 547 | ) |
| 548 | # Use the correct exception |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 549 | self.assertEquals( |
| 550 | codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")), |
| 551 | (u"\\u3042", 1) |
| 552 | ) |
| 553 | self.assertEquals( |
| 554 | codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\x00", 0, 1, "ouch")), |
| 555 | (u"\\x00", 1) |
| 556 | ) |
| 557 | self.assertEquals( |
| 558 | codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\xff", 0, 1, "ouch")), |
| 559 | (u"\\xff", 1) |
| 560 | ) |
| 561 | self.assertEquals( |
| 562 | codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\u0100", 0, 1, "ouch")), |
| 563 | (u"\\u0100", 1) |
| 564 | ) |
| 565 | self.assertEquals( |
| 566 | codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\uffff", 0, 1, "ouch")), |
| 567 | (u"\\uffff", 1) |
| 568 | ) |
| 569 | if sys.maxunicode>0xffff: |
| 570 | self.assertEquals( |
| 571 | codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\U00010000", 0, 1, "ouch")), |
| 572 | (u"\\U00010000", 1) |
| 573 | ) |
| 574 | self.assertEquals( |
| 575 | codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\U0010ffff", 0, 1, "ouch")), |
| 576 | (u"\\U0010ffff", 1) |
| 577 | ) |
| 578 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 579 | def test_badhandlerresults(self): |
| 580 | results = ( 42, u"foo", (1,2,3), (u"foo", 1, 3), (u"foo", None), (u"foo",), ("foo", 1, 3), ("foo", None), ("foo",) ) |
| 581 | encs = ("ascii", "latin-1", "iso-8859-1", "iso-8859-15") |
| 582 | |
| 583 | for res in results: |
| 584 | codecs.register_error("test.badhandler", lambda: res) |
| 585 | for enc in encs: |
| 586 | self.assertRaises( |
| 587 | TypeError, |
| 588 | u"\u3042".encode, |
| 589 | enc, |
| 590 | "test.badhandler" |
| 591 | ) |
| 592 | for (enc, bytes) in ( |
| 593 | ("ascii", "\xff"), |
| 594 | ("utf-8", "\xff"), |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 595 | ("utf-7", "+x-"), |
| 596 | ("unicode-internal", "\x00"), |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 597 | ): |
| 598 | self.assertRaises( |
| 599 | TypeError, |
| 600 | bytes.decode, |
| 601 | enc, |
| 602 | "test.badhandler" |
| 603 | ) |
| 604 | |
| 605 | def test_lookup(self): |
| 606 | self.assertEquals(codecs.strict_errors, codecs.lookup_error("strict")) |
| 607 | self.assertEquals(codecs.ignore_errors, codecs.lookup_error("ignore")) |
| 608 | self.assertEquals(codecs.strict_errors, codecs.lookup_error("strict")) |
| 609 | self.assertEquals( |
| 610 | codecs.xmlcharrefreplace_errors, |
| 611 | codecs.lookup_error("xmlcharrefreplace") |
| 612 | ) |
| 613 | self.assertEquals( |
| 614 | codecs.backslashreplace_errors, |
| 615 | codecs.lookup_error("backslashreplace") |
| 616 | ) |
| 617 | |
Walter Dörwald | 9ab7dd4 | 2002-09-06 17:21:40 +0000 | [diff] [blame] | 618 | def test_unencodablereplacement(self): |
| 619 | def unencrepl(exc): |
| 620 | if isinstance(exc, UnicodeEncodeError): |
| 621 | return (u"\u4242", exc.end) |
| 622 | else: |
| 623 | raise TypeError("don't know how to handle %r" % exc) |
| 624 | codecs.register_error("test.unencreplhandler", unencrepl) |
| 625 | for enc in ("ascii", "iso-8859-1", "iso-8859-15"): |
| 626 | self.assertRaises( |
| 627 | UnicodeEncodeError, |
| 628 | u"\u4242".encode, |
| 629 | enc, |
| 630 | "test.unencreplhandler" |
| 631 | ) |
| 632 | |
Walter Dörwald | 30537a4 | 2003-01-08 23:22:13 +0000 | [diff] [blame] | 633 | def test_badregistercall(self): |
| 634 | # enhance coverage of: |
| 635 | # Modules/_codecsmodule.c::register_error() |
| 636 | # Python/codecs.c::PyCodec_RegisterError() |
| 637 | self.assertRaises(TypeError, codecs.register_error, 42) |
| 638 | self.assertRaises(TypeError, codecs.register_error, "test.dummy", 42) |
| 639 | |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 640 | def test_badlookupcall(self): |
| 641 | # enhance coverage of: |
| 642 | # Modules/_codecsmodule.c::lookup_error() |
| 643 | self.assertRaises(TypeError, codecs.lookup_error) |
| 644 | |
Walter Dörwald | 30537a4 | 2003-01-08 23:22:13 +0000 | [diff] [blame] | 645 | def test_unknownhandler(self): |
| 646 | # enhance coverage of: |
| 647 | # Modules/_codecsmodule.c::lookup_error() |
| 648 | self.assertRaises(LookupError, codecs.lookup_error, "test.unknown") |
| 649 | |
| 650 | def test_xmlcharrefvalues(self): |
| 651 | # enhance coverage of: |
| 652 | # Python/codecs.c::PyCodec_XMLCharRefReplaceErrors() |
| 653 | # and inline implementations |
| 654 | v = (1, 5, 10, 50, 100, 500, 1000, 5000, 10000, 50000) |
Walter Dörwald | 0cb27dd | 2003-01-09 11:38:50 +0000 | [diff] [blame] | 655 | if sys.maxunicode>=100000: |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 656 | v += (100000, 500000, 1000000) |
Walter Dörwald | 30537a4 | 2003-01-08 23:22:13 +0000 | [diff] [blame] | 657 | s = u"".join([unichr(x) for x in v]) |
| 658 | codecs.register_error("test.xmlcharrefreplace", codecs.xmlcharrefreplace_errors) |
| 659 | for enc in ("ascii", "iso-8859-15"): |
| 660 | for err in ("xmlcharrefreplace", "test.xmlcharrefreplace"): |
| 661 | s.encode(enc, err) |
| 662 | |
| 663 | def test_decodehelper(self): |
| 664 | # enhance coverage of: |
| 665 | # Objects/unicodeobject.c::unicode_decode_call_errorhandler() |
| 666 | # and callers |
| 667 | self.assertRaises(LookupError, "\xff".decode, "ascii", "test.unknown") |
| 668 | |
| 669 | def baddecodereturn1(exc): |
| 670 | return 42 |
| 671 | codecs.register_error("test.baddecodereturn1", baddecodereturn1) |
| 672 | self.assertRaises(TypeError, "\xff".decode, "ascii", "test.baddecodereturn1") |
| 673 | self.assertRaises(TypeError, "\\".decode, "unicode-escape", "test.baddecodereturn1") |
| 674 | self.assertRaises(TypeError, "\\x0".decode, "unicode-escape", "test.baddecodereturn1") |
| 675 | self.assertRaises(TypeError, "\\x0y".decode, "unicode-escape", "test.baddecodereturn1") |
| 676 | self.assertRaises(TypeError, "\\Uffffeeee".decode, "unicode-escape", "test.baddecodereturn1") |
| 677 | self.assertRaises(TypeError, "\\uyyyy".decode, "raw-unicode-escape", "test.baddecodereturn1") |
| 678 | |
| 679 | def baddecodereturn2(exc): |
| 680 | return (u"?", None) |
| 681 | codecs.register_error("test.baddecodereturn2", baddecodereturn2) |
| 682 | self.assertRaises(TypeError, "\xff".decode, "ascii", "test.baddecodereturn2") |
| 683 | |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 684 | handler = PosReturn() |
| 685 | codecs.register_error("test.posreturn", handler.handle) |
Walter Dörwald | 30537a4 | 2003-01-08 23:22:13 +0000 | [diff] [blame] | 686 | |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 687 | # Valid negative position |
| 688 | handler.pos = -1 |
| 689 | self.assertEquals("\xff0".decode("ascii", "test.posreturn"), u"<?>0") |
| 690 | |
| 691 | # Valid negative position |
| 692 | handler.pos = -2 |
| 693 | self.assertEquals("\xff0".decode("ascii", "test.posreturn"), u"<?><?>") |
| 694 | |
| 695 | # Negative position out of bounds |
| 696 | handler.pos = -3 |
| 697 | self.assertRaises(IndexError, "\xff0".decode, "ascii", "test.posreturn") |
| 698 | |
| 699 | # Valid positive position |
| 700 | handler.pos = 1 |
| 701 | self.assertEquals("\xff0".decode("ascii", "test.posreturn"), u"<?>0") |
| 702 | |
Walter Dörwald | 29ddfba | 2004-12-14 21:28:07 +0000 | [diff] [blame] | 703 | # Largest valid positive position (one beyond end of input) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 704 | handler.pos = 2 |
| 705 | self.assertEquals("\xff0".decode("ascii", "test.posreturn"), u"<?>") |
| 706 | |
| 707 | # Invalid positive position |
| 708 | handler.pos = 3 |
| 709 | self.assertRaises(IndexError, "\xff0".decode, "ascii", "test.posreturn") |
| 710 | |
| 711 | # Restart at the "0" |
| 712 | handler.pos = 6 |
| 713 | self.assertEquals("\\uyyyy0".decode("raw-unicode-escape", "test.posreturn"), u"<?>0") |
Walter Dörwald | 30537a4 | 2003-01-08 23:22:13 +0000 | [diff] [blame] | 714 | |
| 715 | class D(dict): |
| 716 | def __getitem__(self, key): |
| 717 | raise ValueError |
| 718 | self.assertRaises(UnicodeError, codecs.charmap_decode, "\xff", "strict", {0xff: None}) |
| 719 | self.assertRaises(ValueError, codecs.charmap_decode, "\xff", "strict", D()) |
| 720 | self.assertRaises(TypeError, codecs.charmap_decode, "\xff", "strict", {0xff: sys.maxunicode+1}) |
| 721 | |
| 722 | def test_encodehelper(self): |
| 723 | # enhance coverage of: |
| 724 | # Objects/unicodeobject.c::unicode_encode_call_errorhandler() |
| 725 | # and callers |
| 726 | self.assertRaises(LookupError, u"\xff".encode, "ascii", "test.unknown") |
| 727 | |
| 728 | def badencodereturn1(exc): |
| 729 | return 42 |
| 730 | codecs.register_error("test.badencodereturn1", badencodereturn1) |
| 731 | self.assertRaises(TypeError, u"\xff".encode, "ascii", "test.badencodereturn1") |
| 732 | |
| 733 | def badencodereturn2(exc): |
| 734 | return (u"?", None) |
| 735 | codecs.register_error("test.badencodereturn2", badencodereturn2) |
| 736 | self.assertRaises(TypeError, u"\xff".encode, "ascii", "test.badencodereturn2") |
| 737 | |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 738 | handler = PosReturn() |
| 739 | codecs.register_error("test.posreturn", handler.handle) |
Walter Dörwald | 30537a4 | 2003-01-08 23:22:13 +0000 | [diff] [blame] | 740 | |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 741 | # Valid negative position |
| 742 | handler.pos = -1 |
| 743 | self.assertEquals(u"\xff0".encode("ascii", "test.posreturn"), "<?>0") |
| 744 | |
| 745 | # Valid negative position |
| 746 | handler.pos = -2 |
| 747 | self.assertEquals(u"\xff0".encode("ascii", "test.posreturn"), "<?><?>") |
| 748 | |
| 749 | # Negative position out of bounds |
| 750 | handler.pos = -3 |
| 751 | self.assertRaises(IndexError, u"\xff0".encode, "ascii", "test.posreturn") |
| 752 | |
| 753 | # Valid positive position |
| 754 | handler.pos = 1 |
| 755 | self.assertEquals(u"\xff0".encode("ascii", "test.posreturn"), "<?>0") |
| 756 | |
| 757 | # Largest valid positive position (one beyond end of input |
| 758 | handler.pos = 2 |
| 759 | self.assertEquals(u"\xff0".encode("ascii", "test.posreturn"), "<?>") |
| 760 | |
| 761 | # Invalid positive position |
| 762 | handler.pos = 3 |
| 763 | self.assertRaises(IndexError, u"\xff0".encode, "ascii", "test.posreturn") |
| 764 | |
| 765 | handler.pos = 0 |
Walter Dörwald | 30537a4 | 2003-01-08 23:22:13 +0000 | [diff] [blame] | 766 | |
| 767 | class D(dict): |
| 768 | def __getitem__(self, key): |
| 769 | raise ValueError |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 770 | for err in ("strict", "replace", "xmlcharrefreplace", "backslashreplace", "test.posreturn"): |
Walter Dörwald | 30537a4 | 2003-01-08 23:22:13 +0000 | [diff] [blame] | 771 | self.assertRaises(UnicodeError, codecs.charmap_encode, u"\xff", err, {0xff: None}) |
| 772 | self.assertRaises(ValueError, codecs.charmap_encode, u"\xff", err, D()) |
| 773 | self.assertRaises(TypeError, codecs.charmap_encode, u"\xff", err, {0xff: 300}) |
| 774 | |
| 775 | def test_translatehelper(self): |
| 776 | # enhance coverage of: |
| 777 | # Objects/unicodeobject.c::unicode_encode_call_errorhandler() |
| 778 | # and callers |
| 779 | # (Unfortunately the errors argument is not directly accessible |
| 780 | # from Python, so we can't test that much) |
| 781 | class D(dict): |
| 782 | def __getitem__(self, key): |
| 783 | raise ValueError |
| 784 | self.assertRaises(ValueError, u"\xff".translate, D()) |
| 785 | self.assertRaises(TypeError, u"\xff".translate, {0xff: sys.maxunicode+1}) |
| 786 | self.assertRaises(TypeError, u"\xff".translate, {0xff: ()}) |
| 787 | |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 788 | def test_bug828737(self): |
| 789 | charmap = { |
| 790 | ord("&"): u"&", |
| 791 | ord("<"): u"<", |
| 792 | ord(">"): u">", |
| 793 | ord('"'): u""", |
| 794 | } |
Tim Peters | 58eb11c | 2004-01-18 20:29:55 +0000 | [diff] [blame] | 795 | |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 796 | for n in (1, 10, 100, 1000): |
| 797 | text = u'abc<def>ghi'*n |
| 798 | text.translate(charmap) |
| 799 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 800 | def test_main(): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 801 | test.test_support.run_unittest(CodecCallbackTest) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 802 | |
| 803 | if __name__ == "__main__": |
| 804 | test_main() |