Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1 | import test.support, unittest |
Fred Drake | 3c50ea4 | 2008-05-17 22:02:32 +0000 | [diff] [blame] | 2 | import sys, codecs, html.entities, 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) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 19 | return ("<?>", oldpos) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 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): |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 24 | UnicodeEncodeError.__init__(self, "ascii", "", 0, 1, "bad") |
Walter Dörwald | 690402f | 2005-11-17 18:51:34 +0000 | [diff] [blame] | 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): |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 30 | UnicodeEncodeError.__init__(self, "ascii", "", 0, 1, "bad") |
Walter Dörwald | 690402f | 2005-11-17 18:51:34 +0000 | [diff] [blame] | 31 | self.object = [] |
| 32 | |
| 33 | # A UnicodeDecodeError object without an end attribute |
| 34 | class NoEndUnicodeDecodeError(UnicodeDecodeError): |
| 35 | def __init__(self): |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 36 | UnicodeDecodeError.__init__(self, "ascii", bytearray(b""), 0, 1, "bad") |
Walter Dörwald | 690402f | 2005-11-17 18:51:34 +0000 | [diff] [blame] | 37 | del self.end |
| 38 | |
| 39 | # A UnicodeDecodeError object with a bad object attribute |
| 40 | class BadObjectUnicodeDecodeError(UnicodeDecodeError): |
| 41 | def __init__(self): |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 42 | UnicodeDecodeError.__init__(self, "ascii", bytearray(b""), 0, 1, "bad") |
Walter Dörwald | 690402f | 2005-11-17 18:51:34 +0000 | [diff] [blame] | 43 | self.object = [] |
| 44 | |
| 45 | # A UnicodeTranslateError object without a start attribute |
| 46 | class NoStartUnicodeTranslateError(UnicodeTranslateError): |
| 47 | def __init__(self): |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 48 | UnicodeTranslateError.__init__(self, "", 0, 1, "bad") |
Walter Dörwald | 690402f | 2005-11-17 18:51:34 +0000 | [diff] [blame] | 49 | del self.start |
| 50 | |
| 51 | # A UnicodeTranslateError object without an end attribute |
| 52 | class NoEndUnicodeTranslateError(UnicodeTranslateError): |
| 53 | def __init__(self): |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 54 | UnicodeTranslateError.__init__(self, "", 0, 1, "bad") |
Walter Dörwald | 690402f | 2005-11-17 18:51:34 +0000 | [diff] [blame] | 55 | del self.end |
| 56 | |
| 57 | # A UnicodeTranslateError object without an object attribute |
| 58 | class NoObjectUnicodeTranslateError(UnicodeTranslateError): |
| 59 | def __init__(self): |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 60 | UnicodeTranslateError.__init__(self, "", 0, 1, "bad") |
Walter Dörwald | 690402f | 2005-11-17 18:51:34 +0000 | [diff] [blame] | 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. |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 69 | s = "\u30b9\u30d1\u30e2 \xe4nd eggs" |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 70 | self.assertEqual( |
| 71 | s.encode("ascii", "xmlcharrefreplace"), |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 72 | b"スパモ änd eggs" |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 73 | ) |
| 74 | self.assertEqual( |
| 75 | s.encode("latin-1", "xmlcharrefreplace"), |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 76 | b"スパモ \xe4nd eggs" |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 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 | 3c50ea4 | 2008-05-17 22:02:32 +0000 | [diff] [blame] | 89 | l.append("&%s;" % html.entities.codepoint2name[ord(c)]) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 90 | except KeyError: |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 91 | l.append("&#%d;" % ord(c)) |
| 92 | return ("".join(l), exc.end) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 93 | |
| 94 | codecs.register_error( |
| 95 | "test.xmlcharnamereplace", xmlcharnamereplace) |
| 96 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 97 | sin = "\xab\u211c\xbb = \u2329\u1234\u20ac\u232a" |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 98 | sout = b"«ℜ» = ⟨ሴ€⟩" |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 99 | self.assertEqual(sin.encode("ascii", "test.xmlcharnamereplace"), sout) |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 100 | sout = b"\xabℜ\xbb = ⟨ሴ€⟩" |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 101 | self.assertEqual(sin.encode("latin-1", "test.xmlcharnamereplace"), sout) |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 102 | sout = b"\xabℜ\xbb = ⟨ሴ\xa4⟩" |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 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 |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +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]: |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 119 | l.append(unicodedata.name(c, "0x%x" % ord(c))) |
| 120 | return ("\033[1m%s\033[0m" % ", ".join(l), exc.end) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 121 | |
| 122 | codecs.register_error( |
| 123 | "test.uninamereplace", uninamereplace) |
| 124 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 125 | sin = "\xac\u1234\u20ac\u8000" |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 126 | sout = b"\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 | |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 129 | sout = b"\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 | |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 132 | sout = b"\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. |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 138 | sin = "a\xac\u1234\u20ac\u8000" |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 139 | if sys.maxunicode > 0xffff: |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 140 | sin += chr(sys.maxunicode) |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 141 | sout = b"a\\xac\\u1234\\u20ac\\u8000" |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 142 | if sys.maxunicode > 0xffff: |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 143 | sout += bytes("\\U%08x" % sys.maxunicode, "ascii") |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 144 | self.assertEqual(sin.encode("ascii", "backslashreplace"), sout) |
| 145 | |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 146 | sout = b"a\xac\\u1234\\u20ac\\u8000" |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 147 | if sys.maxunicode > 0xffff: |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 148 | sout += bytes("\\U%08x" % sys.maxunicode, "ascii") |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 149 | self.assertEqual(sin.encode("latin-1", "backslashreplace"), sout) |
| 150 | |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 151 | sout = b"a\xac\\u1234\xa4\\u8000" |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 152 | if sys.maxunicode > 0xffff: |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 153 | sout += bytes("\\U%08x" % sys.maxunicode, "ascii") |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 154 | self.assertEqual(sin.encode("iso-8859-15", "backslashreplace"), sout) |
| 155 | |
Ezio Melotti | 25bc019 | 2010-07-03 05:18:50 +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 | 25bc019 | 2010-07-03 05:18:50 +0000 | [diff] [blame] | 164 | if exc.object[exc.start:exc.start+2] == b"\xc0\x80": |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 165 | return ("\x00", exc.start+2) # retry after two bytes |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 166 | else: |
| 167 | raise exc |
| 168 | |
Ezio Melotti | 25bc019 | 2010-07-03 05:18:50 +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 | 25bc019 | 2010-07-03 05:18:50 +0000 | [diff] [blame] | 171 | # all the "\xc0\x80" will be decoded to "\x00" |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 172 | sin = b"a\x00b\xc0\x80c\xc3\xbc\xc0\x80\xc0\x80" |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 173 | sout = "a\x00b\x00c\xfc\x00\x00" |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 174 | self.assertEqual(sin.decode("utf-8", "test.relaxedutf8"), sout) |
Ezio Melotti | 25bc019 | 2010-07-03 05:18:50 +0000 | [diff] [blame] | 175 | |
| 176 | # "\xc0\x81" is not valid and a UnicodeDecodeError will be raised |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 177 | sin = b"\xc0\x80\xc0\x81" |
Ezio Melotti | 25bc019 | 2010-07-03 05:18:50 +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 "?". |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 186 | charmap = dict((ord(c), bytes(2*c.upper(), 'ascii')) for c in "abcdefgh") |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 187 | sin = "abc" |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 188 | sout = b"AABBCC" |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 189 | self.assertEquals(codecs.charmap_encode(sin, "strict", charmap)[0], sout) |
| 190 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 191 | sin = "abcA" |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 192 | self.assertRaises(UnicodeError, codecs.charmap_encode, sin, "strict", charmap) |
| 193 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 194 | charmap[ord("?")] = b"XYZ" |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 195 | sin = "abcDEF" |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 196 | sout = b"AABBCCXYZXYZXYZ" |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 197 | self.assertEquals(codecs.charmap_encode(sin, "replace", charmap)[0], sout) |
| 198 | |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 199 | charmap[ord("?")] = "XYZ" # wrong type in mapping |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 200 | self.assertRaises(TypeError, codecs.charmap_encode, sin, "replace", charmap) |
| 201 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 202 | def test_decodeunicodeinternal(self): |
| 203 | self.assertRaises( |
| 204 | UnicodeDecodeError, |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 205 | b"\x00\x00\x00\x00\x00".decode, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 206 | "unicode-internal", |
| 207 | ) |
| 208 | if sys.maxunicode > 0xffff: |
| 209 | def handler_unicodeinternal(exc): |
| 210 | if not isinstance(exc, UnicodeDecodeError): |
| 211 | raise TypeError("don't know how to handle %r" % exc) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 212 | return ("\x01", 1) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 213 | |
| 214 | self.assertEqual( |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 215 | b"\x00\x00\x00\x00\x00".decode("unicode-internal", "ignore"), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 216 | "\u0000" |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 217 | ) |
| 218 | |
| 219 | self.assertEqual( |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 220 | b"\x00\x00\x00\x00\x00".decode("unicode-internal", "replace"), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 221 | "\u0000\ufffd" |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 222 | ) |
| 223 | |
| 224 | codecs.register_error("test.hui", handler_unicodeinternal) |
| 225 | |
| 226 | self.assertEqual( |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 227 | b"\x00\x00\x00\x00\x00".decode("unicode-internal", "test.hui"), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 228 | "\u0000\u0001\u0000" |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 229 | ) |
| 230 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 231 | def test_callbacks(self): |
| 232 | def handler1(exc): |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 233 | r = range(exc.start, exc.end) |
| 234 | if isinstance(exc, UnicodeEncodeError): |
| 235 | l = ["<%d>" % ord(exc.object[pos]) for pos in r] |
| 236 | elif isinstance(exc, UnicodeDecodeError): |
| 237 | l = ["<%d>" % exc.object[pos] for pos in r] |
| 238 | else: |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 239 | raise TypeError("don't know how to handle %r" % exc) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 240 | return ("[%s]" % "".join(l), exc.end) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 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) |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 247 | l = ["<%d>" % exc.object[pos] for pos in range(exc.start, exc.end)] |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 248 | return ("[%s]" % "".join(l), exc.end+1) # skip one character |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 249 | |
| 250 | codecs.register_error("test.handler2", handler2) |
| 251 | |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 252 | s = b"\x00\x81\x7f\x80\xff" |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 253 | |
| 254 | self.assertEqual( |
| 255 | s.decode("ascii", "test.handler1"), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 256 | "\x00[<129>]\x7f[<128>][<255>]" |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 257 | ) |
| 258 | self.assertEqual( |
| 259 | s.decode("ascii", "test.handler2"), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 260 | "\x00[<129>][<128>]" |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 261 | ) |
| 262 | |
| 263 | self.assertEqual( |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 264 | b"\\u3042\u3xxx".decode("unicode-escape", "test.handler1"), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 265 | "\u3042[<92><117><51><120>]xx" |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 266 | ) |
| 267 | |
| 268 | self.assertEqual( |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 269 | b"\\u3042\u3xx".decode("unicode-escape", "test.handler1"), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 270 | "\u3042[<92><117><51><120><120>]" |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 271 | ) |
| 272 | |
| 273 | self.assertEqual( |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 274 | codecs.charmap_decode(b"abc", "test.handler1", {ord("a"): "z"})[0], |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 275 | "z[<98>][<99>]" |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 276 | ) |
| 277 | |
| 278 | self.assertEqual( |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 279 | "g\xfc\xdfrk".encode("ascii", "test.handler1"), |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 280 | b"g[<252><223>]rk" |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 281 | ) |
| 282 | |
| 283 | self.assertEqual( |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 284 | "g\xfc\xdf".encode("ascii", "test.handler1"), |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 285 | b"g[<252><223>]" |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 286 | ) |
| 287 | |
| 288 | def test_longstrings(self): |
| 289 | # test long strings to check for memory overflow problems |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +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 ] |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 298 | for uni in [ s*l for s in ("x", "\u3042", "a\xe4") ]: |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +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 |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 314 | wrongargs = [ "spam", b"eggs", b"spam", 42, 1.0, None ] |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 315 | for i in range(len(args)): |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 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 = [] |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 321 | for j in range(len(args)): |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 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, |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 335 | ["ascii", "g\xfcrk", 1, 2, "ouch"], |
Walter Dörwald | 32a4c71 | 2007-06-20 09:25:34 +0000 | [diff] [blame] | 336 | "'ascii' codec can't encode character '\\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, |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 340 | ["ascii", "g\xfcrk", 1, 4, "ouch"], |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 341 | "'ascii' codec can't encode characters in position 1-3: ouch" |
| 342 | ) |
| 343 | self.check_exceptionobjectargs( |
| 344 | UnicodeEncodeError, |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 345 | ["ascii", "\xfcx", 0, 1, "ouch"], |
Walter Dörwald | 32a4c71 | 2007-06-20 09:25:34 +0000 | [diff] [blame] | 346 | "'ascii' codec can't encode character '\\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, |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 350 | ["ascii", "\u0100x", 0, 1, "ouch"], |
Walter Dörwald | 32a4c71 | 2007-06-20 09:25:34 +0000 | [diff] [blame] | 351 | "'ascii' codec can't encode character '\\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, |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 355 | ["ascii", "\uffffx", 0, 1, "ouch"], |
Walter Dörwald | 32a4c71 | 2007-06-20 09:25:34 +0000 | [diff] [blame] | 356 | "'ascii' codec can't encode character '\\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, |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 361 | ["ascii", "\U00010000x", 0, 1, "ouch"], |
Walter Dörwald | 32a4c71 | 2007-06-20 09:25:34 +0000 | [diff] [blame] | 362 | "'ascii' codec can't encode character '\\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, |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 368 | ["ascii", bytearray(b"g\xfcrk"), 1, 2, "ouch"], |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 369 | "'ascii' codec can't decode byte 0xfc in position 1: ouch" |
| 370 | ) |
| 371 | self.check_exceptionobjectargs( |
| 372 | UnicodeDecodeError, |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 373 | ["ascii", bytearray(b"g\xfcrk"), 1, 3, "ouch"], |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 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, |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 380 | ["g\xfcrk", 1, 2, "ouch"], |
Walter Dörwald | 32a4c71 | 2007-06-20 09:25:34 +0000 | [diff] [blame] | 381 | "can't translate character '\\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 | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 385 | ["g\u0100rk", 1, 2, "ouch"], |
Walter Dörwald | 32a4c71 | 2007-06-20 09:25:34 +0000 | [diff] [blame] | 386 | "can't translate character '\\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, |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 390 | ["g\uffffrk", 1, 2, "ouch"], |
Walter Dörwald | 32a4c71 | 2007-06-20 09:25:34 +0000 | [diff] [blame] | 391 | "can't translate character '\\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, |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 396 | ["g\U00010000rk", 1, 2, "ouch"], |
Walter Dörwald | 32a4c71 | 2007-06-20 09:25:34 +0000 | [diff] [blame] | 397 | "can't translate character '\\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 | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 401 | ["g\xfcrk", 1, 3, "ouch"], |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 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, |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 423 | UnicodeEncodeError("ascii", "\u3042", 0, 1, "ouch") |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 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( |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 441 | codecs.ignore_errors( |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 442 | UnicodeEncodeError("ascii", "\u3042", 0, 1, "ouch")), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 443 | ("", 1) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 444 | ) |
| 445 | self.assertEquals( |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 446 | codecs.ignore_errors( |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 447 | UnicodeDecodeError("ascii", bytearray(b"\xff"), 0, 1, "ouch")), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 448 | ("", 1) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 449 | ) |
| 450 | self.assertEquals( |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 451 | codecs.ignore_errors( |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 452 | UnicodeTranslateError("\u3042", 0, 1, "ouch")), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 453 | ("", 1) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 454 | ) |
| 455 | |
| 456 | def test_badandgoodreplaceexceptions(self): |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 457 | # "replace" complains about a non-exception passed in |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 458 | self.assertRaises( |
| 459 | TypeError, |
| 460 | codecs.replace_errors, |
| 461 | 42 |
| 462 | ) |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 463 | # "replace" complains about the wrong exception type |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 464 | self.assertRaises( |
| 465 | TypeError, |
| 466 | codecs.replace_errors, |
| 467 | UnicodeError("ouch") |
| 468 | ) |
Walter Dörwald | 690402f | 2005-11-17 18:51:34 +0000 | [diff] [blame] | 469 | self.assertRaises( |
Walter Dörwald | 690402f | 2005-11-17 18:51:34 +0000 | [diff] [blame] | 470 | TypeError, |
| 471 | codecs.replace_errors, |
| 472 | BadObjectUnicodeEncodeError() |
| 473 | ) |
| 474 | self.assertRaises( |
Walter Dörwald | 690402f | 2005-11-17 18:51:34 +0000 | [diff] [blame] | 475 | TypeError, |
| 476 | codecs.replace_errors, |
| 477 | BadObjectUnicodeDecodeError() |
| 478 | ) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 479 | # With the correct exception, "replace" returns an "?" or "\ufffd" replacement |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 480 | self.assertEquals( |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 481 | codecs.replace_errors( |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 482 | UnicodeEncodeError("ascii", "\u3042", 0, 1, "ouch")), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 483 | ("?", 1) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 484 | ) |
| 485 | self.assertEquals( |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 486 | codecs.replace_errors( |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 487 | UnicodeDecodeError("ascii", bytearray(b"\xff"), 0, 1, "ouch")), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 488 | ("\ufffd", 1) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 489 | ) |
| 490 | self.assertEquals( |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 491 | codecs.replace_errors( |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 492 | UnicodeTranslateError("\u3042", 0, 1, "ouch")), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 493 | ("\ufffd", 1) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 494 | ) |
| 495 | |
| 496 | def test_badandgoodxmlcharrefreplaceexceptions(self): |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 497 | # "xmlcharrefreplace" complains about a non-exception passed in |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 498 | self.assertRaises( |
| 499 | TypeError, |
| 500 | codecs.xmlcharrefreplace_errors, |
| 501 | 42 |
| 502 | ) |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 503 | # "xmlcharrefreplace" complains about the wrong exception types |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 504 | self.assertRaises( |
| 505 | TypeError, |
| 506 | codecs.xmlcharrefreplace_errors, |
| 507 | UnicodeError("ouch") |
| 508 | ) |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 509 | # "xmlcharrefreplace" can only be used for encoding |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 510 | self.assertRaises( |
| 511 | TypeError, |
| 512 | codecs.xmlcharrefreplace_errors, |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 513 | UnicodeDecodeError("ascii", bytearray(b"\xff"), 0, 1, "ouch") |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 514 | ) |
| 515 | self.assertRaises( |
| 516 | TypeError, |
| 517 | codecs.xmlcharrefreplace_errors, |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 518 | UnicodeTranslateError("\u3042", 0, 1, "ouch") |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 519 | ) |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 520 | # Use the correct exception |
Walter Dörwald | 690402f | 2005-11-17 18:51:34 +0000 | [diff] [blame] | 521 | cs = (0, 1, 9, 10, 99, 100, 999, 1000, 9999, 10000, 0x3042) |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 522 | s = "".join(chr(c) for c in cs) |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 523 | self.assertEquals( |
Walter Dörwald | 690402f | 2005-11-17 18:51:34 +0000 | [diff] [blame] | 524 | codecs.xmlcharrefreplace_errors( |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 525 | UnicodeEncodeError("ascii", s, 0, len(s), "ouch") |
Walter Dörwald | 690402f | 2005-11-17 18:51:34 +0000 | [diff] [blame] | 526 | ), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 527 | ("".join("&#%d;" % ord(c) for c in s), len(s)) |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 528 | ) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 529 | |
| 530 | def test_badandgoodbackslashreplaceexceptions(self): |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 531 | # "backslashreplace" complains about a non-exception passed in |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 532 | self.assertRaises( |
| 533 | TypeError, |
| 534 | codecs.backslashreplace_errors, |
| 535 | 42 |
| 536 | ) |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 537 | # "backslashreplace" complains about the wrong exception types |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 538 | self.assertRaises( |
| 539 | TypeError, |
| 540 | codecs.backslashreplace_errors, |
| 541 | UnicodeError("ouch") |
| 542 | ) |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 543 | # "backslashreplace" can only be used for encoding |
| 544 | self.assertRaises( |
| 545 | TypeError, |
| 546 | codecs.backslashreplace_errors, |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 547 | UnicodeDecodeError("ascii", bytearray(b"\xff"), 0, 1, "ouch") |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 548 | ) |
| 549 | self.assertRaises( |
| 550 | TypeError, |
| 551 | codecs.backslashreplace_errors, |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 552 | UnicodeTranslateError("\u3042", 0, 1, "ouch") |
Walter Dörwald | ea4250d | 2003-01-20 02:34:07 +0000 | [diff] [blame] | 553 | ) |
| 554 | # Use the correct exception |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 555 | self.assertEquals( |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 556 | codecs.backslashreplace_errors( |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 557 | UnicodeEncodeError("ascii", "\u3042", 0, 1, "ouch")), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 558 | ("\\u3042", 1) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 559 | ) |
| 560 | self.assertEquals( |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 561 | codecs.backslashreplace_errors( |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 562 | UnicodeEncodeError("ascii", "\x00", 0, 1, "ouch")), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 563 | ("\\x00", 1) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 564 | ) |
| 565 | self.assertEquals( |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 566 | codecs.backslashreplace_errors( |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 567 | UnicodeEncodeError("ascii", "\xff", 0, 1, "ouch")), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 568 | ("\\xff", 1) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 569 | ) |
| 570 | self.assertEquals( |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 571 | codecs.backslashreplace_errors( |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 572 | UnicodeEncodeError("ascii", "\u0100", 0, 1, "ouch")), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 573 | ("\\u0100", 1) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 574 | ) |
| 575 | self.assertEquals( |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 576 | codecs.backslashreplace_errors( |
Walter Dörwald | d203431 | 2007-05-18 16:29:38 +0000 | [diff] [blame] | 577 | UnicodeEncodeError("ascii", "\uffff", 0, 1, "ouch")), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 578 | ("\\uffff", 1) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 579 | ) |
Antoine Pitrou | c9a8df2 | 2010-09-09 20:33:43 +0000 | [diff] [blame] | 580 | # 1 on UCS-4 builds, 2 on UCS-2 |
| 581 | len_wide = len("\U00010000") |
| 582 | self.assertEquals( |
| 583 | codecs.backslashreplace_errors( |
| 584 | UnicodeEncodeError("ascii", "\U00010000", |
| 585 | 0, len_wide, "ouch")), |
| 586 | ("\\U00010000", len_wide) |
| 587 | ) |
| 588 | self.assertEquals( |
| 589 | codecs.backslashreplace_errors( |
| 590 | UnicodeEncodeError("ascii", "\U0010ffff", |
| 591 | 0, len_wide, "ouch")), |
| 592 | ("\\U0010ffff", len_wide) |
| 593 | ) |
| 594 | # Lone surrogates (regardless of unicode width) |
| 595 | self.assertEquals( |
| 596 | codecs.backslashreplace_errors( |
| 597 | UnicodeEncodeError("ascii", "\ud800", 0, 1, "ouch")), |
| 598 | ("\\ud800", 1) |
| 599 | ) |
| 600 | self.assertEquals( |
| 601 | codecs.backslashreplace_errors( |
| 602 | UnicodeEncodeError("ascii", "\udfff", 0, 1, "ouch")), |
| 603 | ("\\udfff", 1) |
| 604 | ) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 605 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 606 | def test_badhandlerresults(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 607 | results = ( 42, "foo", (1,2,3), ("foo", 1, 3), ("foo", None), ("foo",), ("foo", 1, 3), ("foo", None), ("foo",) ) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 608 | encs = ("ascii", "latin-1", "iso-8859-1", "iso-8859-15") |
| 609 | |
| 610 | for res in results: |
Benjamin Peterson | b58dda7 | 2009-01-18 22:27:04 +0000 | [diff] [blame] | 611 | codecs.register_error("test.badhandler", lambda x: res) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 612 | for enc in encs: |
| 613 | self.assertRaises( |
| 614 | TypeError, |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 615 | "\u3042".encode, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 616 | enc, |
| 617 | "test.badhandler" |
| 618 | ) |
| 619 | for (enc, bytes) in ( |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 620 | ("ascii", b"\xff"), |
| 621 | ("utf-8", b"\xff"), |
| 622 | ("utf-7", b"+x-"), |
| 623 | ("unicode-internal", b"\x00"), |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 624 | ): |
| 625 | self.assertRaises( |
| 626 | TypeError, |
| 627 | bytes.decode, |
| 628 | enc, |
| 629 | "test.badhandler" |
| 630 | ) |
| 631 | |
| 632 | def test_lookup(self): |
| 633 | self.assertEquals(codecs.strict_errors, codecs.lookup_error("strict")) |
| 634 | self.assertEquals(codecs.ignore_errors, codecs.lookup_error("ignore")) |
| 635 | self.assertEquals(codecs.strict_errors, codecs.lookup_error("strict")) |
| 636 | self.assertEquals( |
| 637 | codecs.xmlcharrefreplace_errors, |
| 638 | codecs.lookup_error("xmlcharrefreplace") |
| 639 | ) |
| 640 | self.assertEquals( |
| 641 | codecs.backslashreplace_errors, |
| 642 | codecs.lookup_error("backslashreplace") |
| 643 | ) |
| 644 | |
Walter Dörwald | 9ab7dd4 | 2002-09-06 17:21:40 +0000 | [diff] [blame] | 645 | def test_unencodablereplacement(self): |
| 646 | def unencrepl(exc): |
| 647 | if isinstance(exc, UnicodeEncodeError): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 648 | return ("\u4242", exc.end) |
Walter Dörwald | 9ab7dd4 | 2002-09-06 17:21:40 +0000 | [diff] [blame] | 649 | else: |
| 650 | raise TypeError("don't know how to handle %r" % exc) |
| 651 | codecs.register_error("test.unencreplhandler", unencrepl) |
| 652 | for enc in ("ascii", "iso-8859-1", "iso-8859-15"): |
| 653 | self.assertRaises( |
| 654 | UnicodeEncodeError, |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 655 | "\u4242".encode, |
Walter Dörwald | 9ab7dd4 | 2002-09-06 17:21:40 +0000 | [diff] [blame] | 656 | enc, |
| 657 | "test.unencreplhandler" |
| 658 | ) |
| 659 | |
Walter Dörwald | 30537a4 | 2003-01-08 23:22:13 +0000 | [diff] [blame] | 660 | def test_badregistercall(self): |
| 661 | # enhance coverage of: |
| 662 | # Modules/_codecsmodule.c::register_error() |
| 663 | # Python/codecs.c::PyCodec_RegisterError() |
| 664 | self.assertRaises(TypeError, codecs.register_error, 42) |
| 665 | self.assertRaises(TypeError, codecs.register_error, "test.dummy", 42) |
| 666 | |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 667 | def test_badlookupcall(self): |
| 668 | # enhance coverage of: |
| 669 | # Modules/_codecsmodule.c::lookup_error() |
| 670 | self.assertRaises(TypeError, codecs.lookup_error) |
| 671 | |
Walter Dörwald | 30537a4 | 2003-01-08 23:22:13 +0000 | [diff] [blame] | 672 | def test_unknownhandler(self): |
| 673 | # enhance coverage of: |
| 674 | # Modules/_codecsmodule.c::lookup_error() |
| 675 | self.assertRaises(LookupError, codecs.lookup_error, "test.unknown") |
| 676 | |
| 677 | def test_xmlcharrefvalues(self): |
| 678 | # enhance coverage of: |
| 679 | # Python/codecs.c::PyCodec_XMLCharRefReplaceErrors() |
| 680 | # and inline implementations |
| 681 | v = (1, 5, 10, 50, 100, 500, 1000, 5000, 10000, 50000) |
Walter Dörwald | 0cb27dd | 2003-01-09 11:38:50 +0000 | [diff] [blame] | 682 | if sys.maxunicode>=100000: |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 683 | v += (100000, 500000, 1000000) |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 684 | s = "".join([chr(x) for x in v]) |
Walter Dörwald | 30537a4 | 2003-01-08 23:22:13 +0000 | [diff] [blame] | 685 | codecs.register_error("test.xmlcharrefreplace", codecs.xmlcharrefreplace_errors) |
| 686 | for enc in ("ascii", "iso-8859-15"): |
| 687 | for err in ("xmlcharrefreplace", "test.xmlcharrefreplace"): |
| 688 | s.encode(enc, err) |
| 689 | |
| 690 | def test_decodehelper(self): |
| 691 | # enhance coverage of: |
| 692 | # Objects/unicodeobject.c::unicode_decode_call_errorhandler() |
| 693 | # and callers |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 694 | self.assertRaises(LookupError, b"\xff".decode, "ascii", "test.unknown") |
Walter Dörwald | 30537a4 | 2003-01-08 23:22:13 +0000 | [diff] [blame] | 695 | |
| 696 | def baddecodereturn1(exc): |
| 697 | return 42 |
| 698 | codecs.register_error("test.baddecodereturn1", baddecodereturn1) |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 699 | self.assertRaises(TypeError, b"\xff".decode, "ascii", "test.baddecodereturn1") |
| 700 | self.assertRaises(TypeError, b"\\".decode, "unicode-escape", "test.baddecodereturn1") |
| 701 | self.assertRaises(TypeError, b"\\x0".decode, "unicode-escape", "test.baddecodereturn1") |
| 702 | self.assertRaises(TypeError, b"\\x0y".decode, "unicode-escape", "test.baddecodereturn1") |
| 703 | self.assertRaises(TypeError, b"\\Uffffeeee".decode, "unicode-escape", "test.baddecodereturn1") |
| 704 | self.assertRaises(TypeError, b"\\uyyyy".decode, "raw-unicode-escape", "test.baddecodereturn1") |
Walter Dörwald | 30537a4 | 2003-01-08 23:22:13 +0000 | [diff] [blame] | 705 | |
| 706 | def baddecodereturn2(exc): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 707 | return ("?", None) |
Walter Dörwald | 30537a4 | 2003-01-08 23:22:13 +0000 | [diff] [blame] | 708 | codecs.register_error("test.baddecodereturn2", baddecodereturn2) |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 709 | self.assertRaises(TypeError, b"\xff".decode, "ascii", "test.baddecodereturn2") |
Walter Dörwald | 30537a4 | 2003-01-08 23:22:13 +0000 | [diff] [blame] | 710 | |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 711 | handler = PosReturn() |
| 712 | codecs.register_error("test.posreturn", handler.handle) |
Walter Dörwald | 30537a4 | 2003-01-08 23:22:13 +0000 | [diff] [blame] | 713 | |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 714 | # Valid negative position |
| 715 | handler.pos = -1 |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 716 | self.assertEquals(b"\xff0".decode("ascii", "test.posreturn"), "<?>0") |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 717 | |
| 718 | # Valid negative position |
| 719 | handler.pos = -2 |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 720 | self.assertEquals(b"\xff0".decode("ascii", "test.posreturn"), "<?><?>") |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 721 | |
| 722 | # Negative position out of bounds |
| 723 | handler.pos = -3 |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 724 | self.assertRaises(IndexError, b"\xff0".decode, "ascii", "test.posreturn") |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 725 | |
| 726 | # Valid positive position |
| 727 | handler.pos = 1 |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 728 | self.assertEquals(b"\xff0".decode("ascii", "test.posreturn"), "<?>0") |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 729 | |
Walter Dörwald | 29ddfba | 2004-12-14 21:28:07 +0000 | [diff] [blame] | 730 | # Largest valid positive position (one beyond end of input) |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 731 | handler.pos = 2 |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 732 | self.assertEquals(b"\xff0".decode("ascii", "test.posreturn"), "<?>") |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 733 | |
| 734 | # Invalid positive position |
| 735 | handler.pos = 3 |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 736 | self.assertRaises(IndexError, b"\xff0".decode, "ascii", "test.posreturn") |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 737 | |
| 738 | # Restart at the "0" |
| 739 | handler.pos = 6 |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 740 | self.assertEquals(b"\\uyyyy0".decode("raw-unicode-escape", "test.posreturn"), "<?>0") |
Walter Dörwald | 30537a4 | 2003-01-08 23:22:13 +0000 | [diff] [blame] | 741 | |
| 742 | class D(dict): |
| 743 | def __getitem__(self, key): |
| 744 | raise ValueError |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 745 | self.assertRaises(UnicodeError, codecs.charmap_decode, b"\xff", "strict", {0xff: None}) |
| 746 | self.assertRaises(ValueError, codecs.charmap_decode, b"\xff", "strict", D()) |
| 747 | self.assertRaises(TypeError, codecs.charmap_decode, b"\xff", "strict", {0xff: sys.maxunicode+1}) |
Walter Dörwald | 30537a4 | 2003-01-08 23:22:13 +0000 | [diff] [blame] | 748 | |
| 749 | def test_encodehelper(self): |
| 750 | # enhance coverage of: |
| 751 | # Objects/unicodeobject.c::unicode_encode_call_errorhandler() |
| 752 | # and callers |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 753 | self.assertRaises(LookupError, "\xff".encode, "ascii", "test.unknown") |
Walter Dörwald | 30537a4 | 2003-01-08 23:22:13 +0000 | [diff] [blame] | 754 | |
| 755 | def badencodereturn1(exc): |
| 756 | return 42 |
| 757 | codecs.register_error("test.badencodereturn1", badencodereturn1) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 758 | self.assertRaises(TypeError, "\xff".encode, "ascii", "test.badencodereturn1") |
Walter Dörwald | 30537a4 | 2003-01-08 23:22:13 +0000 | [diff] [blame] | 759 | |
| 760 | def badencodereturn2(exc): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 761 | return ("?", None) |
Walter Dörwald | 30537a4 | 2003-01-08 23:22:13 +0000 | [diff] [blame] | 762 | codecs.register_error("test.badencodereturn2", badencodereturn2) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 763 | self.assertRaises(TypeError, "\xff".encode, "ascii", "test.badencodereturn2") |
Walter Dörwald | 30537a4 | 2003-01-08 23:22:13 +0000 | [diff] [blame] | 764 | |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 765 | handler = PosReturn() |
| 766 | codecs.register_error("test.posreturn", handler.handle) |
Walter Dörwald | 30537a4 | 2003-01-08 23:22:13 +0000 | [diff] [blame] | 767 | |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 768 | # Valid negative position |
| 769 | handler.pos = -1 |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 770 | self.assertEquals("\xff0".encode("ascii", "test.posreturn"), b"<?>0") |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 771 | |
| 772 | # Valid negative position |
| 773 | handler.pos = -2 |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 774 | self.assertEquals("\xff0".encode("ascii", "test.posreturn"), b"<?><?>") |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 775 | |
| 776 | # Negative position out of bounds |
| 777 | handler.pos = -3 |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 778 | self.assertRaises(IndexError, "\xff0".encode, "ascii", "test.posreturn") |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 779 | |
| 780 | # Valid positive position |
| 781 | handler.pos = 1 |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 782 | self.assertEquals("\xff0".encode("ascii", "test.posreturn"), b"<?>0") |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 783 | |
| 784 | # Largest valid positive position (one beyond end of input |
| 785 | handler.pos = 2 |
Walter Dörwald | 00048f0 | 2007-05-09 10:44:06 +0000 | [diff] [blame] | 786 | self.assertEquals("\xff0".encode("ascii", "test.posreturn"), b"<?>") |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 787 | |
| 788 | # Invalid positive position |
| 789 | handler.pos = 3 |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 790 | self.assertRaises(IndexError, "\xff0".encode, "ascii", "test.posreturn") |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 791 | |
| 792 | handler.pos = 0 |
Walter Dörwald | 30537a4 | 2003-01-08 23:22:13 +0000 | [diff] [blame] | 793 | |
| 794 | class D(dict): |
| 795 | def __getitem__(self, key): |
| 796 | raise ValueError |
Walter Dörwald | 2e0b18a | 2003-01-31 17:19:08 +0000 | [diff] [blame] | 797 | for err in ("strict", "replace", "xmlcharrefreplace", "backslashreplace", "test.posreturn"): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 798 | self.assertRaises(UnicodeError, codecs.charmap_encode, "\xff", err, {0xff: None}) |
| 799 | self.assertRaises(ValueError, codecs.charmap_encode, "\xff", err, D()) |
| 800 | self.assertRaises(TypeError, codecs.charmap_encode, "\xff", err, {0xff: 300}) |
Walter Dörwald | 30537a4 | 2003-01-08 23:22:13 +0000 | [diff] [blame] | 801 | |
| 802 | def test_translatehelper(self): |
| 803 | # enhance coverage of: |
| 804 | # Objects/unicodeobject.c::unicode_encode_call_errorhandler() |
| 805 | # and callers |
| 806 | # (Unfortunately the errors argument is not directly accessible |
| 807 | # from Python, so we can't test that much) |
| 808 | class D(dict): |
| 809 | def __getitem__(self, key): |
| 810 | raise ValueError |
Georg Brandl | edbcc13 | 2007-10-24 21:25:34 +0000 | [diff] [blame] | 811 | #self.assertRaises(ValueError, "\xff".translate, D()) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 812 | self.assertRaises(TypeError, "\xff".translate, {0xff: sys.maxunicode+1}) |
| 813 | self.assertRaises(TypeError, "\xff".translate, {0xff: ()}) |
Walter Dörwald | 30537a4 | 2003-01-08 23:22:13 +0000 | [diff] [blame] | 814 | |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 815 | def test_bug828737(self): |
| 816 | charmap = { |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 817 | ord("&"): "&", |
| 818 | ord("<"): "<", |
| 819 | ord(">"): ">", |
| 820 | ord('"'): """, |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 821 | } |
Tim Peters | 58eb11c | 2004-01-18 20:29:55 +0000 | [diff] [blame] | 822 | |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 823 | for n in (1, 10, 100, 1000): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 824 | text = 'abc<def>ghi'*n |
Walter Dörwald | 4894c30 | 2003-10-24 14:25:28 +0000 | [diff] [blame] | 825 | text.translate(charmap) |
| 826 | |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 827 | def test_mutatingdecodehandler(self): |
| 828 | baddata = [ |
| 829 | ("ascii", b"\xff"), |
| 830 | ("utf-7", b"++"), |
| 831 | ("utf-8", b"\xff"), |
| 832 | ("utf-16", b"\xff"), |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 833 | ("utf-32", b"\xff"), |
Walter Dörwald | e78178e | 2007-07-30 13:31:40 +0000 | [diff] [blame] | 834 | ("unicode-escape", b"\\u123g"), |
| 835 | ("raw-unicode-escape", b"\\u123g"), |
| 836 | ("unicode-internal", b"\xff"), |
| 837 | ] |
| 838 | |
| 839 | def replacing(exc): |
| 840 | if isinstance(exc, UnicodeDecodeError): |
| 841 | exc.object = 42 |
| 842 | return ("\u4242", 0) |
| 843 | else: |
| 844 | raise TypeError("don't know how to handle %r" % exc) |
| 845 | codecs.register_error("test.replacing", replacing) |
| 846 | for (encoding, data) in baddata: |
| 847 | self.assertRaises(TypeError, data.decode, encoding, "test.replacing") |
| 848 | |
| 849 | def mutating(exc): |
| 850 | if isinstance(exc, UnicodeDecodeError): |
| 851 | exc.object[:] = b"" |
| 852 | return ("\u4242", 0) |
| 853 | else: |
| 854 | raise TypeError("don't know how to handle %r" % exc) |
| 855 | codecs.register_error("test.mutating", mutating) |
| 856 | # If the decoder doesn't pick up the modified input the following |
| 857 | # will lead to an endless loop |
| 858 | for (encoding, data) in baddata: |
| 859 | self.assertRaises(TypeError, data.decode, encoding, "test.replacing") |
| 860 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 861 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 862 | test.support.run_unittest(CodecCallbackTest) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 863 | |
| 864 | if __name__ == "__main__": |
| 865 | test_main() |