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