1 | n/a | import codecs |
---|
2 | n/a | import html.entities |
---|
3 | n/a | import sys |
---|
4 | n/a | import test.support |
---|
5 | n/a | import unicodedata |
---|
6 | n/a | import unittest |
---|
7 | n/a | |
---|
8 | n/a | class PosReturn: |
---|
9 | n/a | # this can be used for configurable callbacks |
---|
10 | n/a | |
---|
11 | n/a | def __init__(self): |
---|
12 | n/a | self.pos = 0 |
---|
13 | n/a | |
---|
14 | n/a | def handle(self, exc): |
---|
15 | n/a | oldpos = self.pos |
---|
16 | n/a | realpos = oldpos |
---|
17 | n/a | if realpos<0: |
---|
18 | n/a | realpos = len(exc.object) + realpos |
---|
19 | n/a | # if we don't advance this time, terminate on the next call |
---|
20 | n/a | # otherwise we'd get an endless loop |
---|
21 | n/a | if realpos <= exc.start: |
---|
22 | n/a | self.pos = len(exc.object) |
---|
23 | n/a | return ("<?>", oldpos) |
---|
24 | n/a | |
---|
25 | n/a | # A UnicodeEncodeError object with a bad start attribute |
---|
26 | n/a | class BadStartUnicodeEncodeError(UnicodeEncodeError): |
---|
27 | n/a | def __init__(self): |
---|
28 | n/a | UnicodeEncodeError.__init__(self, "ascii", "", 0, 1, "bad") |
---|
29 | n/a | self.start = [] |
---|
30 | n/a | |
---|
31 | n/a | # A UnicodeEncodeError object with a bad object attribute |
---|
32 | n/a | class BadObjectUnicodeEncodeError(UnicodeEncodeError): |
---|
33 | n/a | def __init__(self): |
---|
34 | n/a | UnicodeEncodeError.__init__(self, "ascii", "", 0, 1, "bad") |
---|
35 | n/a | self.object = [] |
---|
36 | n/a | |
---|
37 | n/a | # A UnicodeDecodeError object without an end attribute |
---|
38 | n/a | class NoEndUnicodeDecodeError(UnicodeDecodeError): |
---|
39 | n/a | def __init__(self): |
---|
40 | n/a | UnicodeDecodeError.__init__(self, "ascii", bytearray(b""), 0, 1, "bad") |
---|
41 | n/a | del self.end |
---|
42 | n/a | |
---|
43 | n/a | # A UnicodeDecodeError object with a bad object attribute |
---|
44 | n/a | class BadObjectUnicodeDecodeError(UnicodeDecodeError): |
---|
45 | n/a | def __init__(self): |
---|
46 | n/a | UnicodeDecodeError.__init__(self, "ascii", bytearray(b""), 0, 1, "bad") |
---|
47 | n/a | self.object = [] |
---|
48 | n/a | |
---|
49 | n/a | # A UnicodeTranslateError object without a start attribute |
---|
50 | n/a | class NoStartUnicodeTranslateError(UnicodeTranslateError): |
---|
51 | n/a | def __init__(self): |
---|
52 | n/a | UnicodeTranslateError.__init__(self, "", 0, 1, "bad") |
---|
53 | n/a | del self.start |
---|
54 | n/a | |
---|
55 | n/a | # A UnicodeTranslateError object without an end attribute |
---|
56 | n/a | class NoEndUnicodeTranslateError(UnicodeTranslateError): |
---|
57 | n/a | def __init__(self): |
---|
58 | n/a | UnicodeTranslateError.__init__(self, "", 0, 1, "bad") |
---|
59 | n/a | del self.end |
---|
60 | n/a | |
---|
61 | n/a | # A UnicodeTranslateError object without an object attribute |
---|
62 | n/a | class NoObjectUnicodeTranslateError(UnicodeTranslateError): |
---|
63 | n/a | def __init__(self): |
---|
64 | n/a | UnicodeTranslateError.__init__(self, "", 0, 1, "bad") |
---|
65 | n/a | del self.object |
---|
66 | n/a | |
---|
67 | n/a | class CodecCallbackTest(unittest.TestCase): |
---|
68 | n/a | |
---|
69 | n/a | def test_xmlcharrefreplace(self): |
---|
70 | n/a | # replace unencodable characters which numeric character entities. |
---|
71 | n/a | # For ascii, latin-1 and charmaps this is completely implemented |
---|
72 | n/a | # in C and should be reasonably fast. |
---|
73 | n/a | s = "\u30b9\u30d1\u30e2 \xe4nd eggs" |
---|
74 | n/a | self.assertEqual( |
---|
75 | n/a | s.encode("ascii", "xmlcharrefreplace"), |
---|
76 | n/a | b"スパモ änd eggs" |
---|
77 | n/a | ) |
---|
78 | n/a | self.assertEqual( |
---|
79 | n/a | s.encode("latin-1", "xmlcharrefreplace"), |
---|
80 | n/a | b"スパモ \xe4nd eggs" |
---|
81 | n/a | ) |
---|
82 | n/a | |
---|
83 | n/a | def test_xmlcharnamereplace(self): |
---|
84 | n/a | # This time use a named character entity for unencodable |
---|
85 | n/a | # characters, if one is available. |
---|
86 | n/a | |
---|
87 | n/a | def xmlcharnamereplace(exc): |
---|
88 | n/a | if not isinstance(exc, UnicodeEncodeError): |
---|
89 | n/a | raise TypeError("don't know how to handle %r" % exc) |
---|
90 | n/a | l = [] |
---|
91 | n/a | for c in exc.object[exc.start:exc.end]: |
---|
92 | n/a | try: |
---|
93 | n/a | l.append("&%s;" % html.entities.codepoint2name[ord(c)]) |
---|
94 | n/a | except KeyError: |
---|
95 | n/a | l.append("&#%d;" % ord(c)) |
---|
96 | n/a | return ("".join(l), exc.end) |
---|
97 | n/a | |
---|
98 | n/a | codecs.register_error( |
---|
99 | n/a | "test.xmlcharnamereplace", xmlcharnamereplace) |
---|
100 | n/a | |
---|
101 | n/a | sin = "\xab\u211c\xbb = \u2329\u1234\u20ac\u232a" |
---|
102 | n/a | sout = b"«ℜ» = ⟨ሴ€⟩" |
---|
103 | n/a | self.assertEqual(sin.encode("ascii", "test.xmlcharnamereplace"), sout) |
---|
104 | n/a | sout = b"\xabℜ\xbb = ⟨ሴ€⟩" |
---|
105 | n/a | self.assertEqual(sin.encode("latin-1", "test.xmlcharnamereplace"), sout) |
---|
106 | n/a | sout = b"\xabℜ\xbb = ⟨ሴ\xa4⟩" |
---|
107 | n/a | self.assertEqual(sin.encode("iso-8859-15", "test.xmlcharnamereplace"), sout) |
---|
108 | n/a | |
---|
109 | n/a | def test_uninamereplace(self): |
---|
110 | n/a | # We're using the names from the unicode database this time, |
---|
111 | n/a | # and we're doing "syntax highlighting" here, i.e. we include |
---|
112 | n/a | # the replaced text in ANSI escape sequences. For this it is |
---|
113 | n/a | # useful that the error handler is not called for every single |
---|
114 | n/a | # unencodable character, but for a complete sequence of |
---|
115 | n/a | # unencodable characters, otherwise we would output many |
---|
116 | n/a | # unnecessary escape sequences. |
---|
117 | n/a | |
---|
118 | n/a | def uninamereplace(exc): |
---|
119 | n/a | if not isinstance(exc, UnicodeEncodeError): |
---|
120 | n/a | raise TypeError("don't know how to handle %r" % exc) |
---|
121 | n/a | l = [] |
---|
122 | n/a | for c in exc.object[exc.start:exc.end]: |
---|
123 | n/a | l.append(unicodedata.name(c, "0x%x" % ord(c))) |
---|
124 | n/a | return ("\033[1m%s\033[0m" % ", ".join(l), exc.end) |
---|
125 | n/a | |
---|
126 | n/a | codecs.register_error( |
---|
127 | n/a | "test.uninamereplace", uninamereplace) |
---|
128 | n/a | |
---|
129 | n/a | sin = "\xac\u1234\u20ac\u8000" |
---|
130 | n/a | sout = b"\033[1mNOT SIGN, ETHIOPIC SYLLABLE SEE, EURO SIGN, CJK UNIFIED IDEOGRAPH-8000\033[0m" |
---|
131 | n/a | self.assertEqual(sin.encode("ascii", "test.uninamereplace"), sout) |
---|
132 | n/a | |
---|
133 | n/a | sout = b"\xac\033[1mETHIOPIC SYLLABLE SEE, EURO SIGN, CJK UNIFIED IDEOGRAPH-8000\033[0m" |
---|
134 | n/a | self.assertEqual(sin.encode("latin-1", "test.uninamereplace"), sout) |
---|
135 | n/a | |
---|
136 | n/a | sout = b"\xac\033[1mETHIOPIC SYLLABLE SEE\033[0m\xa4\033[1mCJK UNIFIED IDEOGRAPH-8000\033[0m" |
---|
137 | n/a | self.assertEqual(sin.encode("iso-8859-15", "test.uninamereplace"), sout) |
---|
138 | n/a | |
---|
139 | n/a | def test_backslashescape(self): |
---|
140 | n/a | # Does the same as the "unicode-escape" encoding, but with different |
---|
141 | n/a | # base encodings. |
---|
142 | n/a | sin = "a\xac\u1234\u20ac\u8000\U0010ffff" |
---|
143 | n/a | sout = b"a\\xac\\u1234\\u20ac\\u8000\\U0010ffff" |
---|
144 | n/a | self.assertEqual(sin.encode("ascii", "backslashreplace"), sout) |
---|
145 | n/a | |
---|
146 | n/a | sout = b"a\xac\\u1234\\u20ac\\u8000\\U0010ffff" |
---|
147 | n/a | self.assertEqual(sin.encode("latin-1", "backslashreplace"), sout) |
---|
148 | n/a | |
---|
149 | n/a | sout = b"a\xac\\u1234\xa4\\u8000\\U0010ffff" |
---|
150 | n/a | self.assertEqual(sin.encode("iso-8859-15", "backslashreplace"), sout) |
---|
151 | n/a | |
---|
152 | n/a | def test_nameescape(self): |
---|
153 | n/a | # Does the same as backslashescape, but prefers ``\N{...}`` escape |
---|
154 | n/a | # sequences. |
---|
155 | n/a | sin = "a\xac\u1234\u20ac\u8000\U0010ffff" |
---|
156 | n/a | sout = (b'a\\N{NOT SIGN}\\N{ETHIOPIC SYLLABLE SEE}\\N{EURO SIGN}' |
---|
157 | n/a | b'\\N{CJK UNIFIED IDEOGRAPH-8000}\\U0010ffff') |
---|
158 | n/a | self.assertEqual(sin.encode("ascii", "namereplace"), sout) |
---|
159 | n/a | |
---|
160 | n/a | sout = (b'a\xac\\N{ETHIOPIC SYLLABLE SEE}\\N{EURO SIGN}' |
---|
161 | n/a | b'\\N{CJK UNIFIED IDEOGRAPH-8000}\\U0010ffff') |
---|
162 | n/a | self.assertEqual(sin.encode("latin-1", "namereplace"), sout) |
---|
163 | n/a | |
---|
164 | n/a | sout = (b'a\xac\\N{ETHIOPIC SYLLABLE SEE}\xa4' |
---|
165 | n/a | b'\\N{CJK UNIFIED IDEOGRAPH-8000}\\U0010ffff') |
---|
166 | n/a | self.assertEqual(sin.encode("iso-8859-15", "namereplace"), sout) |
---|
167 | n/a | |
---|
168 | n/a | def test_decoding_callbacks(self): |
---|
169 | n/a | # This is a test for a decoding callback handler |
---|
170 | n/a | # that allows the decoding of the invalid sequence |
---|
171 | n/a | # "\xc0\x80" and returns "\x00" instead of raising an error. |
---|
172 | n/a | # All other illegal sequences will be handled strictly. |
---|
173 | n/a | def relaxedutf8(exc): |
---|
174 | n/a | if not isinstance(exc, UnicodeDecodeError): |
---|
175 | n/a | raise TypeError("don't know how to handle %r" % exc) |
---|
176 | n/a | if exc.object[exc.start:exc.start+2] == b"\xc0\x80": |
---|
177 | n/a | return ("\x00", exc.start+2) # retry after two bytes |
---|
178 | n/a | else: |
---|
179 | n/a | raise exc |
---|
180 | n/a | |
---|
181 | n/a | codecs.register_error("test.relaxedutf8", relaxedutf8) |
---|
182 | n/a | |
---|
183 | n/a | # all the "\xc0\x80" will be decoded to "\x00" |
---|
184 | n/a | sin = b"a\x00b\xc0\x80c\xc3\xbc\xc0\x80\xc0\x80" |
---|
185 | n/a | sout = "a\x00b\x00c\xfc\x00\x00" |
---|
186 | n/a | self.assertEqual(sin.decode("utf-8", "test.relaxedutf8"), sout) |
---|
187 | n/a | |
---|
188 | n/a | # "\xc0\x81" is not valid and a UnicodeDecodeError will be raised |
---|
189 | n/a | sin = b"\xc0\x80\xc0\x81" |
---|
190 | n/a | self.assertRaises(UnicodeDecodeError, sin.decode, |
---|
191 | n/a | "utf-8", "test.relaxedutf8") |
---|
192 | n/a | |
---|
193 | n/a | def test_charmapencode(self): |
---|
194 | n/a | # For charmap encodings the replacement string will be |
---|
195 | n/a | # mapped through the encoding again. This means, that |
---|
196 | n/a | # to be able to use e.g. the "replace" handler, the |
---|
197 | n/a | # charmap has to have a mapping for "?". |
---|
198 | n/a | charmap = dict((ord(c), bytes(2*c.upper(), 'ascii')) for c in "abcdefgh") |
---|
199 | n/a | sin = "abc" |
---|
200 | n/a | sout = b"AABBCC" |
---|
201 | n/a | self.assertEqual(codecs.charmap_encode(sin, "strict", charmap)[0], sout) |
---|
202 | n/a | |
---|
203 | n/a | sin = "abcA" |
---|
204 | n/a | self.assertRaises(UnicodeError, codecs.charmap_encode, sin, "strict", charmap) |
---|
205 | n/a | |
---|
206 | n/a | charmap[ord("?")] = b"XYZ" |
---|
207 | n/a | sin = "abcDEF" |
---|
208 | n/a | sout = b"AABBCCXYZXYZXYZ" |
---|
209 | n/a | self.assertEqual(codecs.charmap_encode(sin, "replace", charmap)[0], sout) |
---|
210 | n/a | |
---|
211 | n/a | charmap[ord("?")] = "XYZ" # wrong type in mapping |
---|
212 | n/a | self.assertRaises(TypeError, codecs.charmap_encode, sin, "replace", charmap) |
---|
213 | n/a | |
---|
214 | n/a | def test_decodeunicodeinternal(self): |
---|
215 | n/a | with test.support.check_warnings(('unicode_internal codec has been ' |
---|
216 | n/a | 'deprecated', DeprecationWarning)): |
---|
217 | n/a | self.assertRaises( |
---|
218 | n/a | UnicodeDecodeError, |
---|
219 | n/a | b"\x00\x00\x00\x00\x00".decode, |
---|
220 | n/a | "unicode-internal", |
---|
221 | n/a | ) |
---|
222 | n/a | if len('\0'.encode('unicode-internal')) == 4: |
---|
223 | n/a | def handler_unicodeinternal(exc): |
---|
224 | n/a | if not isinstance(exc, UnicodeDecodeError): |
---|
225 | n/a | raise TypeError("don't know how to handle %r" % exc) |
---|
226 | n/a | return ("\x01", 1) |
---|
227 | n/a | |
---|
228 | n/a | self.assertEqual( |
---|
229 | n/a | b"\x00\x00\x00\x00\x00".decode("unicode-internal", "ignore"), |
---|
230 | n/a | "\u0000" |
---|
231 | n/a | ) |
---|
232 | n/a | |
---|
233 | n/a | self.assertEqual( |
---|
234 | n/a | b"\x00\x00\x00\x00\x00".decode("unicode-internal", "replace"), |
---|
235 | n/a | "\u0000\ufffd" |
---|
236 | n/a | ) |
---|
237 | n/a | |
---|
238 | n/a | self.assertEqual( |
---|
239 | n/a | b"\x00\x00\x00\x00\x00".decode("unicode-internal", "backslashreplace"), |
---|
240 | n/a | "\u0000\\x00" |
---|
241 | n/a | ) |
---|
242 | n/a | |
---|
243 | n/a | codecs.register_error("test.hui", handler_unicodeinternal) |
---|
244 | n/a | |
---|
245 | n/a | self.assertEqual( |
---|
246 | n/a | b"\x00\x00\x00\x00\x00".decode("unicode-internal", "test.hui"), |
---|
247 | n/a | "\u0000\u0001\u0000" |
---|
248 | n/a | ) |
---|
249 | n/a | |
---|
250 | n/a | def test_callbacks(self): |
---|
251 | n/a | def handler1(exc): |
---|
252 | n/a | r = range(exc.start, exc.end) |
---|
253 | n/a | if isinstance(exc, UnicodeEncodeError): |
---|
254 | n/a | l = ["<%d>" % ord(exc.object[pos]) for pos in r] |
---|
255 | n/a | elif isinstance(exc, UnicodeDecodeError): |
---|
256 | n/a | l = ["<%d>" % exc.object[pos] for pos in r] |
---|
257 | n/a | else: |
---|
258 | n/a | raise TypeError("don't know how to handle %r" % exc) |
---|
259 | n/a | return ("[%s]" % "".join(l), exc.end) |
---|
260 | n/a | |
---|
261 | n/a | codecs.register_error("test.handler1", handler1) |
---|
262 | n/a | |
---|
263 | n/a | def handler2(exc): |
---|
264 | n/a | if not isinstance(exc, UnicodeDecodeError): |
---|
265 | n/a | raise TypeError("don't know how to handle %r" % exc) |
---|
266 | n/a | l = ["<%d>" % exc.object[pos] for pos in range(exc.start, exc.end)] |
---|
267 | n/a | return ("[%s]" % "".join(l), exc.end+1) # skip one character |
---|
268 | n/a | |
---|
269 | n/a | codecs.register_error("test.handler2", handler2) |
---|
270 | n/a | |
---|
271 | n/a | s = b"\x00\x81\x7f\x80\xff" |
---|
272 | n/a | |
---|
273 | n/a | self.assertEqual( |
---|
274 | n/a | s.decode("ascii", "test.handler1"), |
---|
275 | n/a | "\x00[<129>]\x7f[<128>][<255>]" |
---|
276 | n/a | ) |
---|
277 | n/a | self.assertEqual( |
---|
278 | n/a | s.decode("ascii", "test.handler2"), |
---|
279 | n/a | "\x00[<129>][<128>]" |
---|
280 | n/a | ) |
---|
281 | n/a | |
---|
282 | n/a | self.assertEqual( |
---|
283 | n/a | b"\\u3042\\u3xxx".decode("unicode-escape", "test.handler1"), |
---|
284 | n/a | "\u3042[<92><117><51>]xxx" |
---|
285 | n/a | ) |
---|
286 | n/a | |
---|
287 | n/a | self.assertEqual( |
---|
288 | n/a | b"\\u3042\\u3xx".decode("unicode-escape", "test.handler1"), |
---|
289 | n/a | "\u3042[<92><117><51>]xx" |
---|
290 | n/a | ) |
---|
291 | n/a | |
---|
292 | n/a | self.assertEqual( |
---|
293 | n/a | codecs.charmap_decode(b"abc", "test.handler1", {ord("a"): "z"})[0], |
---|
294 | n/a | "z[<98>][<99>]" |
---|
295 | n/a | ) |
---|
296 | n/a | |
---|
297 | n/a | self.assertEqual( |
---|
298 | n/a | "g\xfc\xdfrk".encode("ascii", "test.handler1"), |
---|
299 | n/a | b"g[<252><223>]rk" |
---|
300 | n/a | ) |
---|
301 | n/a | |
---|
302 | n/a | self.assertEqual( |
---|
303 | n/a | "g\xfc\xdf".encode("ascii", "test.handler1"), |
---|
304 | n/a | b"g[<252><223>]" |
---|
305 | n/a | ) |
---|
306 | n/a | |
---|
307 | n/a | def test_longstrings(self): |
---|
308 | n/a | # test long strings to check for memory overflow problems |
---|
309 | n/a | errors = [ "strict", "ignore", "replace", "xmlcharrefreplace", |
---|
310 | n/a | "backslashreplace", "namereplace"] |
---|
311 | n/a | # register the handlers under different names, |
---|
312 | n/a | # to prevent the codec from recognizing the name |
---|
313 | n/a | for err in errors: |
---|
314 | n/a | codecs.register_error("test." + err, codecs.lookup_error(err)) |
---|
315 | n/a | l = 1000 |
---|
316 | n/a | errors += [ "test." + err for err in errors ] |
---|
317 | n/a | for uni in [ s*l for s in ("x", "\u3042", "a\xe4") ]: |
---|
318 | n/a | for enc in ("ascii", "latin-1", "iso-8859-1", "iso-8859-15", |
---|
319 | n/a | "utf-8", "utf-7", "utf-16", "utf-32"): |
---|
320 | n/a | for err in errors: |
---|
321 | n/a | try: |
---|
322 | n/a | uni.encode(enc, err) |
---|
323 | n/a | except UnicodeError: |
---|
324 | n/a | pass |
---|
325 | n/a | |
---|
326 | n/a | def check_exceptionobjectargs(self, exctype, args, msg): |
---|
327 | n/a | # Test UnicodeError subclasses: construction, attribute assignment and __str__ conversion |
---|
328 | n/a | # check with one missing argument |
---|
329 | n/a | self.assertRaises(TypeError, exctype, *args[:-1]) |
---|
330 | n/a | # check with one argument too much |
---|
331 | n/a | self.assertRaises(TypeError, exctype, *(args + ["too much"])) |
---|
332 | n/a | # check with one argument of the wrong type |
---|
333 | n/a | wrongargs = [ "spam", b"eggs", b"spam", 42, 1.0, None ] |
---|
334 | n/a | for i in range(len(args)): |
---|
335 | n/a | for wrongarg in wrongargs: |
---|
336 | n/a | if type(wrongarg) is type(args[i]): |
---|
337 | n/a | continue |
---|
338 | n/a | # build argument array |
---|
339 | n/a | callargs = [] |
---|
340 | n/a | for j in range(len(args)): |
---|
341 | n/a | if i==j: |
---|
342 | n/a | callargs.append(wrongarg) |
---|
343 | n/a | else: |
---|
344 | n/a | callargs.append(args[i]) |
---|
345 | n/a | self.assertRaises(TypeError, exctype, *callargs) |
---|
346 | n/a | |
---|
347 | n/a | # check with the correct number and type of arguments |
---|
348 | n/a | exc = exctype(*args) |
---|
349 | n/a | self.assertEqual(str(exc), msg) |
---|
350 | n/a | |
---|
351 | n/a | def test_unicodeencodeerror(self): |
---|
352 | n/a | self.check_exceptionobjectargs( |
---|
353 | n/a | UnicodeEncodeError, |
---|
354 | n/a | ["ascii", "g\xfcrk", 1, 2, "ouch"], |
---|
355 | n/a | "'ascii' codec can't encode character '\\xfc' in position 1: ouch" |
---|
356 | n/a | ) |
---|
357 | n/a | self.check_exceptionobjectargs( |
---|
358 | n/a | UnicodeEncodeError, |
---|
359 | n/a | ["ascii", "g\xfcrk", 1, 4, "ouch"], |
---|
360 | n/a | "'ascii' codec can't encode characters in position 1-3: ouch" |
---|
361 | n/a | ) |
---|
362 | n/a | self.check_exceptionobjectargs( |
---|
363 | n/a | UnicodeEncodeError, |
---|
364 | n/a | ["ascii", "\xfcx", 0, 1, "ouch"], |
---|
365 | n/a | "'ascii' codec can't encode character '\\xfc' in position 0: ouch" |
---|
366 | n/a | ) |
---|
367 | n/a | self.check_exceptionobjectargs( |
---|
368 | n/a | UnicodeEncodeError, |
---|
369 | n/a | ["ascii", "\u0100x", 0, 1, "ouch"], |
---|
370 | n/a | "'ascii' codec can't encode character '\\u0100' in position 0: ouch" |
---|
371 | n/a | ) |
---|
372 | n/a | self.check_exceptionobjectargs( |
---|
373 | n/a | UnicodeEncodeError, |
---|
374 | n/a | ["ascii", "\uffffx", 0, 1, "ouch"], |
---|
375 | n/a | "'ascii' codec can't encode character '\\uffff' in position 0: ouch" |
---|
376 | n/a | ) |
---|
377 | n/a | self.check_exceptionobjectargs( |
---|
378 | n/a | UnicodeEncodeError, |
---|
379 | n/a | ["ascii", "\U00010000x", 0, 1, "ouch"], |
---|
380 | n/a | "'ascii' codec can't encode character '\\U00010000' in position 0: ouch" |
---|
381 | n/a | ) |
---|
382 | n/a | |
---|
383 | n/a | def test_unicodedecodeerror(self): |
---|
384 | n/a | self.check_exceptionobjectargs( |
---|
385 | n/a | UnicodeDecodeError, |
---|
386 | n/a | ["ascii", bytearray(b"g\xfcrk"), 1, 2, "ouch"], |
---|
387 | n/a | "'ascii' codec can't decode byte 0xfc in position 1: ouch" |
---|
388 | n/a | ) |
---|
389 | n/a | self.check_exceptionobjectargs( |
---|
390 | n/a | UnicodeDecodeError, |
---|
391 | n/a | ["ascii", bytearray(b"g\xfcrk"), 1, 3, "ouch"], |
---|
392 | n/a | "'ascii' codec can't decode bytes in position 1-2: ouch" |
---|
393 | n/a | ) |
---|
394 | n/a | |
---|
395 | n/a | def test_unicodetranslateerror(self): |
---|
396 | n/a | self.check_exceptionobjectargs( |
---|
397 | n/a | UnicodeTranslateError, |
---|
398 | n/a | ["g\xfcrk", 1, 2, "ouch"], |
---|
399 | n/a | "can't translate character '\\xfc' in position 1: ouch" |
---|
400 | n/a | ) |
---|
401 | n/a | self.check_exceptionobjectargs( |
---|
402 | n/a | UnicodeTranslateError, |
---|
403 | n/a | ["g\u0100rk", 1, 2, "ouch"], |
---|
404 | n/a | "can't translate character '\\u0100' in position 1: ouch" |
---|
405 | n/a | ) |
---|
406 | n/a | self.check_exceptionobjectargs( |
---|
407 | n/a | UnicodeTranslateError, |
---|
408 | n/a | ["g\uffffrk", 1, 2, "ouch"], |
---|
409 | n/a | "can't translate character '\\uffff' in position 1: ouch" |
---|
410 | n/a | ) |
---|
411 | n/a | self.check_exceptionobjectargs( |
---|
412 | n/a | UnicodeTranslateError, |
---|
413 | n/a | ["g\U00010000rk", 1, 2, "ouch"], |
---|
414 | n/a | "can't translate character '\\U00010000' in position 1: ouch" |
---|
415 | n/a | ) |
---|
416 | n/a | self.check_exceptionobjectargs( |
---|
417 | n/a | UnicodeTranslateError, |
---|
418 | n/a | ["g\xfcrk", 1, 3, "ouch"], |
---|
419 | n/a | "can't translate characters in position 1-2: ouch" |
---|
420 | n/a | ) |
---|
421 | n/a | |
---|
422 | n/a | def test_badandgoodstrictexceptions(self): |
---|
423 | n/a | # "strict" complains about a non-exception passed in |
---|
424 | n/a | self.assertRaises( |
---|
425 | n/a | TypeError, |
---|
426 | n/a | codecs.strict_errors, |
---|
427 | n/a | 42 |
---|
428 | n/a | ) |
---|
429 | n/a | # "strict" complains about the wrong exception type |
---|
430 | n/a | self.assertRaises( |
---|
431 | n/a | Exception, |
---|
432 | n/a | codecs.strict_errors, |
---|
433 | n/a | Exception("ouch") |
---|
434 | n/a | ) |
---|
435 | n/a | |
---|
436 | n/a | # If the correct exception is passed in, "strict" raises it |
---|
437 | n/a | self.assertRaises( |
---|
438 | n/a | UnicodeEncodeError, |
---|
439 | n/a | codecs.strict_errors, |
---|
440 | n/a | UnicodeEncodeError("ascii", "\u3042", 0, 1, "ouch") |
---|
441 | n/a | ) |
---|
442 | n/a | self.assertRaises( |
---|
443 | n/a | UnicodeDecodeError, |
---|
444 | n/a | codecs.strict_errors, |
---|
445 | n/a | UnicodeDecodeError("ascii", bytearray(b"\xff"), 0, 1, "ouch") |
---|
446 | n/a | ) |
---|
447 | n/a | self.assertRaises( |
---|
448 | n/a | UnicodeTranslateError, |
---|
449 | n/a | codecs.strict_errors, |
---|
450 | n/a | UnicodeTranslateError("\u3042", 0, 1, "ouch") |
---|
451 | n/a | ) |
---|
452 | n/a | |
---|
453 | n/a | def test_badandgoodignoreexceptions(self): |
---|
454 | n/a | # "ignore" complains about a non-exception passed in |
---|
455 | n/a | self.assertRaises( |
---|
456 | n/a | TypeError, |
---|
457 | n/a | codecs.ignore_errors, |
---|
458 | n/a | 42 |
---|
459 | n/a | ) |
---|
460 | n/a | # "ignore" complains about the wrong exception type |
---|
461 | n/a | self.assertRaises( |
---|
462 | n/a | TypeError, |
---|
463 | n/a | codecs.ignore_errors, |
---|
464 | n/a | UnicodeError("ouch") |
---|
465 | n/a | ) |
---|
466 | n/a | # If the correct exception is passed in, "ignore" returns an empty replacement |
---|
467 | n/a | self.assertEqual( |
---|
468 | n/a | codecs.ignore_errors( |
---|
469 | n/a | UnicodeEncodeError("ascii", "a\u3042b", 1, 2, "ouch")), |
---|
470 | n/a | ("", 2) |
---|
471 | n/a | ) |
---|
472 | n/a | self.assertEqual( |
---|
473 | n/a | codecs.ignore_errors( |
---|
474 | n/a | UnicodeDecodeError("ascii", bytearray(b"a\xffb"), 1, 2, "ouch")), |
---|
475 | n/a | ("", 2) |
---|
476 | n/a | ) |
---|
477 | n/a | self.assertEqual( |
---|
478 | n/a | codecs.ignore_errors( |
---|
479 | n/a | UnicodeTranslateError("a\u3042b", 1, 2, "ouch")), |
---|
480 | n/a | ("", 2) |
---|
481 | n/a | ) |
---|
482 | n/a | |
---|
483 | n/a | def test_badandgoodreplaceexceptions(self): |
---|
484 | n/a | # "replace" complains about a non-exception passed in |
---|
485 | n/a | self.assertRaises( |
---|
486 | n/a | TypeError, |
---|
487 | n/a | codecs.replace_errors, |
---|
488 | n/a | 42 |
---|
489 | n/a | ) |
---|
490 | n/a | # "replace" complains about the wrong exception type |
---|
491 | n/a | self.assertRaises( |
---|
492 | n/a | TypeError, |
---|
493 | n/a | codecs.replace_errors, |
---|
494 | n/a | UnicodeError("ouch") |
---|
495 | n/a | ) |
---|
496 | n/a | self.assertRaises( |
---|
497 | n/a | TypeError, |
---|
498 | n/a | codecs.replace_errors, |
---|
499 | n/a | BadObjectUnicodeEncodeError() |
---|
500 | n/a | ) |
---|
501 | n/a | self.assertRaises( |
---|
502 | n/a | TypeError, |
---|
503 | n/a | codecs.replace_errors, |
---|
504 | n/a | BadObjectUnicodeDecodeError() |
---|
505 | n/a | ) |
---|
506 | n/a | # With the correct exception, "replace" returns an "?" or "\ufffd" replacement |
---|
507 | n/a | self.assertEqual( |
---|
508 | n/a | codecs.replace_errors( |
---|
509 | n/a | UnicodeEncodeError("ascii", "a\u3042b", 1, 2, "ouch")), |
---|
510 | n/a | ("?", 2) |
---|
511 | n/a | ) |
---|
512 | n/a | self.assertEqual( |
---|
513 | n/a | codecs.replace_errors( |
---|
514 | n/a | UnicodeDecodeError("ascii", bytearray(b"a\xffb"), 1, 2, "ouch")), |
---|
515 | n/a | ("\ufffd", 2) |
---|
516 | n/a | ) |
---|
517 | n/a | self.assertEqual( |
---|
518 | n/a | codecs.replace_errors( |
---|
519 | n/a | UnicodeTranslateError("a\u3042b", 1, 2, "ouch")), |
---|
520 | n/a | ("\ufffd", 2) |
---|
521 | n/a | ) |
---|
522 | n/a | |
---|
523 | n/a | def test_badandgoodxmlcharrefreplaceexceptions(self): |
---|
524 | n/a | # "xmlcharrefreplace" complains about a non-exception passed in |
---|
525 | n/a | self.assertRaises( |
---|
526 | n/a | TypeError, |
---|
527 | n/a | codecs.xmlcharrefreplace_errors, |
---|
528 | n/a | 42 |
---|
529 | n/a | ) |
---|
530 | n/a | # "xmlcharrefreplace" complains about the wrong exception types |
---|
531 | n/a | self.assertRaises( |
---|
532 | n/a | TypeError, |
---|
533 | n/a | codecs.xmlcharrefreplace_errors, |
---|
534 | n/a | UnicodeError("ouch") |
---|
535 | n/a | ) |
---|
536 | n/a | # "xmlcharrefreplace" can only be used for encoding |
---|
537 | n/a | self.assertRaises( |
---|
538 | n/a | TypeError, |
---|
539 | n/a | codecs.xmlcharrefreplace_errors, |
---|
540 | n/a | UnicodeDecodeError("ascii", bytearray(b"\xff"), 0, 1, "ouch") |
---|
541 | n/a | ) |
---|
542 | n/a | self.assertRaises( |
---|
543 | n/a | TypeError, |
---|
544 | n/a | codecs.xmlcharrefreplace_errors, |
---|
545 | n/a | UnicodeTranslateError("\u3042", 0, 1, "ouch") |
---|
546 | n/a | ) |
---|
547 | n/a | # Use the correct exception |
---|
548 | n/a | cs = (0, 1, 9, 10, 99, 100, 999, 1000, 9999, 10000, 99999, 100000, |
---|
549 | n/a | 999999, 1000000) |
---|
550 | n/a | cs += (0xd800, 0xdfff) |
---|
551 | n/a | s = "".join(chr(c) for c in cs) |
---|
552 | n/a | self.assertEqual( |
---|
553 | n/a | codecs.xmlcharrefreplace_errors( |
---|
554 | n/a | UnicodeEncodeError("ascii", "a" + s + "b", |
---|
555 | n/a | 1, 1 + len(s), "ouch") |
---|
556 | n/a | ), |
---|
557 | n/a | ("".join("&#%d;" % c for c in cs), 1 + len(s)) |
---|
558 | n/a | ) |
---|
559 | n/a | |
---|
560 | n/a | def test_badandgoodbackslashreplaceexceptions(self): |
---|
561 | n/a | # "backslashreplace" complains about a non-exception passed in |
---|
562 | n/a | self.assertRaises( |
---|
563 | n/a | TypeError, |
---|
564 | n/a | codecs.backslashreplace_errors, |
---|
565 | n/a | 42 |
---|
566 | n/a | ) |
---|
567 | n/a | # "backslashreplace" complains about the wrong exception types |
---|
568 | n/a | self.assertRaises( |
---|
569 | n/a | TypeError, |
---|
570 | n/a | codecs.backslashreplace_errors, |
---|
571 | n/a | UnicodeError("ouch") |
---|
572 | n/a | ) |
---|
573 | n/a | # Use the correct exception |
---|
574 | n/a | tests = [ |
---|
575 | n/a | ("\u3042", "\\u3042"), |
---|
576 | n/a | ("\n", "\\x0a"), |
---|
577 | n/a | ("a", "\\x61"), |
---|
578 | n/a | ("\x00", "\\x00"), |
---|
579 | n/a | ("\xff", "\\xff"), |
---|
580 | n/a | ("\u0100", "\\u0100"), |
---|
581 | n/a | ("\uffff", "\\uffff"), |
---|
582 | n/a | ("\U00010000", "\\U00010000"), |
---|
583 | n/a | ("\U0010ffff", "\\U0010ffff"), |
---|
584 | n/a | # Lone surrogates |
---|
585 | n/a | ("\ud800", "\\ud800"), |
---|
586 | n/a | ("\udfff", "\\udfff"), |
---|
587 | n/a | ("\ud800\udfff", "\\ud800\\udfff"), |
---|
588 | n/a | ] |
---|
589 | n/a | for s, r in tests: |
---|
590 | n/a | with self.subTest(str=s): |
---|
591 | n/a | self.assertEqual( |
---|
592 | n/a | codecs.backslashreplace_errors( |
---|
593 | n/a | UnicodeEncodeError("ascii", "a" + s + "b", |
---|
594 | n/a | 1, 1 + len(s), "ouch")), |
---|
595 | n/a | (r, 1 + len(s)) |
---|
596 | n/a | ) |
---|
597 | n/a | self.assertEqual( |
---|
598 | n/a | codecs.backslashreplace_errors( |
---|
599 | n/a | UnicodeTranslateError("a" + s + "b", |
---|
600 | n/a | 1, 1 + len(s), "ouch")), |
---|
601 | n/a | (r, 1 + len(s)) |
---|
602 | n/a | ) |
---|
603 | n/a | tests = [ |
---|
604 | n/a | (b"a", "\\x61"), |
---|
605 | n/a | (b"\n", "\\x0a"), |
---|
606 | n/a | (b"\x00", "\\x00"), |
---|
607 | n/a | (b"\xff", "\\xff"), |
---|
608 | n/a | ] |
---|
609 | n/a | for b, r in tests: |
---|
610 | n/a | with self.subTest(bytes=b): |
---|
611 | n/a | self.assertEqual( |
---|
612 | n/a | codecs.backslashreplace_errors( |
---|
613 | n/a | UnicodeDecodeError("ascii", bytearray(b"a" + b + b"b"), |
---|
614 | n/a | 1, 2, "ouch")), |
---|
615 | n/a | (r, 2) |
---|
616 | n/a | ) |
---|
617 | n/a | |
---|
618 | n/a | def test_badandgoodnamereplaceexceptions(self): |
---|
619 | n/a | # "namereplace" complains about a non-exception passed in |
---|
620 | n/a | self.assertRaises( |
---|
621 | n/a | TypeError, |
---|
622 | n/a | codecs.namereplace_errors, |
---|
623 | n/a | 42 |
---|
624 | n/a | ) |
---|
625 | n/a | # "namereplace" complains about the wrong exception types |
---|
626 | n/a | self.assertRaises( |
---|
627 | n/a | TypeError, |
---|
628 | n/a | codecs.namereplace_errors, |
---|
629 | n/a | UnicodeError("ouch") |
---|
630 | n/a | ) |
---|
631 | n/a | # "namereplace" can only be used for encoding |
---|
632 | n/a | self.assertRaises( |
---|
633 | n/a | TypeError, |
---|
634 | n/a | codecs.namereplace_errors, |
---|
635 | n/a | UnicodeDecodeError("ascii", bytearray(b"\xff"), 0, 1, "ouch") |
---|
636 | n/a | ) |
---|
637 | n/a | self.assertRaises( |
---|
638 | n/a | TypeError, |
---|
639 | n/a | codecs.namereplace_errors, |
---|
640 | n/a | UnicodeTranslateError("\u3042", 0, 1, "ouch") |
---|
641 | n/a | ) |
---|
642 | n/a | # Use the correct exception |
---|
643 | n/a | tests = [ |
---|
644 | n/a | ("\u3042", "\\N{HIRAGANA LETTER A}"), |
---|
645 | n/a | ("\x00", "\\x00"), |
---|
646 | n/a | ("\ufbf9", "\\N{ARABIC LIGATURE UIGHUR KIRGHIZ YEH WITH " |
---|
647 | n/a | "HAMZA ABOVE WITH ALEF MAKSURA ISOLATED FORM}"), |
---|
648 | n/a | ("\U000e007f", "\\N{CANCEL TAG}"), |
---|
649 | n/a | ("\U0010ffff", "\\U0010ffff"), |
---|
650 | n/a | # Lone surrogates |
---|
651 | n/a | ("\ud800", "\\ud800"), |
---|
652 | n/a | ("\udfff", "\\udfff"), |
---|
653 | n/a | ("\ud800\udfff", "\\ud800\\udfff"), |
---|
654 | n/a | ] |
---|
655 | n/a | for s, r in tests: |
---|
656 | n/a | with self.subTest(str=s): |
---|
657 | n/a | self.assertEqual( |
---|
658 | n/a | codecs.namereplace_errors( |
---|
659 | n/a | UnicodeEncodeError("ascii", "a" + s + "b", |
---|
660 | n/a | 1, 1 + len(s), "ouch")), |
---|
661 | n/a | (r, 1 + len(s)) |
---|
662 | n/a | ) |
---|
663 | n/a | |
---|
664 | n/a | def test_badandgoodsurrogateescapeexceptions(self): |
---|
665 | n/a | surrogateescape_errors = codecs.lookup_error('surrogateescape') |
---|
666 | n/a | # "surrogateescape" complains about a non-exception passed in |
---|
667 | n/a | self.assertRaises( |
---|
668 | n/a | TypeError, |
---|
669 | n/a | surrogateescape_errors, |
---|
670 | n/a | 42 |
---|
671 | n/a | ) |
---|
672 | n/a | # "surrogateescape" complains about the wrong exception types |
---|
673 | n/a | self.assertRaises( |
---|
674 | n/a | TypeError, |
---|
675 | n/a | surrogateescape_errors, |
---|
676 | n/a | UnicodeError("ouch") |
---|
677 | n/a | ) |
---|
678 | n/a | # "surrogateescape" can not be used for translating |
---|
679 | n/a | self.assertRaises( |
---|
680 | n/a | TypeError, |
---|
681 | n/a | surrogateescape_errors, |
---|
682 | n/a | UnicodeTranslateError("\udc80", 0, 1, "ouch") |
---|
683 | n/a | ) |
---|
684 | n/a | # Use the correct exception |
---|
685 | n/a | for s in ("a", "\udc7f", "\udd00"): |
---|
686 | n/a | with self.subTest(str=s): |
---|
687 | n/a | self.assertRaises( |
---|
688 | n/a | UnicodeEncodeError, |
---|
689 | n/a | surrogateescape_errors, |
---|
690 | n/a | UnicodeEncodeError("ascii", s, 0, 1, "ouch") |
---|
691 | n/a | ) |
---|
692 | n/a | self.assertEqual( |
---|
693 | n/a | surrogateescape_errors( |
---|
694 | n/a | UnicodeEncodeError("ascii", "a\udc80b", 1, 2, "ouch")), |
---|
695 | n/a | (b"\x80", 2) |
---|
696 | n/a | ) |
---|
697 | n/a | self.assertRaises( |
---|
698 | n/a | UnicodeDecodeError, |
---|
699 | n/a | surrogateescape_errors, |
---|
700 | n/a | UnicodeDecodeError("ascii", bytearray(b"a"), 0, 1, "ouch") |
---|
701 | n/a | ) |
---|
702 | n/a | self.assertEqual( |
---|
703 | n/a | surrogateescape_errors( |
---|
704 | n/a | UnicodeDecodeError("ascii", bytearray(b"a\x80b"), 1, 2, "ouch")), |
---|
705 | n/a | ("\udc80", 2) |
---|
706 | n/a | ) |
---|
707 | n/a | |
---|
708 | n/a | def test_badandgoodsurrogatepassexceptions(self): |
---|
709 | n/a | surrogatepass_errors = codecs.lookup_error('surrogatepass') |
---|
710 | n/a | # "surrogatepass" complains about a non-exception passed in |
---|
711 | n/a | self.assertRaises( |
---|
712 | n/a | TypeError, |
---|
713 | n/a | surrogatepass_errors, |
---|
714 | n/a | 42 |
---|
715 | n/a | ) |
---|
716 | n/a | # "surrogatepass" complains about the wrong exception types |
---|
717 | n/a | self.assertRaises( |
---|
718 | n/a | TypeError, |
---|
719 | n/a | surrogatepass_errors, |
---|
720 | n/a | UnicodeError("ouch") |
---|
721 | n/a | ) |
---|
722 | n/a | # "surrogatepass" can not be used for translating |
---|
723 | n/a | self.assertRaises( |
---|
724 | n/a | TypeError, |
---|
725 | n/a | surrogatepass_errors, |
---|
726 | n/a | UnicodeTranslateError("\ud800", 0, 1, "ouch") |
---|
727 | n/a | ) |
---|
728 | n/a | # Use the correct exception |
---|
729 | n/a | for enc in ("utf-8", "utf-16le", "utf-16be", "utf-32le", "utf-32be"): |
---|
730 | n/a | with self.subTest(encoding=enc): |
---|
731 | n/a | self.assertRaises( |
---|
732 | n/a | UnicodeEncodeError, |
---|
733 | n/a | surrogatepass_errors, |
---|
734 | n/a | UnicodeEncodeError(enc, "a", 0, 1, "ouch") |
---|
735 | n/a | ) |
---|
736 | n/a | self.assertRaises( |
---|
737 | n/a | UnicodeDecodeError, |
---|
738 | n/a | surrogatepass_errors, |
---|
739 | n/a | UnicodeDecodeError(enc, "a".encode(enc), 0, 1, "ouch") |
---|
740 | n/a | ) |
---|
741 | n/a | for s in ("\ud800", "\udfff", "\ud800\udfff"): |
---|
742 | n/a | with self.subTest(str=s): |
---|
743 | n/a | self.assertRaises( |
---|
744 | n/a | UnicodeEncodeError, |
---|
745 | n/a | surrogatepass_errors, |
---|
746 | n/a | UnicodeEncodeError("ascii", s, 0, len(s), "ouch") |
---|
747 | n/a | ) |
---|
748 | n/a | tests = [ |
---|
749 | n/a | ("utf-8", "\ud800", b'\xed\xa0\x80', 3), |
---|
750 | n/a | ("utf-16le", "\ud800", b'\x00\xd8', 2), |
---|
751 | n/a | ("utf-16be", "\ud800", b'\xd8\x00', 2), |
---|
752 | n/a | ("utf-32le", "\ud800", b'\x00\xd8\x00\x00', 4), |
---|
753 | n/a | ("utf-32be", "\ud800", b'\x00\x00\xd8\x00', 4), |
---|
754 | n/a | ("utf-8", "\udfff", b'\xed\xbf\xbf', 3), |
---|
755 | n/a | ("utf-16le", "\udfff", b'\xff\xdf', 2), |
---|
756 | n/a | ("utf-16be", "\udfff", b'\xdf\xff', 2), |
---|
757 | n/a | ("utf-32le", "\udfff", b'\xff\xdf\x00\x00', 4), |
---|
758 | n/a | ("utf-32be", "\udfff", b'\x00\x00\xdf\xff', 4), |
---|
759 | n/a | ("utf-8", "\ud800\udfff", b'\xed\xa0\x80\xed\xbf\xbf', 3), |
---|
760 | n/a | ("utf-16le", "\ud800\udfff", b'\x00\xd8\xff\xdf', 2), |
---|
761 | n/a | ("utf-16be", "\ud800\udfff", b'\xd8\x00\xdf\xff', 2), |
---|
762 | n/a | ("utf-32le", "\ud800\udfff", b'\x00\xd8\x00\x00\xff\xdf\x00\x00', 4), |
---|
763 | n/a | ("utf-32be", "\ud800\udfff", b'\x00\x00\xd8\x00\x00\x00\xdf\xff', 4), |
---|
764 | n/a | ] |
---|
765 | n/a | for enc, s, b, n in tests: |
---|
766 | n/a | with self.subTest(encoding=enc, str=s, bytes=b): |
---|
767 | n/a | self.assertEqual( |
---|
768 | n/a | surrogatepass_errors( |
---|
769 | n/a | UnicodeEncodeError(enc, "a" + s + "b", |
---|
770 | n/a | 1, 1 + len(s), "ouch")), |
---|
771 | n/a | (b, 1 + len(s)) |
---|
772 | n/a | ) |
---|
773 | n/a | self.assertEqual( |
---|
774 | n/a | surrogatepass_errors( |
---|
775 | n/a | UnicodeDecodeError(enc, bytearray(b"a" + b[:n] + b"b"), |
---|
776 | n/a | 1, 1 + n, "ouch")), |
---|
777 | n/a | (s[:1], 1 + n) |
---|
778 | n/a | ) |
---|
779 | n/a | |
---|
780 | n/a | def test_badhandlerresults(self): |
---|
781 | n/a | results = ( 42, "foo", (1,2,3), ("foo", 1, 3), ("foo", None), ("foo",), ("foo", 1, 3), ("foo", None), ("foo",) ) |
---|
782 | n/a | encs = ("ascii", "latin-1", "iso-8859-1", "iso-8859-15") |
---|
783 | n/a | |
---|
784 | n/a | for res in results: |
---|
785 | n/a | codecs.register_error("test.badhandler", lambda x: res) |
---|
786 | n/a | for enc in encs: |
---|
787 | n/a | self.assertRaises( |
---|
788 | n/a | TypeError, |
---|
789 | n/a | "\u3042".encode, |
---|
790 | n/a | enc, |
---|
791 | n/a | "test.badhandler" |
---|
792 | n/a | ) |
---|
793 | n/a | for (enc, bytes) in ( |
---|
794 | n/a | ("ascii", b"\xff"), |
---|
795 | n/a | ("utf-8", b"\xff"), |
---|
796 | n/a | ("utf-7", b"+x-"), |
---|
797 | n/a | ("unicode-internal", b"\x00"), |
---|
798 | n/a | ): |
---|
799 | n/a | with test.support.check_warnings(): |
---|
800 | n/a | # unicode-internal has been deprecated |
---|
801 | n/a | self.assertRaises( |
---|
802 | n/a | TypeError, |
---|
803 | n/a | bytes.decode, |
---|
804 | n/a | enc, |
---|
805 | n/a | "test.badhandler" |
---|
806 | n/a | ) |
---|
807 | n/a | |
---|
808 | n/a | def test_lookup(self): |
---|
809 | n/a | self.assertEqual(codecs.strict_errors, codecs.lookup_error("strict")) |
---|
810 | n/a | self.assertEqual(codecs.ignore_errors, codecs.lookup_error("ignore")) |
---|
811 | n/a | self.assertEqual(codecs.strict_errors, codecs.lookup_error("strict")) |
---|
812 | n/a | self.assertEqual( |
---|
813 | n/a | codecs.xmlcharrefreplace_errors, |
---|
814 | n/a | codecs.lookup_error("xmlcharrefreplace") |
---|
815 | n/a | ) |
---|
816 | n/a | self.assertEqual( |
---|
817 | n/a | codecs.backslashreplace_errors, |
---|
818 | n/a | codecs.lookup_error("backslashreplace") |
---|
819 | n/a | ) |
---|
820 | n/a | self.assertEqual( |
---|
821 | n/a | codecs.namereplace_errors, |
---|
822 | n/a | codecs.lookup_error("namereplace") |
---|
823 | n/a | ) |
---|
824 | n/a | |
---|
825 | n/a | def test_unencodablereplacement(self): |
---|
826 | n/a | def unencrepl(exc): |
---|
827 | n/a | if isinstance(exc, UnicodeEncodeError): |
---|
828 | n/a | return ("\u4242", exc.end) |
---|
829 | n/a | else: |
---|
830 | n/a | raise TypeError("don't know how to handle %r" % exc) |
---|
831 | n/a | codecs.register_error("test.unencreplhandler", unencrepl) |
---|
832 | n/a | for enc in ("ascii", "iso-8859-1", "iso-8859-15"): |
---|
833 | n/a | self.assertRaises( |
---|
834 | n/a | UnicodeEncodeError, |
---|
835 | n/a | "\u4242".encode, |
---|
836 | n/a | enc, |
---|
837 | n/a | "test.unencreplhandler" |
---|
838 | n/a | ) |
---|
839 | n/a | |
---|
840 | n/a | def test_badregistercall(self): |
---|
841 | n/a | # enhance coverage of: |
---|
842 | n/a | # Modules/_codecsmodule.c::register_error() |
---|
843 | n/a | # Python/codecs.c::PyCodec_RegisterError() |
---|
844 | n/a | self.assertRaises(TypeError, codecs.register_error, 42) |
---|
845 | n/a | self.assertRaises(TypeError, codecs.register_error, "test.dummy", 42) |
---|
846 | n/a | |
---|
847 | n/a | def test_badlookupcall(self): |
---|
848 | n/a | # enhance coverage of: |
---|
849 | n/a | # Modules/_codecsmodule.c::lookup_error() |
---|
850 | n/a | self.assertRaises(TypeError, codecs.lookup_error) |
---|
851 | n/a | |
---|
852 | n/a | def test_unknownhandler(self): |
---|
853 | n/a | # enhance coverage of: |
---|
854 | n/a | # Modules/_codecsmodule.c::lookup_error() |
---|
855 | n/a | self.assertRaises(LookupError, codecs.lookup_error, "test.unknown") |
---|
856 | n/a | |
---|
857 | n/a | def test_xmlcharrefvalues(self): |
---|
858 | n/a | # enhance coverage of: |
---|
859 | n/a | # Python/codecs.c::PyCodec_XMLCharRefReplaceErrors() |
---|
860 | n/a | # and inline implementations |
---|
861 | n/a | v = (1, 5, 10, 50, 100, 500, 1000, 5000, 10000, 50000, 100000, |
---|
862 | n/a | 500000, 1000000) |
---|
863 | n/a | s = "".join([chr(x) for x in v]) |
---|
864 | n/a | codecs.register_error("test.xmlcharrefreplace", codecs.xmlcharrefreplace_errors) |
---|
865 | n/a | for enc in ("ascii", "iso-8859-15"): |
---|
866 | n/a | for err in ("xmlcharrefreplace", "test.xmlcharrefreplace"): |
---|
867 | n/a | s.encode(enc, err) |
---|
868 | n/a | |
---|
869 | n/a | def test_decodehelper(self): |
---|
870 | n/a | # enhance coverage of: |
---|
871 | n/a | # Objects/unicodeobject.c::unicode_decode_call_errorhandler() |
---|
872 | n/a | # and callers |
---|
873 | n/a | self.assertRaises(LookupError, b"\xff".decode, "ascii", "test.unknown") |
---|
874 | n/a | |
---|
875 | n/a | def baddecodereturn1(exc): |
---|
876 | n/a | return 42 |
---|
877 | n/a | codecs.register_error("test.baddecodereturn1", baddecodereturn1) |
---|
878 | n/a | self.assertRaises(TypeError, b"\xff".decode, "ascii", "test.baddecodereturn1") |
---|
879 | n/a | self.assertRaises(TypeError, b"\\".decode, "unicode-escape", "test.baddecodereturn1") |
---|
880 | n/a | self.assertRaises(TypeError, b"\\x0".decode, "unicode-escape", "test.baddecodereturn1") |
---|
881 | n/a | self.assertRaises(TypeError, b"\\x0y".decode, "unicode-escape", "test.baddecodereturn1") |
---|
882 | n/a | self.assertRaises(TypeError, b"\\Uffffeeee".decode, "unicode-escape", "test.baddecodereturn1") |
---|
883 | n/a | self.assertRaises(TypeError, b"\\uyyyy".decode, "raw-unicode-escape", "test.baddecodereturn1") |
---|
884 | n/a | |
---|
885 | n/a | def baddecodereturn2(exc): |
---|
886 | n/a | return ("?", None) |
---|
887 | n/a | codecs.register_error("test.baddecodereturn2", baddecodereturn2) |
---|
888 | n/a | self.assertRaises(TypeError, b"\xff".decode, "ascii", "test.baddecodereturn2") |
---|
889 | n/a | |
---|
890 | n/a | handler = PosReturn() |
---|
891 | n/a | codecs.register_error("test.posreturn", handler.handle) |
---|
892 | n/a | |
---|
893 | n/a | # Valid negative position |
---|
894 | n/a | handler.pos = -1 |
---|
895 | n/a | self.assertEqual(b"\xff0".decode("ascii", "test.posreturn"), "<?>0") |
---|
896 | n/a | |
---|
897 | n/a | # Valid negative position |
---|
898 | n/a | handler.pos = -2 |
---|
899 | n/a | self.assertEqual(b"\xff0".decode("ascii", "test.posreturn"), "<?><?>") |
---|
900 | n/a | |
---|
901 | n/a | # Negative position out of bounds |
---|
902 | n/a | handler.pos = -3 |
---|
903 | n/a | self.assertRaises(IndexError, b"\xff0".decode, "ascii", "test.posreturn") |
---|
904 | n/a | |
---|
905 | n/a | # Valid positive position |
---|
906 | n/a | handler.pos = 1 |
---|
907 | n/a | self.assertEqual(b"\xff0".decode("ascii", "test.posreturn"), "<?>0") |
---|
908 | n/a | |
---|
909 | n/a | # Largest valid positive position (one beyond end of input) |
---|
910 | n/a | handler.pos = 2 |
---|
911 | n/a | self.assertEqual(b"\xff0".decode("ascii", "test.posreturn"), "<?>") |
---|
912 | n/a | |
---|
913 | n/a | # Invalid positive position |
---|
914 | n/a | handler.pos = 3 |
---|
915 | n/a | self.assertRaises(IndexError, b"\xff0".decode, "ascii", "test.posreturn") |
---|
916 | n/a | |
---|
917 | n/a | # Restart at the "0" |
---|
918 | n/a | handler.pos = 6 |
---|
919 | n/a | self.assertEqual(b"\\uyyyy0".decode("raw-unicode-escape", "test.posreturn"), "<?>0") |
---|
920 | n/a | |
---|
921 | n/a | class D(dict): |
---|
922 | n/a | def __getitem__(self, key): |
---|
923 | n/a | raise ValueError |
---|
924 | n/a | self.assertRaises(UnicodeError, codecs.charmap_decode, b"\xff", "strict", {0xff: None}) |
---|
925 | n/a | self.assertRaises(ValueError, codecs.charmap_decode, b"\xff", "strict", D()) |
---|
926 | n/a | self.assertRaises(TypeError, codecs.charmap_decode, b"\xff", "strict", {0xff: sys.maxunicode+1}) |
---|
927 | n/a | |
---|
928 | n/a | def test_encodehelper(self): |
---|
929 | n/a | # enhance coverage of: |
---|
930 | n/a | # Objects/unicodeobject.c::unicode_encode_call_errorhandler() |
---|
931 | n/a | # and callers |
---|
932 | n/a | self.assertRaises(LookupError, "\xff".encode, "ascii", "test.unknown") |
---|
933 | n/a | |
---|
934 | n/a | def badencodereturn1(exc): |
---|
935 | n/a | return 42 |
---|
936 | n/a | codecs.register_error("test.badencodereturn1", badencodereturn1) |
---|
937 | n/a | self.assertRaises(TypeError, "\xff".encode, "ascii", "test.badencodereturn1") |
---|
938 | n/a | |
---|
939 | n/a | def badencodereturn2(exc): |
---|
940 | n/a | return ("?", None) |
---|
941 | n/a | codecs.register_error("test.badencodereturn2", badencodereturn2) |
---|
942 | n/a | self.assertRaises(TypeError, "\xff".encode, "ascii", "test.badencodereturn2") |
---|
943 | n/a | |
---|
944 | n/a | handler = PosReturn() |
---|
945 | n/a | codecs.register_error("test.posreturn", handler.handle) |
---|
946 | n/a | |
---|
947 | n/a | # Valid negative position |
---|
948 | n/a | handler.pos = -1 |
---|
949 | n/a | self.assertEqual("\xff0".encode("ascii", "test.posreturn"), b"<?>0") |
---|
950 | n/a | |
---|
951 | n/a | # Valid negative position |
---|
952 | n/a | handler.pos = -2 |
---|
953 | n/a | self.assertEqual("\xff0".encode("ascii", "test.posreturn"), b"<?><?>") |
---|
954 | n/a | |
---|
955 | n/a | # Negative position out of bounds |
---|
956 | n/a | handler.pos = -3 |
---|
957 | n/a | self.assertRaises(IndexError, "\xff0".encode, "ascii", "test.posreturn") |
---|
958 | n/a | |
---|
959 | n/a | # Valid positive position |
---|
960 | n/a | handler.pos = 1 |
---|
961 | n/a | self.assertEqual("\xff0".encode("ascii", "test.posreturn"), b"<?>0") |
---|
962 | n/a | |
---|
963 | n/a | # Largest valid positive position (one beyond end of input |
---|
964 | n/a | handler.pos = 2 |
---|
965 | n/a | self.assertEqual("\xff0".encode("ascii", "test.posreturn"), b"<?>") |
---|
966 | n/a | |
---|
967 | n/a | # Invalid positive position |
---|
968 | n/a | handler.pos = 3 |
---|
969 | n/a | self.assertRaises(IndexError, "\xff0".encode, "ascii", "test.posreturn") |
---|
970 | n/a | |
---|
971 | n/a | handler.pos = 0 |
---|
972 | n/a | |
---|
973 | n/a | class D(dict): |
---|
974 | n/a | def __getitem__(self, key): |
---|
975 | n/a | raise ValueError |
---|
976 | n/a | for err in ("strict", "replace", "xmlcharrefreplace", |
---|
977 | n/a | "backslashreplace", "namereplace", "test.posreturn"): |
---|
978 | n/a | self.assertRaises(UnicodeError, codecs.charmap_encode, "\xff", err, {0xff: None}) |
---|
979 | n/a | self.assertRaises(ValueError, codecs.charmap_encode, "\xff", err, D()) |
---|
980 | n/a | self.assertRaises(TypeError, codecs.charmap_encode, "\xff", err, {0xff: 300}) |
---|
981 | n/a | |
---|
982 | n/a | def test_translatehelper(self): |
---|
983 | n/a | # enhance coverage of: |
---|
984 | n/a | # Objects/unicodeobject.c::unicode_encode_call_errorhandler() |
---|
985 | n/a | # and callers |
---|
986 | n/a | # (Unfortunately the errors argument is not directly accessible |
---|
987 | n/a | # from Python, so we can't test that much) |
---|
988 | n/a | class D(dict): |
---|
989 | n/a | def __getitem__(self, key): |
---|
990 | n/a | raise ValueError |
---|
991 | n/a | #self.assertRaises(ValueError, "\xff".translate, D()) |
---|
992 | n/a | self.assertRaises(ValueError, "\xff".translate, {0xff: sys.maxunicode+1}) |
---|
993 | n/a | self.assertRaises(TypeError, "\xff".translate, {0xff: ()}) |
---|
994 | n/a | |
---|
995 | n/a | def test_bug828737(self): |
---|
996 | n/a | charmap = { |
---|
997 | n/a | ord("&"): "&", |
---|
998 | n/a | ord("<"): "<", |
---|
999 | n/a | ord(">"): ">", |
---|
1000 | n/a | ord('"'): """, |
---|
1001 | n/a | } |
---|
1002 | n/a | |
---|
1003 | n/a | for n in (1, 10, 100, 1000): |
---|
1004 | n/a | text = 'abc<def>ghi'*n |
---|
1005 | n/a | text.translate(charmap) |
---|
1006 | n/a | |
---|
1007 | n/a | def test_mutatingdecodehandler(self): |
---|
1008 | n/a | baddata = [ |
---|
1009 | n/a | ("ascii", b"\xff"), |
---|
1010 | n/a | ("utf-7", b"++"), |
---|
1011 | n/a | ("utf-8", b"\xff"), |
---|
1012 | n/a | ("utf-16", b"\xff"), |
---|
1013 | n/a | ("utf-32", b"\xff"), |
---|
1014 | n/a | ("unicode-escape", b"\\u123g"), |
---|
1015 | n/a | ("raw-unicode-escape", b"\\u123g"), |
---|
1016 | n/a | ("unicode-internal", b"\xff"), |
---|
1017 | n/a | ] |
---|
1018 | n/a | |
---|
1019 | n/a | def replacing(exc): |
---|
1020 | n/a | if isinstance(exc, UnicodeDecodeError): |
---|
1021 | n/a | exc.object = 42 |
---|
1022 | n/a | return ("\u4242", 0) |
---|
1023 | n/a | else: |
---|
1024 | n/a | raise TypeError("don't know how to handle %r" % exc) |
---|
1025 | n/a | codecs.register_error("test.replacing", replacing) |
---|
1026 | n/a | |
---|
1027 | n/a | with test.support.check_warnings(): |
---|
1028 | n/a | # unicode-internal has been deprecated |
---|
1029 | n/a | for (encoding, data) in baddata: |
---|
1030 | n/a | with self.assertRaises(TypeError): |
---|
1031 | n/a | data.decode(encoding, "test.replacing") |
---|
1032 | n/a | |
---|
1033 | n/a | def mutating(exc): |
---|
1034 | n/a | if isinstance(exc, UnicodeDecodeError): |
---|
1035 | n/a | exc.object[:] = b"" |
---|
1036 | n/a | return ("\u4242", 0) |
---|
1037 | n/a | else: |
---|
1038 | n/a | raise TypeError("don't know how to handle %r" % exc) |
---|
1039 | n/a | codecs.register_error("test.mutating", mutating) |
---|
1040 | n/a | # If the decoder doesn't pick up the modified input the following |
---|
1041 | n/a | # will lead to an endless loop |
---|
1042 | n/a | with test.support.check_warnings(): |
---|
1043 | n/a | # unicode-internal has been deprecated |
---|
1044 | n/a | for (encoding, data) in baddata: |
---|
1045 | n/a | with self.assertRaises(TypeError): |
---|
1046 | n/a | data.decode(encoding, "test.replacing") |
---|
1047 | n/a | |
---|
1048 | n/a | def test_fake_error_class(self): |
---|
1049 | n/a | handlers = [ |
---|
1050 | n/a | codecs.strict_errors, |
---|
1051 | n/a | codecs.ignore_errors, |
---|
1052 | n/a | codecs.replace_errors, |
---|
1053 | n/a | codecs.backslashreplace_errors, |
---|
1054 | n/a | codecs.namereplace_errors, |
---|
1055 | n/a | codecs.xmlcharrefreplace_errors, |
---|
1056 | n/a | codecs.lookup_error('surrogateescape'), |
---|
1057 | n/a | codecs.lookup_error('surrogatepass'), |
---|
1058 | n/a | ] |
---|
1059 | n/a | for cls in UnicodeEncodeError, UnicodeDecodeError, UnicodeTranslateError: |
---|
1060 | n/a | class FakeUnicodeError(str): |
---|
1061 | n/a | __class__ = cls |
---|
1062 | n/a | for handler in handlers: |
---|
1063 | n/a | with self.subTest(handler=handler, error_class=cls): |
---|
1064 | n/a | self.assertRaises(TypeError, handler, FakeUnicodeError()) |
---|
1065 | n/a | class FakeUnicodeError(Exception): |
---|
1066 | n/a | __class__ = cls |
---|
1067 | n/a | for handler in handlers: |
---|
1068 | n/a | with self.subTest(handler=handler, error_class=cls): |
---|
1069 | n/a | with self.assertRaises((TypeError, FakeUnicodeError)): |
---|
1070 | n/a | handler(FakeUnicodeError()) |
---|
1071 | n/a | |
---|
1072 | n/a | |
---|
1073 | n/a | if __name__ == "__main__": |
---|
1074 | n/a | unittest.main() |
---|