Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_codecs.py @ 78069:a8aa77458e12 3.2
Add c_size_t and c_ssize_t to the ctypes summary table.
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Thu, 12 Jul 2012 20:31:50 +0200 |
| parents | 7063aa2fa127 |
| children | 034ff986019d 1ef213b01dbb |
| rev | line source |
|---|---|
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
43933
diff
changeset
|
1 from test import support |
|
24364
aa0d2d51fbde
Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
2 import unittest |
|
18163
f926eb1f5770
Test by Martin v. Loewis for the new UTF-16 codec handling of BOM
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
3 import codecs |
|
71489
92d02de91cc9
Issue #1813: Fix codec lookup under Turkish locales.
Antoine Pitrou <solipsis@pitrou.net>
parents:
70293
diff
changeset
|
4 import locale |
|
41681
1d334912a104
Use io.BytesIO instead of StringIO.StringIO in all
Walter Dörwald <walter@livinglogic.de>
parents:
41679
diff
changeset
|
5 import sys, _testcapi, io |
|
18163
f926eb1f5770
Test by Martin v. Loewis for the new UTF-16 codec handling of BOM
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
6 |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
7 class Queue(object): |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
8 """ |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
9 queue: write bytes at one end, read bytes from the other end |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
10 """ |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
11 def __init__(self, buffer): |
|
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
12 self._buffer = buffer |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
13 |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
14 def write(self, chars): |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
15 self._buffer += chars |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
16 |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
17 def read(self, size=-1): |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
18 if size<0: |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
19 s = self._buffer |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
20 self._buffer = self._buffer[:0] # make empty |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
21 return s |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
22 else: |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
23 s = self._buffer[:size] |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
24 self._buffer = self._buffer[size:] |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
25 return s |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
26 |
|
41473
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
27 class MixInCheckStateHandling: |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
28 def check_state_handling_decode(self, encoding, u, s): |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41606
diff
changeset
|
29 for i in range(len(s)+1): |
|
41473
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
30 d = codecs.getincrementaldecoder(encoding)() |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
31 part1 = d.decode(s[:i]) |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
32 state = d.getstate() |
|
58473
71acd4482e65
Merged revisions 77727 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56036
diff
changeset
|
33 self.assertIsInstance(state[1], int) |
|
41473
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
34 # Check that the condition stated in the documentation for |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
35 # IncrementalDecoder.getstate() holds |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
36 if not state[1]: |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
37 # reset decoder to the default state without anything buffered |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
38 d.setstate((state[0][:0], 0)) |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
39 # Feeding the previous input may not produce any output |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54988
diff
changeset
|
40 self.assertTrue(not d.decode(state[0])) |
|
41473
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
41 # The decoder must return to the same state |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
42 self.assertEqual(state, d.getstate()) |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
43 # Create a new decoder and set it to the state |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
44 # we extracted from the old one |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
45 d = codecs.getincrementaldecoder(encoding)() |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
46 d.setstate(state) |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
47 part2 = d.decode(s[i:], True) |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
48 self.assertEqual(u, part1+part2) |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
49 |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
50 def check_state_handling_encode(self, encoding, u, s): |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41606
diff
changeset
|
51 for i in range(len(u)+1): |
|
41473
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
52 d = codecs.getincrementalencoder(encoding)() |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
53 part1 = d.encode(u[:i]) |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
54 state = d.getstate() |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
55 d = codecs.getincrementalencoder(encoding)() |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
56 d.setstate(state) |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
57 part2 = d.encode(u[i:], True) |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
58 self.assertEqual(s, part1+part2) |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
59 |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
60 class ReadTest(unittest.TestCase, MixInCheckStateHandling): |
| 34214 | 61 def check_partial(self, input, partialresults): |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
62 # get a StreamReader for the encoding and feed the bytestring version |
|
43912
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
63 # of input to the reader byte by byte. Read everything available from |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
64 # the StreamReader and check that the results equal the appropriate |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
65 # entries from partialresults. |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
66 q = Queue(b"") |
| 34214 | 67 r = codecs.getreader(self.encoding)(q) |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
68 result = "" |
| 34214 | 69 for (c, partialresult) in zip(input.encode(self.encoding), partialresults): |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
70 q.write(bytes([c])) |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
71 result += r.read() |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
72 self.assertEqual(result, partialresult) |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
73 # check that there's nothing left in the buffers |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
74 self.assertEqual(r.read(), "") |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
75 self.assertEqual(r.bytebuffer, b"") |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
76 |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
77 # do the check again, this time using a incremental decoder |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
78 d = codecs.getincrementaldecoder(self.encoding)() |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
79 result = "" |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
80 for (c, partialresult) in zip(input.encode(self.encoding), partialresults): |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
81 result += d.decode(bytes([c])) |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
82 self.assertEqual(result, partialresult) |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
83 # check that there's nothing left in the buffers |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
84 self.assertEqual(d.decode(b"", True), "") |
|
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
85 self.assertEqual(d.buffer, b"") |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
86 |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
87 # Check whether the reset method works properly |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
88 d.reset() |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
89 result = "" |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
90 for (c, partialresult) in zip(input.encode(self.encoding), partialresults): |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
91 result += d.decode(bytes([c])) |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
92 self.assertEqual(result, partialresult) |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
93 # check that there's nothing left in the buffers |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
94 self.assertEqual(d.decode(b"", True), "") |
|
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
95 self.assertEqual(d.buffer, b"") |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
96 |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
97 # check iterdecode() |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
98 encoded = input.encode(self.encoding) |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
99 self.assertEqual( |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
100 input, |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
101 "".join(codecs.iterdecode([bytes([c]) for c in encoded], self.encoding)) |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
102 ) |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
103 |
| 34214 | 104 def test_readline(self): |
| 105 def getreader(input): | |
|
41681
1d334912a104
Use io.BytesIO instead of StringIO.StringIO in all
Walter Dörwald <walter@livinglogic.de>
parents:
41679
diff
changeset
|
106 stream = io.BytesIO(input.encode(self.encoding)) |
| 34214 | 107 return codecs.getreader(self.encoding)(stream) |
| 108 | |
|
36855
204c307b01ce
If size is specified, try to read at least size characters.
Walter Dörwald <walter@livinglogic.de>
parents:
36247
diff
changeset
|
109 def readalllines(input, keepends=True, size=None): |
| 34214 | 110 reader = getreader(input) |
| 111 lines = [] | |
| 112 while True: | |
|
36855
204c307b01ce
If size is specified, try to read at least size characters.
Walter Dörwald <walter@livinglogic.de>
parents:
36247
diff
changeset
|
113 line = reader.readline(size=size, keepends=keepends) |
| 34214 | 114 if not line: |
| 115 break | |
| 116 lines.append(line) | |
|
36855
204c307b01ce
If size is specified, try to read at least size characters.
Walter Dörwald <walter@livinglogic.de>
parents:
36247
diff
changeset
|
117 return "|".join(lines) |
| 34214 | 118 |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
119 s = "foo\nbar\r\nbaz\rspam\u2028eggs" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
120 sexpected = "foo\n|bar\r\n|baz\r|spam\u2028|eggs" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
121 sexpectednoends = "foo|bar|baz|spam|eggs" |
|
36855
204c307b01ce
If size is specified, try to read at least size characters.
Walter Dörwald <walter@livinglogic.de>
parents:
36247
diff
changeset
|
122 self.assertEqual(readalllines(s, True), sexpected) |
|
204c307b01ce
If size is specified, try to read at least size characters.
Walter Dörwald <walter@livinglogic.de>
parents:
36247
diff
changeset
|
123 self.assertEqual(readalllines(s, False), sexpectednoends) |
|
204c307b01ce
If size is specified, try to read at least size characters.
Walter Dörwald <walter@livinglogic.de>
parents:
36247
diff
changeset
|
124 self.assertEqual(readalllines(s, True, 10), sexpected) |
|
204c307b01ce
If size is specified, try to read at least size characters.
Walter Dörwald <walter@livinglogic.de>
parents:
36247
diff
changeset
|
125 self.assertEqual(readalllines(s, False, 10), sexpectednoends) |
| 34214 | 126 |
| 127 # Test long lines (multiple calls to read() in readline()) | |
| 128 vw = [] | |
| 129 vwo = [] | |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
130 for (i, lineend) in enumerate("\n \r\n \r \u2028".split()): |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
131 vw.append((i*200)*"\3042" + lineend) |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
132 vwo.append((i*200)*"\3042") |
| 34214 | 133 self.assertEqual(readalllines("".join(vw), True), "".join(vw)) |
| 134 self.assertEqual(readalllines("".join(vw), False),"".join(vwo)) | |
| 135 | |
| 136 # Test lines where the first read might end with \r, so the | |
| 137 # reader has to look ahead whether this is a lone \r or a \r\n | |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41606
diff
changeset
|
138 for size in range(80): |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
139 for lineend in "\n \r\n \r \u2028".split(): |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
140 s = 10*(size*"a" + lineend + "xxx\n") |
|
34865
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
141 reader = getreader(s) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41606
diff
changeset
|
142 for i in range(10): |
|
34865
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
143 self.assertEqual( |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
144 reader.readline(keepends=True), |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
145 size*"a" + lineend, |
|
34865
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
146 ) |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
147 reader = getreader(s) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41606
diff
changeset
|
148 for i in range(10): |
|
34865
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
149 self.assertEqual( |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
150 reader.readline(keepends=False), |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
151 size*"a", |
|
34865
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
152 ) |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
153 |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
154 def test_bug1175396(self): |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
155 s = [ |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
156 '<%!--===================================================\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
157 ' BLOG index page: show recent articles,\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
158 ' today\'s articles, or articles of a specific date.\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
159 '========================================================--%>\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
160 '<%@inputencoding="ISO-8859-1"%>\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
161 '<%@pagetemplate=TEMPLATE.y%>\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
162 '<%@import=import frog.util, frog%>\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
163 '<%@import=import frog.objects%>\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
164 '<%@import=from frog.storageerrors import StorageError%>\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
165 '<%\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
166 '\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
167 'import logging\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
168 'log=logging.getLogger("Snakelets.logger")\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
169 '\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
170 '\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
171 'user=self.SessionCtx.user\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
172 'storageEngine=self.SessionCtx.storageEngine\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
173 '\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
174 '\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
175 'def readArticlesFromDate(date, count=None):\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
176 ' entryids=storageEngine.listBlogEntries(date)\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
177 ' entryids.reverse() # descending\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
178 ' if count:\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
179 ' entryids=entryids[:count]\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
180 ' try:\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
181 ' return [ frog.objects.BlogEntry.load(storageEngine, date, Id) for Id in entryids ]\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
182 ' except StorageError,x:\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
183 ' log.error("Error loading articles: "+str(x))\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
184 ' self.abort("cannot load articles")\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
185 '\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
186 'showdate=None\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
187 '\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
188 'arg=self.Request.getArg()\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
189 'if arg=="today":\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
190 ' #-------------------- TODAY\'S ARTICLES\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
191 ' self.write("<h2>Today\'s articles</h2>")\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
192 ' showdate = frog.util.isodatestr() \r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
193 ' entries = readArticlesFromDate(showdate)\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
194 'elif arg=="active":\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
195 ' #-------------------- ACTIVE ARTICLES redirect\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
196 ' self.Yredirect("active.y")\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
197 'elif arg=="login":\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
198 ' #-------------------- LOGIN PAGE redirect\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
199 ' self.Yredirect("login.y")\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
200 'elif arg=="date":\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
201 ' #-------------------- ARTICLES OF A SPECIFIC DATE\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
202 ' showdate = self.Request.getParameter("date")\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
203 ' self.write("<h2>Articles written on %s</h2>"% frog.util.mediumdatestr(showdate))\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
204 ' entries = readArticlesFromDate(showdate)\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
205 'else:\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
206 ' #-------------------- RECENT ARTICLES\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
207 ' self.write("<h2>Recent articles</h2>")\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
208 ' dates=storageEngine.listBlogEntryDates()\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
209 ' if dates:\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
210 ' entries=[]\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
211 ' SHOWAMOUNT=10\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
212 ' for showdate in dates:\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
213 ' entries.extend( readArticlesFromDate(showdate, SHOWAMOUNT-len(entries)) )\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
214 ' if len(entries)>=SHOWAMOUNT:\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
215 ' break\r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
216 ' \r\n', |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
217 ] |
|
41681
1d334912a104
Use io.BytesIO instead of StringIO.StringIO in all
Walter Dörwald <walter@livinglogic.de>
parents:
41679
diff
changeset
|
218 stream = io.BytesIO("".join(s).encode(self.encoding)) |
|
34865
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
219 reader = codecs.getreader(self.encoding)(stream) |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
220 for (i, line) in enumerate(reader): |
|
b49d39111c48
Fix for SF bug #1175396: readline() will now read one more character, if
Walter Dörwald <walter@livinglogic.de>
parents:
34750
diff
changeset
|
221 self.assertEqual(line, s[i]) |
| 34214 | 222 |
| 223 def test_readlinequeue(self): | |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
224 q = Queue(b"") |
| 34214 | 225 writer = codecs.getwriter(self.encoding)(q) |
| 226 reader = codecs.getreader(self.encoding)(q) | |
| 227 | |
| 228 # No lineends | |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
229 writer.write("foo\r") |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
230 self.assertEqual(reader.readline(keepends=False), "foo") |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
231 writer.write("\nbar\r") |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
232 self.assertEqual(reader.readline(keepends=False), "") |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
233 self.assertEqual(reader.readline(keepends=False), "bar") |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
234 writer.write("baz") |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
235 self.assertEqual(reader.readline(keepends=False), "baz") |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
236 self.assertEqual(reader.readline(keepends=False), "") |
| 34214 | 237 |
| 238 # Lineends | |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
239 writer.write("foo\r") |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
240 self.assertEqual(reader.readline(keepends=True), "foo\r") |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
241 writer.write("\nbar\r") |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
242 self.assertEqual(reader.readline(keepends=True), "\n") |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
243 self.assertEqual(reader.readline(keepends=True), "bar\r") |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
244 writer.write("baz") |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
245 self.assertEqual(reader.readline(keepends=True), "baz") |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
246 self.assertEqual(reader.readline(keepends=True), "") |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
247 writer.write("foo\r\n") |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
248 self.assertEqual(reader.readline(keepends=True), "foo\r\n") |
| 34214 | 249 |
|
34384
a216003cef04
Fix and test for SF bug #1098990: codec readline() splits lines apart.
Walter Dörwald <walter@livinglogic.de>
parents:
34279
diff
changeset
|
250 def test_bug1098990_a(self): |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
251 s1 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\r\n" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
252 s2 = "offending line: ladfj askldfj klasdj fskla dfzaskdj fasklfj laskd fjasklfzzzzaa%whereisthis!!!\r\n" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
253 s3 = "next line.\r\n" |
|
34384
a216003cef04
Fix and test for SF bug #1098990: codec readline() splits lines apart.
Walter Dörwald <walter@livinglogic.de>
parents:
34279
diff
changeset
|
254 |
|
a216003cef04
Fix and test for SF bug #1098990: codec readline() splits lines apart.
Walter Dörwald <walter@livinglogic.de>
parents:
34279
diff
changeset
|
255 s = (s1+s2+s3).encode(self.encoding) |
|
41681
1d334912a104
Use io.BytesIO instead of StringIO.StringIO in all
Walter Dörwald <walter@livinglogic.de>
parents:
41679
diff
changeset
|
256 stream = io.BytesIO(s) |
|
34384
a216003cef04
Fix and test for SF bug #1098990: codec readline() splits lines apart.
Walter Dörwald <walter@livinglogic.de>
parents:
34279
diff
changeset
|
257 reader = codecs.getreader(self.encoding)(stream) |
|
a216003cef04
Fix and test for SF bug #1098990: codec readline() splits lines apart.
Walter Dörwald <walter@livinglogic.de>
parents:
34279
diff
changeset
|
258 self.assertEqual(reader.readline(), s1) |
|
a216003cef04
Fix and test for SF bug #1098990: codec readline() splits lines apart.
Walter Dörwald <walter@livinglogic.de>
parents:
34279
diff
changeset
|
259 self.assertEqual(reader.readline(), s2) |
|
a216003cef04
Fix and test for SF bug #1098990: codec readline() splits lines apart.
Walter Dörwald <walter@livinglogic.de>
parents:
34279
diff
changeset
|
260 self.assertEqual(reader.readline(), s3) |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
261 self.assertEqual(reader.readline(), "") |
|
34384
a216003cef04
Fix and test for SF bug #1098990: codec readline() splits lines apart.
Walter Dörwald <walter@livinglogic.de>
parents:
34279
diff
changeset
|
262 |
|
a216003cef04
Fix and test for SF bug #1098990: codec readline() splits lines apart.
Walter Dörwald <walter@livinglogic.de>
parents:
34279
diff
changeset
|
263 def test_bug1098990_b(self): |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
264 s1 = "aaaaaaaaaaaaaaaaaaaaaaaa\r\n" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
265 s2 = "bbbbbbbbbbbbbbbbbbbbbbbb\r\n" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
266 s3 = "stillokay:bbbbxx\r\n" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
267 s4 = "broken!!!!badbad\r\n" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
268 s5 = "againokay.\r\n" |
|
34384
a216003cef04
Fix and test for SF bug #1098990: codec readline() splits lines apart.
Walter Dörwald <walter@livinglogic.de>
parents:
34279
diff
changeset
|
269 |
|
a216003cef04
Fix and test for SF bug #1098990: codec readline() splits lines apart.
Walter Dörwald <walter@livinglogic.de>
parents:
34279
diff
changeset
|
270 s = (s1+s2+s3+s4+s5).encode(self.encoding) |
|
41681
1d334912a104
Use io.BytesIO instead of StringIO.StringIO in all
Walter Dörwald <walter@livinglogic.de>
parents:
41679
diff
changeset
|
271 stream = io.BytesIO(s) |
|
34384
a216003cef04
Fix and test for SF bug #1098990: codec readline() splits lines apart.
Walter Dörwald <walter@livinglogic.de>
parents:
34279
diff
changeset
|
272 reader = codecs.getreader(self.encoding)(stream) |
|
a216003cef04
Fix and test for SF bug #1098990: codec readline() splits lines apart.
Walter Dörwald <walter@livinglogic.de>
parents:
34279
diff
changeset
|
273 self.assertEqual(reader.readline(), s1) |
|
a216003cef04
Fix and test for SF bug #1098990: codec readline() splits lines apart.
Walter Dörwald <walter@livinglogic.de>
parents:
34279
diff
changeset
|
274 self.assertEqual(reader.readline(), s2) |
|
a216003cef04
Fix and test for SF bug #1098990: codec readline() splits lines apart.
Walter Dörwald <walter@livinglogic.de>
parents:
34279
diff
changeset
|
275 self.assertEqual(reader.readline(), s3) |
|
a216003cef04
Fix and test for SF bug #1098990: codec readline() splits lines apart.
Walter Dörwald <walter@livinglogic.de>
parents:
34279
diff
changeset
|
276 self.assertEqual(reader.readline(), s4) |
|
a216003cef04
Fix and test for SF bug #1098990: codec readline() splits lines apart.
Walter Dörwald <walter@livinglogic.de>
parents:
34279
diff
changeset
|
277 self.assertEqual(reader.readline(), s5) |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
278 self.assertEqual(reader.readline(), "") |
|
34384
a216003cef04
Fix and test for SF bug #1098990: codec readline() splits lines apart.
Walter Dörwald <walter@livinglogic.de>
parents:
34279
diff
changeset
|
279 |
|
42626
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
280 class UTF32Test(ReadTest): |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
281 encoding = "utf-32" |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
282 |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
283 spamle = (b'\xff\xfe\x00\x00' |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
284 b's\x00\x00\x00p\x00\x00\x00a\x00\x00\x00m\x00\x00\x00' |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
285 b's\x00\x00\x00p\x00\x00\x00a\x00\x00\x00m\x00\x00\x00') |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
286 spambe = (b'\x00\x00\xfe\xff' |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
287 b'\x00\x00\x00s\x00\x00\x00p\x00\x00\x00a\x00\x00\x00m' |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
288 b'\x00\x00\x00s\x00\x00\x00p\x00\x00\x00a\x00\x00\x00m') |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
289 |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
290 def test_only_one_bom(self): |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
291 _,_,reader,writer = codecs.lookup(self.encoding) |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
292 # encode some stream |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
293 s = io.BytesIO() |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
294 f = writer(s) |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
295 f.write("spam") |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
296 f.write("spam") |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
297 d = s.getvalue() |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
298 # check whether there is exactly one BOM in it |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54988
diff
changeset
|
299 self.assertTrue(d == self.spamle or d == self.spambe) |
|
42626
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
300 # try to read it back |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
301 s = io.BytesIO(d) |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
302 f = reader(s) |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
303 self.assertEqual(f.read(), "spamspam") |
|
42626
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
304 |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
305 def test_badbom(self): |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
306 s = io.BytesIO(4*b"\xff") |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
307 f = codecs.getreader(self.encoding)(s) |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
308 self.assertRaises(UnicodeError, f.read) |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
309 |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
310 s = io.BytesIO(8*b"\xff") |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
311 f = codecs.getreader(self.encoding)(s) |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
312 self.assertRaises(UnicodeError, f.read) |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
313 |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
314 def test_partial(self): |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
315 self.check_partial( |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
316 "\x00\xff\u0100\uffff", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
317 [ |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
318 "", # first byte of BOM read |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
319 "", # second byte of BOM read |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
320 "", # third byte of BOM read |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
321 "", # fourth byte of BOM read => byteorder known |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
322 "", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
323 "", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
324 "", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
325 "\x00", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
326 "\x00", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
327 "\x00", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
328 "\x00", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
329 "\x00\xff", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
330 "\x00\xff", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
331 "\x00\xff", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
332 "\x00\xff", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
333 "\x00\xff\u0100", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
334 "\x00\xff\u0100", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
335 "\x00\xff\u0100", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
336 "\x00\xff\u0100", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
337 "\x00\xff\u0100\uffff", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
338 ] |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
339 ) |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
340 |
|
56036
a9809f3b164f
Merged revisions 74871 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55593
diff
changeset
|
341 def test_handlers(self): |
|
a9809f3b164f
Merged revisions 74871 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55593
diff
changeset
|
342 self.assertEqual(('\ufffd', 1), |
|
a9809f3b164f
Merged revisions 74871 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55593
diff
changeset
|
343 codecs.utf_32_decode(b'\x01', 'replace', True)) |
|
a9809f3b164f
Merged revisions 74871 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55593
diff
changeset
|
344 self.assertEqual(('', 1), |
|
a9809f3b164f
Merged revisions 74871 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55593
diff
changeset
|
345 codecs.utf_32_decode(b'\x01', 'ignore', True)) |
|
a9809f3b164f
Merged revisions 74871 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55593
diff
changeset
|
346 |
|
42626
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
347 def test_errors(self): |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
348 self.assertRaises(UnicodeDecodeError, codecs.utf_32_decode, |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
349 b"\xff", "strict", True) |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
350 |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
351 def test_decoder_state(self): |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
352 self.check_state_handling_decode(self.encoding, |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
353 "spamspam", self.spamle) |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
354 self.check_state_handling_decode(self.encoding, |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
355 "spamspam", self.spambe) |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
356 |
|
62258
8d5dd2276680
Merged revisions 81908 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
62215
diff
changeset
|
357 def test_issue8941(self): |
|
8d5dd2276680
Merged revisions 81908 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
62215
diff
changeset
|
358 # Issue #8941: insufficient result allocation when decoding into |
|
8d5dd2276680
Merged revisions 81908 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
62215
diff
changeset
|
359 # surrogate pairs on UCS-2 builds. |
|
8d5dd2276680
Merged revisions 81908 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
62215
diff
changeset
|
360 encoded_le = b'\xff\xfe\x00\x00' + b'\x00\x00\x01\x00' * 1024 |
|
8d5dd2276680
Merged revisions 81908 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
62215
diff
changeset
|
361 self.assertEqual('\U00010000' * 1024, |
|
8d5dd2276680
Merged revisions 81908 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
62215
diff
changeset
|
362 codecs.utf_32_decode(encoded_le)[0]) |
|
8d5dd2276680
Merged revisions 81908 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
62215
diff
changeset
|
363 encoded_be = b'\x00\x00\xfe\xff' + b'\x00\x01\x00\x00' * 1024 |
|
8d5dd2276680
Merged revisions 81908 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
62215
diff
changeset
|
364 self.assertEqual('\U00010000' * 1024, |
|
8d5dd2276680
Merged revisions 81908 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
62215
diff
changeset
|
365 codecs.utf_32_decode(encoded_be)[0]) |
|
8d5dd2276680
Merged revisions 81908 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
62215
diff
changeset
|
366 |
|
42626
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
367 class UTF32LETest(ReadTest): |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
368 encoding = "utf-32-le" |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
369 |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
370 def test_partial(self): |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
371 self.check_partial( |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
372 "\x00\xff\u0100\uffff", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
373 [ |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
374 "", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
375 "", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
376 "", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
377 "\x00", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
378 "\x00", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
379 "\x00", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
380 "\x00", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
381 "\x00\xff", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
382 "\x00\xff", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
383 "\x00\xff", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
384 "\x00\xff", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
385 "\x00\xff\u0100", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
386 "\x00\xff\u0100", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
387 "\x00\xff\u0100", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
388 "\x00\xff\u0100", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
389 "\x00\xff\u0100\uffff", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
390 ] |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
391 ) |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
392 |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
393 def test_simple(self): |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
394 self.assertEqual("\U00010203".encode(self.encoding), b"\x03\x02\x01\x00") |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
395 |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
396 def test_errors(self): |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
397 self.assertRaises(UnicodeDecodeError, codecs.utf_32_le_decode, |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
398 b"\xff", "strict", True) |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
399 |
|
62258
8d5dd2276680
Merged revisions 81908 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
62215
diff
changeset
|
400 def test_issue8941(self): |
|
8d5dd2276680
Merged revisions 81908 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
62215
diff
changeset
|
401 # Issue #8941: insufficient result allocation when decoding into |
|
8d5dd2276680
Merged revisions 81908 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
62215
diff
changeset
|
402 # surrogate pairs on UCS-2 builds. |
|
8d5dd2276680
Merged revisions 81908 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
62215
diff
changeset
|
403 encoded = b'\x00\x00\x01\x00' * 1024 |
|
8d5dd2276680
Merged revisions 81908 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
62215
diff
changeset
|
404 self.assertEqual('\U00010000' * 1024, |
|
8d5dd2276680
Merged revisions 81908 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
62215
diff
changeset
|
405 codecs.utf_32_le_decode(encoded)[0]) |
|
8d5dd2276680
Merged revisions 81908 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
62215
diff
changeset
|
406 |
|
42626
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
407 class UTF32BETest(ReadTest): |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
408 encoding = "utf-32-be" |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
409 |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
410 def test_partial(self): |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
411 self.check_partial( |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
412 "\x00\xff\u0100\uffff", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
413 [ |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
414 "", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
415 "", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
416 "", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
417 "\x00", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
418 "\x00", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
419 "\x00", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
420 "\x00", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
421 "\x00\xff", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
422 "\x00\xff", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
423 "\x00\xff", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
424 "\x00\xff", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
425 "\x00\xff\u0100", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
426 "\x00\xff\u0100", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
427 "\x00\xff\u0100", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
428 "\x00\xff\u0100", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
429 "\x00\xff\u0100\uffff", |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
430 ] |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
431 ) |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
432 |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
433 def test_simple(self): |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
434 self.assertEqual("\U00010203".encode(self.encoding), b"\x00\x01\x02\x03") |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
435 |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
436 def test_errors(self): |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
437 self.assertRaises(UnicodeDecodeError, codecs.utf_32_be_decode, |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
438 b"\xff", "strict", True) |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
439 |
|
62258
8d5dd2276680
Merged revisions 81908 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
62215
diff
changeset
|
440 def test_issue8941(self): |
|
8d5dd2276680
Merged revisions 81908 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
62215
diff
changeset
|
441 # Issue #8941: insufficient result allocation when decoding into |
|
8d5dd2276680
Merged revisions 81908 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
62215
diff
changeset
|
442 # surrogate pairs on UCS-2 builds. |
|
8d5dd2276680
Merged revisions 81908 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
62215
diff
changeset
|
443 encoded = b'\x00\x01\x00\x00' * 1024 |
|
8d5dd2276680
Merged revisions 81908 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
62215
diff
changeset
|
444 self.assertEqual('\U00010000' * 1024, |
|
8d5dd2276680
Merged revisions 81908 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
62215
diff
changeset
|
445 codecs.utf_32_be_decode(encoded)[0]) |
|
8d5dd2276680
Merged revisions 81908 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
62215
diff
changeset
|
446 |
|
8d5dd2276680
Merged revisions 81908 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
62215
diff
changeset
|
447 |
| 34214 | 448 class UTF16Test(ReadTest): |
| 449 encoding = "utf-16" | |
|
18163
f926eb1f5770
Test by Martin v. Loewis for the new UTF-16 codec handling of BOM
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
450 |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
451 spamle = b'\xff\xfes\x00p\x00a\x00m\x00s\x00p\x00a\x00m\x00' |
|
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
452 spambe = b'\xfe\xff\x00s\x00p\x00a\x00m\x00s\x00p\x00a\x00m' |
|
18163
f926eb1f5770
Test by Martin v. Loewis for the new UTF-16 codec handling of BOM
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
453 |
|
f926eb1f5770
Test by Martin v. Loewis for the new UTF-16 codec handling of BOM
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
454 def test_only_one_bom(self): |
| 34214 | 455 _,_,reader,writer = codecs.lookup(self.encoding) |
|
18163
f926eb1f5770
Test by Martin v. Loewis for the new UTF-16 codec handling of BOM
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
456 # encode some stream |
|
41681
1d334912a104
Use io.BytesIO instead of StringIO.StringIO in all
Walter Dörwald <walter@livinglogic.de>
parents:
41679
diff
changeset
|
457 s = io.BytesIO() |
|
18163
f926eb1f5770
Test by Martin v. Loewis for the new UTF-16 codec handling of BOM
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
458 f = writer(s) |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
459 f.write("spam") |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
460 f.write("spam") |
|
18163
f926eb1f5770
Test by Martin v. Loewis for the new UTF-16 codec handling of BOM
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
461 d = s.getvalue() |
|
f926eb1f5770
Test by Martin v. Loewis for the new UTF-16 codec handling of BOM
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
462 # check whether there is exactly one BOM in it |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54988
diff
changeset
|
463 self.assertTrue(d == self.spamle or d == self.spambe) |
|
18163
f926eb1f5770
Test by Martin v. Loewis for the new UTF-16 codec handling of BOM
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
464 # try to read it back |
|
41681
1d334912a104
Use io.BytesIO instead of StringIO.StringIO in all
Walter Dörwald <walter@livinglogic.de>
parents:
41679
diff
changeset
|
465 s = io.BytesIO(d) |
|
18163
f926eb1f5770
Test by Martin v. Loewis for the new UTF-16 codec handling of BOM
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
466 f = reader(s) |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
467 self.assertEqual(f.read(), "spamspam") |
|
18163
f926eb1f5770
Test by Martin v. Loewis for the new UTF-16 codec handling of BOM
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
468 |
|
34538
37b2f11c1a00
Add a test for UTF-16 reading where the byte sequence doesn't start with
Walter Dörwald <walter@livinglogic.de>
parents:
34384
diff
changeset
|
469 def test_badbom(self): |
|
41681
1d334912a104
Use io.BytesIO instead of StringIO.StringIO in all
Walter Dörwald <walter@livinglogic.de>
parents:
41679
diff
changeset
|
470 s = io.BytesIO(b"\xff\xff") |
|
34562
7a1277fad04e
Fix stupid typo: Don't read from a writer.
Walter Dörwald <walter@livinglogic.de>
parents:
34538
diff
changeset
|
471 f = codecs.getreader(self.encoding)(s) |
|
34538
37b2f11c1a00
Add a test for UTF-16 reading where the byte sequence doesn't start with
Walter Dörwald <walter@livinglogic.de>
parents:
34384
diff
changeset
|
472 self.assertRaises(UnicodeError, f.read) |
|
37b2f11c1a00
Add a test for UTF-16 reading where the byte sequence doesn't start with
Walter Dörwald <walter@livinglogic.de>
parents:
34384
diff
changeset
|
473 |
|
41681
1d334912a104
Use io.BytesIO instead of StringIO.StringIO in all
Walter Dörwald <walter@livinglogic.de>
parents:
41679
diff
changeset
|
474 s = io.BytesIO(b"\xff\xff\xff\xff") |
|
34562
7a1277fad04e
Fix stupid typo: Don't read from a writer.
Walter Dörwald <walter@livinglogic.de>
parents:
34538
diff
changeset
|
475 f = codecs.getreader(self.encoding)(s) |
|
34538
37b2f11c1a00
Add a test for UTF-16 reading where the byte sequence doesn't start with
Walter Dörwald <walter@livinglogic.de>
parents:
34384
diff
changeset
|
476 self.assertRaises(UnicodeError, f.read) |
|
37b2f11c1a00
Add a test for UTF-16 reading where the byte sequence doesn't start with
Walter Dörwald <walter@livinglogic.de>
parents:
34384
diff
changeset
|
477 |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
478 def test_partial(self): |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
479 self.check_partial( |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
480 "\x00\xff\u0100\uffff", |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
481 [ |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
482 "", # first byte of BOM read |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
483 "", # second byte of BOM read => byteorder known |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
484 "", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
485 "\x00", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
486 "\x00", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
487 "\x00\xff", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
488 "\x00\xff", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
489 "\x00\xff\u0100", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
490 "\x00\xff\u0100", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
491 "\x00\xff\u0100\uffff", |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
492 ] |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
493 ) |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
494 |
|
56036
a9809f3b164f
Merged revisions 74871 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55593
diff
changeset
|
495 def test_handlers(self): |
|
a9809f3b164f
Merged revisions 74871 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55593
diff
changeset
|
496 self.assertEqual(('\ufffd', 1), |
|
a9809f3b164f
Merged revisions 74871 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55593
diff
changeset
|
497 codecs.utf_16_decode(b'\x01', 'replace', True)) |
|
a9809f3b164f
Merged revisions 74871 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55593
diff
changeset
|
498 self.assertEqual(('', 1), |
|
a9809f3b164f
Merged revisions 74871 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55593
diff
changeset
|
499 codecs.utf_16_decode(b'\x01', 'ignore', True)) |
|
a9809f3b164f
Merged revisions 74871 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55593
diff
changeset
|
500 |
|
35882
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
501 def test_errors(self): |
|
41473
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
502 self.assertRaises(UnicodeDecodeError, codecs.utf_16_decode, |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
503 b"\xff", "strict", True) |
|
41473
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
504 |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
505 def test_decoder_state(self): |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
506 self.check_state_handling_decode(self.encoding, |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
507 "spamspam", self.spamle) |
|
41473
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
508 self.check_state_handling_decode(self.encoding, |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
509 "spamspam", self.spambe) |
|
35882
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
510 |
|
59171
0d590e5e840c
Recorded merge of revisions 78462,78484 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56036
diff
changeset
|
511 def test_bug691291(self): |
|
0d590e5e840c
Recorded merge of revisions 78462,78484 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56036
diff
changeset
|
512 # Files are always opened in binary mode, even if no binary mode was |
|
0d590e5e840c
Recorded merge of revisions 78462,78484 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56036
diff
changeset
|
513 # specified. This means that no automatic conversion of '\n' is done |
|
0d590e5e840c
Recorded merge of revisions 78462,78484 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56036
diff
changeset
|
514 # on reading and writing. |
|
0d590e5e840c
Recorded merge of revisions 78462,78484 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56036
diff
changeset
|
515 s1 = 'Hello\r\nworld\r\n' |
|
0d590e5e840c
Recorded merge of revisions 78462,78484 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56036
diff
changeset
|
516 |
|
0d590e5e840c
Recorded merge of revisions 78462,78484 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56036
diff
changeset
|
517 s = s1.encode(self.encoding) |
|
70292
b538445a9df8
test_codecs now removes the temporay file (created by the test)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
66340
diff
changeset
|
518 self.addCleanup(support.unlink, support.TESTFN) |
|
b538445a9df8
test_codecs now removes the temporay file (created by the test)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
66340
diff
changeset
|
519 with open(support.TESTFN, 'wb') as fp: |
|
b538445a9df8
test_codecs now removes the temporay file (created by the test)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
66340
diff
changeset
|
520 fp.write(s) |
|
b538445a9df8
test_codecs now removes the temporay file (created by the test)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
66340
diff
changeset
|
521 with codecs.open(support.TESTFN, 'U', encoding=self.encoding) as reader: |
|
b538445a9df8
test_codecs now removes the temporay file (created by the test)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
66340
diff
changeset
|
522 self.assertEqual(reader.read(), s1) |
|
59171
0d590e5e840c
Recorded merge of revisions 78462,78484 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56036
diff
changeset
|
523 |
| 34214 | 524 class UTF16LETest(ReadTest): |
| 525 encoding = "utf-16-le" | |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
526 |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
527 def test_partial(self): |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
528 self.check_partial( |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
529 "\x00\xff\u0100\uffff", |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
530 [ |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
531 "", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
532 "\x00", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
533 "\x00", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
534 "\x00\xff", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
535 "\x00\xff", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
536 "\x00\xff\u0100", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
537 "\x00\xff\u0100", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
538 "\x00\xff\u0100\uffff", |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
539 ] |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
540 ) |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
541 |
|
35882
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
542 def test_errors(self): |
|
41473
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
543 self.assertRaises(UnicodeDecodeError, codecs.utf_16_le_decode, |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
544 b"\xff", "strict", True) |
|
35882
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
545 |
|
66797
113925b79b97
Issue #10546: UTF-16-LE and UTF-16-BE *do* support non-BMP characters
Victor Stinner <victor.stinner@haypocalc.com>
parents:
66605
diff
changeset
|
546 def test_nonbmp(self): |
|
113925b79b97
Issue #10546: UTF-16-LE and UTF-16-BE *do* support non-BMP characters
Victor Stinner <victor.stinner@haypocalc.com>
parents:
66605
diff
changeset
|
547 self.assertEqual("\U00010203".encode(self.encoding), |
|
113925b79b97
Issue #10546: UTF-16-LE and UTF-16-BE *do* support non-BMP characters
Victor Stinner <victor.stinner@haypocalc.com>
parents:
66605
diff
changeset
|
548 b'\x00\xd8\x03\xde') |
|
113925b79b97
Issue #10546: UTF-16-LE and UTF-16-BE *do* support non-BMP characters
Victor Stinner <victor.stinner@haypocalc.com>
parents:
66605
diff
changeset
|
549 self.assertEqual(b'\x00\xd8\x03\xde'.decode(self.encoding), |
|
113925b79b97
Issue #10546: UTF-16-LE and UTF-16-BE *do* support non-BMP characters
Victor Stinner <victor.stinner@haypocalc.com>
parents:
66605
diff
changeset
|
550 "\U00010203") |
|
113925b79b97
Issue #10546: UTF-16-LE and UTF-16-BE *do* support non-BMP characters
Victor Stinner <victor.stinner@haypocalc.com>
parents:
66605
diff
changeset
|
551 |
| 34214 | 552 class UTF16BETest(ReadTest): |
| 553 encoding = "utf-16-be" | |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
554 |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
555 def test_partial(self): |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
556 self.check_partial( |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
557 "\x00\xff\u0100\uffff", |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
558 [ |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
559 "", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
560 "\x00", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
561 "\x00", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
562 "\x00\xff", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
563 "\x00\xff", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
564 "\x00\xff\u0100", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
565 "\x00\xff\u0100", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
566 "\x00\xff\u0100\uffff", |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
567 ] |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
568 ) |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
569 |
|
35882
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
570 def test_errors(self): |
|
41473
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
571 self.assertRaises(UnicodeDecodeError, codecs.utf_16_be_decode, |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
572 b"\xff", "strict", True) |
|
35882
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
573 |
|
66797
113925b79b97
Issue #10546: UTF-16-LE and UTF-16-BE *do* support non-BMP characters
Victor Stinner <victor.stinner@haypocalc.com>
parents:
66605
diff
changeset
|
574 def test_nonbmp(self): |
|
113925b79b97
Issue #10546: UTF-16-LE and UTF-16-BE *do* support non-BMP characters
Victor Stinner <victor.stinner@haypocalc.com>
parents:
66605
diff
changeset
|
575 self.assertEqual("\U00010203".encode(self.encoding), |
|
113925b79b97
Issue #10546: UTF-16-LE and UTF-16-BE *do* support non-BMP characters
Victor Stinner <victor.stinner@haypocalc.com>
parents:
66605
diff
changeset
|
576 b'\xd8\x00\xde\x03') |
|
113925b79b97
Issue #10546: UTF-16-LE and UTF-16-BE *do* support non-BMP characters
Victor Stinner <victor.stinner@haypocalc.com>
parents:
66605
diff
changeset
|
577 self.assertEqual(b'\xd8\x00\xde\x03'.decode(self.encoding), |
|
113925b79b97
Issue #10546: UTF-16-LE and UTF-16-BE *do* support non-BMP characters
Victor Stinner <victor.stinner@haypocalc.com>
parents:
66605
diff
changeset
|
578 "\U00010203") |
|
113925b79b97
Issue #10546: UTF-16-LE and UTF-16-BE *do* support non-BMP characters
Victor Stinner <victor.stinner@haypocalc.com>
parents:
66605
diff
changeset
|
579 |
| 34214 | 580 class UTF8Test(ReadTest): |
| 581 encoding = "utf-8" | |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
582 |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
583 def test_partial(self): |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
584 self.check_partial( |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
585 "\x00\xff\u07ff\u0800\uffff", |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
586 [ |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
587 "\x00", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
588 "\x00", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
589 "\x00\xff", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
590 "\x00\xff", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
591 "\x00\xff\u07ff", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
592 "\x00\xff\u07ff", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
593 "\x00\xff\u07ff", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
594 "\x00\xff\u07ff\u0800", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
595 "\x00\xff\u07ff\u0800", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
596 "\x00\xff\u07ff\u0800", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
597 "\x00\xff\u07ff\u0800\uffff", |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
598 ] |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
599 ) |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
600 |
|
41473
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
601 def test_decoder_state(self): |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
602 u = "\x00\x7f\x80\xff\u0100\u07ff\u0800\uffff\U0010ffff" |
|
41473
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
603 self.check_state_handling_decode(self.encoding, |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
604 u, u.encode(self.encoding)) |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
605 |
|
53681
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
50975
diff
changeset
|
606 def test_lone_surrogates(self): |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
50975
diff
changeset
|
607 self.assertRaises(UnicodeEncodeError, "\ud800".encode, "utf-8") |
|
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
50975
diff
changeset
|
608 self.assertRaises(UnicodeDecodeError, b"\xed\xa0\x80".decode, "utf-8") |
|
60875
149badf73bca
Merged revisions 80382 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59171
diff
changeset
|
609 self.assertEqual("[\uDC80]".encode("utf-8", "backslashreplace"), |
|
149badf73bca
Merged revisions 80382 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59171
diff
changeset
|
610 b'[\\udc80]') |
|
149badf73bca
Merged revisions 80382 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59171
diff
changeset
|
611 self.assertEqual("[\uDC80]".encode("utf-8", "xmlcharrefreplace"), |
|
149badf73bca
Merged revisions 80382 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59171
diff
changeset
|
612 b'[�]') |
|
149badf73bca
Merged revisions 80382 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59171
diff
changeset
|
613 self.assertEqual("[\uDC80]".encode("utf-8", "surrogateescape"), |
|
149badf73bca
Merged revisions 80382 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59171
diff
changeset
|
614 b'[\x80]') |
|
149badf73bca
Merged revisions 80382 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59171
diff
changeset
|
615 self.assertEqual("[\uDC80]".encode("utf-8", "ignore"), |
|
149badf73bca
Merged revisions 80382 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59171
diff
changeset
|
616 b'[]') |
|
149badf73bca
Merged revisions 80382 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59171
diff
changeset
|
617 self.assertEqual("[\uDC80]".encode("utf-8", "replace"), |
|
149badf73bca
Merged revisions 80382 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59171
diff
changeset
|
618 b'[?]') |
|
53681
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
50975
diff
changeset
|
619 |
|
53965
fbcdae3e0ca1
Rename the surrogates error handler to surrogatepass.
Martin v. Löwis <martin@v.loewis.de>
parents:
53859
diff
changeset
|
620 def test_surrogatepass_handler(self): |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
621 self.assertEqual("abc\ud800def".encode("utf-8", "surrogatepass"), |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
622 b"abc\xed\xa0\x80def") |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
623 self.assertEqual(b"abc\xed\xa0\x80def".decode("utf-8", "surrogatepass"), |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
624 "abc\ud800def") |
|
53965
fbcdae3e0ca1
Rename the surrogates error handler to surrogatepass.
Martin v. Löwis <martin@v.loewis.de>
parents:
53859
diff
changeset
|
625 self.assertTrue(codecs.lookup_error("surrogatepass")) |
|
53681
52a77ef069cd
Issue #3672: Reject surrogates in utf-8 codec; add surrogates error
Martin v. Löwis <martin@v.loewis.de>
parents:
50975
diff
changeset
|
626 |
|
35882
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
627 class UTF7Test(ReadTest): |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
628 encoding = "utf-7" |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
629 |
|
43933
9c4bf18e5d30
Merged revisions 59056-59076 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
630 def test_partial(self): |
|
9c4bf18e5d30
Merged revisions 59056-59076 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
631 self.check_partial( |
|
9c4bf18e5d30
Merged revisions 59056-59076 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
632 "a+-b", |
|
9c4bf18e5d30
Merged revisions 59056-59076 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
633 [ |
|
9c4bf18e5d30
Merged revisions 59056-59076 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
634 "a", |
|
9c4bf18e5d30
Merged revisions 59056-59076 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
635 "a", |
|
9c4bf18e5d30
Merged revisions 59056-59076 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
636 "a+", |
|
9c4bf18e5d30
Merged revisions 59056-59076 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
637 "a+-", |
|
9c4bf18e5d30
Merged revisions 59056-59076 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
638 "a+-b", |
|
9c4bf18e5d30
Merged revisions 59056-59076 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
639 ] |
|
9c4bf18e5d30
Merged revisions 59056-59076 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43912
diff
changeset
|
640 ) |
|
35882
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
641 |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
642 class UTF16ExTest(unittest.TestCase): |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
643 |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
644 def test_errors(self): |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
645 self.assertRaises(UnicodeDecodeError, codecs.utf_16_ex_decode, b"\xff", "strict", 0, True) |
|
35882
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
646 |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
647 def test_bad_args(self): |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
648 self.assertRaises(TypeError, codecs.utf_16_ex_decode) |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
649 |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
650 class ReadBufferTest(unittest.TestCase): |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
651 |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
652 def test_array(self): |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
653 import array |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
654 self.assertEqual( |
|
42911
cb28e14ee808
Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents:
42626
diff
changeset
|
655 codecs.readbuffer_encode(array.array("b", b"spam")), |
|
42124
4590129e4c2b
Change readbuffer_encode() and charbuffer_encode() to
Walter Dörwald <walter@livinglogic.de>
parents:
42118
diff
changeset
|
656 (b"spam", 4) |
|
35882
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
657 ) |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
658 |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
659 def test_empty(self): |
|
42124
4590129e4c2b
Change readbuffer_encode() and charbuffer_encode() to
Walter Dörwald <walter@livinglogic.de>
parents:
42118
diff
changeset
|
660 self.assertEqual(codecs.readbuffer_encode(""), (b"", 0)) |
|
35882
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
661 |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
662 def test_bad_args(self): |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
663 self.assertRaises(TypeError, codecs.readbuffer_encode) |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
664 self.assertRaises(TypeError, codecs.readbuffer_encode, 42) |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
665 |
|
36247
3ba9ff79bb29
Patch #1177307: UTF-8-Sig codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
35889
diff
changeset
|
666 class UTF8SigTest(ReadTest): |
|
3ba9ff79bb29
Patch #1177307: UTF-8-Sig codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
35889
diff
changeset
|
667 encoding = "utf-8-sig" |
|
3ba9ff79bb29
Patch #1177307: UTF-8-Sig codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
35889
diff
changeset
|
668 |
|
3ba9ff79bb29
Patch #1177307: UTF-8-Sig codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
35889
diff
changeset
|
669 def test_partial(self): |
|
3ba9ff79bb29
Patch #1177307: UTF-8-Sig codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
35889
diff
changeset
|
670 self.check_partial( |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
671 "\ufeff\x00\xff\u07ff\u0800\uffff", |
|
36247
3ba9ff79bb29
Patch #1177307: UTF-8-Sig codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
35889
diff
changeset
|
672 [ |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
673 "", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
674 "", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
675 "", # First BOM has been read and skipped |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
676 "", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
677 "", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
678 "\ufeff", # Second BOM has been read and emitted |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
679 "\ufeff\x00", # "\x00" read and emitted |
|
42118
70a0831feda6
Patch by Ron Adam: Don't use u prefix in unicode error messages
Walter Dörwald <walter@livinglogic.de>
parents:
42064
diff
changeset
|
680 "\ufeff\x00", # First byte of encoded "\xff" read |
|
70a0831feda6
Patch by Ron Adam: Don't use u prefix in unicode error messages
Walter Dörwald <walter@livinglogic.de>
parents:
42064
diff
changeset
|
681 "\ufeff\x00\xff", # Second byte of encoded "\xff" read |
|
70a0831feda6
Patch by Ron Adam: Don't use u prefix in unicode error messages
Walter Dörwald <walter@livinglogic.de>
parents:
42064
diff
changeset
|
682 "\ufeff\x00\xff", # First byte of encoded "\u07ff" read |
|
70a0831feda6
Patch by Ron Adam: Don't use u prefix in unicode error messages
Walter Dörwald <walter@livinglogic.de>
parents:
42064
diff
changeset
|
683 "\ufeff\x00\xff\u07ff", # Second byte of encoded "\u07ff" read |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
684 "\ufeff\x00\xff\u07ff", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
685 "\ufeff\x00\xff\u07ff", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
686 "\ufeff\x00\xff\u07ff\u0800", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
687 "\ufeff\x00\xff\u07ff\u0800", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
688 "\ufeff\x00\xff\u07ff\u0800", |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
689 "\ufeff\x00\xff\u07ff\u0800\uffff", |
|
36247
3ba9ff79bb29
Patch #1177307: UTF-8-Sig codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
35889
diff
changeset
|
690 ] |
|
3ba9ff79bb29
Patch #1177307: UTF-8-Sig codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
35889
diff
changeset
|
691 ) |
|
3ba9ff79bb29
Patch #1177307: UTF-8-Sig codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
35889
diff
changeset
|
692 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
693 def test_bug1601501(self): |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
694 # SF bug #1601501: check that the codec works with a buffer |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
695 self.assertEqual(str(b"\xef\xbb\xbf", "utf-8-sig"), "") |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
696 |
|
41473
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
697 def test_bom(self): |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
698 d = codecs.getincrementaldecoder("utf-8-sig")() |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
699 s = "spam" |
|
41473
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
700 self.assertEqual(d.decode(s.encode("utf-8-sig")), s) |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
701 |
|
43912
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
702 def test_stream_bom(self): |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
703 unistring = "ABC\u00A1\u2200XYZ" |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
704 bytestring = codecs.BOM_UTF8 + b"ABC\xC2\xA1\xE2\x88\x80XYZ" |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
705 |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
706 reader = codecs.getreader("utf-8-sig") |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
707 for sizehint in [None] + list(range(1, 11)) + \ |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
708 [64, 128, 256, 512, 1024]: |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
709 istream = reader(io.BytesIO(bytestring)) |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
710 ostream = io.StringIO() |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
711 while 1: |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
712 if sizehint is not None: |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
713 data = istream.read(sizehint) |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
714 else: |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
715 data = istream.read() |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
716 |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
717 if not data: |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
718 break |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
719 ostream.write(data) |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
720 |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
721 got = ostream.getvalue() |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
722 self.assertEqual(got, unistring) |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
723 |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
724 def test_stream_bare(self): |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
725 unistring = "ABC\u00A1\u2200XYZ" |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
726 bytestring = b"ABC\xC2\xA1\xE2\x88\x80XYZ" |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
727 |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
728 reader = codecs.getreader("utf-8-sig") |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
729 for sizehint in [None] + list(range(1, 11)) + \ |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
730 [64, 128, 256, 512, 1024]: |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
731 istream = reader(io.BytesIO(bytestring)) |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
732 ostream = io.StringIO() |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
733 while 1: |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
734 if sizehint is not None: |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
735 data = istream.read(sizehint) |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
736 else: |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
737 data = istream.read() |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
738 |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
739 if not data: |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
740 break |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
741 ostream.write(data) |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
742 |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
743 got = ostream.getvalue() |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
744 self.assertEqual(got, unistring) |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
745 |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
746 class EscapeDecodeTest(unittest.TestCase): |
|
4747ef9640ae
Merged revisions 59041-59055 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
747 def test_empty(self): |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
748 self.assertEqual(codecs.escape_decode(""), ("", 0)) |
|
41473
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
749 |
|
27680
b19aaaeb6d64
Fix for [ 543344 ] Interpreter crashes when recoding; suggested
Marc-André Lemburg <mal@egenix.com>
parents:
25100
diff
changeset
|
750 class RecodingTest(unittest.TestCase): |
|
b19aaaeb6d64
Fix for [ 543344 ] Interpreter crashes when recoding; suggested
Marc-André Lemburg <mal@egenix.com>
parents:
25100
diff
changeset
|
751 def test_recoding(self): |
|
41757
13a6c6bef609
Make test_codecs work. The CJK codecs now use bytes instead of str8 for
Guido van Rossum <guido@python.org>
parents:
41750
diff
changeset
|
752 f = io.BytesIO() |
|
27680
b19aaaeb6d64
Fix for [ 543344 ] Interpreter crashes when recoding; suggested
Marc-André Lemburg <mal@egenix.com>
parents:
25100
diff
changeset
|
753 f2 = codecs.EncodedFile(f, "unicode_internal", "utf-8") |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
754 f2.write("a") |
|
27680
b19aaaeb6d64
Fix for [ 543344 ] Interpreter crashes when recoding; suggested
Marc-André Lemburg <mal@egenix.com>
parents:
25100
diff
changeset
|
755 f2.close() |
|
b19aaaeb6d64
Fix for [ 543344 ] Interpreter crashes when recoding; suggested
Marc-André Lemburg <mal@egenix.com>
parents:
25100
diff
changeset
|
756 # Python used to crash on this at exit because of a refcount |
|
b19aaaeb6d64
Fix for [ 543344 ] Interpreter crashes when recoding; suggested
Marc-André Lemburg <mal@egenix.com>
parents:
25100
diff
changeset
|
757 # bug in _codecsmodule.c |
|
20013
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
18163
diff
changeset
|
758 |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
759 # From RFC 3492 |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
760 punycode_testcases = [ |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
761 # A Arabic (Egyptian): |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
762 ("\u0644\u064A\u0647\u0645\u0627\u0628\u062A\u0643\u0644" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
763 "\u0645\u0648\u0634\u0639\u0631\u0628\u064A\u061F", |
|
41679
98709189d9e1
Fix punycode codec and tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41676
diff
changeset
|
764 b"egbpdaj6bu4bxfgehfvwxn"), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
765 # B Chinese (simplified): |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
766 ("\u4ED6\u4EEC\u4E3A\u4EC0\u4E48\u4E0D\u8BF4\u4E2D\u6587", |
|
41679
98709189d9e1
Fix punycode codec and tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41676
diff
changeset
|
767 b"ihqwcrb4cv8a8dqg056pqjye"), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
768 # C Chinese (traditional): |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
769 ("\u4ED6\u5011\u7232\u4EC0\u9EBD\u4E0D\u8AAA\u4E2D\u6587", |
|
41679
98709189d9e1
Fix punycode codec and tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41676
diff
changeset
|
770 b"ihqwctvzc91f659drss3x8bo0yb"), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
771 # D Czech: Pro<ccaron>prost<ecaron>nemluv<iacute><ccaron>esky |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
772 ("\u0050\u0072\u006F\u010D\u0070\u0072\u006F\u0073\u0074" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
773 "\u011B\u006E\u0065\u006D\u006C\u0075\u0076\u00ED\u010D" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
774 "\u0065\u0073\u006B\u0079", |
|
41679
98709189d9e1
Fix punycode codec and tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41676
diff
changeset
|
775 b"Proprostnemluvesky-uyb24dma41a"), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
776 # E Hebrew: |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
777 ("\u05DC\u05DE\u05D4\u05D4\u05DD\u05E4\u05E9\u05D5\u05D8" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
778 "\u05DC\u05D0\u05DE\u05D3\u05D1\u05E8\u05D9\u05DD\u05E2" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
779 "\u05D1\u05E8\u05D9\u05EA", |
|
41679
98709189d9e1
Fix punycode codec and tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41676
diff
changeset
|
780 b"4dbcagdahymbxekheh6e0a7fei0b"), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
781 # F Hindi (Devanagari): |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
782 ("\u092F\u0939\u0932\u094B\u0917\u0939\u093F\u0928\u094D" |
|
41679
98709189d9e1
Fix punycode codec and tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41676
diff
changeset
|
783 "\u0926\u0940\u0915\u094D\u092F\u094B\u0902\u0928\u0939" |
|
98709189d9e1
Fix punycode codec and tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41676
diff
changeset
|
784 "\u0940\u0902\u092C\u094B\u0932\u0938\u0915\u0924\u0947" |
|
98709189d9e1
Fix punycode codec and tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41676
diff
changeset
|
785 "\u0939\u0948\u0902", |
|
98709189d9e1
Fix punycode codec and tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41676
diff
changeset
|
786 b"i1baa7eci9glrd9b2ae1bj0hfcgg6iyaf8o0a1dig0cd"), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
787 |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
788 #(G) Japanese (kanji and hiragana): |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
789 ("\u306A\u305C\u307F\u3093\u306A\u65E5\u672C\u8A9E\u3092" |
|
41679
98709189d9e1
Fix punycode codec and tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41676
diff
changeset
|
790 "\u8A71\u3057\u3066\u304F\u308C\u306A\u3044\u306E\u304B", |
|
98709189d9e1
Fix punycode codec and tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41676
diff
changeset
|
791 b"n8jok5ay5dzabd5bym9f0cm5685rrjetr6pdxa"), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
792 |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
793 # (H) Korean (Hangul syllables): |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
794 ("\uC138\uACC4\uC758\uBAA8\uB4E0\uC0AC\uB78C\uB4E4\uC774" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
795 "\uD55C\uAD6D\uC5B4\uB97C\uC774\uD574\uD55C\uB2E4\uBA74" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
796 "\uC5BC\uB9C8\uB098\uC88B\uC744\uAE4C", |
|
41679
98709189d9e1
Fix punycode codec and tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41676
diff
changeset
|
797 b"989aomsvi5e83db1d2a355cv1e0vak1dwrv93d5xbh15a0dt30a5j" |
|
98709189d9e1
Fix punycode codec and tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41676
diff
changeset
|
798 b"psd879ccm6fea98c"), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
799 |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
800 # (I) Russian (Cyrillic): |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
801 ("\u043F\u043E\u0447\u0435\u043C\u0443\u0436\u0435\u043E" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
802 "\u043D\u0438\u043D\u0435\u0433\u043E\u0432\u043E\u0440" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
803 "\u044F\u0442\u043F\u043E\u0440\u0443\u0441\u0441\u043A" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
804 "\u0438", |
|
41679
98709189d9e1
Fix punycode codec and tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41676
diff
changeset
|
805 b"b1abfaaepdrnnbgefbaDotcwatmq2g4l"), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
806 |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
807 # (J) Spanish: Porqu<eacute>nopuedensimplementehablarenEspa<ntilde>ol |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
808 ("\u0050\u006F\u0072\u0071\u0075\u00E9\u006E\u006F\u0070" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
809 "\u0075\u0065\u0064\u0065\u006E\u0073\u0069\u006D\u0070" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
810 "\u006C\u0065\u006D\u0065\u006E\u0074\u0065\u0068\u0061" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
811 "\u0062\u006C\u0061\u0072\u0065\u006E\u0045\u0073\u0070" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
812 "\u0061\u00F1\u006F\u006C", |
|
41679
98709189d9e1
Fix punycode codec and tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41676
diff
changeset
|
813 b"PorqunopuedensimplementehablarenEspaol-fmd56a"), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
814 |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
815 # (K) Vietnamese: |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
816 # T<adotbelow>isaoh<odotbelow>kh<ocirc>ngth<ecirchookabove>ch\ |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
817 # <ihookabove>n<oacute>iti<ecircacute>ngVi<ecircdotbelow>t |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
818 ("\u0054\u1EA1\u0069\u0073\u0061\u006F\u0068\u1ECD\u006B" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
819 "\u0068\u00F4\u006E\u0067\u0074\u0068\u1EC3\u0063\u0068" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
820 "\u1EC9\u006E\u00F3\u0069\u0074\u0069\u1EBF\u006E\u0067" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
821 "\u0056\u0069\u1EC7\u0074", |
|
41679
98709189d9e1
Fix punycode codec and tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41676
diff
changeset
|
822 b"TisaohkhngthchnitingVit-kjcr8268qyxafd2f1b9g"), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
823 |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
824 #(L) 3<nen>B<gumi><kinpachi><sensei> |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
825 ("\u0033\u5E74\u0042\u7D44\u91D1\u516B\u5148\u751F", |
|
41679
98709189d9e1
Fix punycode codec and tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41676
diff
changeset
|
826 b"3B-ww4c5e180e575a65lsy2b"), |
|
28774
c9bee11eaa8b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
28678
diff
changeset
|
827 |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
828 # (M) <amuro><namie>-with-SUPER-MONKEYS |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
829 ("\u5B89\u5BA4\u5948\u7F8E\u6075\u002D\u0077\u0069\u0074" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
830 "\u0068\u002D\u0053\u0055\u0050\u0045\u0052\u002D\u004D" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
831 "\u004F\u004E\u004B\u0045\u0059\u0053", |
|
41679
98709189d9e1
Fix punycode codec and tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41676
diff
changeset
|
832 b"-with-SUPER-MONKEYS-pc58ag80a8qai00g7n9n"), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
833 |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
834 # (N) Hello-Another-Way-<sorezore><no><basho> |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
835 ("\u0048\u0065\u006C\u006C\u006F\u002D\u0041\u006E\u006F" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
836 "\u0074\u0068\u0065\u0072\u002D\u0057\u0061\u0079\u002D" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
837 "\u305D\u308C\u305E\u308C\u306E\u5834\u6240", |
|
41679
98709189d9e1
Fix punycode codec and tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41676
diff
changeset
|
838 b"Hello-Another-Way--fc4qua05auwb3674vfr0b"), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
839 |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
840 # (O) <hitotsu><yane><no><shita>2 |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
841 ("\u3072\u3068\u3064\u5C4B\u6839\u306E\u4E0B\u0032", |
|
41679
98709189d9e1
Fix punycode codec and tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41676
diff
changeset
|
842 b"2-u9tlzr9756bt3uc0v"), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
843 |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
844 # (P) Maji<de>Koi<suru>5<byou><mae> |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
845 ("\u004D\u0061\u006A\u0069\u3067\u004B\u006F\u0069\u3059" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
846 "\u308B\u0035\u79D2\u524D", |
|
41679
98709189d9e1
Fix punycode codec and tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41676
diff
changeset
|
847 b"MajiKoi5-783gue6qz075azm5e"), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
848 |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
849 # (Q) <pafii>de<runba> |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
850 ("\u30D1\u30D5\u30A3\u30FC\u0064\u0065\u30EB\u30F3\u30D0", |
|
41679
98709189d9e1
Fix punycode codec and tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41676
diff
changeset
|
851 b"de-jg4avhby1noc0d"), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
852 |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
853 # (R) <sono><supiido><de> |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
854 ("\u305D\u306E\u30B9\u30D4\u30FC\u30C9\u3067", |
|
41679
98709189d9e1
Fix punycode codec and tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41676
diff
changeset
|
855 b"d9juau41awczczp"), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
856 |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
857 # (S) -> $1.00 <- |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
858 ("\u002D\u003E\u0020\u0024\u0031\u002E\u0030\u0030\u0020" |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
859 "\u003C\u002D", |
|
41679
98709189d9e1
Fix punycode codec and tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41676
diff
changeset
|
860 b"-> $1.00 <--") |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
861 ] |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
862 |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
863 for i in punycode_testcases: |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
864 if len(i)!=2: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
865 print(repr(i)) |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
866 |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
867 class PunycodeTest(unittest.TestCase): |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
868 def test_encode(self): |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
869 for uni, puny in punycode_testcases: |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
870 # Need to convert both strings to lower case, since |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
871 # some of the extended encodings use upper case, but our |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
872 # code produces only lower case. Converting just puny to |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
873 # lower is also insufficient, since some of the input characters |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
874 # are upper case. |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
875 self.assertEqual( |
|
41679
98709189d9e1
Fix punycode codec and tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41676
diff
changeset
|
876 str(uni.encode("punycode"), "ascii").lower(), |
|
98709189d9e1
Fix punycode codec and tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41676
diff
changeset
|
877 str(puny, "ascii").lower() |
|
98709189d9e1
Fix punycode codec and tests.
Walter Dörwald <walter@livinglogic.de>
parents:
41676
diff
changeset
|
878 ) |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
879 |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
880 def test_decode(self): |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
881 for uni, puny in punycode_testcases: |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
882 self.assertEqual(uni, puny.decode("punycode")) |
|
42985
f4851dc0df04
Fix the one failing test (can't decode twice).
Guido van Rossum <guido@python.org>
parents:
42911
diff
changeset
|
883 puny = puny.decode("ascii").encode("ascii") |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
884 self.assertEqual(uni, puny.decode("punycode")) |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
885 |
|
35484
4fc7a7edcb4b
SF bug #1251300: On UCS-4 builds the "unicode-internal" codec will now complain
Walter Dörwald <walter@livinglogic.de>
parents:
35435
diff
changeset
|
886 class UnicodeInternalTest(unittest.TestCase): |
|
4fc7a7edcb4b
SF bug #1251300: On UCS-4 builds the "unicode-internal" codec will now complain
Walter Dörwald <walter@livinglogic.de>
parents:
35435
diff
changeset
|
887 def test_bug1251300(self): |
|
4fc7a7edcb4b
SF bug #1251300: On UCS-4 builds the "unicode-internal" codec will now complain
Walter Dörwald <walter@livinglogic.de>
parents:
35435
diff
changeset
|
888 # Decoding with unicode_internal used to not correctly handle "code |
|
4fc7a7edcb4b
SF bug #1251300: On UCS-4 builds the "unicode-internal" codec will now complain
Walter Dörwald <walter@livinglogic.de>
parents:
35435
diff
changeset
|
889 # points" above 0x10ffff on UCS-4 builds. |
|
4fc7a7edcb4b
SF bug #1251300: On UCS-4 builds the "unicode-internal" codec will now complain
Walter Dörwald <walter@livinglogic.de>
parents:
35435
diff
changeset
|
890 if sys.maxunicode > 0xffff: |
|
4fc7a7edcb4b
SF bug #1251300: On UCS-4 builds the "unicode-internal" codec will now complain
Walter Dörwald <walter@livinglogic.de>
parents:
35435
diff
changeset
|
891 ok = [ |
|
41971
a3261cde3a37
Fix tests for unicode-internal codec.
Walter Dörwald <walter@livinglogic.de>
parents:
41757
diff
changeset
|
892 (b"\x00\x10\xff\xff", "\U0010ffff"), |
|
a3261cde3a37
Fix tests for unicode-internal codec.
Walter Dörwald <walter@livinglogic.de>
parents:
41757
diff
changeset
|
893 (b"\x00\x00\x01\x01", "\U00000101"), |
|
a3261cde3a37
Fix tests for unicode-internal codec.
Walter Dörwald <walter@livinglogic.de>
parents:
41757
diff
changeset
|
894 (b"", ""), |
|
35484
4fc7a7edcb4b
SF bug #1251300: On UCS-4 builds the "unicode-internal" codec will now complain
Walter Dörwald <walter@livinglogic.de>
parents:
35435
diff
changeset
|
895 ] |
|
4fc7a7edcb4b
SF bug #1251300: On UCS-4 builds the "unicode-internal" codec will now complain
Walter Dörwald <walter@livinglogic.de>
parents:
35435
diff
changeset
|
896 not_ok = [ |
|
41971
a3261cde3a37
Fix tests for unicode-internal codec.
Walter Dörwald <walter@livinglogic.de>
parents:
41757
diff
changeset
|
897 b"\x7f\xff\xff\xff", |
|
a3261cde3a37
Fix tests for unicode-internal codec.
Walter Dörwald <walter@livinglogic.de>
parents:
41757
diff
changeset
|
898 b"\x80\x00\x00\x00", |
|
a3261cde3a37
Fix tests for unicode-internal codec.
Walter Dörwald <walter@livinglogic.de>
parents:
41757
diff
changeset
|
899 b"\x81\x00\x00\x00", |
|
a3261cde3a37
Fix tests for unicode-internal codec.
Walter Dörwald <walter@livinglogic.de>
parents:
41757
diff
changeset
|
900 b"\x00", |
|
a3261cde3a37
Fix tests for unicode-internal codec.
Walter Dörwald <walter@livinglogic.de>
parents:
41757
diff
changeset
|
901 b"\x00\x00\x00\x00\x00", |
|
35484
4fc7a7edcb4b
SF bug #1251300: On UCS-4 builds the "unicode-internal" codec will now complain
Walter Dörwald <walter@livinglogic.de>
parents:
35435
diff
changeset
|
902 ] |
|
4fc7a7edcb4b
SF bug #1251300: On UCS-4 builds the "unicode-internal" codec will now complain
Walter Dörwald <walter@livinglogic.de>
parents:
35435
diff
changeset
|
903 for internal, uni in ok: |
|
4fc7a7edcb4b
SF bug #1251300: On UCS-4 builds the "unicode-internal" codec will now complain
Walter Dörwald <walter@livinglogic.de>
parents:
35435
diff
changeset
|
904 if sys.byteorder == "little": |
|
41971
a3261cde3a37
Fix tests for unicode-internal codec.
Walter Dörwald <walter@livinglogic.de>
parents:
41757
diff
changeset
|
905 internal = bytes(reversed(internal)) |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
906 self.assertEqual(uni, internal.decode("unicode_internal")) |
|
35484
4fc7a7edcb4b
SF bug #1251300: On UCS-4 builds the "unicode-internal" codec will now complain
Walter Dörwald <walter@livinglogic.de>
parents:
35435
diff
changeset
|
907 for internal in not_ok: |
|
4fc7a7edcb4b
SF bug #1251300: On UCS-4 builds the "unicode-internal" codec will now complain
Walter Dörwald <walter@livinglogic.de>
parents:
35435
diff
changeset
|
908 if sys.byteorder == "little": |
|
41971
a3261cde3a37
Fix tests for unicode-internal codec.
Walter Dörwald <walter@livinglogic.de>
parents:
41757
diff
changeset
|
909 internal = bytes(reversed(internal)) |
|
35484
4fc7a7edcb4b
SF bug #1251300: On UCS-4 builds the "unicode-internal" codec will now complain
Walter Dörwald <walter@livinglogic.de>
parents:
35435
diff
changeset
|
910 self.assertRaises(UnicodeDecodeError, internal.decode, |
|
4fc7a7edcb4b
SF bug #1251300: On UCS-4 builds the "unicode-internal" codec will now complain
Walter Dörwald <walter@livinglogic.de>
parents:
35435
diff
changeset
|
911 "unicode_internal") |
|
4fc7a7edcb4b
SF bug #1251300: On UCS-4 builds the "unicode-internal" codec will now complain
Walter Dörwald <walter@livinglogic.de>
parents:
35435
diff
changeset
|
912 |
|
4fc7a7edcb4b
SF bug #1251300: On UCS-4 builds the "unicode-internal" codec will now complain
Walter Dörwald <walter@livinglogic.de>
parents:
35435
diff
changeset
|
913 def test_decode_error_attributes(self): |
|
4fc7a7edcb4b
SF bug #1251300: On UCS-4 builds the "unicode-internal" codec will now complain
Walter Dörwald <walter@livinglogic.de>
parents:
35435
diff
changeset
|
914 if sys.maxunicode > 0xffff: |
|
4fc7a7edcb4b
SF bug #1251300: On UCS-4 builds the "unicode-internal" codec will now complain
Walter Dörwald <walter@livinglogic.de>
parents:
35435
diff
changeset
|
915 try: |
|
41971
a3261cde3a37
Fix tests for unicode-internal codec.
Walter Dörwald <walter@livinglogic.de>
parents:
41757
diff
changeset
|
916 b"\x00\x00\x00\x00\x00\x11\x11\x00".decode("unicode_internal") |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
917 except UnicodeDecodeError as ex: |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
918 self.assertEqual("unicode_internal", ex.encoding) |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
919 self.assertEqual(b"\x00\x00\x00\x00\x00\x11\x11\x00", ex.object) |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
920 self.assertEqual(4, ex.start) |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
921 self.assertEqual(8, ex.end) |
|
35484
4fc7a7edcb4b
SF bug #1251300: On UCS-4 builds the "unicode-internal" codec will now complain
Walter Dörwald <walter@livinglogic.de>
parents:
35435
diff
changeset
|
922 else: |
|
4fc7a7edcb4b
SF bug #1251300: On UCS-4 builds the "unicode-internal" codec will now complain
Walter Dörwald <walter@livinglogic.de>
parents:
35435
diff
changeset
|
923 self.fail() |
|
4fc7a7edcb4b
SF bug #1251300: On UCS-4 builds the "unicode-internal" codec will now complain
Walter Dörwald <walter@livinglogic.de>
parents:
35435
diff
changeset
|
924 |
|
4fc7a7edcb4b
SF bug #1251300: On UCS-4 builds the "unicode-internal" codec will now complain
Walter Dörwald <walter@livinglogic.de>
parents:
35435
diff
changeset
|
925 def test_decode_callback(self): |
|
4fc7a7edcb4b
SF bug #1251300: On UCS-4 builds the "unicode-internal" codec will now complain
Walter Dörwald <walter@livinglogic.de>
parents:
35435
diff
changeset
|
926 if sys.maxunicode > 0xffff: |
|
4fc7a7edcb4b
SF bug #1251300: On UCS-4 builds the "unicode-internal" codec will now complain
Walter Dörwald <walter@livinglogic.de>
parents:
35435
diff
changeset
|
927 codecs.register_error("UnicodeInternalTest", codecs.ignore_errors) |
|
4fc7a7edcb4b
SF bug #1251300: On UCS-4 builds the "unicode-internal" codec will now complain
Walter Dörwald <walter@livinglogic.de>
parents:
35435
diff
changeset
|
928 decoder = codecs.getdecoder("unicode_internal") |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43577
diff
changeset
|
929 ab = "ab".encode("unicode_internal").decode() |
|
43577
ace45b361e87
Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents:
42985
diff
changeset
|
930 ignored = decoder(bytes("%s\x22\x22\x22\x22%s" % (ab[:4], ab[4:]), |
|
ace45b361e87
Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents:
42985
diff
changeset
|
931 "ascii"), |
|
ace45b361e87
Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents:
42985
diff
changeset
|
932 "UnicodeInternalTest") |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
933 self.assertEqual(("ab", 12), ignored) |
|
35484
4fc7a7edcb4b
SF bug #1251300: On UCS-4 builds the "unicode-internal" codec will now complain
Walter Dörwald <walter@livinglogic.de>
parents:
35435
diff
changeset
|
934 |
|
53859
63b23bd3904d
Merged revisions 72404-72406 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
53767
diff
changeset
|
935 def test_encode_length(self): |
|
63b23bd3904d
Merged revisions 72404-72406 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
53767
diff
changeset
|
936 # Issue 3739 |
|
63b23bd3904d
Merged revisions 72404-72406 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
53767
diff
changeset
|
937 encoder = codecs.getencoder("unicode_internal") |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
938 self.assertEqual(encoder("a")[1], 1) |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
939 self.assertEqual(encoder("\xe9\u0142")[1], 2) |
|
53859
63b23bd3904d
Merged revisions 72404-72406 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
53767
diff
changeset
|
940 |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
941 self.assertEqual(codecs.escape_encode(br'\x00')[1], 4) |
|
62215
f33e464a9a60
Merged revisions 79780 via svnmerge from
Philip Jenvey <pjenvey@underboss.org>
parents:
61863
diff
changeset
|
942 |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
943 # From http://www.gnu.org/software/libidn/draft-josefsson-idn-test-vectors.html |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
944 nameprep_tests = [ |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
945 # 3.1 Map to nothing. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
946 (b'foo\xc2\xad\xcd\x8f\xe1\xa0\x86\xe1\xa0\x8bbar' |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
947 b'\xe2\x80\x8b\xe2\x81\xa0baz\xef\xb8\x80\xef\xb8\x88\xef' |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
948 b'\xb8\x8f\xef\xbb\xbf', |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
949 b'foobarbaz'), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
950 # 3.2 Case folding ASCII U+0043 U+0041 U+0046 U+0045. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
951 (b'CAFE', |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
952 b'cafe'), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
953 # 3.3 Case folding 8bit U+00DF (german sharp s). |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
954 # The original test case is bogus; it says \xc3\xdf |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
955 (b'\xc3\x9f', |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
956 b'ss'), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
957 # 3.4 Case folding U+0130 (turkish capital I with dot). |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
958 (b'\xc4\xb0', |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
959 b'i\xcc\x87'), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
960 # 3.5 Case folding multibyte U+0143 U+037A. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
961 (b'\xc5\x83\xcd\xba', |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
962 b'\xc5\x84 \xce\xb9'), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
963 # 3.6 Case folding U+2121 U+33C6 U+1D7BB. |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
964 # XXX: skip this as it fails in UCS-2 mode |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
965 #('\xe2\x84\xa1\xe3\x8f\x86\xf0\x9d\x9e\xbb', |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
966 # 'telc\xe2\x88\x95kg\xcf\x83'), |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
967 (None, None), |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
968 # 3.7 Normalization of U+006a U+030c U+00A0 U+00AA. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
969 (b'j\xcc\x8c\xc2\xa0\xc2\xaa', |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
970 b'\xc7\xb0 a'), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
971 # 3.8 Case folding U+1FB7 and normalization. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
972 (b'\xe1\xbe\xb7', |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
973 b'\xe1\xbe\xb6\xce\xb9'), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
974 # 3.9 Self-reverting case folding U+01F0 and normalization. |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
975 # The original test case is bogus, it says `\xc7\xf0' |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
976 (b'\xc7\xb0', |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
977 b'\xc7\xb0'), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
978 # 3.10 Self-reverting case folding U+0390 and normalization. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
979 (b'\xce\x90', |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
980 b'\xce\x90'), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
981 # 3.11 Self-reverting case folding U+03B0 and normalization. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
982 (b'\xce\xb0', |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
983 b'\xce\xb0'), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
984 # 3.12 Self-reverting case folding U+1E96 and normalization. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
985 (b'\xe1\xba\x96', |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
986 b'\xe1\xba\x96'), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
987 # 3.13 Self-reverting case folding U+1F56 and normalization. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
988 (b'\xe1\xbd\x96', |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
989 b'\xe1\xbd\x96'), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
990 # 3.14 ASCII space character U+0020. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
991 (b' ', |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
992 b' '), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
993 # 3.15 Non-ASCII 8bit space character U+00A0. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
994 (b'\xc2\xa0', |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
995 b' '), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
996 # 3.16 Non-ASCII multibyte space character U+1680. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
997 (b'\xe1\x9a\x80', |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
998 None), |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
999 # 3.17 Non-ASCII multibyte space character U+2000. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1000 (b'\xe2\x80\x80', |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1001 b' '), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1002 # 3.18 Zero Width Space U+200b. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1003 (b'\xe2\x80\x8b', |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1004 b''), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1005 # 3.19 Non-ASCII multibyte space character U+3000. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1006 (b'\xe3\x80\x80', |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1007 b' '), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1008 # 3.20 ASCII control characters U+0010 U+007F. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1009 (b'\x10\x7f', |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1010 b'\x10\x7f'), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1011 # 3.21 Non-ASCII 8bit control character U+0085. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1012 (b'\xc2\x85', |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1013 None), |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1014 # 3.22 Non-ASCII multibyte control character U+180E. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1015 (b'\xe1\xa0\x8e', |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1016 None), |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1017 # 3.23 Zero Width No-Break Space U+FEFF. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1018 (b'\xef\xbb\xbf', |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1019 b''), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1020 # 3.24 Non-ASCII control character U+1D175. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1021 (b'\xf0\x9d\x85\xb5', |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1022 None), |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1023 # 3.25 Plane 0 private use character U+F123. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1024 (b'\xef\x84\xa3', |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1025 None), |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1026 # 3.26 Plane 15 private use character U+F1234. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1027 (b'\xf3\xb1\x88\xb4', |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1028 None), |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1029 # 3.27 Plane 16 private use character U+10F234. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1030 (b'\xf4\x8f\x88\xb4', |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1031 None), |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1032 # 3.28 Non-character code point U+8FFFE. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1033 (b'\xf2\x8f\xbf\xbe', |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1034 None), |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1035 # 3.29 Non-character code point U+10FFFF. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1036 (b'\xf4\x8f\xbf\xbf', |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1037 None), |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1038 # 3.30 Surrogate code U+DF42. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1039 (b'\xed\xbd\x82', |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1040 None), |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1041 # 3.31 Non-plain text character U+FFFD. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1042 (b'\xef\xbf\xbd', |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1043 None), |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1044 # 3.32 Ideographic description character U+2FF5. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1045 (b'\xe2\xbf\xb5', |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1046 None), |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1047 # 3.33 Display property character U+0341. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1048 (b'\xcd\x81', |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1049 b'\xcc\x81'), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1050 # 3.34 Left-to-right mark U+200E. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1051 (b'\xe2\x80\x8e', |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1052 None), |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1053 # 3.35 Deprecated U+202A. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1054 (b'\xe2\x80\xaa', |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1055 None), |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1056 # 3.36 Language tagging character U+E0001. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1057 (b'\xf3\xa0\x80\x81', |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1058 None), |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1059 # 3.37 Language tagging character U+E0042. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1060 (b'\xf3\xa0\x81\x82', |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1061 None), |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1062 # 3.38 Bidi: RandALCat character U+05BE and LCat characters. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1063 (b'foo\xd6\xbebar', |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1064 None), |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1065 # 3.39 Bidi: RandALCat character U+FD50 and LCat characters. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1066 (b'foo\xef\xb5\x90bar', |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1067 None), |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1068 # 3.40 Bidi: RandALCat character U+FB38 and LCat characters. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1069 (b'foo\xef\xb9\xb6bar', |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1070 b'foo \xd9\x8ebar'), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1071 # 3.41 Bidi: RandALCat without trailing RandALCat U+0627 U+0031. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1072 (b'\xd8\xa71', |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1073 None), |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1074 # 3.42 Bidi: RandALCat character U+0627 U+0031 U+0628. |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1075 (b'\xd8\xa71\xd8\xa8', |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1076 b'\xd8\xa71\xd8\xa8'), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1077 # 3.43 Unassigned code point U+E0002. |
|
28678
31cb8e0260db
Skip nameprep test 3.43, as we do allow unassigned characters. The test
Martin v. Löwis <martin@v.loewis.de>
parents:
28671
diff
changeset
|
1078 # Skip this test as we allow unassigned |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1079 #(b'\xf3\xa0\x80\x82', |
|
28678
31cb8e0260db
Skip nameprep test 3.43, as we do allow unassigned characters. The test
Martin v. Löwis <martin@v.loewis.de>
parents:
28671
diff
changeset
|
1080 # None), |
|
31cb8e0260db
Skip nameprep test 3.43, as we do allow unassigned characters. The test
Martin v. Löwis <martin@v.loewis.de>
parents:
28671
diff
changeset
|
1081 (None, None), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1082 # 3.44 Larger test (shrinking). |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1083 # Original test case reads \xc3\xdf |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1084 (b'X\xc2\xad\xc3\x9f\xc4\xb0\xe2\x84\xa1j\xcc\x8c\xc2\xa0\xc2' |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1085 b'\xaa\xce\xb0\xe2\x80\x80', |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1086 b'xssi\xcc\x87tel\xc7\xb0 a\xce\xb0 '), |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1087 # 3.45 Larger test (expanding). |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1088 # Original test case reads \xc3\x9f |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1089 (b'X\xc3\x9f\xe3\x8c\x96\xc4\xb0\xe2\x84\xa1\xe2\x92\x9f\xe3\x8c' |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1090 b'\x80', |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1091 b'xss\xe3\x82\xad\xe3\x83\xad\xe3\x83\xa1\xe3\x83\xbc\xe3' |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1092 b'\x83\x88\xe3\x83\xabi\xcc\x87tel\x28d\x29\xe3\x82' |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1093 b'\xa2\xe3\x83\x91\xe3\x83\xbc\xe3\x83\x88') |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1094 ] |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1095 |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1096 |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1097 class NameprepTest(unittest.TestCase): |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1098 def test_nameprep(self): |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1099 from encodings.idna import nameprep |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1100 for pos, (orig, prepped) in enumerate(nameprep_tests): |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1101 if orig is None: |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1102 # Skipped |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1103 continue |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1104 # The Unicode strings are given in UTF-8 |
|
53965
fbcdae3e0ca1
Rename the surrogates error handler to surrogatepass.
Martin v. Löwis <martin@v.loewis.de>
parents:
53859
diff
changeset
|
1105 orig = str(orig, "utf-8", "surrogatepass") |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1106 if prepped is None: |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1107 # Input contains prohibited characters |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1108 self.assertRaises(UnicodeError, nameprep, orig) |
|
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1109 else: |
|
53965
fbcdae3e0ca1
Rename the surrogates error handler to surrogatepass.
Martin v. Löwis <martin@v.loewis.de>
parents:
53859
diff
changeset
|
1110 prepped = str(prepped, "utf-8", "surrogatepass") |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1111 try: |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1112 self.assertEqual(nameprep(orig), prepped) |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
1113 except Exception as e: |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
43933
diff
changeset
|
1114 raise support.TestFailed("Test 3.%d: %s" % (pos+1, str(e))) |
|
28671
d41d8de9768f
Implement IDNA (Internationalized Domain Names in Applications).
Martin v. Löwis <martin@v.loewis.de>
parents:
27680
diff
changeset
|
1115 |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1116 class IDNACodecTest(unittest.TestCase): |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1117 def test_builtin_decode(self): |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1118 self.assertEqual(str(b"python.org", "idna"), "python.org") |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1119 self.assertEqual(str(b"python.org.", "idna"), "python.org.") |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1120 self.assertEqual(str(b"xn--pythn-mua.org", "idna"), "pyth\xf6n.org") |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1121 self.assertEqual(str(b"xn--pythn-mua.org.", "idna"), "pyth\xf6n.org.") |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1122 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1123 def test_builtin_encode(self): |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1124 self.assertEqual("python.org".encode("idna"), b"python.org") |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1125 self.assertEqual("python.org.".encode("idna"), b"python.org.") |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1126 self.assertEqual("pyth\xf6n.org".encode("idna"), b"xn--pythn-mua.org") |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1127 self.assertEqual("pyth\xf6n.org.".encode("idna"), b"xn--pythn-mua.org.") |
|
31795
bf37429f6d9f
Add test case for unicode(somestring, "idna").
Martin v. Löwis <martin@v.loewis.de>
parents:
28917
diff
changeset
|
1128 |
|
35435
9d343bc81863
Make IDNA return an empty string when the input is empty. Fixes #1163178.
Martin v. Löwis <martin@v.loewis.de>
parents:
35269
diff
changeset
|
1129 def test_stream(self): |
|
41681
1d334912a104
Use io.BytesIO instead of StringIO.StringIO in all
Walter Dörwald <walter@livinglogic.de>
parents:
41679
diff
changeset
|
1130 r = codecs.getreader("idna")(io.BytesIO(b"abc")) |
|
35435
9d343bc81863
Make IDNA return an empty string when the input is empty. Fixes #1163178.
Martin v. Löwis <martin@v.loewis.de>
parents:
35269
diff
changeset
|
1131 r.read(3) |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1132 self.assertEqual(r.read(), "") |
|
35435
9d343bc81863
Make IDNA return an empty string when the input is empty. Fixes #1163178.
Martin v. Löwis <martin@v.loewis.de>
parents:
35269
diff
changeset
|
1133 |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1134 def test_incremental_decode(self): |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1135 self.assertEqual( |
|
42911
cb28e14ee808
Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents:
42626
diff
changeset
|
1136 "".join(codecs.iterdecode((bytes([c]) for c in b"python.org"), "idna")), |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
1137 "python.org" |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1138 ) |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1139 self.assertEqual( |
|
42911
cb28e14ee808
Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents:
42626
diff
changeset
|
1140 "".join(codecs.iterdecode((bytes([c]) for c in b"python.org."), "idna")), |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
1141 "python.org." |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1142 ) |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1143 self.assertEqual( |
|
42911
cb28e14ee808
Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents:
42626
diff
changeset
|
1144 "".join(codecs.iterdecode((bytes([c]) for c in b"xn--pythn-mua.org."), "idna")), |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
1145 "pyth\xf6n.org." |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1146 ) |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1147 self.assertEqual( |
|
42911
cb28e14ee808
Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents:
42626
diff
changeset
|
1148 "".join(codecs.iterdecode((bytes([c]) for c in b"xn--pythn-mua.org."), "idna")), |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
1149 "pyth\xf6n.org." |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1150 ) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1151 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1152 decoder = codecs.getincrementaldecoder("idna")() |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1153 self.assertEqual(decoder.decode(b"xn--xam", ), "") |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1154 self.assertEqual(decoder.decode(b"ple-9ta.o", ), "\xe4xample.") |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1155 self.assertEqual(decoder.decode(b"rg"), "") |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1156 self.assertEqual(decoder.decode(b"", True), "org") |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1157 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1158 decoder.reset() |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1159 self.assertEqual(decoder.decode(b"xn--xam", ), "") |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1160 self.assertEqual(decoder.decode(b"ple-9ta.o", ), "\xe4xample.") |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1161 self.assertEqual(decoder.decode(b"rg."), "org.") |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1162 self.assertEqual(decoder.decode(b"", True), "") |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1163 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1164 def test_incremental_encode(self): |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1165 self.assertEqual( |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1166 b"".join(codecs.iterencode("python.org", "idna")), |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1167 b"python.org" |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1168 ) |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1169 self.assertEqual( |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1170 b"".join(codecs.iterencode("python.org.", "idna")), |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1171 b"python.org." |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1172 ) |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1173 self.assertEqual( |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1174 b"".join(codecs.iterencode("pyth\xf6n.org.", "idna")), |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1175 b"xn--pythn-mua.org." |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1176 ) |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1177 self.assertEqual( |
|
41693
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1178 b"".join(codecs.iterencode("pyth\xf6n.org.", "idna")), |
|
d91cc8d49294
Enhance the punycode decoder so that it can decode
Walter Dörwald <walter@livinglogic.de>
parents:
41681
diff
changeset
|
1179 b"xn--pythn-mua.org." |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1180 ) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1181 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1182 encoder = codecs.getincrementalencoder("idna")() |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1183 self.assertEqual(encoder.encode("\xe4x"), b"") |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1184 self.assertEqual(encoder.encode("ample.org"), b"xn--xample-9ta.") |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1185 self.assertEqual(encoder.encode("", True), b"org") |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1186 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1187 encoder.reset() |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1188 self.assertEqual(encoder.encode("\xe4x"), b"") |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1189 self.assertEqual(encoder.encode("ample.org."), b"xn--xample-9ta.org.") |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1190 self.assertEqual(encoder.encode("", True), b"") |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1191 |
|
32572
8ea2cb1ec598
Add generic codecs.encode() and .decode() APIs that don't impose
Marc-André Lemburg <mal@egenix.com>
parents:
32538
diff
changeset
|
1192 class CodecsModuleTest(unittest.TestCase): |
|
8ea2cb1ec598
Add generic codecs.encode() and .decode() APIs that don't impose
Marc-André Lemburg <mal@egenix.com>
parents:
32538
diff
changeset
|
1193 |
|
8ea2cb1ec598
Add generic codecs.encode() and .decode() APIs that don't impose
Marc-André Lemburg <mal@egenix.com>
parents:
32538
diff
changeset
|
1194 def test_decode(self): |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1195 self.assertEqual(codecs.decode(b'\xe4\xf6\xfc', 'latin-1'), |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1196 '\xe4\xf6\xfc') |
|
33855
1cd19daaa9d4
Trigger a few error cases in Modules/_codecsmodule.c.
Walter Dörwald <walter@livinglogic.de>
parents:
33782
diff
changeset
|
1197 self.assertRaises(TypeError, codecs.decode) |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1198 self.assertEqual(codecs.decode(b'abc'), 'abc') |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
1199 self.assertRaises(UnicodeDecodeError, codecs.decode, b'\xff', 'ascii') |
|
33855
1cd19daaa9d4
Trigger a few error cases in Modules/_codecsmodule.c.
Walter Dörwald <walter@livinglogic.de>
parents:
33782
diff
changeset
|
1200 |
|
32572
8ea2cb1ec598
Add generic codecs.encode() and .decode() APIs that don't impose
Marc-André Lemburg <mal@egenix.com>
parents:
32538
diff
changeset
|
1201 def test_encode(self): |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1202 self.assertEqual(codecs.encode('\xe4\xf6\xfc', 'latin-1'), |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1203 b'\xe4\xf6\xfc') |
|
33855
1cd19daaa9d4
Trigger a few error cases in Modules/_codecsmodule.c.
Walter Dörwald <walter@livinglogic.de>
parents:
33782
diff
changeset
|
1204 self.assertRaises(TypeError, codecs.encode) |
|
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
|
1205 self.assertRaises(LookupError, codecs.encode, "foo", "__spam__") |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1206 self.assertEqual(codecs.encode('abc'), b'abc') |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
1207 self.assertRaises(UnicodeEncodeError, codecs.encode, '\xffff', 'ascii') |
|
33855
1cd19daaa9d4
Trigger a few error cases in Modules/_codecsmodule.c.
Walter Dörwald <walter@livinglogic.de>
parents:
33782
diff
changeset
|
1208 |
|
1cd19daaa9d4
Trigger a few error cases in Modules/_codecsmodule.c.
Walter Dörwald <walter@livinglogic.de>
parents:
33782
diff
changeset
|
1209 def test_register(self): |
|
1cd19daaa9d4
Trigger a few error cases in Modules/_codecsmodule.c.
Walter Dörwald <walter@livinglogic.de>
parents:
33782
diff
changeset
|
1210 self.assertRaises(TypeError, codecs.register) |
|
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
|
1211 self.assertRaises(TypeError, codecs.register, 42) |
|
33855
1cd19daaa9d4
Trigger a few error cases in Modules/_codecsmodule.c.
Walter Dörwald <walter@livinglogic.de>
parents:
33782
diff
changeset
|
1212 |
|
1cd19daaa9d4
Trigger a few error cases in Modules/_codecsmodule.c.
Walter Dörwald <walter@livinglogic.de>
parents:
33782
diff
changeset
|
1213 def test_lookup(self): |
|
1cd19daaa9d4
Trigger a few error cases in Modules/_codecsmodule.c.
Walter Dörwald <walter@livinglogic.de>
parents:
33782
diff
changeset
|
1214 self.assertRaises(TypeError, codecs.lookup) |
|
1cd19daaa9d4
Trigger a few error cases in Modules/_codecsmodule.c.
Walter Dörwald <walter@livinglogic.de>
parents:
33782
diff
changeset
|
1215 self.assertRaises(LookupError, codecs.lookup, "__spam__") |
|
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
|
1216 self.assertRaises(LookupError, codecs.lookup, " ") |
|
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
|
1217 |
|
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
|
1218 def test_getencoder(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
|
1219 self.assertRaises(TypeError, codecs.getencoder) |
|
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
|
1220 self.assertRaises(LookupError, codecs.getencoder, "__spam__") |
|
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
|
1221 |
|
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
|
1222 def test_getdecoder(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
|
1223 self.assertRaises(TypeError, codecs.getdecoder) |
|
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
|
1224 self.assertRaises(LookupError, codecs.getdecoder, "__spam__") |
|
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
|
1225 |
|
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
|
1226 def test_getreader(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
|
1227 self.assertRaises(TypeError, codecs.getreader) |
|
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
|
1228 self.assertRaises(LookupError, codecs.getreader, "__spam__") |
|
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
|
1229 |
|
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
|
1230 def test_getwriter(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
|
1231 self.assertRaises(TypeError, codecs.getwriter) |
|
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
|
1232 self.assertRaises(LookupError, codecs.getwriter, "__spam__") |
|
32572
8ea2cb1ec598
Add generic codecs.encode() and .decode() APIs that don't impose
Marc-André Lemburg <mal@egenix.com>
parents:
32538
diff
changeset
|
1233 |
|
71489
92d02de91cc9
Issue #1813: Fix codec lookup under Turkish locales.
Antoine Pitrou <solipsis@pitrou.net>
parents:
70293
diff
changeset
|
1234 def test_lookup_issue1813(self): |
|
92d02de91cc9
Issue #1813: Fix codec lookup under Turkish locales.
Antoine Pitrou <solipsis@pitrou.net>
parents:
70293
diff
changeset
|
1235 # Issue #1813: under Turkish locales, lookup of some codecs failed |
|
92d02de91cc9
Issue #1813: Fix codec lookup under Turkish locales.
Antoine Pitrou <solipsis@pitrou.net>
parents:
70293
diff
changeset
|
1236 # because 'I' is lowercased as "ı" (dotless i) |
|
71511
7063aa2fa127
Backport 0398f07d4827 (fix for weird buildbot failures)
Antoine Pitrou <solipsis@pitrou.net>
parents:
71489
diff
changeset
|
1237 oldlocale = locale.setlocale(locale.LC_CTYPE) |
|
71489
92d02de91cc9
Issue #1813: Fix codec lookup under Turkish locales.
Antoine Pitrou <solipsis@pitrou.net>
parents:
70293
diff
changeset
|
1238 self.addCleanup(locale.setlocale, locale.LC_CTYPE, oldlocale) |
|
92d02de91cc9
Issue #1813: Fix codec lookup under Turkish locales.
Antoine Pitrou <solipsis@pitrou.net>
parents:
70293
diff
changeset
|
1239 try: |
|
92d02de91cc9
Issue #1813: Fix codec lookup under Turkish locales.
Antoine Pitrou <solipsis@pitrou.net>
parents:
70293
diff
changeset
|
1240 locale.setlocale(locale.LC_CTYPE, 'tr_TR') |
|
92d02de91cc9
Issue #1813: Fix codec lookup under Turkish locales.
Antoine Pitrou <solipsis@pitrou.net>
parents:
70293
diff
changeset
|
1241 except locale.Error: |
|
92d02de91cc9
Issue #1813: Fix codec lookup under Turkish locales.
Antoine Pitrou <solipsis@pitrou.net>
parents:
70293
diff
changeset
|
1242 # Unsupported locale on this system |
|
92d02de91cc9
Issue #1813: Fix codec lookup under Turkish locales.
Antoine Pitrou <solipsis@pitrou.net>
parents:
70293
diff
changeset
|
1243 self.skipTest('test needs Turkish locale') |
|
92d02de91cc9
Issue #1813: Fix codec lookup under Turkish locales.
Antoine Pitrou <solipsis@pitrou.net>
parents:
70293
diff
changeset
|
1244 c = codecs.lookup('ASCII') |
|
92d02de91cc9
Issue #1813: Fix codec lookup under Turkish locales.
Antoine Pitrou <solipsis@pitrou.net>
parents:
70293
diff
changeset
|
1245 self.assertEqual(c.name, 'ascii') |
|
92d02de91cc9
Issue #1813: Fix codec lookup under Turkish locales.
Antoine Pitrou <solipsis@pitrou.net>
parents:
70293
diff
changeset
|
1246 |
|
33782
a52de911b284
SF #1048865: Fix a trivial typo that breaks StreamReader.readlines()
Hye-Shik Chang <hyeshik@gmail.com>
parents:
33428
diff
changeset
|
1247 class StreamReaderTest(unittest.TestCase): |
|
a52de911b284
SF #1048865: Fix a trivial typo that breaks StreamReader.readlines()
Hye-Shik Chang <hyeshik@gmail.com>
parents:
33428
diff
changeset
|
1248 |
|
a52de911b284
SF #1048865: Fix a trivial typo that breaks StreamReader.readlines()
Hye-Shik Chang <hyeshik@gmail.com>
parents:
33428
diff
changeset
|
1249 def setUp(self): |
|
a52de911b284
SF #1048865: Fix a trivial typo that breaks StreamReader.readlines()
Hye-Shik Chang <hyeshik@gmail.com>
parents:
33428
diff
changeset
|
1250 self.reader = codecs.getreader('utf-8') |
|
41681
1d334912a104
Use io.BytesIO instead of StringIO.StringIO in all
Walter Dörwald <walter@livinglogic.de>
parents:
41679
diff
changeset
|
1251 self.stream = io.BytesIO(b'\xed\x95\x9c\n\xea\xb8\x80') |
|
33782
a52de911b284
SF #1048865: Fix a trivial typo that breaks StreamReader.readlines()
Hye-Shik Chang <hyeshik@gmail.com>
parents:
33428
diff
changeset
|
1252 |
|
a52de911b284
SF #1048865: Fix a trivial typo that breaks StreamReader.readlines()
Hye-Shik Chang <hyeshik@gmail.com>
parents:
33428
diff
changeset
|
1253 def test_readlines(self): |
|
a52de911b284
SF #1048865: Fix a trivial typo that breaks StreamReader.readlines()
Hye-Shik Chang <hyeshik@gmail.com>
parents:
33428
diff
changeset
|
1254 f = self.reader(self.stream) |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1255 self.assertEqual(f.readlines(), ['\ud55c\n', '\uae00']) |
|
33782
a52de911b284
SF #1048865: Fix a trivial typo that breaks StreamReader.readlines()
Hye-Shik Chang <hyeshik@gmail.com>
parents:
33428
diff
changeset
|
1256 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1257 class EncodedFileTest(unittest.TestCase): |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1258 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1259 def test_basic(self): |
|
41681
1d334912a104
Use io.BytesIO instead of StringIO.StringIO in all
Walter Dörwald <walter@livinglogic.de>
parents:
41679
diff
changeset
|
1260 f = io.BytesIO(b'\xed\x95\x9c\n\xea\xb8\x80') |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1261 ef = codecs.EncodedFile(f, 'utf-16-le', 'utf-8') |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1262 self.assertEqual(ef.read(), b'\\\xd5\n\x00\x00\xae') |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1263 |
|
41681
1d334912a104
Use io.BytesIO instead of StringIO.StringIO in all
Walter Dörwald <walter@livinglogic.de>
parents:
41679
diff
changeset
|
1264 f = io.BytesIO() |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1265 ef = codecs.EncodedFile(f, 'utf-8', 'latin1') |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
1266 ef.write(b'\xc3\xbc') |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1267 self.assertEqual(f.getvalue(), b'\xfc') |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1268 |
|
34279
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1269 all_unicode_encodings = [ |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1270 "ascii", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1271 "big5", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1272 "big5hkscs", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1273 "charmap", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1274 "cp037", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1275 "cp1006", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1276 "cp1026", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1277 "cp1140", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1278 "cp1250", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1279 "cp1251", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1280 "cp1252", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1281 "cp1253", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1282 "cp1254", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1283 "cp1255", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1284 "cp1256", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1285 "cp1257", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1286 "cp1258", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1287 "cp424", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1288 "cp437", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1289 "cp500", |
|
62572
1d1797af4622
Merged revisions 81499,81506 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
62367
diff
changeset
|
1290 "cp720", |
|
34279
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1291 "cp737", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1292 "cp775", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1293 "cp850", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1294 "cp852", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1295 "cp855", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1296 "cp856", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1297 "cp857", |
|
62572
1d1797af4622
Merged revisions 81499,81506 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
62367
diff
changeset
|
1298 "cp858", |
|
34279
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1299 "cp860", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1300 "cp861", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1301 "cp862", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1302 "cp863", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1303 "cp864", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1304 "cp865", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1305 "cp866", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1306 "cp869", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1307 "cp874", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1308 "cp875", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1309 "cp932", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1310 "cp949", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1311 "cp950", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1312 "euc_jis_2004", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1313 "euc_jisx0213", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1314 "euc_jp", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1315 "euc_kr", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1316 "gb18030", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1317 "gb2312", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1318 "gbk", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1319 "hp_roman8", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1320 "hz", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1321 "idna", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1322 "iso2022_jp", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1323 "iso2022_jp_1", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1324 "iso2022_jp_2", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1325 "iso2022_jp_2004", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1326 "iso2022_jp_3", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1327 "iso2022_jp_ext", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1328 "iso2022_kr", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1329 "iso8859_1", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1330 "iso8859_10", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1331 "iso8859_11", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1332 "iso8859_13", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1333 "iso8859_14", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1334 "iso8859_15", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1335 "iso8859_16", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1336 "iso8859_2", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1337 "iso8859_3", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1338 "iso8859_4", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1339 "iso8859_5", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1340 "iso8859_6", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1341 "iso8859_7", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1342 "iso8859_8", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1343 "iso8859_9", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1344 "johab", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1345 "koi8_r", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1346 "koi8_u", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1347 "latin_1", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1348 "mac_cyrillic", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1349 "mac_greek", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1350 "mac_iceland", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1351 "mac_latin2", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1352 "mac_roman", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1353 "mac_turkish", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1354 "palmos", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1355 "ptcp154", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1356 "punycode", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1357 "raw_unicode_escape", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1358 "shift_jis", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1359 "shift_jis_2004", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1360 "shift_jisx0213", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1361 "tis_620", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1362 "unicode_escape", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1363 "unicode_internal", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1364 "utf_16", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1365 "utf_16_be", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1366 "utf_16_le", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1367 "utf_7", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1368 "utf_8", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1369 ] |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1370 |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1371 if hasattr(codecs, "mbcs_encode"): |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1372 all_unicode_encodings.append("mbcs") |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1373 |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1374 # The following encoding is not tested, because it's not supposed |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1375 # to work: |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1376 # "undefined" |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1377 |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1378 # The following encodings don't work in stateful mode |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1379 broken_unicode_with_streams = [ |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1380 "punycode", |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1381 "unicode_internal" |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1382 ] |
|
41473
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
1383 broken_incremental_coders = broken_unicode_with_streams + [ |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
1384 "idna", |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
1385 ] |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1386 |
|
41473
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
1387 class BasicUnicodeTest(unittest.TestCase, MixInCheckStateHandling): |
|
34279
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1388 def test_basics(self): |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
1389 s = "abc123" # all codecs should be able to encode these |
|
34279
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1390 for encoding in all_unicode_encodings: |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
1391 name = codecs.lookup(encoding).name |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
1392 if encoding.endswith("_codec"): |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
1393 name += "_codec" |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
1394 elif encoding == "latin_1": |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
1395 name = "latin_1" |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
1396 self.assertEqual(encoding.replace("_", "-"), name.replace("_", "-")) |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
1397 (b, size) = codecs.getencoder(encoding)(s) |
|
53859
63b23bd3904d
Merged revisions 72404-72406 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents:
53767
diff
changeset
|
1398 self.assertEqual(size, len(s), "%r != %r (encoding=%r)" % (size, len(s), encoding)) |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
1399 (chars, size) = codecs.getdecoder(encoding)(b) |
|
34279
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1400 self.assertEqual(chars, s, "%r != %r (encoding=%r)" % (chars, s, encoding)) |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1401 |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1402 if encoding not in broken_unicode_with_streams: |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1403 # check stream reader/writer |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
1404 q = Queue(b"") |
|
34279
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1405 writer = codecs.getwriter(encoding)(q) |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
1406 encodedresult = b"" |
|
34279
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1407 for c in s: |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1408 writer.write(c) |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43577
diff
changeset
|
1409 chunk = q.read() |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54988
diff
changeset
|
1410 self.assertTrue(type(chunk) is bytes, type(chunk)) |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43577
diff
changeset
|
1411 encodedresult += chunk |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
1412 q = Queue(b"") |
|
34279
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1413 reader = codecs.getreader(encoding)(q) |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
1414 decodedresult = "" |
|
34279
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1415 for c in encodedresult: |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
1416 q.write(bytes([c])) |
|
34279
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1417 decodedresult += reader.read() |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1418 self.assertEqual(decodedresult, s, "%r != %r (encoding=%r)" % (decodedresult, s, encoding)) |
|
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1419 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1420 if encoding not in broken_incremental_coders: |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1421 # check incremental decoder/encoder (fetched via the Python |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1422 # and C API) and iterencode()/iterdecode() |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
1423 try: |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
1424 encoder = codecs.getincrementalencoder(encoding)() |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1425 cencoder = _testcapi.codec_incrementalencoder(encoding) |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
1426 except LookupError: # no IncrementalEncoder |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
1427 pass |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
1428 else: |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
1429 # check incremental decoder/encoder |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
1430 encodedresult = b"" |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
1431 for c in s: |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
1432 encodedresult += encoder.encode(c) |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
1433 encodedresult += encoder.encode("", True) |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
1434 decoder = codecs.getincrementaldecoder(encoding)() |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
1435 decodedresult = "" |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
1436 for c in encodedresult: |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
1437 decodedresult += decoder.decode(bytes([c])) |
|
41757
13a6c6bef609
Make test_codecs work. The CJK codecs now use bytes instead of str8 for
Guido van Rossum <guido@python.org>
parents:
41750
diff
changeset
|
1438 decodedresult += decoder.decode(b"", True) |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1439 self.assertEqual(decodedresult, s, "%r != %r (encoding=%r)" % (decodedresult, s, encoding)) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1440 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1441 # check C API |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
1442 encodedresult = b"" |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1443 for c in s: |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1444 encodedresult += cencoder.encode(c) |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
1445 encodedresult += cencoder.encode("", True) |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1446 cdecoder = _testcapi.codec_incrementaldecoder(encoding) |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
1447 decodedresult = "" |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1448 for c in encodedresult: |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
1449 decodedresult += cdecoder.decode(bytes([c])) |
|
41757
13a6c6bef609
Make test_codecs work. The CJK codecs now use bytes instead of str8 for
Guido van Rossum <guido@python.org>
parents:
41750
diff
changeset
|
1450 decodedresult += cdecoder.decode(b"", True) |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
1451 self.assertEqual(decodedresult, s, "%r != %r (encoding=%r)" % (decodedresult, s, encoding)) |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
1452 |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
1453 # check iterencode()/iterdecode() |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
1454 result = "".join(codecs.iterdecode(codecs.iterencode(s, encoding), encoding)) |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
1455 self.assertEqual(result, s, "%r != %r (encoding=%r)" % (result, s, encoding)) |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
1456 |
|
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
1457 # check iterencode()/iterdecode() with empty string |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
1458 result = "".join(codecs.iterdecode(codecs.iterencode("", encoding), encoding)) |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
1459 self.assertEqual(result, "") |
|
37875
a4c70f3968be
Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
36855
diff
changeset
|
1460 |
|
62367
283d9472df4e
Issue #850997: mbcs encoding (Windows only) handles errors argument: strict
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62256
diff
changeset
|
1461 if encoding not in ("idna", "mbcs"): |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1462 # check incremental decoder/encoder with errors argument |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1463 try: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1464 encoder = codecs.getincrementalencoder(encoding)("ignore") |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1465 cencoder = _testcapi.codec_incrementalencoder(encoding, "ignore") |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1466 except LookupError: # no IncrementalEncoder |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1467 pass |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1468 else: |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
1469 encodedresult = b"".join(encoder.encode(c) for c in s) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1470 decoder = codecs.getincrementaldecoder(encoding)("ignore") |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
1471 decodedresult = "".join(decoder.decode(bytes([c])) for c in encodedresult) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1472 self.assertEqual(decodedresult, s, "%r != %r (encoding=%r)" % (decodedresult, s, encoding)) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1473 |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
1474 encodedresult = b"".join(cencoder.encode(c) for c in s) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1475 cdecoder = _testcapi.codec_incrementaldecoder(encoding, "ignore") |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
1476 decodedresult = "".join(cdecoder.decode(bytes([c])) for c in encodedresult) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1477 self.assertEqual(decodedresult, s, "%r != %r (encoding=%r)" % (decodedresult, s, encoding)) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1478 |
|
34750
679c16115613
Reset internal buffers when seek() is called. This fixes SF bug #1156259.
Walter Dörwald <walter@livinglogic.de>
parents:
34562
diff
changeset
|
1479 def test_seek(self): |
|
679c16115613
Reset internal buffers when seek() is called. This fixes SF bug #1156259.
Walter Dörwald <walter@livinglogic.de>
parents:
34562
diff
changeset
|
1480 # all codecs should be able to encode these |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
1481 s = "%s\n%s\n" % (100*"abc123", 100*"def456") |
|
34750
679c16115613
Reset internal buffers when seek() is called. This fixes SF bug #1156259.
Walter Dörwald <walter@livinglogic.de>
parents:
34562
diff
changeset
|
1482 for encoding in all_unicode_encodings: |
|
679c16115613
Reset internal buffers when seek() is called. This fixes SF bug #1156259.
Walter Dörwald <walter@livinglogic.de>
parents:
34562
diff
changeset
|
1483 if encoding == "idna": # FIXME: See SF bug #1163178 |
|
679c16115613
Reset internal buffers when seek() is called. This fixes SF bug #1156259.
Walter Dörwald <walter@livinglogic.de>
parents:
34562
diff
changeset
|
1484 continue |
|
679c16115613
Reset internal buffers when seek() is called. This fixes SF bug #1156259.
Walter Dörwald <walter@livinglogic.de>
parents:
34562
diff
changeset
|
1485 if encoding in broken_unicode_with_streams: |
|
679c16115613
Reset internal buffers when seek() is called. This fixes SF bug #1156259.
Walter Dörwald <walter@livinglogic.de>
parents:
34562
diff
changeset
|
1486 continue |
|
41681
1d334912a104
Use io.BytesIO instead of StringIO.StringIO in all
Walter Dörwald <walter@livinglogic.de>
parents:
41679
diff
changeset
|
1487 reader = codecs.getreader(encoding)(io.BytesIO(s.encode(encoding))) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41606
diff
changeset
|
1488 for t in range(5): |
|
34750
679c16115613
Reset internal buffers when seek() is called. This fixes SF bug #1156259.
Walter Dörwald <walter@livinglogic.de>
parents:
34562
diff
changeset
|
1489 # Test that calling seek resets the internal codec state and buffers |
|
679c16115613
Reset internal buffers when seek() is called. This fixes SF bug #1156259.
Walter Dörwald <walter@livinglogic.de>
parents:
34562
diff
changeset
|
1490 reader.seek(0, 0) |
|
41757
13a6c6bef609
Make test_codecs work. The CJK codecs now use bytes instead of str8 for
Guido van Rossum <guido@python.org>
parents:
41750
diff
changeset
|
1491 data = reader.read() |
|
13a6c6bef609
Make test_codecs work. The CJK codecs now use bytes instead of str8 for
Guido van Rossum <guido@python.org>
parents:
41750
diff
changeset
|
1492 self.assertEqual(s, data) |
|
34750
679c16115613
Reset internal buffers when seek() is called. This fixes SF bug #1156259.
Walter Dörwald <walter@livinglogic.de>
parents:
34562
diff
changeset
|
1493 |
|
35882
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
1494 def test_bad_decode_args(self): |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
1495 for encoding in all_unicode_encodings: |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
1496 decoder = codecs.getdecoder(encoding) |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
1497 self.assertRaises(TypeError, decoder) |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
1498 if encoding not in ("idna", "punycode"): |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
1499 self.assertRaises(TypeError, decoder, 42) |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
1500 |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
1501 def test_bad_encode_args(self): |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
1502 for encoding in all_unicode_encodings: |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
1503 encoder = codecs.getencoder(encoding) |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
1504 self.assertRaises(TypeError, encoder) |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
1505 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
1506 def test_encoding_map_type_initialized(self): |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
1507 from encodings import cp1140 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
1508 # This used to crash, we are only verifying there's no crash. |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
1509 table_type = type(cp1140.encoding_table) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
1510 self.assertEqual(table_type, table_type) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
1511 |
|
41473
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
1512 def test_decoder_state(self): |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
1513 # Check that getstate() and setstate() handle the state properly |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
1514 u = "abc123" |
|
41473
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
1515 for encoding in all_unicode_encodings: |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
1516 if encoding not in broken_incremental_coders: |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
1517 self.check_state_handling_decode(encoding, u, u.encode(encoding)) |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
1518 self.check_state_handling_encode(encoding, u, u.encode(encoding)) |
|
a49859f44c43
Apply SF patch #1698994: Add getstate() and setstate()
Walter Dörwald <walter@livinglogic.de>
parents:
40775
diff
changeset
|
1519 |
|
35702
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35484
diff
changeset
|
1520 class CharmapTest(unittest.TestCase): |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35484
diff
changeset
|
1521 def test_decode_with_string_map(self): |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1522 self.assertEqual( |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
1523 codecs.charmap_decode(b"\x00\x01\x02", "strict", "abc"), |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
1524 ("abc", 3) |
|
35702
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35484
diff
changeset
|
1525 ) |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35484
diff
changeset
|
1526 |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1527 self.assertEqual( |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
1528 codecs.charmap_decode(b"\x00\x01\x02", "replace", "ab"), |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
1529 ("ab\ufffd", 3) |
|
35702
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35484
diff
changeset
|
1530 ) |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35484
diff
changeset
|
1531 |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1532 self.assertEqual( |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
1533 codecs.charmap_decode(b"\x00\x01\x02", "replace", "ab\ufffe"), |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
1534 ("ab\ufffd", 3) |
|
35702
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35484
diff
changeset
|
1535 ) |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35484
diff
changeset
|
1536 |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1537 self.assertEqual( |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
1538 codecs.charmap_decode(b"\x00\x01\x02", "ignore", "ab"), |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
1539 ("ab", 3) |
|
35702
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35484
diff
changeset
|
1540 ) |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35484
diff
changeset
|
1541 |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1542 self.assertEqual( |
|
41606
a3e3001b35bf
Make the BOM constants in codecs.py bytes.
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
1543 codecs.charmap_decode(b"\x00\x01\x02", "ignore", "ab\ufffe"), |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
1544 ("ab", 3) |
|
35702
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35484
diff
changeset
|
1545 ) |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35484
diff
changeset
|
1546 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41606
diff
changeset
|
1547 allbytes = bytes(range(256)) |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1548 self.assertEqual( |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
1549 codecs.charmap_decode(allbytes, "ignore", ""), |
|
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41473
diff
changeset
|
1550 ("", len(allbytes)) |
|
35702
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35484
diff
changeset
|
1551 ) |
|
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35484
diff
changeset
|
1552 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1553 class WithStmtTest(unittest.TestCase): |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1554 def test_encodedfile(self): |
|
41681
1d334912a104
Use io.BytesIO instead of StringIO.StringIO in all
Walter Dörwald <walter@livinglogic.de>
parents:
41679
diff
changeset
|
1555 f = io.BytesIO(b"\xc3\xbc") |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1556 with codecs.EncodedFile(f, "latin-1", "utf-8") as ef: |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1557 self.assertEqual(ef.read(), b"\xfc") |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1558 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1559 def test_streamreaderwriter(self): |
|
41681
1d334912a104
Use io.BytesIO instead of StringIO.StringIO in all
Walter Dörwald <walter@livinglogic.de>
parents:
41679
diff
changeset
|
1560 f = io.BytesIO(b"\xc3\xbc") |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1561 info = codecs.lookup("utf-8") |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1562 with codecs.StreamReaderWriter(f, info.streamreader, |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1563 info.streamwriter, 'strict') as srw: |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1564 self.assertEqual(srw.read(), "\xfc") |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1565 |
|
50975
ec6f29608bf0
Merged revisions 68855 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
1566 class TypesTest(unittest.TestCase): |
|
ec6f29608bf0
Merged revisions 68855 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
1567 def test_decode_unicode(self): |
|
ec6f29608bf0
Merged revisions 68855 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
1568 # Most decoders don't accept unicode input |
|
ec6f29608bf0
Merged revisions 68855 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
1569 decoders = [ |
|
ec6f29608bf0
Merged revisions 68855 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
1570 codecs.utf_7_decode, |
|
ec6f29608bf0
Merged revisions 68855 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
1571 codecs.utf_8_decode, |
|
ec6f29608bf0
Merged revisions 68855 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
1572 codecs.utf_16_le_decode, |
|
ec6f29608bf0
Merged revisions 68855 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
1573 codecs.utf_16_be_decode, |
|
ec6f29608bf0
Merged revisions 68855 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
1574 codecs.utf_16_ex_decode, |
|
ec6f29608bf0
Merged revisions 68855 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
1575 codecs.utf_32_decode, |
|
ec6f29608bf0
Merged revisions 68855 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
1576 codecs.utf_32_le_decode, |
|
ec6f29608bf0
Merged revisions 68855 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
1577 codecs.utf_32_be_decode, |
|
ec6f29608bf0
Merged revisions 68855 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
1578 codecs.utf_32_ex_decode, |
|
ec6f29608bf0
Merged revisions 68855 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
1579 codecs.latin_1_decode, |
|
ec6f29608bf0
Merged revisions 68855 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
1580 codecs.ascii_decode, |
|
ec6f29608bf0
Merged revisions 68855 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
1581 codecs.charmap_decode, |
|
ec6f29608bf0
Merged revisions 68855 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
1582 ] |
|
ec6f29608bf0
Merged revisions 68855 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
1583 if hasattr(codecs, "mbcs_decode"): |
|
ec6f29608bf0
Merged revisions 68855 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
1584 decoders.append(codecs.mbcs_decode) |
|
ec6f29608bf0
Merged revisions 68855 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
1585 for decoder in decoders: |
|
ec6f29608bf0
Merged revisions 68855 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
1586 self.assertRaises(TypeError, decoder, "xxx") |
|
ec6f29608bf0
Merged revisions 68855 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
1587 |
|
ec6f29608bf0
Merged revisions 68855 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
1588 def test_unicode_escape(self): |
|
ec6f29608bf0
Merged revisions 68855 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
1589 # Escape-decoding an unicode string is supported ang gives the same |
|
ec6f29608bf0
Merged revisions 68855 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
1590 # result as decoding the equivalent ASCII bytes string. |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1591 self.assertEqual(codecs.unicode_escape_decode(r"\u1234"), ("\u1234", 6)) |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1592 self.assertEqual(codecs.unicode_escape_decode(br"\u1234"), ("\u1234", 6)) |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1593 self.assertEqual(codecs.raw_unicode_escape_decode(r"\u1234"), ("\u1234", 6)) |
|
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1594 self.assertEqual(codecs.raw_unicode_escape_decode(br"\u1234"), ("\u1234", 6)) |
|
50975
ec6f29608bf0
Merged revisions 68855 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
1595 |
|
53966
7c29bfbfd352
Rename utf8b error handler to surrogateescape.
Martin v. Löwis <martin@v.loewis.de>
parents:
53965
diff
changeset
|
1596 class SurrogateEscapeTest(unittest.TestCase): |
|
53767
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53681
diff
changeset
|
1597 |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53681
diff
changeset
|
1598 def test_utf8(self): |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53681
diff
changeset
|
1599 # Bad byte |
|
53966
7c29bfbfd352
Rename utf8b error handler to surrogateescape.
Martin v. Löwis <martin@v.loewis.de>
parents:
53965
diff
changeset
|
1600 self.assertEqual(b"foo\x80bar".decode("utf-8", "surrogateescape"), |
|
53767
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53681
diff
changeset
|
1601 "foo\udc80bar") |
|
53966
7c29bfbfd352
Rename utf8b error handler to surrogateescape.
Martin v. Löwis <martin@v.loewis.de>
parents:
53965
diff
changeset
|
1602 self.assertEqual("foo\udc80bar".encode("utf-8", "surrogateescape"), |
|
53767
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53681
diff
changeset
|
1603 b"foo\x80bar") |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53681
diff
changeset
|
1604 # bad-utf-8 encoded surrogate |
|
53966
7c29bfbfd352
Rename utf8b error handler to surrogateescape.
Martin v. Löwis <martin@v.loewis.de>
parents:
53965
diff
changeset
|
1605 self.assertEqual(b"\xed\xb0\x80".decode("utf-8", "surrogateescape"), |
|
53767
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53681
diff
changeset
|
1606 "\udced\udcb0\udc80") |
|
53966
7c29bfbfd352
Rename utf8b error handler to surrogateescape.
Martin v. Löwis <martin@v.loewis.de>
parents:
53965
diff
changeset
|
1607 self.assertEqual("\udced\udcb0\udc80".encode("utf-8", "surrogateescape"), |
|
53767
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53681
diff
changeset
|
1608 b"\xed\xb0\x80") |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53681
diff
changeset
|
1609 |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53681
diff
changeset
|
1610 def test_ascii(self): |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53681
diff
changeset
|
1611 # bad byte |
|
53966
7c29bfbfd352
Rename utf8b error handler to surrogateescape.
Martin v. Löwis <martin@v.loewis.de>
parents:
53965
diff
changeset
|
1612 self.assertEqual(b"foo\x80bar".decode("ascii", "surrogateescape"), |
|
53767
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53681
diff
changeset
|
1613 "foo\udc80bar") |
|
53966
7c29bfbfd352
Rename utf8b error handler to surrogateescape.
Martin v. Löwis <martin@v.loewis.de>
parents:
53965
diff
changeset
|
1614 self.assertEqual("foo\udc80bar".encode("ascii", "surrogateescape"), |
|
53767
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53681
diff
changeset
|
1615 b"foo\x80bar") |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53681
diff
changeset
|
1616 |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53681
diff
changeset
|
1617 def test_charmap(self): |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53681
diff
changeset
|
1618 # bad byte: \xa5 is unmapped in iso-8859-3 |
|
53966
7c29bfbfd352
Rename utf8b error handler to surrogateescape.
Martin v. Löwis <martin@v.loewis.de>
parents:
53965
diff
changeset
|
1619 self.assertEqual(b"foo\xa5bar".decode("iso-8859-3", "surrogateescape"), |
|
53767
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53681
diff
changeset
|
1620 "foo\udca5bar") |
|
53966
7c29bfbfd352
Rename utf8b error handler to surrogateescape.
Martin v. Löwis <martin@v.loewis.de>
parents:
53965
diff
changeset
|
1621 self.assertEqual("foo\udca5bar".encode("iso-8859-3", "surrogateescape"), |
|
53767
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53681
diff
changeset
|
1622 b"foo\xa5bar") |
|
ac209f132b17
Issue #5915: Implement PEP 383, Non-decodable Bytes in
Martin v. Löwis <martin@v.loewis.de>
parents:
53681
diff
changeset
|
1623 |
|
54988
29fac8848489
Merged revisions 73698 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
53966
diff
changeset
|
1624 def test_latin1(self): |
|
29fac8848489
Merged revisions 73698 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
53966
diff
changeset
|
1625 # Issue6373 |
|
29fac8848489
Merged revisions 73698 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
53966
diff
changeset
|
1626 self.assertEqual("\udce4\udceb\udcef\udcf6\udcfc".encode("latin1", "surrogateescape"), |
|
29fac8848489
Merged revisions 73698 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
53966
diff
changeset
|
1627 b"\xe4\xeb\xef\xf6\xfc") |
|
29fac8848489
Merged revisions 73698 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
53966
diff
changeset
|
1628 |
|
35702
22a720b35314
Part of SF patch #1313939: Speedup charmap decoding by extending
Walter Dörwald <walter@livinglogic.de>
parents:
35484
diff
changeset
|
1629 |
|
61850
846dbcf970ab
Merged revisions 81461 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60875
diff
changeset
|
1630 class BomTest(unittest.TestCase): |
|
846dbcf970ab
Merged revisions 81461 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60875
diff
changeset
|
1631 def test_seek0(self): |
|
846dbcf970ab
Merged revisions 81461 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60875
diff
changeset
|
1632 data = "1234567890" |
|
846dbcf970ab
Merged revisions 81461 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60875
diff
changeset
|
1633 tests = ("utf-16", |
|
846dbcf970ab
Merged revisions 81461 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60875
diff
changeset
|
1634 "utf-16-le", |
|
846dbcf970ab
Merged revisions 81461 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60875
diff
changeset
|
1635 "utf-16-be", |
|
846dbcf970ab
Merged revisions 81461 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60875
diff
changeset
|
1636 "utf-32", |
|
846dbcf970ab
Merged revisions 81461 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60875
diff
changeset
|
1637 "utf-32-le", |
|
846dbcf970ab
Merged revisions 81461 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60875
diff
changeset
|
1638 "utf-32-be") |
|
70292
b538445a9df8
test_codecs now removes the temporay file (created by the test)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
66340
diff
changeset
|
1639 self.addCleanup(support.unlink, support.TESTFN) |
|
61850
846dbcf970ab
Merged revisions 81461 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60875
diff
changeset
|
1640 for encoding in tests: |
|
61863
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1641 # Check if the BOM is written only once |
|
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1642 with codecs.open(support.TESTFN, 'w+', encoding=encoding) as f: |
|
61850
846dbcf970ab
Merged revisions 81461 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60875
diff
changeset
|
1643 f.write(data) |
|
846dbcf970ab
Merged revisions 81461 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60875
diff
changeset
|
1644 f.write(data) |
|
846dbcf970ab
Merged revisions 81461 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60875
diff
changeset
|
1645 f.seek(0) |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1646 self.assertEqual(f.read(), data * 2) |
|
61850
846dbcf970ab
Merged revisions 81461 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60875
diff
changeset
|
1647 f.seek(0) |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1648 self.assertEqual(f.read(), data * 2) |
|
61850
846dbcf970ab
Merged revisions 81461 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60875
diff
changeset
|
1649 |
|
61863
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1650 # Check that the BOM is written after a seek(0) |
|
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1651 with codecs.open(support.TESTFN, 'w+', encoding=encoding) as f: |
|
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1652 f.write(data[0]) |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1653 self.assertNotEqual(f.tell(), 0) |
|
61863
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1654 f.seek(0) |
|
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1655 f.write(data) |
|
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1656 f.seek(0) |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1657 self.assertEqual(f.read(), data) |
|
61863
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1658 |
|
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1659 # (StreamWriter) Check that the BOM is written after a seek(0) |
|
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1660 with codecs.open(support.TESTFN, 'w+', encoding=encoding) as f: |
|
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1661 f.writer.write(data[0]) |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1662 self.assertNotEqual(f.writer.tell(), 0) |
|
61863
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1663 f.writer.seek(0) |
|
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1664 f.writer.write(data) |
|
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1665 f.seek(0) |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1666 self.assertEqual(f.read(), data) |
|
61863
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1667 |
|
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1668 # Check that the BOM is not written after a seek() at a position |
|
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1669 # different than the start |
|
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1670 with codecs.open(support.TESTFN, 'w+', encoding=encoding) as f: |
|
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1671 f.write(data) |
|
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1672 f.seek(f.tell()) |
|
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1673 f.write(data) |
|
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1674 f.seek(0) |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1675 self.assertEqual(f.read(), data * 2) |
|
61863
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1676 |
|
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1677 # (StreamWriter) Check that the BOM is not written after a seek() |
|
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1678 # at a position different than the start |
|
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1679 with codecs.open(support.TESTFN, 'w+', encoding=encoding) as f: |
|
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1680 f.writer.write(data) |
|
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1681 f.writer.seek(f.writer.tell()) |
|
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1682 f.writer.write(data) |
|
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1683 f.seek(0) |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
62258
diff
changeset
|
1684 self.assertEqual(f.read(), data * 2) |
|
61863
cd4936691163
Merged revisions 81474 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
61850
diff
changeset
|
1685 |
|
61850
846dbcf970ab
Merged revisions 81461 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60875
diff
changeset
|
1686 |
|
66605
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1687 bytes_transform_encodings = [ |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1688 "base64_codec", |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1689 "uu_codec", |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1690 "quopri_codec", |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1691 "hex_codec", |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1692 ] |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1693 try: |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1694 import zlib |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1695 except ImportError: |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1696 pass |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1697 else: |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1698 bytes_transform_encodings.append("zlib_codec") |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1699 try: |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1700 import bz2 |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1701 except ImportError: |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1702 pass |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1703 else: |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1704 bytes_transform_encodings.append("bz2_codec") |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1705 |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1706 class TransformCodecTest(unittest.TestCase): |
|
66837
a811f7c10e82
remove (un)transform methods
Benjamin Peterson <benjamin@python.org>
parents:
66797
diff
changeset
|
1707 |
|
66605
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1708 def test_basics(self): |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1709 binput = bytes(range(256)) |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1710 for encoding in bytes_transform_encodings: |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1711 # generic codecs interface |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1712 (o, size) = codecs.getencoder(encoding)(binput) |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1713 self.assertEqual(size, len(binput)) |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1714 (i, size) = codecs.getdecoder(encoding)(o) |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1715 self.assertEqual(size, len(o)) |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1716 self.assertEqual(i, binput) |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1717 |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1718 def test_read(self): |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1719 for encoding in bytes_transform_encodings: |
|
66837
a811f7c10e82
remove (un)transform methods
Benjamin Peterson <benjamin@python.org>
parents:
66797
diff
changeset
|
1720 sin = codecs.encode(b"\x80", encoding) |
|
66605
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1721 reader = codecs.getreader(encoding)(io.BytesIO(sin)) |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1722 sout = reader.read() |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1723 self.assertEqual(sout, b"\x80") |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1724 |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1725 def test_readline(self): |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1726 for encoding in bytes_transform_encodings: |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1727 if encoding in ['uu_codec', 'zlib_codec']: |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1728 continue |
|
66837
a811f7c10e82
remove (un)transform methods
Benjamin Peterson <benjamin@python.org>
parents:
66797
diff
changeset
|
1729 sin = codecs.encode(b"\x80", encoding) |
|
66605
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1730 reader = codecs.getreader(encoding)(io.BytesIO(sin)) |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1731 sout = reader.readline() |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1732 self.assertEqual(sout, b"\x80") |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1733 |
|
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1734 |
|
20013
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
18163
diff
changeset
|
1735 def test_main(): |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
43933
diff
changeset
|
1736 support.run_unittest( |
|
42626
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
1737 UTF32Test, |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
1738 UTF32LETest, |
|
f1a2d51f1cab
Apply SF patch #1775604: This adds three new codecs (utf-32, utf-32-le and
Walter Dörwald <walter@livinglogic.de>
parents:
42124
diff
changeset
|
1739 UTF32BETest, |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28774
diff
changeset
|
1740 UTF16Test, |
|
33428
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
1741 UTF16LETest, |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
1742 UTF16BETest, |
|
57478a13fbf6
SF patch #998993: The UTF-8 and the UTF-16 stateful decoders now support
Walter Dörwald <walter@livinglogic.de>
parents:
32572
diff
changeset
|
1743 UTF8Test, |
|
36247
3ba9ff79bb29
Patch #1177307: UTF-8-Sig codec.
Martin v. Löwis <martin@v.loewis.de>
parents:
35889
diff
changeset
|
1744 UTF8SigTest, |
|
35882
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
1745 UTF7Test, |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
1746 UTF16ExTest, |
|
3236b5fb1fb7
Add tests for various error cases and for readbuffer_encode() and
Walter Dörwald <walter@livinglogic.de>
parents:
35702
diff
changeset
|
1747 ReadBufferTest, |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28774
diff
changeset
|
1748 RecodingTest, |
|
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28774
diff
changeset
|
1749 PunycodeTest, |
|
35484
4fc7a7edcb4b
SF bug #1251300: On UCS-4 builds the "unicode-internal" codec will now complain
Walter Dörwald <walter@livinglogic.de>
parents:
35435
diff
changeset
|
1750 UnicodeInternalTest, |
|
31795
bf37429f6d9f
Add test case for unicode(somestring, "idna").
Martin v. Löwis <martin@v.loewis.de>
parents:
28917
diff
changeset
|
1751 NameprepTest, |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37875
diff
changeset
|
1752 IDNACodecTest, |
|
33782
a52de911b284
SF #1048865: Fix a trivial typo that breaks StreamReader.readlines()
Hye-Shik Chang <hyeshik@gmail.com>
parents:
33428
diff
changeset
|
1753 CodecsModuleTest, |
|
34279
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1754 StreamReaderTest, |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1755 EncodedFileTest, |
|
34279
b3af7c6db1c6
Add a test that checks the basic functionality of every encoding.
Walter Dörwald <walter@livinglogic.de>
parents:
34214
diff
changeset
|
1756 BasicUnicodeTest, |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1757 CharmapTest, |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
1758 WithStmtTest, |
|
50975
ec6f29608bf0
Merged revisions 68855 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
1759 TypesTest, |
|
53966
7c29bfbfd352
Rename utf8b error handler to surrogateescape.
Martin v. Löwis <martin@v.loewis.de>
parents:
53965
diff
changeset
|
1760 SurrogateEscapeTest, |
|
61850
846dbcf970ab
Merged revisions 81461 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60875
diff
changeset
|
1761 BomTest, |
|
66605
7e4833764c88
#7475: add (un)transform method to bytes/bytearray and str, add back codecs that can be used with them from Python 2.
Georg Brandl <georg@python.org>
parents:
66310
diff
changeset
|
1762 TransformCodecTest, |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28774
diff
changeset
|
1763 ) |
|
20013
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
18163
diff
changeset
|
1764 |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
18163
diff
changeset
|
1765 |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
18163
diff
changeset
|
1766 if __name__ == "__main__": |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
18163
diff
changeset
|
1767 test_main() |
