blob: b9cd9c2d622df6ff768fd4d9554f3105b1fffd64 [file] [log] [blame]
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001import test.test_support, unittest
Fred Draked995e112008-05-20 06:08:38 +00002import sys, codecs, htmlentitydefs, unicodedata
Walter Dörwald3aeb6322002-09-02 13:14:32 +00003
Walter Dörwald2e0b18a2003-01-31 17:19:08 +00004class PosReturn:
5 # this can be used for configurable callbacks
6
7 def __init__(self):
8 self.pos = 0
9
10 def handle(self, exc):
11 oldpos = self.pos
12 realpos = oldpos
13 if realpos<0:
Tim Petersf2715e02003-02-19 02:35:07 +000014 realpos = len(exc.object) + realpos
Walter Dörwald2e0b18a2003-01-31 17:19:08 +000015 # if we don't advance this time, terminate on the next call
16 # otherwise we'd get an endless loop
17 if realpos <= exc.start:
18 self.pos = len(exc.object)
19 return (u"<?>", oldpos)
20
Walter Dörwald690402f2005-11-17 18:51:34 +000021# A UnicodeEncodeError object with a bad start attribute
22class BadStartUnicodeEncodeError(UnicodeEncodeError):
23 def __init__(self):
24 UnicodeEncodeError.__init__(self, "ascii", u"", 0, 1, "bad")
25 self.start = []
26
Walter Dörwald690402f2005-11-17 18:51:34 +000027# A UnicodeEncodeError object with a bad object attribute
28class BadObjectUnicodeEncodeError(UnicodeEncodeError):
29 def __init__(self):
30 UnicodeEncodeError.__init__(self, "ascii", u"", 0, 1, "bad")
31 self.object = []
32
33# A UnicodeDecodeError object without an end attribute
34class NoEndUnicodeDecodeError(UnicodeDecodeError):
35 def __init__(self):
36 UnicodeDecodeError.__init__(self, "ascii", "", 0, 1, "bad")
37 del self.end
38
39# A UnicodeDecodeError object with a bad object attribute
40class BadObjectUnicodeDecodeError(UnicodeDecodeError):
41 def __init__(self):
42 UnicodeDecodeError.__init__(self, "ascii", "", 0, 1, "bad")
43 self.object = []
44
45# A UnicodeTranslateError object without a start attribute
46class NoStartUnicodeTranslateError(UnicodeTranslateError):
47 def __init__(self):
48 UnicodeTranslateError.__init__(self, u"", 0, 1, "bad")
49 del self.start
50
51# A UnicodeTranslateError object without an end attribute
52class NoEndUnicodeTranslateError(UnicodeTranslateError):
53 def __init__(self):
54 UnicodeTranslateError.__init__(self, u"", 0, 1, "bad")
55 del self.end
56
57# A UnicodeTranslateError object without an object attribute
58class NoObjectUnicodeTranslateError(UnicodeTranslateError):
59 def __init__(self):
60 UnicodeTranslateError.__init__(self, u"", 0, 1, "bad")
61 del self.object
62
Walter Dörwald3aeb6322002-09-02 13:14:32 +000063class CodecCallbackTest(unittest.TestCase):
64
65 def test_xmlcharrefreplace(self):
66 # replace unencodable characters which numeric character entities.
67 # For ascii, latin-1 and charmaps this is completely implemented
68 # in C and should be reasonably fast.
Serhiy Storchakae822b032013-08-06 16:56:26 +030069 s = u"\u30b9\u30d1\u30e2 \xe4nd egg\u0161"
Walter Dörwald3aeb6322002-09-02 13:14:32 +000070 self.assertEqual(
71 s.encode("ascii", "xmlcharrefreplace"),
Serhiy Storchakae822b032013-08-06 16:56:26 +030072 "&#12473;&#12497;&#12514; &#228;nd egg&#353;"
Walter Dörwald3aeb6322002-09-02 13:14:32 +000073 )
74 self.assertEqual(
75 s.encode("latin-1", "xmlcharrefreplace"),
Serhiy Storchakae822b032013-08-06 16:56:26 +030076 "&#12473;&#12497;&#12514; \xe4nd egg&#353;"
Walter Dörwald3aeb6322002-09-02 13:14:32 +000077 )
Serhiy Storchakae822b032013-08-06 16:56:26 +030078 self.assertEqual(
79 s.encode("iso-8859-15", "xmlcharrefreplace"),
80 "&#12473;&#12497;&#12514; \xe4nd egg\xa8"
81 )
82
83 def test_xmlcharrefreplace_with_surrogates(self):
84 tests = [(u'\U0001f49d', '&#128157;'),
85 (u'\ud83d', '&#55357;'),
86 (u'\udc9d', '&#56477;'),
Serhiy Storchakae822b032013-08-06 16:56:26 +030087 ]
Serhiy Storchaka1fdc7022013-10-31 17:06:03 +020088 if u'\ud83d\udc9d' != u'\U0001f49d':
89 tests += [(u'\ud83d\udc9d', '&#55357;&#56477;')]
Serhiy Storchakae822b032013-08-06 16:56:26 +030090 for encoding in ['ascii', 'latin1', 'iso-8859-15']:
91 for s, exp in tests:
92 self.assertEqual(s.encode(encoding, 'xmlcharrefreplace'),
93 exp, msg='%r.encode(%r)' % (s, encoding))
94 self.assertEqual((s+'X').encode(encoding, 'xmlcharrefreplace'),
95 exp+'X',
96 msg='%r.encode(%r)' % (s + 'X', encoding))
Walter Dörwald3aeb6322002-09-02 13:14:32 +000097
98 def test_xmlcharnamereplace(self):
99 # This time use a named character entity for unencodable
100 # characters, if one is available.
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000101
102 def xmlcharnamereplace(exc):
103 if not isinstance(exc, UnicodeEncodeError):
104 raise TypeError("don't know how to handle %r" % exc)
105 l = []
106 for c in exc.object[exc.start:exc.end]:
107 try:
Fred Draked995e112008-05-20 06:08:38 +0000108 l.append(u"&%s;" % htmlentitydefs.codepoint2name[ord(c)])
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000109 except KeyError:
110 l.append(u"&#%d;" % ord(c))
111 return (u"".join(l), exc.end)
112
113 codecs.register_error(
114 "test.xmlcharnamereplace", xmlcharnamereplace)
115
116 sin = u"\xab\u211c\xbb = \u2329\u1234\u20ac\u232a"
117 sout = "&laquo;&real;&raquo; = &lang;&#4660;&euro;&rang;"
118 self.assertEqual(sin.encode("ascii", "test.xmlcharnamereplace"), sout)
119 sout = "\xab&real;\xbb = &lang;&#4660;&euro;&rang;"
120 self.assertEqual(sin.encode("latin-1", "test.xmlcharnamereplace"), sout)
121 sout = "\xab&real;\xbb = &lang;&#4660;\xa4&rang;"
122 self.assertEqual(sin.encode("iso-8859-15", "test.xmlcharnamereplace"), sout)
123
124 def test_uninamereplace(self):
125 # We're using the names from the unicode database this time,
Walter Dörwald00445d22002-11-25 17:58:02 +0000126 # and we're doing "syntax highlighting" here, i.e. we include
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000127 # the replaced text in ANSI escape sequences. For this it is
128 # useful that the error handler is not called for every single
129 # unencodable character, but for a complete sequence of
130 # unencodable characters, otherwise we would output many
Mark Dickinson3e4caeb2009-02-21 20:27:01 +0000131 # unnecessary escape sequences.
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000132
133 def uninamereplace(exc):
134 if not isinstance(exc, UnicodeEncodeError):
135 raise TypeError("don't know how to handle %r" % exc)
136 l = []
137 for c in exc.object[exc.start:exc.end]:
138 l.append(unicodedata.name(c, u"0x%x" % ord(c)))
139 return (u"\033[1m%s\033[0m" % u", ".join(l), exc.end)
140
141 codecs.register_error(
142 "test.uninamereplace", uninamereplace)
143
144 sin = u"\xac\u1234\u20ac\u8000"
Martin v. Löwis74a530d2002-11-23 19:41:01 +0000145 sout = "\033[1mNOT SIGN, ETHIOPIC SYLLABLE SEE, EURO SIGN, CJK UNIFIED IDEOGRAPH-8000\033[0m"
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000146 self.assertEqual(sin.encode("ascii", "test.uninamereplace"), sout)
147
Martin v. Löwis74a530d2002-11-23 19:41:01 +0000148 sout = "\xac\033[1mETHIOPIC SYLLABLE SEE, EURO SIGN, CJK UNIFIED IDEOGRAPH-8000\033[0m"
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000149 self.assertEqual(sin.encode("latin-1", "test.uninamereplace"), sout)
150
Martin v. Löwis74a530d2002-11-23 19:41:01 +0000151 sout = "\xac\033[1mETHIOPIC SYLLABLE SEE\033[0m\xa4\033[1mCJK UNIFIED IDEOGRAPH-8000\033[0m"
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000152 self.assertEqual(sin.encode("iso-8859-15", "test.uninamereplace"), sout)
153
154 def test_backslashescape(self):
155 # Does the same as the "unicode-escape" encoding, but with different
156 # base encodings.
157 sin = u"a\xac\u1234\u20ac\u8000"
158 if sys.maxunicode > 0xffff:
159 sin += unichr(sys.maxunicode)
160 sout = "a\\xac\\u1234\\u20ac\\u8000"
161 if sys.maxunicode > 0xffff:
162 sout += "\\U%08x" % sys.maxunicode
163 self.assertEqual(sin.encode("ascii", "backslashreplace"), sout)
164
165 sout = "a\xac\\u1234\\u20ac\\u8000"
166 if sys.maxunicode > 0xffff:
167 sout += "\\U%08x" % sys.maxunicode
168 self.assertEqual(sin.encode("latin-1", "backslashreplace"), sout)
169
170 sout = "a\xac\\u1234\xa4\\u8000"
171 if sys.maxunicode > 0xffff:
172 sout += "\\U%08x" % sys.maxunicode
173 self.assertEqual(sin.encode("iso-8859-15", "backslashreplace"), sout)
174
Ezio Melottie57e50c2010-06-05 17:51:07 +0000175 def test_decoding_callbacks(self):
176 # This is a test for a decoding callback handler
177 # that allows the decoding of the invalid sequence
178 # "\xc0\x80" and returns "\x00" instead of raising an error.
179 # All other illegal sequences will be handled strictly.
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000180 def relaxedutf8(exc):
181 if not isinstance(exc, UnicodeDecodeError):
182 raise TypeError("don't know how to handle %r" % exc)
Ezio Melottie57e50c2010-06-05 17:51:07 +0000183 if exc.object[exc.start:exc.start+2] == "\xc0\x80":
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000184 return (u"\x00", exc.start+2) # retry after two bytes
185 else:
186 raise exc
187
Ezio Melottie57e50c2010-06-05 17:51:07 +0000188 codecs.register_error("test.relaxedutf8", relaxedutf8)
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000189
Ezio Melottie57e50c2010-06-05 17:51:07 +0000190 # all the "\xc0\x80" will be decoded to "\x00"
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000191 sin = "a\x00b\xc0\x80c\xc3\xbc\xc0\x80\xc0\x80"
192 sout = u"a\x00b\x00c\xfc\x00\x00"
193 self.assertEqual(sin.decode("utf-8", "test.relaxedutf8"), sout)
Ezio Melottie57e50c2010-06-05 17:51:07 +0000194
195 # "\xc0\x81" is not valid and a UnicodeDecodeError will be raised
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000196 sin = "\xc0\x80\xc0\x81"
Ezio Melottie57e50c2010-06-05 17:51:07 +0000197 self.assertRaises(UnicodeDecodeError, sin.decode,
198 "utf-8", "test.relaxedutf8")
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000199
200 def test_charmapencode(self):
201 # For charmap encodings the replacement string will be
202 # mapped through the encoding again. This means, that
203 # to be able to use e.g. the "replace" handler, the
204 # charmap has to have a mapping for "?".
205 charmap = dict([ (ord(c), 2*c.upper()) for c in "abcdefgh"])
206 sin = u"abc"
207 sout = "AABBCC"
Ezio Melotti2623a372010-11-21 13:34:58 +0000208 self.assertEqual(codecs.charmap_encode(sin, "strict", charmap)[0], sout)
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000209
210 sin = u"abcA"
211 self.assertRaises(UnicodeError, codecs.charmap_encode, sin, "strict", charmap)
212
213 charmap[ord("?")] = "XYZ"
214 sin = u"abcDEF"
215 sout = "AABBCCXYZXYZXYZ"
Ezio Melotti2623a372010-11-21 13:34:58 +0000216 self.assertEqual(codecs.charmap_encode(sin, "replace", charmap)[0], sout)
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000217
218 charmap[ord("?")] = u"XYZ"
219 self.assertRaises(TypeError, codecs.charmap_encode, sin, "replace", charmap)
220
221 charmap[ord("?")] = u"XYZ"
222 self.assertRaises(TypeError, codecs.charmap_encode, sin, "replace", charmap)
223
Walter Dörwalda47d1c02005-08-30 10:23:14 +0000224 def test_decodeunicodeinternal(self):
225 self.assertRaises(
226 UnicodeDecodeError,
227 "\x00\x00\x00\x00\x00".decode,
228 "unicode-internal",
229 )
230 if sys.maxunicode > 0xffff:
231 def handler_unicodeinternal(exc):
232 if not isinstance(exc, UnicodeDecodeError):
233 raise TypeError("don't know how to handle %r" % exc)
234 return (u"\x01", 1)
235
236 self.assertEqual(
237 "\x00\x00\x00\x00\x00".decode("unicode-internal", "ignore"),
238 u"\u0000"
239 )
240
241 self.assertEqual(
242 "\x00\x00\x00\x00\x00".decode("unicode-internal", "replace"),
243 u"\u0000\ufffd"
244 )
245
246 codecs.register_error("test.hui", handler_unicodeinternal)
247
248 self.assertEqual(
249 "\x00\x00\x00\x00\x00".decode("unicode-internal", "test.hui"),
250 u"\u0000\u0001\u0000"
251 )
252
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000253 def test_callbacks(self):
254 def handler1(exc):
255 if not isinstance(exc, UnicodeEncodeError) \
256 and not isinstance(exc, UnicodeDecodeError):
257 raise TypeError("don't know how to handle %r" % exc)
258 l = [u"<%d>" % ord(exc.object[pos]) for pos in xrange(exc.start, exc.end)]
259 return (u"[%s]" % u"".join(l), exc.end)
260
261 codecs.register_error("test.handler1", handler1)
262
263 def handler2(exc):
264 if not isinstance(exc, UnicodeDecodeError):
265 raise TypeError("don't know how to handle %r" % exc)
266 l = [u"<%d>" % ord(exc.object[pos]) for pos in xrange(exc.start, exc.end)]
267 return (u"[%s]" % u"".join(l), exc.end+1) # skip one character
268
269 codecs.register_error("test.handler2", handler2)
270
271 s = "\x00\x81\x7f\x80\xff"
272
273 self.assertEqual(
274 s.decode("ascii", "test.handler1"),
275 u"\x00[<129>]\x7f[<128>][<255>]"
276 )
277 self.assertEqual(
278 s.decode("ascii", "test.handler2"),
279 u"\x00[<129>][<128>]"
280 )
281
282 self.assertEqual(
283 "\\u3042\u3xxx".decode("unicode-escape", "test.handler1"),
Serhiy Storchakac8e58122013-01-29 10:20:34 +0200284 u"\u3042[<92><117><51>]xxx"
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000285 )
286
287 self.assertEqual(
288 "\\u3042\u3xx".decode("unicode-escape", "test.handler1"),
Serhiy Storchakac8e58122013-01-29 10:20:34 +0200289 u"\u3042[<92><117><51>]xx"
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000290 )
291
292 self.assertEqual(
293 codecs.charmap_decode("abc", "test.handler1", {ord("a"): u"z"})[0],
294 u"z[<98>][<99>]"
295 )
296
297 self.assertEqual(
298 u"g\xfc\xdfrk".encode("ascii", "test.handler1"),
299 u"g[<252><223>]rk"
300 )
301
302 self.assertEqual(
303 u"g\xfc\xdf".encode("ascii", "test.handler1"),
304 u"g[<252><223>]"
305 )
306
307 def test_longstrings(self):
308 # test long strings to check for memory overflow problems
Walter Dörwald6e390802007-08-17 16:41:28 +0000309 errors = [ "strict", "ignore", "replace", "xmlcharrefreplace",
310 "backslashreplace"]
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000311 # register the handlers under different names,
312 # to prevent the codec from recognizing the name
313 for err in errors:
314 codecs.register_error("test." + err, codecs.lookup_error(err))
315 l = 1000
316 errors += [ "test." + err for err in errors ]
317 for uni in [ s*l for s in (u"x", u"\u3042", u"a\xe4") ]:
Walter Dörwald6e390802007-08-17 16:41:28 +0000318 for enc in ("ascii", "latin-1", "iso-8859-1", "iso-8859-15",
319 "utf-8", "utf-7", "utf-16", "utf-32"):
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000320 for err in errors:
Tim Peters3de75262002-11-09 05:26:15 +0000321 try:
322 uni.encode(enc, err)
323 except UnicodeError:
324 pass
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000325
326 def check_exceptionobjectargs(self, exctype, args, msg):
327 # Test UnicodeError subclasses: construction, attribute assignment and __str__ conversion
328 # check with one missing argument
329 self.assertRaises(TypeError, exctype, *args[:-1])
Walter Dörwaldea4250d2003-01-20 02:34:07 +0000330 # check with one argument too much
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000331 self.assertRaises(TypeError, exctype, *(args + ["too much"]))
332 # check with one argument of the wrong type
333 wrongargs = [ "spam", u"eggs", 42, 1.0, None ]
334 for i in xrange(len(args)):
335 for wrongarg in wrongargs:
336 if type(wrongarg) is type(args[i]):
Tim Peters3de75262002-11-09 05:26:15 +0000337 continue
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000338 # build argument array
339 callargs = []
340 for j in xrange(len(args)):
341 if i==j:
342 callargs.append(wrongarg)
343 else:
344 callargs.append(args[i])
345 self.assertRaises(TypeError, exctype, *callargs)
Walter Dörwaldea4250d2003-01-20 02:34:07 +0000346
347 # check with the correct number and type of arguments
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000348 exc = exctype(*args)
Ezio Melotti2623a372010-11-21 13:34:58 +0000349 self.assertEqual(str(exc), msg)
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000350
351 def test_unicodeencodeerror(self):
352 self.check_exceptionobjectargs(
353 UnicodeEncodeError,
354 ["ascii", u"g\xfcrk", 1, 2, "ouch"],
Walter Dörwalda54b92b2003-08-12 17:34:49 +0000355 "'ascii' codec can't encode character u'\\xfc' in position 1: ouch"
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000356 )
357 self.check_exceptionobjectargs(
358 UnicodeEncodeError,
359 ["ascii", u"g\xfcrk", 1, 4, "ouch"],
360 "'ascii' codec can't encode characters in position 1-3: ouch"
361 )
362 self.check_exceptionobjectargs(
363 UnicodeEncodeError,
364 ["ascii", u"\xfcx", 0, 1, "ouch"],
Walter Dörwalda54b92b2003-08-12 17:34:49 +0000365 "'ascii' codec can't encode character u'\\xfc' in position 0: ouch"
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000366 )
Walter Dörwaldfd196bd2003-08-12 17:32:43 +0000367 self.check_exceptionobjectargs(
368 UnicodeEncodeError,
369 ["ascii", u"\u0100x", 0, 1, "ouch"],
Walter Dörwalda54b92b2003-08-12 17:34:49 +0000370 "'ascii' codec can't encode character u'\\u0100' in position 0: ouch"
Walter Dörwaldfd196bd2003-08-12 17:32:43 +0000371 )
372 self.check_exceptionobjectargs(
373 UnicodeEncodeError,
374 ["ascii", u"\uffffx", 0, 1, "ouch"],
Walter Dörwalda54b92b2003-08-12 17:34:49 +0000375 "'ascii' codec can't encode character u'\\uffff' in position 0: ouch"
Walter Dörwaldfd196bd2003-08-12 17:32:43 +0000376 )
377 if sys.maxunicode > 0xffff:
378 self.check_exceptionobjectargs(
379 UnicodeEncodeError,
380 ["ascii", u"\U00010000x", 0, 1, "ouch"],
Walter Dörwalda54b92b2003-08-12 17:34:49 +0000381 "'ascii' codec can't encode character u'\\U00010000' in position 0: ouch"
Walter Dörwaldfd196bd2003-08-12 17:32:43 +0000382 )
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000383
384 def test_unicodedecodeerror(self):
385 self.check_exceptionobjectargs(
386 UnicodeDecodeError,
387 ["ascii", "g\xfcrk", 1, 2, "ouch"],
388 "'ascii' codec can't decode byte 0xfc in position 1: ouch"
389 )
390 self.check_exceptionobjectargs(
391 UnicodeDecodeError,
392 ["ascii", "g\xfcrk", 1, 3, "ouch"],
393 "'ascii' codec can't decode bytes in position 1-2: ouch"
394 )
395
396 def test_unicodetranslateerror(self):
397 self.check_exceptionobjectargs(
398 UnicodeTranslateError,
399 [u"g\xfcrk", 1, 2, "ouch"],
Walter Dörwalda54b92b2003-08-12 17:34:49 +0000400 "can't translate character u'\\xfc' in position 1: ouch"
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000401 )
402 self.check_exceptionobjectargs(
403 UnicodeTranslateError,
Walter Dörwaldfd196bd2003-08-12 17:32:43 +0000404 [u"g\u0100rk", 1, 2, "ouch"],
Walter Dörwalda54b92b2003-08-12 17:34:49 +0000405 "can't translate character u'\\u0100' in position 1: ouch"
Walter Dörwaldfd196bd2003-08-12 17:32:43 +0000406 )
407 self.check_exceptionobjectargs(
408 UnicodeTranslateError,
409 [u"g\uffffrk", 1, 2, "ouch"],
Walter Dörwalda54b92b2003-08-12 17:34:49 +0000410 "can't translate character u'\\uffff' in position 1: ouch"
Walter Dörwaldfd196bd2003-08-12 17:32:43 +0000411 )
412 if sys.maxunicode > 0xffff:
413 self.check_exceptionobjectargs(
414 UnicodeTranslateError,
415 [u"g\U00010000rk", 1, 2, "ouch"],
Walter Dörwalda54b92b2003-08-12 17:34:49 +0000416 "can't translate character u'\\U00010000' in position 1: ouch"
Walter Dörwaldfd196bd2003-08-12 17:32:43 +0000417 )
418 self.check_exceptionobjectargs(
419 UnicodeTranslateError,
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000420 [u"g\xfcrk", 1, 3, "ouch"],
421 "can't translate characters in position 1-2: ouch"
422 )
423
424 def test_badandgoodstrictexceptions(self):
Walter Dörwaldea4250d2003-01-20 02:34:07 +0000425 # "strict" complains about a non-exception passed in
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000426 self.assertRaises(
427 TypeError,
428 codecs.strict_errors,
429 42
430 )
Walter Dörwaldea4250d2003-01-20 02:34:07 +0000431 # "strict" complains about the wrong exception type
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000432 self.assertRaises(
433 Exception,
434 codecs.strict_errors,
435 Exception("ouch")
436 )
437
Walter Dörwaldea4250d2003-01-20 02:34:07 +0000438 # If the correct exception is passed in, "strict" raises it
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000439 self.assertRaises(
440 UnicodeEncodeError,
441 codecs.strict_errors,
442 UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")
443 )
Serhiy Storchaka27923892015-03-15 23:41:10 +0200444 self.assertRaises(
445 UnicodeDecodeError,
446 codecs.strict_errors,
447 UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch")
448 )
449 self.assertRaises(
450 UnicodeTranslateError,
451 codecs.strict_errors,
452 UnicodeTranslateError(u"\u3042", 0, 1, "ouch")
453 )
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000454
455 def test_badandgoodignoreexceptions(self):
Walter Dörwaldea4250d2003-01-20 02:34:07 +0000456 # "ignore" complains about a non-exception passed in
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000457 self.assertRaises(
458 TypeError,
459 codecs.ignore_errors,
460 42
461 )
Walter Dörwaldea4250d2003-01-20 02:34:07 +0000462 # "ignore" complains about the wrong exception type
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000463 self.assertRaises(
464 TypeError,
465 codecs.ignore_errors,
466 UnicodeError("ouch")
467 )
Walter Dörwaldea4250d2003-01-20 02:34:07 +0000468 # If the correct exception is passed in, "ignore" returns an empty replacement
Ezio Melotti2623a372010-11-21 13:34:58 +0000469 self.assertEqual(
Serhiy Storchaka3d15b5d2015-03-16 08:29:35 +0200470 codecs.ignore_errors(
471 UnicodeEncodeError("ascii", u"a\u3042b", 1, 2, "ouch")),
472 (u"", 2)
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000473 )
Ezio Melotti2623a372010-11-21 13:34:58 +0000474 self.assertEqual(
Serhiy Storchaka3d15b5d2015-03-16 08:29:35 +0200475 codecs.ignore_errors(
476 UnicodeDecodeError("ascii", "a\xffb", 1, 2, "ouch")),
477 (u"", 2)
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000478 )
Ezio Melotti2623a372010-11-21 13:34:58 +0000479 self.assertEqual(
Serhiy Storchaka3d15b5d2015-03-16 08:29:35 +0200480 codecs.ignore_errors(
481 UnicodeTranslateError(u"a\u3042b", 1, 2, "ouch")),
482 (u"", 2)
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000483 )
484
485 def test_badandgoodreplaceexceptions(self):
Walter Dörwaldea4250d2003-01-20 02:34:07 +0000486 # "replace" complains about a non-exception passed in
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000487 self.assertRaises(
488 TypeError,
489 codecs.replace_errors,
490 42
491 )
Walter Dörwaldea4250d2003-01-20 02:34:07 +0000492 # "replace" complains about the wrong exception type
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000493 self.assertRaises(
494 TypeError,
495 codecs.replace_errors,
496 UnicodeError("ouch")
497 )
Walter Dörwald690402f2005-11-17 18:51:34 +0000498 self.assertRaises(
Walter Dörwald690402f2005-11-17 18:51:34 +0000499 TypeError,
500 codecs.replace_errors,
501 BadObjectUnicodeEncodeError()
502 )
503 self.assertRaises(
Walter Dörwald690402f2005-11-17 18:51:34 +0000504 TypeError,
505 codecs.replace_errors,
506 BadObjectUnicodeDecodeError()
507 )
Walter Dörwald29ddfba2004-12-14 21:28:07 +0000508 # With the correct exception, "replace" returns an "?" or u"\ufffd" replacement
Ezio Melotti2623a372010-11-21 13:34:58 +0000509 self.assertEqual(
Serhiy Storchaka3d15b5d2015-03-16 08:29:35 +0200510 codecs.replace_errors(
511 UnicodeEncodeError("ascii", u"a\u3042b", 1, 2, "ouch")),
512 (u"?", 2)
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000513 )
Ezio Melotti2623a372010-11-21 13:34:58 +0000514 self.assertEqual(
Serhiy Storchaka3d15b5d2015-03-16 08:29:35 +0200515 codecs.replace_errors(
516 UnicodeDecodeError("ascii", "a\xffb", 1, 2, "ouch")),
517 (u"\ufffd", 2)
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000518 )
Ezio Melotti2623a372010-11-21 13:34:58 +0000519 self.assertEqual(
Serhiy Storchaka3d15b5d2015-03-16 08:29:35 +0200520 codecs.replace_errors(
521 UnicodeTranslateError(u"a\u3042b", 1, 2, "ouch")),
522 (u"\ufffd", 2)
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000523 )
524
525 def test_badandgoodxmlcharrefreplaceexceptions(self):
Walter Dörwaldea4250d2003-01-20 02:34:07 +0000526 # "xmlcharrefreplace" complains about a non-exception passed in
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000527 self.assertRaises(
528 TypeError,
529 codecs.xmlcharrefreplace_errors,
530 42
531 )
Walter Dörwaldea4250d2003-01-20 02:34:07 +0000532 # "xmlcharrefreplace" complains about the wrong exception types
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000533 self.assertRaises(
534 TypeError,
535 codecs.xmlcharrefreplace_errors,
536 UnicodeError("ouch")
537 )
Walter Dörwaldea4250d2003-01-20 02:34:07 +0000538 # "xmlcharrefreplace" can only be used for encoding
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000539 self.assertRaises(
540 TypeError,
541 codecs.xmlcharrefreplace_errors,
542 UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch")
543 )
544 self.assertRaises(
545 TypeError,
546 codecs.xmlcharrefreplace_errors,
547 UnicodeTranslateError(u"\u3042", 0, 1, "ouch")
548 )
Walter Dörwaldea4250d2003-01-20 02:34:07 +0000549 # Use the correct exception
Serhiy Storchaka27923892015-03-15 23:41:10 +0200550 cs = (0, 1, 9, 10, 99, 100, 999, 1000, 9999, 10000)
551 cs += (0xdfff, 0xd800)
552 s = u"".join(unichr(c) for c in cs)
553 s += u"\U0001869f\U000186a0\U000f423f\U000f4240"
554 cs += (99999, 100000, 999999, 1000000)
Ezio Melotti2623a372010-11-21 13:34:58 +0000555 self.assertEqual(
Walter Dörwald690402f2005-11-17 18:51:34 +0000556 codecs.xmlcharrefreplace_errors(
Serhiy Storchaka3d15b5d2015-03-16 08:29:35 +0200557 UnicodeEncodeError("ascii", u"a" + s + u"b",
558 1, 1 + len(s), "ouch")
Walter Dörwald690402f2005-11-17 18:51:34 +0000559 ),
Serhiy Storchaka3d15b5d2015-03-16 08:29:35 +0200560 (u"".join(u"&#%d;" % c for c in cs), 1 + len(s))
Walter Dörwaldea4250d2003-01-20 02:34:07 +0000561 )
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000562
563 def test_badandgoodbackslashreplaceexceptions(self):
Walter Dörwaldea4250d2003-01-20 02:34:07 +0000564 # "backslashreplace" complains about a non-exception passed in
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000565 self.assertRaises(
566 TypeError,
567 codecs.backslashreplace_errors,
568 42
569 )
Walter Dörwaldea4250d2003-01-20 02:34:07 +0000570 # "backslashreplace" complains about the wrong exception types
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000571 self.assertRaises(
572 TypeError,
573 codecs.backslashreplace_errors,
574 UnicodeError("ouch")
575 )
Walter Dörwaldea4250d2003-01-20 02:34:07 +0000576 # "backslashreplace" can only be used for encoding
577 self.assertRaises(
578 TypeError,
579 codecs.backslashreplace_errors,
580 UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch")
581 )
582 self.assertRaises(
583 TypeError,
584 codecs.backslashreplace_errors,
585 UnicodeTranslateError(u"\u3042", 0, 1, "ouch")
586 )
587 # Use the correct exception
Serhiy Storchaka27923892015-03-15 23:41:10 +0200588 tests = [
589 (u"\u3042", u"\\u3042"),
590 (u"\n", u"\\x0a"),
591 (u"a", u"\\x61"),
592 (u"\x00", u"\\x00"),
593 (u"\xff", u"\\xff"),
594 (u"\u0100", u"\\u0100"),
595 (u"\uffff", u"\\uffff"),
596 # Lone surrogates
597 (u"\ud800", u"\\ud800"),
598 (u"\udfff", u"\\udfff"),
599 ]
600 if sys.maxunicode > 0xffff:
601 tests += [
602 (u"\U00010000", u"\\U00010000"),
603 (u"\U0010ffff", u"\\U0010ffff"),
604 ]
605 else:
606 tests += [
607 (u"\U00010000", u"\\ud800\\udc00"),
608 (u"\U0010ffff", u"\\udbff\\udfff"),
609 ]
610 for s, r in tests:
Ezio Melotti2623a372010-11-21 13:34:58 +0000611 self.assertEqual(
Serhiy Storchaka27923892015-03-15 23:41:10 +0200612 codecs.backslashreplace_errors(
Serhiy Storchaka3d15b5d2015-03-16 08:29:35 +0200613 UnicodeEncodeError("ascii", u"a" + s + u"b",
614 1, 1 + len(s), "ouch")),
615 (r, 1 + len(s))
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000616 )
617
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000618 def test_badhandlerresults(self):
619 results = ( 42, u"foo", (1,2,3), (u"foo", 1, 3), (u"foo", None), (u"foo",), ("foo", 1, 3), ("foo", None), ("foo",) )
620 encs = ("ascii", "latin-1", "iso-8859-1", "iso-8859-15")
621
622 for res in results:
Benjamin Peterson910f2162009-01-18 21:11:38 +0000623 codecs.register_error("test.badhandler", lambda x: res)
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000624 for enc in encs:
625 self.assertRaises(
626 TypeError,
627 u"\u3042".encode,
628 enc,
629 "test.badhandler"
630 )
631 for (enc, bytes) in (
632 ("ascii", "\xff"),
633 ("utf-8", "\xff"),
Walter Dörwalda47d1c02005-08-30 10:23:14 +0000634 ("utf-7", "+x-"),
635 ("unicode-internal", "\x00"),
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000636 ):
637 self.assertRaises(
638 TypeError,
639 bytes.decode,
640 enc,
641 "test.badhandler"
642 )
643
644 def test_lookup(self):
Ezio Melotti2623a372010-11-21 13:34:58 +0000645 self.assertEqual(codecs.strict_errors, codecs.lookup_error("strict"))
646 self.assertEqual(codecs.ignore_errors, codecs.lookup_error("ignore"))
647 self.assertEqual(codecs.strict_errors, codecs.lookup_error("strict"))
648 self.assertEqual(
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000649 codecs.xmlcharrefreplace_errors,
650 codecs.lookup_error("xmlcharrefreplace")
651 )
Ezio Melotti2623a372010-11-21 13:34:58 +0000652 self.assertEqual(
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000653 codecs.backslashreplace_errors,
654 codecs.lookup_error("backslashreplace")
655 )
656
Walter Dörwald9ab7dd42002-09-06 17:21:40 +0000657 def test_unencodablereplacement(self):
658 def unencrepl(exc):
659 if isinstance(exc, UnicodeEncodeError):
660 return (u"\u4242", exc.end)
661 else:
662 raise TypeError("don't know how to handle %r" % exc)
663 codecs.register_error("test.unencreplhandler", unencrepl)
664 for enc in ("ascii", "iso-8859-1", "iso-8859-15"):
665 self.assertRaises(
666 UnicodeEncodeError,
667 u"\u4242".encode,
668 enc,
669 "test.unencreplhandler"
670 )
671
Walter Dörwald30537a42003-01-08 23:22:13 +0000672 def test_badregistercall(self):
673 # enhance coverage of:
674 # Modules/_codecsmodule.c::register_error()
675 # Python/codecs.c::PyCodec_RegisterError()
676 self.assertRaises(TypeError, codecs.register_error, 42)
677 self.assertRaises(TypeError, codecs.register_error, "test.dummy", 42)
678
Walter Dörwalde22d3392005-11-17 08:52:34 +0000679 def test_badlookupcall(self):
680 # enhance coverage of:
681 # Modules/_codecsmodule.c::lookup_error()
682 self.assertRaises(TypeError, codecs.lookup_error)
683
Walter Dörwald30537a42003-01-08 23:22:13 +0000684 def test_unknownhandler(self):
685 # enhance coverage of:
686 # Modules/_codecsmodule.c::lookup_error()
687 self.assertRaises(LookupError, codecs.lookup_error, "test.unknown")
688
689 def test_xmlcharrefvalues(self):
690 # enhance coverage of:
691 # Python/codecs.c::PyCodec_XMLCharRefReplaceErrors()
692 # and inline implementations
693 v = (1, 5, 10, 50, 100, 500, 1000, 5000, 10000, 50000)
Walter Dörwald0cb27dd2003-01-09 11:38:50 +0000694 if sys.maxunicode>=100000:
Tim Petersf2715e02003-02-19 02:35:07 +0000695 v += (100000, 500000, 1000000)
Walter Dörwald30537a42003-01-08 23:22:13 +0000696 s = u"".join([unichr(x) for x in v])
697 codecs.register_error("test.xmlcharrefreplace", codecs.xmlcharrefreplace_errors)
698 for enc in ("ascii", "iso-8859-15"):
699 for err in ("xmlcharrefreplace", "test.xmlcharrefreplace"):
700 s.encode(enc, err)
701
702 def test_decodehelper(self):
703 # enhance coverage of:
704 # Objects/unicodeobject.c::unicode_decode_call_errorhandler()
705 # and callers
706 self.assertRaises(LookupError, "\xff".decode, "ascii", "test.unknown")
707
708 def baddecodereturn1(exc):
709 return 42
710 codecs.register_error("test.baddecodereturn1", baddecodereturn1)
711 self.assertRaises(TypeError, "\xff".decode, "ascii", "test.baddecodereturn1")
712 self.assertRaises(TypeError, "\\".decode, "unicode-escape", "test.baddecodereturn1")
713 self.assertRaises(TypeError, "\\x0".decode, "unicode-escape", "test.baddecodereturn1")
714 self.assertRaises(TypeError, "\\x0y".decode, "unicode-escape", "test.baddecodereturn1")
715 self.assertRaises(TypeError, "\\Uffffeeee".decode, "unicode-escape", "test.baddecodereturn1")
716 self.assertRaises(TypeError, "\\uyyyy".decode, "raw-unicode-escape", "test.baddecodereturn1")
717
718 def baddecodereturn2(exc):
719 return (u"?", None)
720 codecs.register_error("test.baddecodereturn2", baddecodereturn2)
721 self.assertRaises(TypeError, "\xff".decode, "ascii", "test.baddecodereturn2")
722
Walter Dörwald2e0b18a2003-01-31 17:19:08 +0000723 handler = PosReturn()
724 codecs.register_error("test.posreturn", handler.handle)
Walter Dörwald30537a42003-01-08 23:22:13 +0000725
Walter Dörwald2e0b18a2003-01-31 17:19:08 +0000726 # Valid negative position
727 handler.pos = -1
Ezio Melotti2623a372010-11-21 13:34:58 +0000728 self.assertEqual("\xff0".decode("ascii", "test.posreturn"), u"<?>0")
Walter Dörwald2e0b18a2003-01-31 17:19:08 +0000729
730 # Valid negative position
731 handler.pos = -2
Ezio Melotti2623a372010-11-21 13:34:58 +0000732 self.assertEqual("\xff0".decode("ascii", "test.posreturn"), u"<?><?>")
Walter Dörwald2e0b18a2003-01-31 17:19:08 +0000733
734 # Negative position out of bounds
735 handler.pos = -3
736 self.assertRaises(IndexError, "\xff0".decode, "ascii", "test.posreturn")
737
738 # Valid positive position
739 handler.pos = 1
Ezio Melotti2623a372010-11-21 13:34:58 +0000740 self.assertEqual("\xff0".decode("ascii", "test.posreturn"), u"<?>0")
Walter Dörwald2e0b18a2003-01-31 17:19:08 +0000741
Walter Dörwald29ddfba2004-12-14 21:28:07 +0000742 # Largest valid positive position (one beyond end of input)
Walter Dörwald2e0b18a2003-01-31 17:19:08 +0000743 handler.pos = 2
Ezio Melotti2623a372010-11-21 13:34:58 +0000744 self.assertEqual("\xff0".decode("ascii", "test.posreturn"), u"<?>")
Walter Dörwald2e0b18a2003-01-31 17:19:08 +0000745
746 # Invalid positive position
747 handler.pos = 3
748 self.assertRaises(IndexError, "\xff0".decode, "ascii", "test.posreturn")
749
750 # Restart at the "0"
751 handler.pos = 6
Ezio Melotti2623a372010-11-21 13:34:58 +0000752 self.assertEqual("\\uyyyy0".decode("raw-unicode-escape", "test.posreturn"), u"<?>0")
Walter Dörwald30537a42003-01-08 23:22:13 +0000753
754 class D(dict):
755 def __getitem__(self, key):
756 raise ValueError
757 self.assertRaises(UnicodeError, codecs.charmap_decode, "\xff", "strict", {0xff: None})
758 self.assertRaises(ValueError, codecs.charmap_decode, "\xff", "strict", D())
Antoine Pitroue3ae3212012-11-17 21:14:58 +0100759 self.assertRaises(TypeError, codecs.charmap_decode, "\xff", "strict", {0xff: 0x110000})
Walter Dörwald30537a42003-01-08 23:22:13 +0000760
761 def test_encodehelper(self):
762 # enhance coverage of:
763 # Objects/unicodeobject.c::unicode_encode_call_errorhandler()
764 # and callers
765 self.assertRaises(LookupError, u"\xff".encode, "ascii", "test.unknown")
766
767 def badencodereturn1(exc):
768 return 42
769 codecs.register_error("test.badencodereturn1", badencodereturn1)
770 self.assertRaises(TypeError, u"\xff".encode, "ascii", "test.badencodereturn1")
771
772 def badencodereturn2(exc):
773 return (u"?", None)
774 codecs.register_error("test.badencodereturn2", badencodereturn2)
775 self.assertRaises(TypeError, u"\xff".encode, "ascii", "test.badencodereturn2")
776
Walter Dörwald2e0b18a2003-01-31 17:19:08 +0000777 handler = PosReturn()
778 codecs.register_error("test.posreturn", handler.handle)
Walter Dörwald30537a42003-01-08 23:22:13 +0000779
Walter Dörwald2e0b18a2003-01-31 17:19:08 +0000780 # Valid negative position
781 handler.pos = -1
Ezio Melotti2623a372010-11-21 13:34:58 +0000782 self.assertEqual(u"\xff0".encode("ascii", "test.posreturn"), "<?>0")
Walter Dörwald2e0b18a2003-01-31 17:19:08 +0000783
784 # Valid negative position
785 handler.pos = -2
Ezio Melotti2623a372010-11-21 13:34:58 +0000786 self.assertEqual(u"\xff0".encode("ascii", "test.posreturn"), "<?><?>")
Walter Dörwald2e0b18a2003-01-31 17:19:08 +0000787
788 # Negative position out of bounds
789 handler.pos = -3
790 self.assertRaises(IndexError, u"\xff0".encode, "ascii", "test.posreturn")
791
792 # Valid positive position
793 handler.pos = 1
Ezio Melotti2623a372010-11-21 13:34:58 +0000794 self.assertEqual(u"\xff0".encode("ascii", "test.posreturn"), "<?>0")
Walter Dörwald2e0b18a2003-01-31 17:19:08 +0000795
796 # Largest valid positive position (one beyond end of input
797 handler.pos = 2
Ezio Melotti2623a372010-11-21 13:34:58 +0000798 self.assertEqual(u"\xff0".encode("ascii", "test.posreturn"), "<?>")
Walter Dörwald2e0b18a2003-01-31 17:19:08 +0000799
800 # Invalid positive position
801 handler.pos = 3
802 self.assertRaises(IndexError, u"\xff0".encode, "ascii", "test.posreturn")
803
804 handler.pos = 0
Walter Dörwald30537a42003-01-08 23:22:13 +0000805
806 class D(dict):
807 def __getitem__(self, key):
808 raise ValueError
Walter Dörwald2e0b18a2003-01-31 17:19:08 +0000809 for err in ("strict", "replace", "xmlcharrefreplace", "backslashreplace", "test.posreturn"):
Walter Dörwald30537a42003-01-08 23:22:13 +0000810 self.assertRaises(UnicodeError, codecs.charmap_encode, u"\xff", err, {0xff: None})
811 self.assertRaises(ValueError, codecs.charmap_encode, u"\xff", err, D())
812 self.assertRaises(TypeError, codecs.charmap_encode, u"\xff", err, {0xff: 300})
813
814 def test_translatehelper(self):
815 # enhance coverage of:
816 # Objects/unicodeobject.c::unicode_encode_call_errorhandler()
817 # and callers
818 # (Unfortunately the errors argument is not directly accessible
819 # from Python, so we can't test that much)
820 class D(dict):
821 def __getitem__(self, key):
822 raise ValueError
823 self.assertRaises(ValueError, u"\xff".translate, D())
824 self.assertRaises(TypeError, u"\xff".translate, {0xff: sys.maxunicode+1})
825 self.assertRaises(TypeError, u"\xff".translate, {0xff: ()})
826
Walter Dörwald4894c302003-10-24 14:25:28 +0000827 def test_bug828737(self):
828 charmap = {
829 ord("&"): u"&amp;",
830 ord("<"): u"&lt;",
831 ord(">"): u"&gt;",
832 ord('"'): u"&quot;",
833 }
Tim Peters58eb11c2004-01-18 20:29:55 +0000834
Walter Dörwald4894c302003-10-24 14:25:28 +0000835 for n in (1, 10, 100, 1000):
836 text = u'abc<def>ghi'*n
837 text.translate(charmap)
838
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000839def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000840 test.test_support.run_unittest(CodecCallbackTest)
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000841
842if __name__ == "__main__":
843 test_main()