annotate Lib/test/test_codeccallbacks.py @ 43577:ace45b361e87

Patch# 1258 by Christian Heimes: kill basestring. I like this because it makes the code shorter! :-)
author Guido van Rossum <guido@python.org>
date Tue, 16 Oct 2007 18:12:55 +0000
parents cb28e14ee808
children 33efbfc28d2b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
1 import test.test_support, unittest
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
2 import sys, codecs, htmlentitydefs, unicodedata
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
3
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
4 class PosReturn:
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
5 # this can be used for configurable callbacks
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
6
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
7 def __init__(self):
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
8 self.pos = 0
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
9
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
10 def handle(self, exc):
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
11 oldpos = self.pos
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
12 realpos = oldpos
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
13 if realpos<0:
27963
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27555
diff changeset
14 realpos = len(exc.object) + realpos
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
15 # if we don't advance this time, terminate on the next call
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
16 # otherwise we'd get an endless loop
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
17 if realpos <= exc.start:
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
18 self.pos = len(exc.object)
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
19 return ("<?>", oldpos)
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
20
35889
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
21 # A UnicodeEncodeError object with a bad start attribute
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
22 class BadStartUnicodeEncodeError(UnicodeEncodeError):
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
23 def __init__(self):
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
24 UnicodeEncodeError.__init__(self, "ascii", "", 0, 1, "bad")
35889
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
25 self.start = []
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
26
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
27 # A UnicodeEncodeError object with a bad object attribute
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
28 class BadObjectUnicodeEncodeError(UnicodeEncodeError):
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
29 def __init__(self):
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
30 UnicodeEncodeError.__init__(self, "ascii", "", 0, 1, "bad")
35889
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
31 self.object = []
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
32
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
33 # A UnicodeDecodeError object without an end attribute
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
34 class NoEndUnicodeDecodeError(UnicodeDecodeError):
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
35 def __init__(self):
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
36 UnicodeDecodeError.__init__(self, "ascii", b"", 0, 1, "bad")
35889
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
37 del self.end
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
38
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
39 # A UnicodeDecodeError object with a bad object attribute
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
40 class BadObjectUnicodeDecodeError(UnicodeDecodeError):
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
41 def __init__(self):
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
42 UnicodeDecodeError.__init__(self, "ascii", b"", 0, 1, "bad")
35889
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
43 self.object = []
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
44
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
45 # A UnicodeTranslateError object without a start attribute
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
46 class NoStartUnicodeTranslateError(UnicodeTranslateError):
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
47 def __init__(self):
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
48 UnicodeTranslateError.__init__(self, "", 0, 1, "bad")
35889
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
49 del self.start
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
50
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
51 # A UnicodeTranslateError object without an end attribute
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
52 class NoEndUnicodeTranslateError(UnicodeTranslateError):
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
53 def __init__(self):
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
54 UnicodeTranslateError.__init__(self, "", 0, 1, "bad")
35889
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
55 del self.end
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
56
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
57 # A UnicodeTranslateError object without an object attribute
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
58 class NoObjectUnicodeTranslateError(UnicodeTranslateError):
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
59 def __init__(self):
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
60 UnicodeTranslateError.__init__(self, "", 0, 1, "bad")
35889
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
61 del self.object
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
62
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
63 class CodecCallbackTest(unittest.TestCase):
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
64
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
65 def test_xmlcharrefreplace(self):
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
66 # replace unencodable characters which numeric character entities.
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
67 # For ascii, latin-1 and charmaps this is completely implemented
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
68 # in C and should be reasonably fast.
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
69 s = "\u30b9\u30d1\u30e2 \xe4nd eggs"
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
70 self.assertEqual(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
71 s.encode("ascii", "xmlcharrefreplace"),
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
72 b"&#12473;&#12497;&#12514; &#228;nd eggs"
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
73 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
74 self.assertEqual(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
75 s.encode("latin-1", "xmlcharrefreplace"),
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
76 b"&#12473;&#12497;&#12514; \xe4nd eggs"
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
77 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
78
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
79 def test_xmlcharnamereplace(self):
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
80 # This time use a named character entity for unencodable
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
81 # characters, if one is available.
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
82
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
83 def xmlcharnamereplace(exc):
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
84 if not isinstance(exc, UnicodeEncodeError):
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
85 raise TypeError("don't know how to handle %r" % exc)
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
86 l = []
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
87 for c in exc.object[exc.start:exc.end]:
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
88 try:
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
89 l.append("&%s;" % htmlentitydefs.codepoint2name[ord(c)])
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
90 except KeyError:
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
91 l.append("&#%d;" % ord(c))
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
92 return ("".join(l), exc.end)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
93
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
94 codecs.register_error(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
95 "test.xmlcharnamereplace", xmlcharnamereplace)
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
96
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
97 sin = "\xab\u211c\xbb = \u2329\u1234\u20ac\u232a"
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
98 sout = b"&laquo;&real;&raquo; = &lang;&#4660;&euro;&rang;"
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
99 self.assertEqual(sin.encode("ascii", "test.xmlcharnamereplace"), sout)
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
100 sout = b"\xab&real;\xbb = &lang;&#4660;&euro;&rang;"
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
101 self.assertEqual(sin.encode("latin-1", "test.xmlcharnamereplace"), sout)
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
102 sout = b"\xab&real;\xbb = &lang;&#4660;\xa4&rang;"
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
103 self.assertEqual(sin.encode("iso-8859-15", "test.xmlcharnamereplace"), sout)
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
104
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
105 def test_uninamereplace(self):
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
106 # We're using the names from the unicode database this time,
26405
b38533e01d65 Fix typo in comment.
Walter Dörwald <walter@livinglogic.de>
parents: 26378
diff changeset
107 # and we're doing "syntax highlighting" here, i.e. we include
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
108 # the replaced text in ANSI escape sequences. For this it is
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
109 # useful that the error handler is not called for every single
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
110 # unencodable character, but for a complete sequence of
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
111 # unencodable characters, otherwise we would output many
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
112 # unneccessary escape sequences.
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
113
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
114 def uninamereplace(exc):
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
115 if not isinstance(exc, UnicodeEncodeError):
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
116 raise TypeError("don't know how to handle %r" % exc)
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
117 l = []
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
118 for c in exc.object[exc.start:exc.end]:
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
119 l.append(unicodedata.name(c, "0x%x" % ord(c)))
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
120 return ("\033[1m%s\033[0m" % ", ".join(l), exc.end)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
121
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
122 codecs.register_error(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
123 "test.uninamereplace", uninamereplace)
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
124
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
125 sin = "\xac\u1234\u20ac\u8000"
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
126 sout = b"\033[1mNOT SIGN, ETHIOPIC SYLLABLE SEE, EURO SIGN, CJK UNIFIED IDEOGRAPH-8000\033[0m"
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
127 self.assertEqual(sin.encode("ascii", "test.uninamereplace"), sout)
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
128
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
129 sout = b"\xac\033[1mETHIOPIC SYLLABLE SEE, EURO SIGN, CJK UNIFIED IDEOGRAPH-8000\033[0m"
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
130 self.assertEqual(sin.encode("latin-1", "test.uninamereplace"), sout)
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
131
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
132 sout = b"\xac\033[1mETHIOPIC SYLLABLE SEE\033[0m\xa4\033[1mCJK UNIFIED IDEOGRAPH-8000\033[0m"
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
133 self.assertEqual(sin.encode("iso-8859-15", "test.uninamereplace"), sout)
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
134
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
135 def test_backslashescape(self):
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
136 # Does the same as the "unicode-escape" encoding, but with different
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
137 # base encodings.
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
138 sin = "a\xac\u1234\u20ac\u8000"
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
139 if sys.maxunicode > 0xffff:
41585
825d966338ea Rename 'unicode' to 'str' in its tp_name field. Rename 'str' to 'str8'.
Guido van Rossum <guido@python.org>
parents: 41575
diff changeset
140 sin += chr(sys.maxunicode)
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
141 sout = b"a\\xac\\u1234\\u20ac\\u8000"
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
142 if sys.maxunicode > 0xffff:
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 42911
diff changeset
143 sout += bytes("\\U%08x" % sys.maxunicode, "ascii")
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
144 self.assertEqual(sin.encode("ascii", "backslashreplace"), sout)
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
145
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
146 sout = b"a\xac\\u1234\\u20ac\\u8000"
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
147 if sys.maxunicode > 0xffff:
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 42911
diff changeset
148 sout += bytes("\\U%08x" % sys.maxunicode, "ascii")
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
149 self.assertEqual(sin.encode("latin-1", "backslashreplace"), sout)
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
150
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
151 sout = b"a\xac\\u1234\xa4\\u8000"
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
152 if sys.maxunicode > 0xffff:
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 42911
diff changeset
153 sout += bytes("\\U%08x" % sys.maxunicode, "ascii")
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
154 self.assertEqual(sin.encode("iso-8859-15", "backslashreplace"), sout)
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
155
35485
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
156 def test_decoderelaxedutf8(self):
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
157 # This is the test for a decoding callback handler,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
158 # that relaxes the UTF-8 minimal encoding restriction.
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
159 # A null byte that is encoded as "\xc0\x80" will be
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
160 # decoded as a null byte. All other illegal sequences
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
161 # will be handled strictly.
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
162 def relaxedutf8(exc):
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
163 if not isinstance(exc, UnicodeDecodeError):
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
164 raise TypeError("don't know how to handle %r" % exc)
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
165 if exc.object[exc.start:exc.end].startswith(b"\xc0\x80"):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
166 return ("\x00", exc.start+2) # retry after two bytes
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
167 else:
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
168 raise exc
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
169
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
170 codecs.register_error(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
171 "test.relaxedutf8", relaxedutf8)
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
172
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
173 sin = b"a\x00b\xc0\x80c\xc3\xbc\xc0\x80\xc0\x80"
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
174 sout = "a\x00b\x00c\xfc\x00\x00"
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
175 self.assertEqual(sin.decode("utf-8", "test.relaxedutf8"), sout)
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
176 sin = b"\xc0\x80\xc0\x81"
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
177 self.assertRaises(UnicodeError, sin.decode, "utf-8", "test.relaxedutf8")
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
178
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
179 def test_charmapencode(self):
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
180 # For charmap encodings the replacement string will be
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
181 # mapped through the encoding again. This means, that
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
182 # to be able to use e.g. the "replace" handler, the
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
183 # charmap has to have a mapping for "?".
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
184 charmap = dict((ord(c), str8(2*c.upper())) for c in "abcdefgh")
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
185 sin = "abc"
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
186 sout = b"AABBCC"
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
187 self.assertEquals(codecs.charmap_encode(sin, "strict", charmap)[0], sout)
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
188
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
189 sin = "abcA"
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
190 self.assertRaises(UnicodeError, codecs.charmap_encode, sin, "strict", charmap)
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
191
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
192 charmap[ord("?")] = str8("XYZ")
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
193 sin = "abcDEF"
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
194 sout = b"AABBCCXYZXYZXYZ"
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
195 self.assertEquals(codecs.charmap_encode(sin, "replace", charmap)[0], sout)
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
196
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
197 charmap[ord("?")] = "XYZ" # wrong type in mapping
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
198 self.assertRaises(TypeError, codecs.charmap_encode, sin, "replace", charmap)
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
199
35485
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
200 def test_decodeunicodeinternal(self):
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
201 self.assertRaises(
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
202 UnicodeDecodeError,
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
203 b"\x00\x00\x00\x00\x00".decode,
35485
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
204 "unicode-internal",
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
205 )
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
206 if sys.maxunicode > 0xffff:
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
207 def handler_unicodeinternal(exc):
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
208 if not isinstance(exc, UnicodeDecodeError):
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
209 raise TypeError("don't know how to handle %r" % exc)
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
210 return ("\x01", 1)
35485
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
211
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
212 self.assertEqual(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
213 b"\x00\x00\x00\x00\x00".decode("unicode-internal", "ignore"),
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
214 "\u0000"
35485
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
215 )
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
216
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
217 self.assertEqual(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
218 b"\x00\x00\x00\x00\x00".decode("unicode-internal", "replace"),
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
219 "\u0000\ufffd"
35485
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
220 )
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
221
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
222 codecs.register_error("test.hui", handler_unicodeinternal)
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
223
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
224 self.assertEqual(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
225 b"\x00\x00\x00\x00\x00".decode("unicode-internal", "test.hui"),
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
226 "\u0000\u0001\u0000"
35485
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
227 )
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
228
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
229 def test_callbacks(self):
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
230 def handler1(exc):
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
231 r = range(exc.start, exc.end)
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
232 if isinstance(exc, UnicodeEncodeError):
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
233 l = ["<%d>" % ord(exc.object[pos]) for pos in r]
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
234 elif isinstance(exc, UnicodeDecodeError):
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
235 l = ["<%d>" % exc.object[pos] for pos in r]
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
236 else:
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
237 raise TypeError("don't know how to handle %r" % exc)
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
238 return ("[%s]" % "".join(l), exc.end)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
239
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
240 codecs.register_error("test.handler1", handler1)
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
241
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
242 def handler2(exc):
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
243 if not isinstance(exc, UnicodeDecodeError):
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
244 raise TypeError("don't know how to handle %r" % exc)
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
245 l = ["<%d>" % exc.object[pos] for pos in range(exc.start, exc.end)]
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
246 return ("[%s]" % "".join(l), exc.end+1) # skip one character
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
247
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
248 codecs.register_error("test.handler2", handler2)
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
249
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
250 s = b"\x00\x81\x7f\x80\xff"
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
251
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
252 self.assertEqual(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
253 s.decode("ascii", "test.handler1"),
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
254 "\x00[<129>]\x7f[<128>][<255>]"
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
255 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
256 self.assertEqual(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
257 s.decode("ascii", "test.handler2"),
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
258 "\x00[<129>][<128>]"
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
259 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
260
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
261 self.assertEqual(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
262 b"\\u3042\u3xxx".decode("unicode-escape", "test.handler1"),
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
263 "\u3042[<92><117><51><120>]xx"
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
264 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
265
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
266 self.assertEqual(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
267 b"\\u3042\u3xx".decode("unicode-escape", "test.handler1"),
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
268 "\u3042[<92><117><51><120><120>]"
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
269 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
270
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
271 self.assertEqual(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
272 codecs.charmap_decode(b"abc", "test.handler1", {ord("a"): "z"})[0],
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
273 "z[<98>][<99>]"
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
274 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
275
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
276 self.assertEqual(
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
277 "g\xfc\xdfrk".encode("ascii", "test.handler1"),
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
278 b"g[<252><223>]rk"
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
279 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
280
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
281 self.assertEqual(
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
282 "g\xfc\xdf".encode("ascii", "test.handler1"),
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
283 b"g[<252><223>]"
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
284 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
285
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
286 def test_longstrings(self):
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
287 # test long strings to check for memory overflow problems
42626
f1a2d51f1cab Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents: 42408
diff changeset
288 errors = [ "strict", "ignore", "replace", "xmlcharrefreplace",
f1a2d51f1cab Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents: 42408
diff changeset
289 "backslashreplace"]
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
290 # register the handlers under different names,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
291 # to prevent the codec from recognizing the name
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
292 for err in errors:
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
293 codecs.register_error("test." + err, codecs.lookup_error(err))
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
294 l = 1000
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
295 errors += [ "test." + err for err in errors ]
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
296 for uni in [ s*l for s in ("x", "\u3042", "a\xe4") ]:
42626
f1a2d51f1cab Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents: 42408
diff changeset
297 for enc in ("ascii", "latin-1", "iso-8859-1", "iso-8859-15",
f1a2d51f1cab Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents: 42408
diff changeset
298 "utf-8", "utf-7", "utf-16", "utf-32"):
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
299 for err in errors:
26128
23ef7c163a12 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 25139
diff changeset
300 try:
23ef7c163a12 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 25139
diff changeset
301 uni.encode(enc, err)
23ef7c163a12 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 25139
diff changeset
302 except UnicodeError:
23ef7c163a12 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 25139
diff changeset
303 pass
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
304
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
305 def check_exceptionobjectargs(self, exctype, args, msg):
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
306 # Test UnicodeError subclasses: construction, attribute assignment and __str__ conversion
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
307 # check with one missing argument
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
308 self.assertRaises(TypeError, exctype, *args[:-1])
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
309 # check with one argument too much
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
310 self.assertRaises(TypeError, exctype, *(args + ["too much"]))
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
311 # check with one argument of the wrong type
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
312 wrongargs = [ "spam", str8("eggs"), b"spam", 42, 1.0, None ]
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41585
diff changeset
313 for i in range(len(args)):
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
314 for wrongarg in wrongargs:
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
315 if type(wrongarg) is type(args[i]):
26128
23ef7c163a12 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 25139
diff changeset
316 continue
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
317 # build argument array
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
318 callargs = []
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41585
diff changeset
319 for j in range(len(args)):
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
320 if i==j:
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
321 callargs.append(wrongarg)
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
322 else:
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
323 callargs.append(args[i])
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
324 self.assertRaises(TypeError, exctype, *callargs)
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
325
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
326 # check with the correct number and type of arguments
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
327 exc = exctype(*args)
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
328 self.assertEquals(str(exc), msg)
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
329
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
330 def test_unicodeencodeerror(self):
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
331 self.check_exceptionobjectargs(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
332 UnicodeEncodeError,
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
333 ["ascii", "g\xfcrk", 1, 2, "ouch"],
42118
70a0831feda6 Patch by Ron Adam: Don't use u prefix in unicode error messages
Walter Dörwald <walter@livinglogic.de>
parents: 41963
diff changeset
334 "'ascii' codec can't encode character '\\xfc' in position 1: ouch"
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
335 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
336 self.check_exceptionobjectargs(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
337 UnicodeEncodeError,
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
338 ["ascii", "g\xfcrk", 1, 4, "ouch"],
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
339 "'ascii' codec can't encode characters in position 1-3: ouch"
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
340 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
341 self.check_exceptionobjectargs(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
342 UnicodeEncodeError,
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
343 ["ascii", "\xfcx", 0, 1, "ouch"],
42118
70a0831feda6 Patch by Ron Adam: Don't use u prefix in unicode error messages
Walter Dörwald <walter@livinglogic.de>
parents: 41963
diff changeset
344 "'ascii' codec can't encode character '\\xfc' in position 0: ouch"
30137
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
345 )
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
346 self.check_exceptionobjectargs(
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
347 UnicodeEncodeError,
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
348 ["ascii", "\u0100x", 0, 1, "ouch"],
42118
70a0831feda6 Patch by Ron Adam: Don't use u prefix in unicode error messages
Walter Dörwald <walter@livinglogic.de>
parents: 41963
diff changeset
349 "'ascii' codec can't encode character '\\u0100' in position 0: ouch"
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
350 )
30137
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
351 self.check_exceptionobjectargs(
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
352 UnicodeEncodeError,
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
353 ["ascii", "\uffffx", 0, 1, "ouch"],
42118
70a0831feda6 Patch by Ron Adam: Don't use u prefix in unicode error messages
Walter Dörwald <walter@livinglogic.de>
parents: 41963
diff changeset
354 "'ascii' codec can't encode character '\\uffff' in position 0: ouch"
30137
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
355 )
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
356 if sys.maxunicode > 0xffff:
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
357 self.check_exceptionobjectargs(
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
358 UnicodeEncodeError,
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
359 ["ascii", "\U00010000x", 0, 1, "ouch"],
42118
70a0831feda6 Patch by Ron Adam: Don't use u prefix in unicode error messages
Walter Dörwald <walter@livinglogic.de>
parents: 41963
diff changeset
360 "'ascii' codec can't encode character '\\U00010000' in position 0: ouch"
30137
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
361 )
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
362
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
363 def test_unicodedecodeerror(self):
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
364 self.check_exceptionobjectargs(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
365 UnicodeDecodeError,
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
366 ["ascii", b"g\xfcrk", 1, 2, "ouch"],
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
367 "'ascii' codec can't decode byte 0xfc in position 1: ouch"
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
368 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
369 self.check_exceptionobjectargs(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
370 UnicodeDecodeError,
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
371 ["ascii", b"g\xfcrk", 1, 3, "ouch"],
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
372 "'ascii' codec can't decode bytes in position 1-2: ouch"
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
373 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
374
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
375 def test_unicodetranslateerror(self):
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
376 self.check_exceptionobjectargs(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
377 UnicodeTranslateError,
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
378 ["g\xfcrk", 1, 2, "ouch"],
42118
70a0831feda6 Patch by Ron Adam: Don't use u prefix in unicode error messages
Walter Dörwald <walter@livinglogic.de>
parents: 41963
diff changeset
379 "can't translate character '\\xfc' in position 1: ouch"
30137
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
380 )
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
381 self.check_exceptionobjectargs(
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
382 UnicodeTranslateError,
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
383 ["g\u0100rk", 1, 2, "ouch"],
42118
70a0831feda6 Patch by Ron Adam: Don't use u prefix in unicode error messages
Walter Dörwald <walter@livinglogic.de>
parents: 41963
diff changeset
384 "can't translate character '\\u0100' in position 1: ouch"
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
385 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
386 self.check_exceptionobjectargs(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
387 UnicodeTranslateError,
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
388 ["g\uffffrk", 1, 2, "ouch"],
42118
70a0831feda6 Patch by Ron Adam: Don't use u prefix in unicode error messages
Walter Dörwald <walter@livinglogic.de>
parents: 41963
diff changeset
389 "can't translate character '\\uffff' in position 1: ouch"
30137
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
390 )
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
391 if sys.maxunicode > 0xffff:
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
392 self.check_exceptionobjectargs(
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
393 UnicodeTranslateError,
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
394 ["g\U00010000rk", 1, 2, "ouch"],
42118
70a0831feda6 Patch by Ron Adam: Don't use u prefix in unicode error messages
Walter Dörwald <walter@livinglogic.de>
parents: 41963
diff changeset
395 "can't translate character '\\U00010000' in position 1: ouch"
30137
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
396 )
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
397 self.check_exceptionobjectargs(
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
398 UnicodeTranslateError,
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
399 ["g\xfcrk", 1, 3, "ouch"],
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
400 "can't translate characters in position 1-2: ouch"
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
401 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
402
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
403 def test_badandgoodstrictexceptions(self):
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
404 # "strict" complains about a non-exception passed in
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
405 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
406 TypeError,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
407 codecs.strict_errors,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
408 42
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
409 )
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
410 # "strict" complains about the wrong exception type
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
411 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
412 Exception,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
413 codecs.strict_errors,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
414 Exception("ouch")
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
415 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
416
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
417 # If the correct exception is passed in, "strict" raises it
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
418 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
419 UnicodeEncodeError,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
420 codecs.strict_errors,
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
421 UnicodeEncodeError("ascii", "\u3042", 0, 1, "ouch")
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
422 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
423
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
424 def test_badandgoodignoreexceptions(self):
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
425 # "ignore" complains about a non-exception passed in
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
426 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
427 TypeError,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
428 codecs.ignore_errors,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
429 42
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
430 )
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
431 # "ignore" complains about the wrong exception type
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
432 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
433 TypeError,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
434 codecs.ignore_errors,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
435 UnicodeError("ouch")
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
436 )
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
437 # If the correct exception is passed in, "ignore" returns an empty replacement
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
438 self.assertEquals(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
439 codecs.ignore_errors(
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
440 UnicodeEncodeError("ascii", "\u3042", 0, 1, "ouch")),
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
441 ("", 1)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
442 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
443 self.assertEquals(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
444 codecs.ignore_errors(
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
445 UnicodeDecodeError("ascii", b"\xff", 0, 1, "ouch")),
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
446 ("", 1)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
447 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
448 self.assertEquals(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
449 codecs.ignore_errors(
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
450 UnicodeTranslateError("\u3042", 0, 1, "ouch")),
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
451 ("", 1)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
452 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
453
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
454 def test_badandgoodreplaceexceptions(self):
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
455 # "replace" complains about a non-exception passed in
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
456 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
457 TypeError,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
458 codecs.replace_errors,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
459 42
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
460 )
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
461 # "replace" complains about the wrong exception type
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
462 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
463 TypeError,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
464 codecs.replace_errors,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
465 UnicodeError("ouch")
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
466 )
35889
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
467 self.assertRaises(
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
468 TypeError,
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
469 codecs.replace_errors,
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
470 BadObjectUnicodeEncodeError()
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
471 )
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
472 self.assertRaises(
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
473 TypeError,
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
474 codecs.replace_errors,
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
475 BadObjectUnicodeDecodeError()
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
476 )
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41585
diff changeset
477 # With the correct exception, "replace" returns an "?" or "\ufffd" replacement
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
478 self.assertEquals(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
479 codecs.replace_errors(
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
480 UnicodeEncodeError("ascii", "\u3042", 0, 1, "ouch")),
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
481 ("?", 1)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
482 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
483 self.assertEquals(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
484 codecs.replace_errors(
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
485 UnicodeDecodeError("ascii", b"\xff", 0, 1, "ouch")),
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
486 ("\ufffd", 1)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
487 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
488 self.assertEquals(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
489 codecs.replace_errors(
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
490 UnicodeTranslateError("\u3042", 0, 1, "ouch")),
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
491 ("\ufffd", 1)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
492 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
493
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
494 def test_badandgoodxmlcharrefreplaceexceptions(self):
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
495 # "xmlcharrefreplace" complains about a non-exception passed in
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
496 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
497 TypeError,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
498 codecs.xmlcharrefreplace_errors,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
499 42
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
500 )
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
501 # "xmlcharrefreplace" complains about the wrong exception types
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
502 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
503 TypeError,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
504 codecs.xmlcharrefreplace_errors,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
505 UnicodeError("ouch")
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
506 )
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
507 # "xmlcharrefreplace" can only be used for encoding
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
508 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
509 TypeError,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
510 codecs.xmlcharrefreplace_errors,
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
511 UnicodeDecodeError("ascii", b"\xff", 0, 1, "ouch")
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
512 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
513 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
514 TypeError,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
515 codecs.xmlcharrefreplace_errors,
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
516 UnicodeTranslateError("\u3042", 0, 1, "ouch")
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
517 )
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
518 # Use the correct exception
35889
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
519 cs = (0, 1, 9, 10, 99, 100, 999, 1000, 9999, 10000, 0x3042)
41585
825d966338ea Rename 'unicode' to 'str' in its tp_name field. Rename 'str' to 'str8'.
Guido van Rossum <guido@python.org>
parents: 41575
diff changeset
520 s = "".join(chr(c) for c in cs)
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
521 self.assertEquals(
35889
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
522 codecs.xmlcharrefreplace_errors(
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
523 UnicodeEncodeError("ascii", s, 0, len(s), "ouch")
35889
e2473c4f2ee0 Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents: 35882
diff changeset
524 ),
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
525 ("".join("&#%d;" % ord(c) for c in s), len(s))
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
526 )
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
527
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
528 def test_badandgoodbackslashreplaceexceptions(self):
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
529 # "backslashreplace" complains about a non-exception passed in
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
530 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
531 TypeError,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
532 codecs.backslashreplace_errors,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
533 42
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
534 )
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
535 # "backslashreplace" complains about the wrong exception types
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
536 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
537 TypeError,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
538 codecs.backslashreplace_errors,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
539 UnicodeError("ouch")
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
540 )
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
541 # "backslashreplace" can only be used for encoding
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
542 self.assertRaises(
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
543 TypeError,
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
544 codecs.backslashreplace_errors,
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
545 UnicodeDecodeError("ascii", b"\xff", 0, 1, "ouch")
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
546 )
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
547 self.assertRaises(
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
548 TypeError,
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
549 codecs.backslashreplace_errors,
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
550 UnicodeTranslateError("\u3042", 0, 1, "ouch")
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
551 )
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
552 # Use the correct exception
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
553 self.assertEquals(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
554 codecs.backslashreplace_errors(
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
555 UnicodeEncodeError("ascii", "\u3042", 0, 1, "ouch")),
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
556 ("\\u3042", 1)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
557 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
558 self.assertEquals(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
559 codecs.backslashreplace_errors(
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
560 UnicodeEncodeError("ascii", "\x00", 0, 1, "ouch")),
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
561 ("\\x00", 1)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
562 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
563 self.assertEquals(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
564 codecs.backslashreplace_errors(
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
565 UnicodeEncodeError("ascii", "\xff", 0, 1, "ouch")),
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
566 ("\\xff", 1)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
567 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
568 self.assertEquals(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
569 codecs.backslashreplace_errors(
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
570 UnicodeEncodeError("ascii", "\u0100", 0, 1, "ouch")),
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
571 ("\\u0100", 1)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
572 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
573 self.assertEquals(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
574 codecs.backslashreplace_errors(
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
575 UnicodeEncodeError("ascii", "\uffff", 0, 1, "ouch")),
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
576 ("\\uffff", 1)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
577 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
578 if sys.maxunicode>0xffff:
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
579 self.assertEquals(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
580 codecs.backslashreplace_errors(
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
581 UnicodeEncodeError("ascii", "\U00010000", 0, 1, "ouch")),
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
582 ("\\U00010000", 1)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
583 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
584 self.assertEquals(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
585 codecs.backslashreplace_errors(
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
586 UnicodeEncodeError("ascii", "\U0010ffff", 0, 1, "ouch")),
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
587 ("\\U0010ffff", 1)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
588 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
589
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
590 def test_badhandlerresults(self):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
591 results = ( 42, "foo", (1,2,3), ("foo", 1, 3), ("foo", None), ("foo",), ("foo", 1, 3), ("foo", None), ("foo",) )
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
592 encs = ("ascii", "latin-1", "iso-8859-1", "iso-8859-15")
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
593
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
594 for res in results:
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
595 codecs.register_error("test.badhandler", lambda: res)
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
596 for enc in encs:
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
597 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
598 TypeError,
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
599 "\u3042".encode,
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
600 enc,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
601 "test.badhandler"
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
602 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
603 for (enc, bytes) in (
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
604 ("ascii", b"\xff"),
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
605 ("utf-8", b"\xff"),
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
606 ("utf-7", b"+x-"),
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
607 ("unicode-internal", b"\x00"),
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
608 ):
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
609 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
610 TypeError,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
611 bytes.decode,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
612 enc,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
613 "test.badhandler"
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
614 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
615
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
616 def test_lookup(self):
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
617 self.assertEquals(codecs.strict_errors, codecs.lookup_error("strict"))
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
618 self.assertEquals(codecs.ignore_errors, codecs.lookup_error("ignore"))
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
619 self.assertEquals(codecs.strict_errors, codecs.lookup_error("strict"))
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
620 self.assertEquals(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
621 codecs.xmlcharrefreplace_errors,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
622 codecs.lookup_error("xmlcharrefreplace")
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
623 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
624 self.assertEquals(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
625 codecs.backslashreplace_errors,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
626 codecs.lookup_error("backslashreplace")
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
627 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
628
25139
d446b5151316 Add a test case that checks that the proper exception is raises
Walter Dörwald <walter@livinglogic.de>
parents: 25091
diff changeset
629 def test_unencodablereplacement(self):
d446b5151316 Add a test case that checks that the proper exception is raises
Walter Dörwald <walter@livinglogic.de>
parents: 25091
diff changeset
630 def unencrepl(exc):
d446b5151316 Add a test case that checks that the proper exception is raises
Walter Dörwald <walter@livinglogic.de>
parents: 25091
diff changeset
631 if isinstance(exc, UnicodeEncodeError):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
632 return ("\u4242", exc.end)
25139
d446b5151316 Add a test case that checks that the proper exception is raises
Walter Dörwald <walter@livinglogic.de>
parents: 25091
diff changeset
633 else:
d446b5151316 Add a test case that checks that the proper exception is raises
Walter Dörwald <walter@livinglogic.de>
parents: 25091
diff changeset
634 raise TypeError("don't know how to handle %r" % exc)
d446b5151316 Add a test case that checks that the proper exception is raises
Walter Dörwald <walter@livinglogic.de>
parents: 25091
diff changeset
635 codecs.register_error("test.unencreplhandler", unencrepl)
d446b5151316 Add a test case that checks that the proper exception is raises
Walter Dörwald <walter@livinglogic.de>
parents: 25091
diff changeset
636 for enc in ("ascii", "iso-8859-1", "iso-8859-15"):
d446b5151316 Add a test case that checks that the proper exception is raises
Walter Dörwald <walter@livinglogic.de>
parents: 25091
diff changeset
637 self.assertRaises(
d446b5151316 Add a test case that checks that the proper exception is raises
Walter Dörwald <walter@livinglogic.de>
parents: 25091
diff changeset
638 UnicodeEncodeError,
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
639 "\u4242".encode,
25139
d446b5151316 Add a test case that checks that the proper exception is raises
Walter Dörwald <walter@livinglogic.de>
parents: 25091
diff changeset
640 enc,
d446b5151316 Add a test case that checks that the proper exception is raises
Walter Dörwald <walter@livinglogic.de>
parents: 25091
diff changeset
641 "test.unencreplhandler"
d446b5151316 Add a test case that checks that the proper exception is raises
Walter Dörwald <walter@livinglogic.de>
parents: 25091
diff changeset
642 )
d446b5151316 Add a test case that checks that the proper exception is raises
Walter Dörwald <walter@livinglogic.de>
parents: 25091
diff changeset
643
27177
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
644 def test_badregistercall(self):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
645 # enhance coverage of:
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
646 # Modules/_codecsmodule.c::register_error()
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
647 # Python/codecs.c::PyCodec_RegisterError()
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
648 self.assertRaises(TypeError, codecs.register_error, 42)
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
649 self.assertRaises(TypeError, codecs.register_error, "test.dummy", 42)
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
650
35882
3236b5fb1fb7 Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents: 35485
diff changeset
651 def test_badlookupcall(self):
3236b5fb1fb7 Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents: 35485
diff changeset
652 # enhance coverage of:
3236b5fb1fb7 Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents: 35485
diff changeset
653 # Modules/_codecsmodule.c::lookup_error()
3236b5fb1fb7 Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents: 35485
diff changeset
654 self.assertRaises(TypeError, codecs.lookup_error)
3236b5fb1fb7 Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents: 35485
diff changeset
655
27177
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
656 def test_unknownhandler(self):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
657 # enhance coverage of:
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
658 # Modules/_codecsmodule.c::lookup_error()
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
659 self.assertRaises(LookupError, codecs.lookup_error, "test.unknown")
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
660
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
661 def test_xmlcharrefvalues(self):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
662 # enhance coverage of:
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
663 # Python/codecs.c::PyCodec_XMLCharRefReplaceErrors()
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
664 # and inline implementations
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
665 v = (1, 5, 10, 50, 100, 500, 1000, 5000, 10000, 50000)
27183
01d9fd595375 Make the test scripts work again with narrow Python builds.
Walter Dörwald <walter@livinglogic.de>
parents: 27177
diff changeset
666 if sys.maxunicode>=100000:
27963
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27555
diff changeset
667 v += (100000, 500000, 1000000)
41585
825d966338ea Rename 'unicode' to 'str' in its tp_name field. Rename 'str' to 'str8'.
Guido van Rossum <guido@python.org>
parents: 41575
diff changeset
668 s = "".join([chr(x) for x in v])
27177
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
669 codecs.register_error("test.xmlcharrefreplace", codecs.xmlcharrefreplace_errors)
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
670 for enc in ("ascii", "iso-8859-15"):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
671 for err in ("xmlcharrefreplace", "test.xmlcharrefreplace"):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
672 s.encode(enc, err)
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
673
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
674 def test_decodehelper(self):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
675 # enhance coverage of:
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
676 # Objects/unicodeobject.c::unicode_decode_call_errorhandler()
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
677 # and callers
42911
cb28e14ee808 Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42626
diff changeset
678 self.assertRaises(LookupError, b"\xff".decode, "ascii", "test.unknown")
27177
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
679
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
680 def baddecodereturn1(exc):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
681 return 42
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
682 codecs.register_error("test.baddecodereturn1", baddecodereturn1)
42911
cb28e14ee808 Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42626
diff changeset
683 self.assertRaises(TypeError, b"\xff".decode, "ascii", "test.baddecodereturn1")
cb28e14ee808 Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42626
diff changeset
684 self.assertRaises(TypeError, b"\\".decode, "unicode-escape", "test.baddecodereturn1")
cb28e14ee808 Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42626
diff changeset
685 self.assertRaises(TypeError, b"\\x0".decode, "unicode-escape", "test.baddecodereturn1")
cb28e14ee808 Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42626
diff changeset
686 self.assertRaises(TypeError, b"\\x0y".decode, "unicode-escape", "test.baddecodereturn1")
cb28e14ee808 Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42626
diff changeset
687 self.assertRaises(TypeError, b"\\Uffffeeee".decode, "unicode-escape", "test.baddecodereturn1")
cb28e14ee808 Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42626
diff changeset
688 self.assertRaises(TypeError, b"\\uyyyy".decode, "raw-unicode-escape", "test.baddecodereturn1")
27177
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
689
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
690 def baddecodereturn2(exc):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
691 return ("?", None)
27177
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
692 codecs.register_error("test.baddecodereturn2", baddecodereturn2)
42911
cb28e14ee808 Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42626
diff changeset
693 self.assertRaises(TypeError, b"\xff".decode, "ascii", "test.baddecodereturn2")
27177
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
694
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
695 handler = PosReturn()
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
696 codecs.register_error("test.posreturn", handler.handle)
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
697
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
698 # Valid negative position
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
699 handler.pos = -1
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
700 self.assertEquals(b"\xff0".decode("ascii", "test.posreturn"), "<?>0")
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
701
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
702 # Valid negative position
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
703 handler.pos = -2
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
704 self.assertEquals(b"\xff0".decode("ascii", "test.posreturn"), "<?><?>")
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
705
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
706 # Negative position out of bounds
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
707 handler.pos = -3
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
708 self.assertRaises(IndexError, b"\xff0".decode, "ascii", "test.posreturn")
27177
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
709
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
710 # Valid positive position
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
711 handler.pos = 1
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
712 self.assertEquals(b"\xff0".decode("ascii", "test.posreturn"), "<?>0")
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
713
34166
e53693a02cf0 Fix copy & paste error in comments.
Walter Dörwald <walter@livinglogic.de>
parents: 31389
diff changeset
714 # Largest valid positive position (one beyond end of input)
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
715 handler.pos = 2
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
716 self.assertEquals(b"\xff0".decode("ascii", "test.posreturn"), "<?>")
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
717
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
718 # Invalid positive position
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
719 handler.pos = 3
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
720 self.assertRaises(IndexError, b"\xff0".decode, "ascii", "test.posreturn")
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
721
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
722 # Restart at the "0"
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
723 handler.pos = 6
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
724 self.assertEquals(b"\\uyyyy0".decode("raw-unicode-escape", "test.posreturn"), "<?>0")
27177
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
725
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
726 class D(dict):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
727 def __getitem__(self, key):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
728 raise ValueError
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
729 self.assertRaises(UnicodeError, codecs.charmap_decode, b"\xff", "strict", {0xff: None})
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
730 self.assertRaises(ValueError, codecs.charmap_decode, b"\xff", "strict", D())
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
731 self.assertRaises(TypeError, codecs.charmap_decode, b"\xff", "strict", {0xff: sys.maxunicode+1})
27177
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
732
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
733 def test_encodehelper(self):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
734 # enhance coverage of:
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
735 # Objects/unicodeobject.c::unicode_encode_call_errorhandler()
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
736 # and callers
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
737 self.assertRaises(LookupError, "\xff".encode, "ascii", "test.unknown")
27177
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
738
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
739 def badencodereturn1(exc):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
740 return 42
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
741 codecs.register_error("test.badencodereturn1", badencodereturn1)
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
742 self.assertRaises(TypeError, "\xff".encode, "ascii", "test.badencodereturn1")
27177
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
743
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
744 def badencodereturn2(exc):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
745 return ("?", None)
27177
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
746 codecs.register_error("test.badencodereturn2", badencodereturn2)
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
747 self.assertRaises(TypeError, "\xff".encode, "ascii", "test.badencodereturn2")
27177
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
748
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
749 handler = PosReturn()
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
750 codecs.register_error("test.posreturn", handler.handle)
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
751
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
752 # Valid negative position
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
753 handler.pos = -1
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
754 self.assertEquals("\xff0".encode("ascii", "test.posreturn"), b"<?>0")
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
755
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
756 # Valid negative position
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
757 handler.pos = -2
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
758 self.assertEquals("\xff0".encode("ascii", "test.posreturn"), b"<?><?>")
27177
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
759
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
760 # Negative position out of bounds
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
761 handler.pos = -3
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
762 self.assertRaises(IndexError, "\xff0".encode, "ascii", "test.posreturn")
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
763
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
764 # Valid positive position
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
765 handler.pos = 1
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
766 self.assertEquals("\xff0".encode("ascii", "test.posreturn"), b"<?>0")
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
767
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
768 # Largest valid positive position (one beyond end of input
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
769 handler.pos = 2
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
770 self.assertEquals("\xff0".encode("ascii", "test.posreturn"), b"<?>")
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
771
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
772 # Invalid positive position
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
773 handler.pos = 3
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
774 self.assertRaises(IndexError, "\xff0".encode, "ascii", "test.posreturn")
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
775
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
776 handler.pos = 0
27177
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
777
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
778 class D(dict):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
779 def __getitem__(self, key):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
780 raise ValueError
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
781 for err in ("strict", "replace", "xmlcharrefreplace", "backslashreplace", "test.posreturn"):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
782 self.assertRaises(UnicodeError, codecs.charmap_encode, "\xff", err, {0xff: None})
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
783 self.assertRaises(ValueError, codecs.charmap_encode, "\xff", err, D())
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
784 self.assertRaises(TypeError, codecs.charmap_encode, "\xff", err, {0xff: 300})
27177
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
785
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
786 def test_translatehelper(self):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
787 # enhance coverage of:
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
788 # Objects/unicodeobject.c::unicode_encode_call_errorhandler()
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
789 # and callers
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
790 # (Unfortunately the errors argument is not directly accessible
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
791 # from Python, so we can't test that much)
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
792 class D(dict):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
793 def __getitem__(self, key):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
794 raise ValueError
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
795 self.assertRaises(ValueError, "\xff".translate, D())
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
796 self.assertRaises(TypeError, "\xff".translate, {0xff: sys.maxunicode+1})
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
797 self.assertRaises(TypeError, "\xff".translate, {0xff: ()})
27177
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
798
30765
fe3f5592305d Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents: 30138
diff changeset
799 def test_bug828737(self):
fe3f5592305d Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents: 30138
diff changeset
800 charmap = {
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
801 ord("&"): "&amp;",
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
802 ord("<"): "&lt;",
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
803 ord(">"): "&gt;",
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
804 ord('"'): "&quot;",
30765
fe3f5592305d Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents: 30138
diff changeset
805 }
31389
aca1ce1b4b61 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 30765
diff changeset
806
30765
fe3f5592305d Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents: 30138
diff changeset
807 for n in (1, 10, 100, 1000):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
808 text = 'abc<def>ghi'*n
30765
fe3f5592305d Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents: 30138
diff changeset
809 text.translate(charmap)
fe3f5592305d Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents: 30138
diff changeset
810
42408
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
811 def test_mutatingdecodehandler(self):
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
812 baddata = [
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
813 ("ascii", b"\xff"),
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
814 ("utf-7", b"++"),
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
815 ("utf-8", b"\xff"),
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
816 ("utf-16", b"\xff"),
42626
f1a2d51f1cab Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents: 42408
diff changeset
817 ("utf-32", b"\xff"),
42408
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
818 ("unicode-escape", b"\\u123g"),
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
819 ("raw-unicode-escape", b"\\u123g"),
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
820 ("unicode-internal", b"\xff"),
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
821 ]
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
822
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
823 def replacing(exc):
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
824 if isinstance(exc, UnicodeDecodeError):
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
825 exc.object = 42
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
826 return ("\u4242", 0)
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
827 else:
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
828 raise TypeError("don't know how to handle %r" % exc)
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
829 codecs.register_error("test.replacing", replacing)
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
830 for (encoding, data) in baddata:
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
831 self.assertRaises(TypeError, data.decode, encoding, "test.replacing")
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
832
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
833 def mutating(exc):
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
834 if isinstance(exc, UnicodeDecodeError):
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
835 exc.object[:] = b""
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
836 return ("\u4242", 0)
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
837 else:
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
838 raise TypeError("don't know how to handle %r" % exc)
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
839 codecs.register_error("test.mutating", mutating)
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
840 # If the decoder doesn't pick up the modified input the following
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
841 # will lead to an endless loop
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
842 for (encoding, data) in baddata:
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
843 self.assertRaises(TypeError, data.decode, encoding, "test.replacing")
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
844
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
845 def test_main():
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28892
diff changeset
846 test.test_support.run_unittest(CodecCallbackTest)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
847
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
848 if __name__ == "__main__":
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
849 test_main()