Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_codeccallbacks.py @ 58765:2ba7677bfa95 legacy-trunk
testCopysign was defined twice in test_math; combine the definitions
| author | Mark Dickinson <dickinsm@gmail.com> |
|---|---|
| date | Sat, 06 Feb 2010 23:11:25 +0000 |
| parents | af9b6c43804e |
| children | 5032f1d5a1e1 |
| rev | line source |
|---|---|
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
1 import test.test_support, unittest |
|
47177
454d38c1f47e
revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
47143
diff
changeset
|
2 import sys, codecs, htmlentitydefs, 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) |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
19 return (u"<?>", oldpos) |
|
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): |
|
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
|
24 UnicodeEncodeError.__init__(self, "ascii", u"", 0, 1, "bad") |
|
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): |
|
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
|
30 UnicodeEncodeError.__init__(self, "ascii", u"", 0, 1, "bad") |
|
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): |
|
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
|
36 UnicodeDecodeError.__init__(self, "ascii", "", 0, 1, "bad") |
|
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): |
|
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
|
42 UnicodeDecodeError.__init__(self, "ascii", "", 0, 1, "bad") |
|
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): |
|
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
|
48 UnicodeTranslateError.__init__(self, u"", 0, 1, "bad") |
|
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): |
|
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
|
54 UnicodeTranslateError.__init__(self, u"", 0, 1, "bad") |
|
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): |
|
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
|
60 UnicodeTranslateError.__init__(self, u"", 0, 1, "bad") |
|
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. |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
69 s = u"\u30b9\u30d1\u30e2 \xe4nd eggs" |
|
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"), |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
72 "スパモ änd eggs" |
|
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"), |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
76 "スパモ \xe4nd eggs" |
|
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: |
|
47177
454d38c1f47e
revert creation of the html.entities and html.parser modules
Fred Drake <fdrake@acm.org>
parents:
47143
diff
changeset
|
89 l.append(u"&%s;" % htmlentitydefs.codepoint2name[ord(c)]) |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
90 except KeyError: |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
91 l.append(u"&#%d;" % ord(c)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
92 return (u"".join(l), exc.end) |
|
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 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
97 sin = u"\xab\u211c\xbb = \u2329\u1234\u20ac\u232a" |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
98 sout = "«ℜ» = ⟨ሴ€⟩" |
|
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) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
100 sout = "\xabℜ\xbb = ⟨ሴ€⟩" |
|
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) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
102 sout = "\xabℜ\xbb = ⟨ሴ\xa4⟩" |
|
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 |
|
51775
af9b6c43804e
Issue #5341: Fix a variety of spelling errors.
Mark Dickinson <dickinsm@gmail.com>
parents:
50882
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]: |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
119 l.append(unicodedata.name(c, u"0x%x" % ord(c))) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
120 return (u"\033[1m%s\033[0m" % u", ".join(l), exc.end) |
|
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 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
125 sin = u"\xac\u1234\u20ac\u8000" |
|
26378
ab3763d477cc
Update character names.
Martin v. Löwis <martin@v.loewis.de>
parents:
26128
diff
changeset
|
126 sout = "\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 |
|
26378
ab3763d477cc
Update character names.
Martin v. Löwis <martin@v.loewis.de>
parents:
26128
diff
changeset
|
129 sout = "\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 |
|
26378
ab3763d477cc
Update character names.
Martin v. Löwis <martin@v.loewis.de>
parents:
26128
diff
changeset
|
132 sout = "\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. |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
138 sin = u"a\xac\u1234\u20ac\u8000" |
|
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: |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
140 sin += unichr(sys.maxunicode) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
141 sout = "a\\xac\\u1234\\u20ac\\u8000" |
|
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: |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
143 sout += "\\U%08x" % sys.maxunicode |
|
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 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
146 sout = "a\xac\\u1234\\u20ac\\u8000" |
|
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: |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
148 sout += "\\U%08x" % sys.maxunicode |
|
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 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
151 sout = "a\xac\\u1234\xa4\\u8000" |
|
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: |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
153 sout += "\\U%08x" % sys.maxunicode |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
154 self.assertEqual(sin.encode("iso-8859-15", "backslashreplace"), sout) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
155 |
| 35485 | 156 def test_decoderelaxedutf8(self): |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
157 # This is the test for a decoding callback handler, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
158 # that relaxes the UTF-8 minimal encoding restriction. |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
159 # A null byte that is encoded as "\xc0\x80" will be |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
160 # decoded as a null byte. All other illegal sequences |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
161 # will be handled strictly. |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
162 def relaxedutf8(exc): |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
163 if not isinstance(exc, UnicodeDecodeError): |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
164 raise TypeError("don't know how to handle %r" % exc) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
165 if exc.object[exc.start:exc.end].startswith("\xc0\x80"): |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
166 return (u"\x00", exc.start+2) # retry after two bytes |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
167 else: |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
168 raise exc |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
169 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
170 codecs.register_error( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
171 "test.relaxedutf8", relaxedutf8) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
172 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
173 sin = "a\x00b\xc0\x80c\xc3\xbc\xc0\x80\xc0\x80" |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
174 sout = u"a\x00b\x00c\xfc\x00\x00" |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
175 self.assertEqual(sin.decode("utf-8", "test.relaxedutf8"), sout) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
176 sin = "\xc0\x80\xc0\x81" |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
177 self.assertRaises(UnicodeError, sin.decode, "utf-8", "test.relaxedutf8") |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
178 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
179 def test_charmapencode(self): |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
180 # For charmap encodings the replacement string will be |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
181 # mapped through the encoding again. This means, that |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
182 # to be able to use e.g. the "replace" handler, the |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
183 # charmap has to have a mapping for "?". |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
184 charmap = dict([ (ord(c), 2*c.upper()) for c in "abcdefgh"]) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
185 sin = u"abc" |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
186 sout = "AABBCC" |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
187 self.assertEquals(codecs.charmap_encode(sin, "strict", charmap)[0], sout) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
188 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
189 sin = u"abcA" |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
190 self.assertRaises(UnicodeError, codecs.charmap_encode, sin, "strict", charmap) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
191 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
192 charmap[ord("?")] = "XYZ" |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
193 sin = u"abcDEF" |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
194 sout = "AABBCCXYZXYZXYZ" |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
195 self.assertEquals(codecs.charmap_encode(sin, "replace", charmap)[0], sout) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
196 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
197 charmap[ord("?")] = u"XYZ" |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
198 self.assertRaises(TypeError, codecs.charmap_encode, sin, "replace", charmap) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
199 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
200 charmap[ord("?")] = u"XYZ" |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
201 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
|
202 |
| 35485 | 203 def test_decodeunicodeinternal(self): |
| 204 self.assertRaises( | |
| 205 UnicodeDecodeError, | |
| 206 "\x00\x00\x00\x00\x00".decode, | |
| 207 "unicode-internal", | |
| 208 ) | |
| 209 if sys.maxunicode > 0xffff: | |
| 210 def handler_unicodeinternal(exc): | |
| 211 if not isinstance(exc, UnicodeDecodeError): | |
| 212 raise TypeError("don't know how to handle %r" % exc) | |
| 213 return (u"\x01", 1) | |
| 214 | |
| 215 self.assertEqual( | |
| 216 "\x00\x00\x00\x00\x00".decode("unicode-internal", "ignore"), | |
| 217 u"\u0000" | |
| 218 ) | |
| 219 | |
| 220 self.assertEqual( | |
| 221 "\x00\x00\x00\x00\x00".decode("unicode-internal", "replace"), | |
| 222 u"\u0000\ufffd" | |
| 223 ) | |
| 224 | |
| 225 codecs.register_error("test.hui", handler_unicodeinternal) | |
| 226 | |
| 227 self.assertEqual( | |
| 228 "\x00\x00\x00\x00\x00".decode("unicode-internal", "test.hui"), | |
| 229 u"\u0000\u0001\u0000" | |
| 230 ) | |
| 231 | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
232 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
|
233 def handler1(exc): |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
234 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
|
235 and 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
|
236 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
|
237 l = [u"<%d>" % ord(exc.object[pos]) for pos in xrange(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
|
238 return (u"[%s]" % u"".join(l), exc.end) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
239 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
240 codecs.register_error("test.handler1", handler1) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
241 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
242 def handler2(exc): |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
243 if not isinstance(exc, UnicodeDecodeError): |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
244 raise TypeError("don't know how to handle %r" % exc) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
245 l = [u"<%d>" % ord(exc.object[pos]) for pos in xrange(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
|
246 return (u"[%s]" % u"".join(l), exc.end+1) # skip one character |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
247 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
248 codecs.register_error("test.handler2", handler2) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
249 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
250 s = "\x00\x81\x7f\x80\xff" |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
251 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
252 self.assertEqual( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
253 s.decode("ascii", "test.handler1"), |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
254 u"\x00[<129>]\x7f[<128>][<255>]" |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
255 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
256 self.assertEqual( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
257 s.decode("ascii", "test.handler2"), |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
258 u"\x00[<129>][<128>]" |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
259 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
260 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
261 self.assertEqual( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
262 "\\u3042\u3xxx".decode("unicode-escape", "test.handler1"), |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
263 u"\u3042[<92><117><51><120>]xx" |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
264 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
265 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
266 self.assertEqual( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
267 "\\u3042\u3xx".decode("unicode-escape", "test.handler1"), |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
268 u"\u3042[<92><117><51><120><120>]" |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
269 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
270 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
271 self.assertEqual( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
272 codecs.charmap_decode("abc", "test.handler1", {ord("a"): u"z"})[0], |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
273 u"z[<98>][<99>]" |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
274 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
275 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
276 self.assertEqual( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
277 u"g\xfc\xdfrk".encode("ascii", "test.handler1"), |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
278 u"g[<252><223>]rk" |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
279 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
280 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
281 self.assertEqual( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
282 u"g\xfc\xdf".encode("ascii", "test.handler1"), |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
283 u"g[<252><223>]" |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
284 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
285 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
286 def test_longstrings(self): |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
287 # test long strings to check for memory overflow problems |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
38431
diff
changeset
|
288 errors = [ "strict", "ignore", "replace", "xmlcharrefreplace", |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
38431
diff
changeset
|
289 "backslashreplace"] |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
290 # register the handlers under different names, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
291 # to prevent the codec from recognizing the name |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
292 for err in errors: |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
293 codecs.register_error("test." + err, codecs.lookup_error(err)) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
294 l = 1000 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
295 errors += [ "test." + err for err in errors ] |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
296 for uni in [ s*l for s in (u"x", u"\u3042", u"a\xe4") ]: |
|
42649
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
38431
diff
changeset
|
297 for enc in ("ascii", "latin-1", "iso-8859-1", "iso-8859-15", |
|
c916731321c0
Backport r57105 and r57145 from the py3k branch: UTF-32 codecs.
Walter Dörwald <walter@livinglogic.de>
parents:
38431
diff
changeset
|
298 "utf-8", "utf-7", "utf-16", "utf-32"): |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
299 for err in errors: |
|
26128
23ef7c163a12
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
25139
diff
changeset
|
300 try: |
|
23ef7c163a12
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
25139
diff
changeset
|
301 uni.encode(enc, err) |
|
23ef7c163a12
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
25139
diff
changeset
|
302 except UnicodeError: |
|
23ef7c163a12
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
25139
diff
changeset
|
303 pass |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
304 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
305 def check_exceptionobjectargs(self, exctype, args, msg): |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
306 # Test UnicodeError subclasses: construction, attribute assignment and __str__ conversion |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
307 # check with one missing argument |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
308 self.assertRaises(TypeError, exctype, *args[:-1]) |
|
27310
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
309 # check with one argument too much |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
310 self.assertRaises(TypeError, exctype, *(args + ["too much"])) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
311 # check with one argument of the wrong type |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
312 wrongargs = [ "spam", u"eggs", 42, 1.0, None ] |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
313 for i in xrange(len(args)): |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
314 for wrongarg in wrongargs: |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
315 if type(wrongarg) is type(args[i]): |
|
26128
23ef7c163a12
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
25139
diff
changeset
|
316 continue |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
317 # build argument array |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
318 callargs = [] |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
319 for j in xrange(len(args)): |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
320 if i==j: |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
321 callargs.append(wrongarg) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
322 else: |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
323 callargs.append(args[i]) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
324 self.assertRaises(TypeError, exctype, *callargs) |
|
27310
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
325 |
|
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
326 # check with the correct number and type of arguments |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
327 exc = exctype(*args) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
328 self.assertEquals(str(exc), msg) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
329 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
330 def test_unicodeencodeerror(self): |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
331 self.check_exceptionobjectargs( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
332 UnicodeEncodeError, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
333 ["ascii", u"g\xfcrk", 1, 2, "ouch"], |
|
30138
f746d5690ad6
Add a unicode prefix to the characters in the UnicodeEncodeError and
Walter Dörwald <walter@livinglogic.de>
parents:
30137
diff
changeset
|
334 "'ascii' codec can't encode character u'\\xfc' in position 1: ouch" |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
335 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
336 self.check_exceptionobjectargs( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
337 UnicodeEncodeError, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
338 ["ascii", u"g\xfcrk", 1, 4, "ouch"], |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
339 "'ascii' codec can't encode characters in position 1-3: ouch" |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
340 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
341 self.check_exceptionobjectargs( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
342 UnicodeEncodeError, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
343 ["ascii", u"\xfcx", 0, 1, "ouch"], |
|
30138
f746d5690ad6
Add a unicode prefix to the characters in the UnicodeEncodeError and
Walter Dörwald <walter@livinglogic.de>
parents:
30137
diff
changeset
|
344 "'ascii' codec can't encode character u'\\xfc' in position 0: ouch" |
|
30137
df654861ccf6
Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents:
28917
diff
changeset
|
345 ) |
|
df654861ccf6
Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents:
28917
diff
changeset
|
346 self.check_exceptionobjectargs( |
|
df654861ccf6
Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents:
28917
diff
changeset
|
347 UnicodeEncodeError, |
|
df654861ccf6
Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents:
28917
diff
changeset
|
348 ["ascii", u"\u0100x", 0, 1, "ouch"], |
|
30138
f746d5690ad6
Add a unicode prefix to the characters in the UnicodeEncodeError and
Walter Dörwald <walter@livinglogic.de>
parents:
30137
diff
changeset
|
349 "'ascii' codec can't encode character u'\\u0100' in position 0: ouch" |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
350 ) |
|
30137
df654861ccf6
Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents:
28917
diff
changeset
|
351 self.check_exceptionobjectargs( |
|
df654861ccf6
Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents:
28917
diff
changeset
|
352 UnicodeEncodeError, |
|
df654861ccf6
Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents:
28917
diff
changeset
|
353 ["ascii", u"\uffffx", 0, 1, "ouch"], |
|
30138
f746d5690ad6
Add a unicode prefix to the characters in the UnicodeEncodeError and
Walter Dörwald <walter@livinglogic.de>
parents:
30137
diff
changeset
|
354 "'ascii' codec can't encode character u'\\uffff' in position 0: ouch" |
|
30137
df654861ccf6
Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents:
28917
diff
changeset
|
355 ) |
|
df654861ccf6
Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents:
28917
diff
changeset
|
356 if sys.maxunicode > 0xffff: |
|
df654861ccf6
Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents:
28917
diff
changeset
|
357 self.check_exceptionobjectargs( |
|
df654861ccf6
Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents:
28917
diff
changeset
|
358 UnicodeEncodeError, |
|
df654861ccf6
Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents:
28917
diff
changeset
|
359 ["ascii", u"\U00010000x", 0, 1, "ouch"], |
|
30138
f746d5690ad6
Add a unicode prefix to the characters in the UnicodeEncodeError and
Walter Dörwald <walter@livinglogic.de>
parents:
30137
diff
changeset
|
360 "'ascii' codec can't encode character u'\\U00010000' in position 0: ouch" |
|
30137
df654861ccf6
Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents:
28917
diff
changeset
|
361 ) |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
362 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
363 def test_unicodedecodeerror(self): |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
364 self.check_exceptionobjectargs( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
365 UnicodeDecodeError, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
366 ["ascii", "g\xfcrk", 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
|
367 "'ascii' codec can't decode byte 0xfc in position 1: ouch" |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
368 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
369 self.check_exceptionobjectargs( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
370 UnicodeDecodeError, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
371 ["ascii", "g\xfcrk", 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
|
372 "'ascii' codec can't decode bytes in position 1-2: ouch" |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
373 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
374 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
375 def test_unicodetranslateerror(self): |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
376 self.check_exceptionobjectargs( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
377 UnicodeTranslateError, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
378 [u"g\xfcrk", 1, 2, "ouch"], |
|
30138
f746d5690ad6
Add a unicode prefix to the characters in the UnicodeEncodeError and
Walter Dörwald <walter@livinglogic.de>
parents:
30137
diff
changeset
|
379 "can't translate character u'\\xfc' in position 1: ouch" |
|
30137
df654861ccf6
Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents:
28917
diff
changeset
|
380 ) |
|
df654861ccf6
Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents:
28917
diff
changeset
|
381 self.check_exceptionobjectargs( |
|
df654861ccf6
Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents:
28917
diff
changeset
|
382 UnicodeTranslateError, |
|
df654861ccf6
Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents:
28917
diff
changeset
|
383 [u"g\u0100rk", 1, 2, "ouch"], |
|
30138
f746d5690ad6
Add a unicode prefix to the characters in the UnicodeEncodeError and
Walter Dörwald <walter@livinglogic.de>
parents:
30137
diff
changeset
|
384 "can't translate character u'\\u0100' in position 1: ouch" |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
385 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
386 self.check_exceptionobjectargs( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
387 UnicodeTranslateError, |
|
30137
df654861ccf6
Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents:
28917
diff
changeset
|
388 [u"g\uffffrk", 1, 2, "ouch"], |
|
30138
f746d5690ad6
Add a unicode prefix to the characters in the UnicodeEncodeError and
Walter Dörwald <walter@livinglogic.de>
parents:
30137
diff
changeset
|
389 "can't translate character u'\\uffff' in position 1: ouch" |
|
30137
df654861ccf6
Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents:
28917
diff
changeset
|
390 ) |
|
df654861ccf6
Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents:
28917
diff
changeset
|
391 if sys.maxunicode > 0xffff: |
|
df654861ccf6
Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents:
28917
diff
changeset
|
392 self.check_exceptionobjectargs( |
|
df654861ccf6
Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents:
28917
diff
changeset
|
393 UnicodeTranslateError, |
|
df654861ccf6
Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents:
28917
diff
changeset
|
394 [u"g\U00010000rk", 1, 2, "ouch"], |
|
30138
f746d5690ad6
Add a unicode prefix to the characters in the UnicodeEncodeError and
Walter Dörwald <walter@livinglogic.de>
parents:
30137
diff
changeset
|
395 "can't translate character u'\\U00010000' in position 1: ouch" |
|
30137
df654861ccf6
Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents:
28917
diff
changeset
|
396 ) |
|
df654861ccf6
Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents:
28917
diff
changeset
|
397 self.check_exceptionobjectargs( |
|
df654861ccf6
Enhance message for UnicodeEncodeError and UnicodeTranslateError.
Walter Dörwald <walter@livinglogic.de>
parents:
28917
diff
changeset
|
398 UnicodeTranslateError, |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
399 [u"g\xfcrk", 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
|
400 "can't translate characters in position 1-2: ouch" |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
401 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
402 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
403 def test_badandgoodstrictexceptions(self): |
|
27310
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
404 # "strict" complains about a non-exception passed in |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
405 self.assertRaises( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
406 TypeError, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
407 codecs.strict_errors, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
408 42 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
409 ) |
|
27310
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
410 # "strict" complains about the wrong exception type |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
411 self.assertRaises( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
412 Exception, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
413 codecs.strict_errors, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
414 Exception("ouch") |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
415 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
416 |
|
27310
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
417 # If the correct exception is passed in, "strict" raises it |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
418 self.assertRaises( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
419 UnicodeEncodeError, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
420 codecs.strict_errors, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
421 UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch") |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
422 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
423 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
424 def test_badandgoodignoreexceptions(self): |
|
27310
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
425 # "ignore" complains about a non-exception passed in |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
426 self.assertRaises( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
427 TypeError, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
428 codecs.ignore_errors, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
429 42 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
430 ) |
|
27310
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
431 # "ignore" complains about the wrong exception type |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
432 self.assertRaises( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
433 TypeError, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
434 codecs.ignore_errors, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
435 UnicodeError("ouch") |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
436 ) |
|
27310
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
437 # If the correct exception is passed in, "ignore" returns an empty replacement |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
438 self.assertEquals( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
439 codecs.ignore_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")), |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
440 (u"", 1) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
441 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
442 self.assertEquals( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
443 codecs.ignore_errors(UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch")), |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
444 (u"", 1) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
445 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
446 self.assertEquals( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
447 codecs.ignore_errors(UnicodeTranslateError(u"\u3042", 0, 1, "ouch")), |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
448 (u"", 1) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
449 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
450 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
451 def test_badandgoodreplaceexceptions(self): |
|
27310
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
452 # "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
|
453 self.assertRaises( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
454 TypeError, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
455 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
|
456 42 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
457 ) |
|
27310
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
458 # "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
|
459 self.assertRaises( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
460 TypeError, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
461 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
|
462 UnicodeError("ouch") |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
463 ) |
|
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
|
464 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
|
465 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
|
466 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
|
467 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
|
468 ) |
|
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 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
|
473 ) |
|
34166
e53693a02cf0
Fix copy & paste error in comments.
Walter Dörwald <walter@livinglogic.de>
parents:
31389
diff
changeset
|
474 # With the correct exception, "replace" returns an "?" or u"\ufffd" replacement |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
475 self.assertEquals( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
476 codecs.replace_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")), |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
477 (u"?", 1) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
478 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
479 self.assertEquals( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
480 codecs.replace_errors(UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch")), |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
481 (u"\ufffd", 1) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
482 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
483 self.assertEquals( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
484 codecs.replace_errors(UnicodeTranslateError(u"\u3042", 0, 1, "ouch")), |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
485 (u"\ufffd", 1) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
486 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
487 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
488 def test_badandgoodxmlcharrefreplaceexceptions(self): |
|
27310
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
489 # "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
|
490 self.assertRaises( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
491 TypeError, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
492 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
|
493 42 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
494 ) |
|
27310
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
495 # "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
|
496 self.assertRaises( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
497 TypeError, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
498 codecs.xmlcharrefreplace_errors, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
499 UnicodeError("ouch") |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
500 ) |
|
27310
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
501 # "xmlcharrefreplace" 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
|
502 self.assertRaises( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
503 TypeError, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
504 codecs.xmlcharrefreplace_errors, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
505 UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch") |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
506 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
507 self.assertRaises( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
508 TypeError, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
509 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
|
510 UnicodeTranslateError(u"\u3042", 0, 1, "ouch") |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
511 ) |
|
27310
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
512 # 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
|
513 cs = (0, 1, 9, 10, 99, 100, 999, 1000, 9999, 10000, 0x3042) |
|
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
|
514 s = "".join(unichr(c) for c in cs) |
|
27310
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
515 self.assertEquals( |
|
35889
e2473c4f2ee0
Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents:
35882
diff
changeset
|
516 codecs.xmlcharrefreplace_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
|
517 UnicodeEncodeError("ascii", s, 0, len(s), "ouch") |
|
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
|
518 ), |
|
e2473c4f2ee0
Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Walter Dörwald <walter@livinglogic.de>
parents:
35882
diff
changeset
|
519 (u"".join(u"&#%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
|
520 ) |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
521 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
522 def test_badandgoodbackslashreplaceexceptions(self): |
|
27310
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
523 # "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
|
524 self.assertRaises( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
525 TypeError, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
526 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
|
527 42 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
528 ) |
|
27310
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
529 # "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
|
530 self.assertRaises( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
531 TypeError, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
532 codecs.backslashreplace_errors, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
533 UnicodeError("ouch") |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
534 ) |
|
27310
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
535 # "backslashreplace" can only be used for encoding |
|
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
536 self.assertRaises( |
|
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
537 TypeError, |
|
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
538 codecs.backslashreplace_errors, |
|
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
539 UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch") |
|
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
540 ) |
|
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
541 self.assertRaises( |
|
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
542 TypeError, |
|
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
543 codecs.backslashreplace_errors, |
|
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
544 UnicodeTranslateError(u"\u3042", 0, 1, "ouch") |
|
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
545 ) |
|
a2f84d2835fa
Add comments and remove duplicate tests.
Walter Dörwald <walter@livinglogic.de>
parents:
27183
diff
changeset
|
546 # Use the correct exception |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
547 self.assertEquals( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
548 codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")), |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
549 (u"\\u3042", 1) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
550 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
551 self.assertEquals( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
552 codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\x00", 0, 1, "ouch")), |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
553 (u"\\x00", 1) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
554 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
555 self.assertEquals( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
556 codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\xff", 0, 1, "ouch")), |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
557 (u"\\xff", 1) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
558 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
559 self.assertEquals( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
560 codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\u0100", 0, 1, "ouch")), |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
561 (u"\\u0100", 1) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
562 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
563 self.assertEquals( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
564 codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\uffff", 0, 1, "ouch")), |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
565 (u"\\uffff", 1) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
566 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
567 if sys.maxunicode>0xffff: |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
568 self.assertEquals( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
569 codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\U00010000", 0, 1, "ouch")), |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
570 (u"\\U00010000", 1) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
571 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
572 self.assertEquals( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
573 codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\U0010ffff", 0, 1, "ouch")), |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
574 (u"\\U0010ffff", 1) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
575 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
576 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
577 def test_badhandlerresults(self): |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
578 results = ( 42, u"foo", (1,2,3), (u"foo", 1, 3), (u"foo", None), (u"foo",), ("foo", 1, 3), ("foo", None), ("foo",) ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
579 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
|
580 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
581 for res in results: |
|
50882
5b48d6000916
fix test that wasn't working as expected #4990
Benjamin Peterson <benjamin@python.org>
parents:
47177
diff
changeset
|
582 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
|
583 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
|
584 self.assertRaises( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
585 TypeError, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
586 u"\u3042".encode, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
587 enc, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
588 "test.badhandler" |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
589 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
590 for (enc, bytes) in ( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
591 ("ascii", "\xff"), |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
592 ("utf-8", "\xff"), |
| 35485 | 593 ("utf-7", "+x-"), |
| 594 ("unicode-internal", "\x00"), | |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
595 ): |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
596 self.assertRaises( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
597 TypeError, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
598 bytes.decode, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
599 enc, |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
600 "test.badhandler" |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
601 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
602 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
603 def test_lookup(self): |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
604 self.assertEquals(codecs.strict_errors, codecs.lookup_error("strict")) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
605 self.assertEquals(codecs.ignore_errors, codecs.lookup_error("ignore")) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
606 self.assertEquals(codecs.strict_errors, codecs.lookup_error("strict")) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
607 self.assertEquals( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
608 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
|
609 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
|
610 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
611 self.assertEquals( |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
612 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
|
613 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
|
614 ) |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
615 |
|
25139
d446b5151316
Add a test case that checks that the proper exception is raises
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
616 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
|
617 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
|
618 if isinstance(exc, UnicodeEncodeError): |
|
d446b5151316
Add a test case that checks that the proper exception is raises
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
619 return (u"\u4242", exc.end) |
|
d446b5151316
Add a test case that checks that the proper exception is raises
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
620 else: |
|
d446b5151316
Add a test case that checks that the proper exception is raises
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
621 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
|
622 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
|
623 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
|
624 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
|
625 UnicodeEncodeError, |
|
d446b5151316
Add a test case that checks that the proper exception is raises
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
626 u"\u4242".encode, |
|
d446b5151316
Add a test case that checks that the proper exception is raises
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
627 enc, |
|
d446b5151316
Add a test case that checks that the proper exception is raises
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
628 "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
|
629 ) |
|
d446b5151316
Add a test case that checks that the proper exception is raises
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
630 |
|
27177
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
631 def test_badregistercall(self): |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
632 # enhance coverage of: |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
633 # 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
|
634 # 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
|
635 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
|
636 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
|
637 |
|
35882
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35485
diff
changeset
|
638 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
|
639 # 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
|
640 # 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
|
641 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
|
642 |
|
27177
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
643 def test_unknownhandler(self): |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
644 # enhance coverage of: |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
645 # 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
|
646 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
|
647 |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
648 def test_xmlcharrefvalues(self): |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
649 # enhance coverage of: |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
650 # 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
|
651 # and inline implementations |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
652 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
|
653 if sys.maxunicode>=100000: |
|
27963
226463445697
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
27555
diff
changeset
|
654 v += (100000, 500000, 1000000) |
|
27177
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
655 s = u"".join([unichr(x) for x in v]) |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
656 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
|
657 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
|
658 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
|
659 s.encode(enc, err) |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
660 |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
661 def test_decodehelper(self): |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
662 # enhance coverage of: |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
663 # 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
|
664 # and callers |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
665 self.assertRaises(LookupError, "\xff".decode, "ascii", "test.unknown") |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
666 |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
667 def baddecodereturn1(exc): |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
668 return 42 |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
669 codecs.register_error("test.baddecodereturn1", baddecodereturn1) |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
670 self.assertRaises(TypeError, "\xff".decode, "ascii", "test.baddecodereturn1") |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
671 self.assertRaises(TypeError, "\\".decode, "unicode-escape", "test.baddecodereturn1") |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
672 self.assertRaises(TypeError, "\\x0".decode, "unicode-escape", "test.baddecodereturn1") |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
673 self.assertRaises(TypeError, "\\x0y".decode, "unicode-escape", "test.baddecodereturn1") |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
674 self.assertRaises(TypeError, "\\Uffffeeee".decode, "unicode-escape", "test.baddecodereturn1") |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
675 self.assertRaises(TypeError, "\\uyyyy".decode, "raw-unicode-escape", "test.baddecodereturn1") |
|
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 baddecodereturn2(exc): |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
678 return (u"?", None) |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
679 codecs.register_error("test.baddecodereturn2", baddecodereturn2) |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
680 self.assertRaises(TypeError, "\xff".decode, "ascii", "test.baddecodereturn2") |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
681 |
|
27555
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
682 handler = PosReturn() |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
683 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
|
684 |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
685 # Valid negative position |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
686 handler.pos = -1 |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
687 self.assertEquals("\xff0".decode("ascii", "test.posreturn"), u"<?>0") |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
688 |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
689 # Valid negative position |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
690 handler.pos = -2 |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
691 self.assertEquals("\xff0".decode("ascii", "test.posreturn"), u"<?><?>") |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
692 |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
693 # Negative position out of bounds |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
694 handler.pos = -3 |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
695 self.assertRaises(IndexError, "\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
|
696 |
|
27555
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
697 # Valid positive position |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
698 handler.pos = 1 |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
699 self.assertEquals("\xff0".decode("ascii", "test.posreturn"), u"<?>0") |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
700 |
|
34166
e53693a02cf0
Fix copy & paste error in comments.
Walter Dörwald <walter@livinglogic.de>
parents:
31389
diff
changeset
|
701 # 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
|
702 handler.pos = 2 |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
703 self.assertEquals("\xff0".decode("ascii", "test.posreturn"), u"<?>") |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
704 |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
705 # Invalid positive position |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
706 handler.pos = 3 |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
707 self.assertRaises(IndexError, "\xff0".decode, "ascii", "test.posreturn") |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
708 |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
709 # Restart at the "0" |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
710 handler.pos = 6 |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
711 self.assertEquals("\\uyyyy0".decode("raw-unicode-escape", "test.posreturn"), u"<?>0") |
|
27177
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
712 |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
713 class D(dict): |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
714 def __getitem__(self, key): |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
715 raise ValueError |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
716 self.assertRaises(UnicodeError, codecs.charmap_decode, "\xff", "strict", {0xff: None}) |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
717 self.assertRaises(ValueError, codecs.charmap_decode, "\xff", "strict", D()) |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
718 self.assertRaises(TypeError, codecs.charmap_decode, "\xff", "strict", {0xff: sys.maxunicode+1}) |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
719 |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
720 def test_encodehelper(self): |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
721 # enhance coverage of: |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
722 # 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
|
723 # and callers |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
724 self.assertRaises(LookupError, u"\xff".encode, "ascii", "test.unknown") |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
725 |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
726 def badencodereturn1(exc): |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
727 return 42 |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
728 codecs.register_error("test.badencodereturn1", badencodereturn1) |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
729 self.assertRaises(TypeError, u"\xff".encode, "ascii", "test.badencodereturn1") |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
730 |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
731 def badencodereturn2(exc): |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
732 return (u"?", None) |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
733 codecs.register_error("test.badencodereturn2", badencodereturn2) |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
734 self.assertRaises(TypeError, u"\xff".encode, "ascii", "test.badencodereturn2") |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
735 |
|
27555
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
736 handler = PosReturn() |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
737 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
|
738 |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
739 # Valid negative position |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
740 handler.pos = -1 |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
741 self.assertEquals(u"\xff0".encode("ascii", "test.posreturn"), "<?>0") |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
742 |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
743 # Valid negative position |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
744 handler.pos = -2 |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
745 self.assertEquals(u"\xff0".encode("ascii", "test.posreturn"), "<?><?>") |
|
27177
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
746 |
|
27555
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
747 # Negative position out of bounds |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
748 handler.pos = -3 |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
749 self.assertRaises(IndexError, u"\xff0".encode, "ascii", "test.posreturn") |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
750 |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
751 # Valid positive position |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
752 handler.pos = 1 |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
753 self.assertEquals(u"\xff0".encode("ascii", "test.posreturn"), "<?>0") |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
754 |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
755 # 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
|
756 handler.pos = 2 |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
757 self.assertEquals(u"\xff0".encode("ascii", "test.posreturn"), "<?>") |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
758 |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
759 # Invalid positive position |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
760 handler.pos = 3 |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
761 self.assertRaises(IndexError, u"\xff0".encode, "ascii", "test.posreturn") |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
762 |
|
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
763 handler.pos = 0 |
|
27177
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
764 |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
765 class D(dict): |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
766 def __getitem__(self, key): |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
767 raise ValueError |
|
27555
c267f28724dd
Change the treatment of positions returned by PEP293
Walter Dörwald <walter@livinglogic.de>
parents:
27310
diff
changeset
|
768 for err in ("strict", "replace", "xmlcharrefreplace", "backslashreplace", "test.posreturn"): |
|
27177
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
769 self.assertRaises(UnicodeError, codecs.charmap_encode, u"\xff", err, {0xff: None}) |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
770 self.assertRaises(ValueError, codecs.charmap_encode, u"\xff", err, D()) |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
771 self.assertRaises(TypeError, codecs.charmap_encode, u"\xff", err, {0xff: 300}) |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
772 |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
773 def test_translatehelper(self): |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
774 # enhance coverage of: |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
775 # 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
|
776 # and callers |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
777 # (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
|
778 # 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
|
779 class D(dict): |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
780 def __getitem__(self, key): |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
781 raise ValueError |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
782 self.assertRaises(ValueError, u"\xff".translate, D()) |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
783 self.assertRaises(TypeError, u"\xff".translate, {0xff: sys.maxunicode+1}) |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
784 self.assertRaises(TypeError, u"\xff".translate, {0xff: ()}) |
|
925a7b1f93ee
Add a few test cases to increase code coverage:
Walter Dörwald <walter@livinglogic.de>
parents:
26405
diff
changeset
|
785 |
|
30765
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30138
diff
changeset
|
786 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
|
787 charmap = { |
|
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30138
diff
changeset
|
788 ord("&"): u"&", |
|
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30138
diff
changeset
|
789 ord("<"): u"<", |
|
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30138
diff
changeset
|
790 ord(">"): u">", |
|
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30138
diff
changeset
|
791 ord('"'): u""", |
|
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30138
diff
changeset
|
792 } |
|
31389
aca1ce1b4b61
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
30765
diff
changeset
|
793 |
|
30765
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30138
diff
changeset
|
794 for n in (1, 10, 100, 1000): |
|
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30138
diff
changeset
|
795 text = u'abc<def>ghi'*n |
|
fe3f5592305d
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
Walter Dörwald <walter@livinglogic.de>
parents:
30138
diff
changeset
|
796 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
|
797 |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
798 def test_main(): |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28892
diff
changeset
|
799 test.test_support.run_unittest(CodecCallbackTest) |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
800 |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
801 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
|
802 test_main() |
