annotate Lib/test/test_codeccallbacks.py @ 74135:9c19df6c8ea0 3.2

News entry for Issue12798
author Senthil Kumaran <senthil@uthcode.com>
date Fri, 23 Dec 2011 11:11:21 +0800
parents 9fe76c26ea9f
children 620d23f7ad41 8beaa9a37387
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 47147
diff changeset
1 import test.support, unittest
47147
7296a2fb4786 rename HTMLParser to html.parser and htmlentitydefs to html.entities;
Fred Drake <fdrake@acm.org>
parents: 43947
diff changeset
2 import sys, codecs, html.entities, unicodedata
25091
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):
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
36 UnicodeDecodeError.__init__(self, "ascii", bytearray(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):
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
42 UnicodeDecodeError.__init__(self, "ascii", bytearray(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:
47147
7296a2fb4786 rename HTMLParser to html.parser and htmlentitydefs to html.entities;
Fred Drake <fdrake@acm.org>
parents: 43947
diff changeset
89 l.append("&%s;" % html.entities.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
51776
d06a3ebf306f Merged revisions 69846 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50886
diff changeset
112 # unnecessary escape sequences.
25091
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
62665
e0d5a4a49112 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 51776
diff changeset
156 def test_decoding_callbacks(self):
e0d5a4a49112 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 51776
diff changeset
157 # This is a test for a decoding callback handler
e0d5a4a49112 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 51776
diff changeset
158 # that allows the decoding of the invalid sequence
e0d5a4a49112 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 51776
diff changeset
159 # "\xc0\x80" and returns "\x00" instead of raising an error.
e0d5a4a49112 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 51776
diff changeset
160 # All other illegal sequences will be handled strictly.
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
161 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
162 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
163 raise TypeError("don't know how to handle %r" % exc)
62665
e0d5a4a49112 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 51776
diff changeset
164 if exc.object[exc.start:exc.start+2] == 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
165 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
166 else:
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
167 raise exc
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
168
62665
e0d5a4a49112 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 51776
diff changeset
169 codecs.register_error("test.relaxedutf8", relaxedutf8)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
170
62665
e0d5a4a49112 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 51776
diff changeset
171 # all the "\xc0\x80" will be decoded to "\x00"
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
172 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
173 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
174 self.assertEqual(sin.decode("utf-8", "test.relaxedutf8"), sout)
62665
e0d5a4a49112 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 51776
diff changeset
175
e0d5a4a49112 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 51776
diff changeset
176 # "\xc0\x81" is not valid and a UnicodeDecodeError will be raised
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
177 sin = b"\xc0\x80\xc0\x81"
62665
e0d5a4a49112 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 51776
diff changeset
178 self.assertRaises(UnicodeDecodeError, sin.decode,
e0d5a4a49112 Update PyUnicode_DecodeUTF8 from RFC 2279 to RFC 3629.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 51776
diff changeset
179 "utf-8", "test.relaxedutf8")
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
180
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
181 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
182 # 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
183 # 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
184 # 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
185 # charmap has to have a mapping for "?".
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43673
diff changeset
186 charmap = dict((ord(c), bytes(2*c.upper(), 'ascii')) 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
187 sin = "abc"
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
188 sout = b"AABBCC"
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
189 self.assertEqual(codecs.charmap_encode(sin, "strict", charmap)[0], sout)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
190
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
191 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
192 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
193
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43673
diff changeset
194 charmap[ord("?")] = b"XYZ"
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
195 sin = "abcDEF"
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
196 sout = b"AABBCCXYZXYZXYZ"
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
197 self.assertEqual(codecs.charmap_encode(sin, "replace", charmap)[0], sout)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
198
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
199 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
200 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
201
35485
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
202 def test_decodeunicodeinternal(self):
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
203 self.assertRaises(
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
204 UnicodeDecodeError,
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
205 b"\x00\x00\x00\x00\x00".decode,
35485
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
206 "unicode-internal",
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
207 )
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
208 if sys.maxunicode > 0xffff:
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
209 def handler_unicodeinternal(exc):
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
210 if not isinstance(exc, UnicodeDecodeError):
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
211 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
212 return ("\x01", 1)
35485
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
213
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
214 self.assertEqual(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
215 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
216 "\u0000"
35485
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
217 )
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
218
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
219 self.assertEqual(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
220 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
221 "\u0000\ufffd"
35485
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
222 )
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 codecs.register_error("test.hui", handler_unicodeinternal)
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
225
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
226 self.assertEqual(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
227 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
228 "\u0000\u0001\u0000"
35485
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
229 )
6b8c55877755 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 34166
diff changeset
230
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
231 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
232 def handler1(exc):
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
233 r = range(exc.start, exc.end)
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
234 if isinstance(exc, UnicodeEncodeError):
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
235 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
236 elif isinstance(exc, UnicodeDecodeError):
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
237 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
238 else:
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
239 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
240 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
241
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
242 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
243
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
244 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
245 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
246 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
247 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
248 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
249
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
250 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
251
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
252 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
253
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
254 self.assertEqual(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
255 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
256 "\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
257 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
258 self.assertEqual(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
259 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
260 "\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
261 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
262
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
263 self.assertEqual(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
264 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
265 "\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
266 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
267
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
268 self.assertEqual(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
269 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
270 "\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
271 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
272
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
273 self.assertEqual(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
274 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
275 "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
276 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
277
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
278 self.assertEqual(
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
279 "g\xfc\xdfrk".encode("ascii", "test.handler1"),
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
280 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
281 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
282
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
283 self.assertEqual(
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
284 "g\xfc\xdf".encode("ascii", "test.handler1"),
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
285 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
286 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
287
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
288 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
289 # 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
290 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
291 "backslashreplace"]
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
292 # 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
293 # 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
294 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
295 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
296 l = 1000
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
297 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
298 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
299 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
300 "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
301 for err in errors:
26128
23ef7c163a12 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 25139
diff changeset
302 try:
23ef7c163a12 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 25139
diff changeset
303 uni.encode(enc, err)
23ef7c163a12 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 25139
diff changeset
304 except UnicodeError:
23ef7c163a12 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 25139
diff changeset
305 pass
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
306
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
307 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
308 # 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
309 # 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
310 self.assertRaises(TypeError, exctype, *args[:-1])
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
311 # 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
312 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
313 # check with one argument of the wrong type
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43673
diff changeset
314 wrongargs = [ "spam", b"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
315 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
316 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
317 if type(wrongarg) is type(args[i]):
26128
23ef7c163a12 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 25139
diff changeset
318 continue
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
319 # 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
320 callargs = []
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41585
diff changeset
321 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
322 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
323 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
324 else:
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
325 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
326 self.assertRaises(TypeError, exctype, *callargs)
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
327
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
328 # 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
329 exc = exctype(*args)
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
330 self.assertEqual(str(exc), msg)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
331
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
332 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
333 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
334 UnicodeEncodeError,
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
335 ["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
336 "'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
337 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
338 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
339 UnicodeEncodeError,
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
340 ["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
341 "'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
342 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
343 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
344 UnicodeEncodeError,
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
345 ["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
346 "'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
347 )
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
348 self.check_exceptionobjectargs(
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
349 UnicodeEncodeError,
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
350 ["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
351 "'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
352 )
30137
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
353 self.check_exceptionobjectargs(
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
354 UnicodeEncodeError,
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
355 ["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
356 "'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
357 )
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
358 if sys.maxunicode > 0xffff:
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
359 self.check_exceptionobjectargs(
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
360 UnicodeEncodeError,
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
361 ["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
362 "'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
363 )
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
364
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
365 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
366 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
367 UnicodeDecodeError,
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
368 ["ascii", bytearray(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
369 "'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
370 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
371 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
372 UnicodeDecodeError,
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
373 ["ascii", bytearray(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
374 "'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
375 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
376
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
377 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
378 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
379 UnicodeTranslateError,
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
380 ["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
381 "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
382 )
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
383 self.check_exceptionobjectargs(
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
384 UnicodeTranslateError,
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
385 ["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
386 "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
387 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
388 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
389 UnicodeTranslateError,
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
390 ["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
391 "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
392 )
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
393 if sys.maxunicode > 0xffff:
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
394 self.check_exceptionobjectargs(
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
395 UnicodeTranslateError,
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
396 ["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
397 "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
398 )
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
399 self.check_exceptionobjectargs(
df654861ccf6 Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents: 28917
diff changeset
400 UnicodeTranslateError,
41766
d6aa7edf217a Add 'U'/'U#' format characters to Py_BuildValue (and thus
Walter Dörwald <walter@livinglogic.de>
parents: 41659
diff changeset
401 ["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
402 "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
403 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
404
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
405 def test_badandgoodstrictexceptions(self):
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
406 # "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
407 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
408 TypeError,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
409 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
410 42
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
411 )
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
412 # "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
413 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
414 Exception,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
415 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
416 Exception("ouch")
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
417 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
418
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
419 # 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
420 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
421 UnicodeEncodeError,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
422 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
423 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
424 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
425
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
426 def test_badandgoodignoreexceptions(self):
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
427 # "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
428 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
429 TypeError,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
430 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
431 42
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
432 )
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
433 # "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
434 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
435 TypeError,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
436 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
437 UnicodeError("ouch")
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
438 )
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
439 # If the correct exception is passed in, "ignore" returns an empty replacement
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
440 self.assertEqual(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
441 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
442 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
443 ("", 1)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
444 )
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
445 self.assertEqual(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
446 codecs.ignore_errors(
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
447 UnicodeDecodeError("ascii", bytearray(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
448 ("", 1)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
449 )
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
450 self.assertEqual(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
451 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
452 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
453 ("", 1)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
454 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
455
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
456 def test_badandgoodreplaceexceptions(self):
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
457 # "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
458 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
459 TypeError,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
460 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
461 42
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
462 )
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
463 # "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
464 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
465 TypeError,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
466 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
467 UnicodeError("ouch")
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
468 )
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
469 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
470 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
471 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
472 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
473 )
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 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
475 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
476 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
477 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
478 )
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41585
diff changeset
479 # With the correct exception, "replace" returns an "?" or "\ufffd" replacement
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
480 self.assertEqual(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
481 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
482 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
483 ("?", 1)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
484 )
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
485 self.assertEqual(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
486 codecs.replace_errors(
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
487 UnicodeDecodeError("ascii", bytearray(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
488 ("\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
489 )
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
490 self.assertEqual(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
491 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
492 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
493 ("\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
494 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
495
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
496 def test_badandgoodxmlcharrefreplaceexceptions(self):
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
497 # "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
498 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
499 TypeError,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
500 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
501 42
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
502 )
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
503 # "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
504 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
505 TypeError,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
506 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
507 UnicodeError("ouch")
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
508 )
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
509 # "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
510 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
511 TypeError,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
512 codecs.xmlcharrefreplace_errors,
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
513 UnicodeDecodeError("ascii", bytearray(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
514 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
515 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
516 TypeError,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
517 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
518 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
519 )
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
520 # 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
521 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
522 s = "".join(chr(c) for c in cs)
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
523 self.assertEqual(
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 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
525 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
526 ),
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
527 ("".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
528 )
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
529
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
530 def test_badandgoodbackslashreplaceexceptions(self):
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
531 # "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
532 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
533 TypeError,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
534 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
535 42
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
536 )
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
537 # "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
538 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
539 TypeError,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
540 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
541 UnicodeError("ouch")
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
542 )
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
543 # "backslashreplace" can only be used for encoding
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
544 self.assertRaises(
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
545 TypeError,
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
546 codecs.backslashreplace_errors,
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
547 UnicodeDecodeError("ascii", bytearray(b"\xff"), 0, 1, "ouch")
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
548 )
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
549 self.assertRaises(
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
550 TypeError,
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
551 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
552 UnicodeTranslateError("\u3042", 0, 1, "ouch")
27310
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
553 )
a2f84d2835fa Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents: 27183
diff changeset
554 # Use the correct exception
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
555 self.assertEqual(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
556 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
557 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
558 ("\\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
559 )
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
560 self.assertEqual(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
561 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
562 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
563 ("\\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
564 )
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
565 self.assertEqual(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
566 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
567 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
568 ("\\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
569 )
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
570 self.assertEqual(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
571 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
572 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
573 ("\\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
574 )
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
575 self.assertEqual(
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
576 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
577 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
578 ("\\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
579 )
64590
fc76e8c617dc Issue #9804: ascii() now always represents unicode surrogate pairs as
Antoine Pitrou <solipsis@pitrou.net>
parents: 62665
diff changeset
580 # 1 on UCS-4 builds, 2 on UCS-2
fc76e8c617dc Issue #9804: ascii() now always represents unicode surrogate pairs as
Antoine Pitrou <solipsis@pitrou.net>
parents: 62665
diff changeset
581 len_wide = len("\U00010000")
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
582 self.assertEqual(
64590
fc76e8c617dc Issue #9804: ascii() now always represents unicode surrogate pairs as
Antoine Pitrou <solipsis@pitrou.net>
parents: 62665
diff changeset
583 codecs.backslashreplace_errors(
fc76e8c617dc Issue #9804: ascii() now always represents unicode surrogate pairs as
Antoine Pitrou <solipsis@pitrou.net>
parents: 62665
diff changeset
584 UnicodeEncodeError("ascii", "\U00010000",
fc76e8c617dc Issue #9804: ascii() now always represents unicode surrogate pairs as
Antoine Pitrou <solipsis@pitrou.net>
parents: 62665
diff changeset
585 0, len_wide, "ouch")),
fc76e8c617dc Issue #9804: ascii() now always represents unicode surrogate pairs as
Antoine Pitrou <solipsis@pitrou.net>
parents: 62665
diff changeset
586 ("\\U00010000", len_wide)
fc76e8c617dc Issue #9804: ascii() now always represents unicode surrogate pairs as
Antoine Pitrou <solipsis@pitrou.net>
parents: 62665
diff changeset
587 )
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
588 self.assertEqual(
64590
fc76e8c617dc Issue #9804: ascii() now always represents unicode surrogate pairs as
Antoine Pitrou <solipsis@pitrou.net>
parents: 62665
diff changeset
589 codecs.backslashreplace_errors(
fc76e8c617dc Issue #9804: ascii() now always represents unicode surrogate pairs as
Antoine Pitrou <solipsis@pitrou.net>
parents: 62665
diff changeset
590 UnicodeEncodeError("ascii", "\U0010ffff",
fc76e8c617dc Issue #9804: ascii() now always represents unicode surrogate pairs as
Antoine Pitrou <solipsis@pitrou.net>
parents: 62665
diff changeset
591 0, len_wide, "ouch")),
fc76e8c617dc Issue #9804: ascii() now always represents unicode surrogate pairs as
Antoine Pitrou <solipsis@pitrou.net>
parents: 62665
diff changeset
592 ("\\U0010ffff", len_wide)
fc76e8c617dc Issue #9804: ascii() now always represents unicode surrogate pairs as
Antoine Pitrou <solipsis@pitrou.net>
parents: 62665
diff changeset
593 )
fc76e8c617dc Issue #9804: ascii() now always represents unicode surrogate pairs as
Antoine Pitrou <solipsis@pitrou.net>
parents: 62665
diff changeset
594 # Lone surrogates (regardless of unicode width)
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
595 self.assertEqual(
64590
fc76e8c617dc Issue #9804: ascii() now always represents unicode surrogate pairs as
Antoine Pitrou <solipsis@pitrou.net>
parents: 62665
diff changeset
596 codecs.backslashreplace_errors(
fc76e8c617dc Issue #9804: ascii() now always represents unicode surrogate pairs as
Antoine Pitrou <solipsis@pitrou.net>
parents: 62665
diff changeset
597 UnicodeEncodeError("ascii", "\ud800", 0, 1, "ouch")),
fc76e8c617dc Issue #9804: ascii() now always represents unicode surrogate pairs as
Antoine Pitrou <solipsis@pitrou.net>
parents: 62665
diff changeset
598 ("\\ud800", 1)
fc76e8c617dc Issue #9804: ascii() now always represents unicode surrogate pairs as
Antoine Pitrou <solipsis@pitrou.net>
parents: 62665
diff changeset
599 )
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
600 self.assertEqual(
64590
fc76e8c617dc Issue #9804: ascii() now always represents unicode surrogate pairs as
Antoine Pitrou <solipsis@pitrou.net>
parents: 62665
diff changeset
601 codecs.backslashreplace_errors(
fc76e8c617dc Issue #9804: ascii() now always represents unicode surrogate pairs as
Antoine Pitrou <solipsis@pitrou.net>
parents: 62665
diff changeset
602 UnicodeEncodeError("ascii", "\udfff", 0, 1, "ouch")),
fc76e8c617dc Issue #9804: ascii() now always represents unicode surrogate pairs as
Antoine Pitrou <solipsis@pitrou.net>
parents: 62665
diff changeset
603 ("\\udfff", 1)
fc76e8c617dc Issue #9804: ascii() now always represents unicode surrogate pairs as
Antoine Pitrou <solipsis@pitrou.net>
parents: 62665
diff changeset
604 )
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
605
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
606 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
607 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
608 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
609
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
610 for res in results:
50886
581d8602f3f7 Merged revisions 68746 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
611 codecs.register_error("test.badhandler", lambda x: res)
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
612 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
613 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
614 TypeError,
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
615 "\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
616 enc,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
617 "test.badhandler"
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
618 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
619 for (enc, bytes) in (
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
620 ("ascii", b"\xff"),
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
621 ("utf-8", b"\xff"),
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
622 ("utf-7", b"+x-"),
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
623 ("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
624 ):
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
625 self.assertRaises(
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
626 TypeError,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
627 bytes.decode,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
628 enc,
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
629 "test.badhandler"
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
630 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
631
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
632 def test_lookup(self):
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
633 self.assertEqual(codecs.strict_errors, codecs.lookup_error("strict"))
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
634 self.assertEqual(codecs.ignore_errors, codecs.lookup_error("ignore"))
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
635 self.assertEqual(codecs.strict_errors, codecs.lookup_error("strict"))
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
636 self.assertEqual(
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
637 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
638 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
639 )
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
640 self.assertEqual(
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
641 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
642 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
643 )
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
644
25139
d446b5151316 Add a test case that checks that the proper exception is raises
Walter Dörwald <walter@livinglogic.de>
parents: 25091
diff changeset
645 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
646 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
647 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
648 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
649 else:
d446b5151316 Add a test case that checks that the proper exception is raises
Walter Dörwald <walter@livinglogic.de>
parents: 25091
diff changeset
650 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
651 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
652 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
653 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
654 UnicodeEncodeError,
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
655 "\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
656 enc,
d446b5151316 Add a test case that checks that the proper exception is raises
Walter Dörwald <walter@livinglogic.de>
parents: 25091
diff changeset
657 "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
658 )
d446b5151316 Add a test case that checks that the proper exception is raises
Walter Dörwald <walter@livinglogic.de>
parents: 25091
diff changeset
659
27177
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
660 def test_badregistercall(self):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
661 # enhance coverage of:
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
662 # 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
663 # 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
664 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
665 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
666
35882
3236b5fb1fb7 Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents: 35485
diff changeset
667 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
668 # 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
669 # 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
670 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
671
27177
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
672 def test_unknownhandler(self):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
673 # enhance coverage of:
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
674 # 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
675 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
676
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
677 def test_xmlcharrefvalues(self):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
678 # enhance coverage of:
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
679 # 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
680 # and inline implementations
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
681 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
682 if sys.maxunicode>=100000:
27963
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 27555
diff changeset
683 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
684 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
685 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
686 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
687 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
688 s.encode(enc, err)
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 test_decodehelper(self):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
691 # enhance coverage of:
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
692 # 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
693 # and callers
42911
cb28e14ee808 Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42626
diff changeset
694 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
695
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
696 def baddecodereturn1(exc):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
697 return 42
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
698 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
699 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
700 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
701 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
702 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
703 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
704 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
705
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
706 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
707 return ("?", None)
27177
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
708 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
709 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
710
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
711 handler = PosReturn()
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
712 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
713
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
714 # Valid negative position
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
715 handler.pos = -1
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
716 self.assertEqual(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
717
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
718 # Valid negative position
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
719 handler.pos = -2
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
720 self.assertEqual(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 # Negative position out of bounds
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
723 handler.pos = -3
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
724 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
725
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
726 # Valid positive position
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
727 handler.pos = 1
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
728 self.assertEqual(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
729
34166
e53693a02cf0 Fix copy & paste error in comments.
Walter Dörwald <walter@livinglogic.de>
parents: 31389
diff changeset
730 # 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
731 handler.pos = 2
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
732 self.assertEqual(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
733
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
734 # Invalid positive position
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
735 handler.pos = 3
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
736 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
737
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
738 # Restart at the "0"
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
739 handler.pos = 6
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
740 self.assertEqual(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
741
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
742 class D(dict):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
743 def __getitem__(self, key):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
744 raise ValueError
41659
650ff8d7cc38 test_codeccallbacks.py passes again.
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
745 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
746 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
747 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
748
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
749 def test_encodehelper(self):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
750 # enhance coverage of:
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
751 # 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
752 # and callers
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
753 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
754
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
755 def badencodereturn1(exc):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
756 return 42
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
757 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
758 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
759
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
760 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
761 return ("?", None)
27177
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
762 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
763 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
764
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
765 handler = PosReturn()
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
766 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
767
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
768 # Valid negative position
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
769 handler.pos = -1
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
770 self.assertEqual("\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
771
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
772 # Valid negative position
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
773 handler.pos = -2
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
774 self.assertEqual("\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
775
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
776 # Negative position out of bounds
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
777 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
778 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
779
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
780 # Valid positive position
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
781 handler.pos = 1
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
782 self.assertEqual("\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
783
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
784 # 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
785 handler.pos = 2
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64590
diff changeset
786 self.assertEqual("\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
787
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
788 # Invalid positive position
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
789 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
790 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
791
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
792 handler.pos = 0
27177
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
793
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
794 class D(dict):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
795 def __getitem__(self, key):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
796 raise ValueError
27555
c267f28724dd Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents: 27310
diff changeset
797 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
798 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
799 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
800 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
801
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
802 def test_translatehelper(self):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
803 # enhance coverage of:
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
804 # 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
805 # and callers
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
806 # (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
807 # 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
808 class D(dict):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
809 def __getitem__(self, key):
925a7b1f93ee Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents: 26405
diff changeset
810 raise ValueError
43673
d66018ed3ded Remove a test case which is no longer valid.
Georg Brandl <georg@python.org>
parents: 43664
diff changeset
811 #self.assertRaises(ValueError, "\xff".translate, D())
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
812 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
813 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
814
30765
fe3f5592305d Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents: 30138
diff changeset
815 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
816 charmap = {
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
817 ord("&"): "&amp;",
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
818 ord("<"): "&lt;",
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
819 ord(">"): "&gt;",
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
820 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
821 }
31389
aca1ce1b4b61 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 30765
diff changeset
822
30765
fe3f5592305d Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents: 30138
diff changeset
823 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
824 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
825 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
826
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
827 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
828 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
829 ("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
830 ("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
831 ("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
832 ("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
833 ("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
834 ("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
835 ("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
836 ("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
837 ]
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
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 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
840 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
841 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
842 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
843 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
844 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
845 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
846 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
847 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
848
504084c04ac0 Bytes (which are the input for decoding) are mutable now. If a decoding
Walter Dörwald <walter@livinglogic.de>
parents: 42118
diff changeset
849 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
850 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
851 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
852 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
853 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
854 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
855 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
856 # 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
857 # 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
858 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
859 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
860
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
861 def test_main():
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 47147
diff changeset
862 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
863
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
864 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
865 test_main()