Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1 | from test import support |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 2 | import unittest |
Marc-André Lemburg | a37171d | 2001-06-19 20:09:28 +0000 | [diff] [blame] | 3 | import codecs |
Walter Dörwald | c3ab0a7 | 2007-05-10 15:02:49 +0000 | [diff] [blame] | 4 | import sys, _testcapi, io |
Marc-André Lemburg | a37171d | 2001-06-19 20:09:28 +0000 | [diff] [blame] | 5 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 6 | class Queue(object): |
| 7 | """ |
| 8 | queue: write bytes at one end, read bytes from the other end |
| 9 | """ |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 10 | def __init__(self, buffer): |
| 11 | self._buffer = buffer |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 12 | |
| 13 | def write(self, chars): |
| 14 | self._buffer += chars |
| 15 | |
| 16 | def read(self, size=-1): |
| 17 | if size<0: |
| 18 | s = self._buffer |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 19 | self._buffer = self._buffer[:0] # make empty |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 20 | return s |
| 21 | else: |
| 22 | s = self._buffer[:size] |
| 23 | self._buffer = self._buffer[size:] |
| 24 | return s |
| 25 | |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 26 | class MixInCheckStateHandling: |
| 27 | def check_state_handling_decode(self, encoding, u, s): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 28 | for i in range(len(s)+1): |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 29 | d = codecs.getincrementaldecoder(encoding)() |
| 30 | part1 = d.decode(s[:i]) |
| 31 | state = d.getstate() |
| 32 | self.assert_(isinstance(state[1], int)) |
| 33 | # Check that the condition stated in the documentation for |
| 34 | # IncrementalDecoder.getstate() holds |
| 35 | if not state[1]: |
| 36 | # reset decoder to the default state without anything buffered |
| 37 | d.setstate((state[0][:0], 0)) |
| 38 | # Feeding the previous input may not produce any output |
| 39 | self.assert_(not d.decode(state[0])) |
| 40 | # The decoder must return to the same state |
| 41 | self.assertEqual(state, d.getstate()) |
| 42 | # Create a new decoder and set it to the state |
| 43 | # we extracted from the old one |
| 44 | d = codecs.getincrementaldecoder(encoding)() |
| 45 | d.setstate(state) |
| 46 | part2 = d.decode(s[i:], True) |
| 47 | self.assertEqual(u, part1+part2) |
| 48 | |
| 49 | def check_state_handling_encode(self, encoding, u, s): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 50 | for i in range(len(u)+1): |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 51 | d = codecs.getincrementalencoder(encoding)() |
| 52 | part1 = d.encode(u[:i]) |
| 53 | state = d.getstate() |
| 54 | d = codecs.getincrementalencoder(encoding)() |
| 55 | d.setstate(state) |
| 56 | part2 = d.encode(u[i:], True) |
| 57 | self.assertEqual(s, part1+part2) |
| 58 | |
| 59 | class ReadTest(unittest.TestCase, MixInCheckStateHandling): |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 60 | def check_partial(self, input, partialresults): |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 61 | # get a StreamReader for the encoding and feed the bytestring version |
Guido van Rossum | 87c0f1d | 2007-11-19 18:03:44 +0000 | [diff] [blame] | 62 | # of input to the reader byte by byte. Read everything available from |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 63 | # the StreamReader and check that the results equal the appropriate |
| 64 | # entries from partialresults. |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 65 | q = Queue(b"") |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 66 | r = codecs.getreader(self.encoding)(q) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 67 | result = "" |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 68 | for (c, partialresult) in zip(input.encode(self.encoding), partialresults): |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 69 | q.write(bytes([c])) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 70 | result += r.read() |
| 71 | self.assertEqual(result, partialresult) |
| 72 | # check that there's nothing left in the buffers |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 73 | self.assertEqual(r.read(), "") |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 74 | self.assertEqual(r.bytebuffer, b"") |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 75 | self.assertEqual(r.charbuffer, "") |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 76 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 77 | # do the check again, this time using a incremental decoder |
| 78 | d = codecs.getincrementaldecoder(self.encoding)() |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 79 | result = "" |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 80 | for (c, partialresult) in zip(input.encode(self.encoding), partialresults): |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 81 | result += d.decode(bytes([c])) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 82 | self.assertEqual(result, partialresult) |
| 83 | # check that there's nothing left in the buffers |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 84 | self.assertEqual(d.decode(b"", True), "") |
| 85 | self.assertEqual(d.buffer, b"") |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 86 | |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 87 | # Check whether the reset method works properly |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 88 | d.reset() |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 89 | result = "" |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 90 | for (c, partialresult) in zip(input.encode(self.encoding), partialresults): |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 91 | result += d.decode(bytes([c])) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 92 | self.assertEqual(result, partialresult) |
| 93 | # check that there's nothing left in the buffers |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 94 | self.assertEqual(d.decode(b"", True), "") |
| 95 | self.assertEqual(d.buffer, b"") |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 96 | |
| 97 | # check iterdecode() |
| 98 | encoded = input.encode(self.encoding) |
| 99 | self.assertEqual( |
| 100 | input, |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 101 | "".join(codecs.iterdecode([bytes([c]) for c in encoded], self.encoding)) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 102 | ) |
| 103 | |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 104 | def test_readline(self): |
| 105 | def getreader(input): |
Walter Dörwald | c3ab0a7 | 2007-05-10 15:02:49 +0000 | [diff] [blame] | 106 | stream = io.BytesIO(input.encode(self.encoding)) |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 107 | return codecs.getreader(self.encoding)(stream) |
| 108 | |
Walter Dörwald | ca19943 | 2006-03-06 22:39:12 +0000 | [diff] [blame] | 109 | def readalllines(input, keepends=True, size=None): |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 110 | reader = getreader(input) |
| 111 | lines = [] |
| 112 | while True: |
Walter Dörwald | ca19943 | 2006-03-06 22:39:12 +0000 | [diff] [blame] | 113 | line = reader.readline(size=size, keepends=keepends) |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 114 | if not line: |
| 115 | break |
| 116 | lines.append(line) |
Walter Dörwald | ca19943 | 2006-03-06 22:39:12 +0000 | [diff] [blame] | 117 | return "|".join(lines) |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 118 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 119 | s = "foo\nbar\r\nbaz\rspam\u2028eggs" |
| 120 | sexpected = "foo\n|bar\r\n|baz\r|spam\u2028|eggs" |
| 121 | sexpectednoends = "foo|bar|baz|spam|eggs" |
Walter Dörwald | ca19943 | 2006-03-06 22:39:12 +0000 | [diff] [blame] | 122 | self.assertEqual(readalllines(s, True), sexpected) |
| 123 | self.assertEqual(readalllines(s, False), sexpectednoends) |
| 124 | self.assertEqual(readalllines(s, True, 10), sexpected) |
| 125 | self.assertEqual(readalllines(s, False, 10), sexpectednoends) |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 126 | |
| 127 | # Test long lines (multiple calls to read() in readline()) |
| 128 | vw = [] |
| 129 | vwo = [] |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 130 | for (i, lineend) in enumerate("\n \r\n \r \u2028".split()): |
| 131 | vw.append((i*200)*"\3042" + lineend) |
| 132 | vwo.append((i*200)*"\3042") |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 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 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 138 | for size in range(80): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 139 | for lineend in "\n \r\n \r \u2028".split(): |
| 140 | s = 10*(size*"a" + lineend + "xxx\n") |
Walter Dörwald | 7a6dc13 | 2005-04-04 21:38:47 +0000 | [diff] [blame] | 141 | reader = getreader(s) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 142 | for i in range(10): |
Walter Dörwald | 7a6dc13 | 2005-04-04 21:38:47 +0000 | [diff] [blame] | 143 | self.assertEqual( |
| 144 | reader.readline(keepends=True), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 145 | size*"a" + lineend, |
Walter Dörwald | 7a6dc13 | 2005-04-04 21:38:47 +0000 | [diff] [blame] | 146 | ) |
| 147 | reader = getreader(s) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 148 | for i in range(10): |
Walter Dörwald | 7a6dc13 | 2005-04-04 21:38:47 +0000 | [diff] [blame] | 149 | self.assertEqual( |
| 150 | reader.readline(keepends=False), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 151 | size*"a", |
Walter Dörwald | 7a6dc13 | 2005-04-04 21:38:47 +0000 | [diff] [blame] | 152 | ) |
| 153 | |
| 154 | def test_bug1175396(self): |
| 155 | s = [ |
| 156 | '<%!--===================================================\r\n', |
| 157 | ' BLOG index page: show recent articles,\r\n', |
| 158 | ' today\'s articles, or articles of a specific date.\r\n', |
| 159 | '========================================================--%>\r\n', |
| 160 | '<%@inputencoding="ISO-8859-1"%>\r\n', |
| 161 | '<%@pagetemplate=TEMPLATE.y%>\r\n', |
| 162 | '<%@import=import frog.util, frog%>\r\n', |
| 163 | '<%@import=import frog.objects%>\r\n', |
| 164 | '<%@import=from frog.storageerrors import StorageError%>\r\n', |
| 165 | '<%\r\n', |
| 166 | '\r\n', |
| 167 | 'import logging\r\n', |
| 168 | 'log=logging.getLogger("Snakelets.logger")\r\n', |
| 169 | '\r\n', |
| 170 | '\r\n', |
| 171 | 'user=self.SessionCtx.user\r\n', |
| 172 | 'storageEngine=self.SessionCtx.storageEngine\r\n', |
| 173 | '\r\n', |
| 174 | '\r\n', |
| 175 | 'def readArticlesFromDate(date, count=None):\r\n', |
| 176 | ' entryids=storageEngine.listBlogEntries(date)\r\n', |
| 177 | ' entryids.reverse() # descending\r\n', |
| 178 | ' if count:\r\n', |
| 179 | ' entryids=entryids[:count]\r\n', |
| 180 | ' try:\r\n', |
| 181 | ' return [ frog.objects.BlogEntry.load(storageEngine, date, Id) for Id in entryids ]\r\n', |
| 182 | ' except StorageError,x:\r\n', |
| 183 | ' log.error("Error loading articles: "+str(x))\r\n', |
| 184 | ' self.abort("cannot load articles")\r\n', |
| 185 | '\r\n', |
| 186 | 'showdate=None\r\n', |
| 187 | '\r\n', |
| 188 | 'arg=self.Request.getArg()\r\n', |
| 189 | 'if arg=="today":\r\n', |
| 190 | ' #-------------------- TODAY\'S ARTICLES\r\n', |
| 191 | ' self.write("<h2>Today\'s articles</h2>")\r\n', |
| 192 | ' showdate = frog.util.isodatestr() \r\n', |
| 193 | ' entries = readArticlesFromDate(showdate)\r\n', |
| 194 | 'elif arg=="active":\r\n', |
| 195 | ' #-------------------- ACTIVE ARTICLES redirect\r\n', |
| 196 | ' self.Yredirect("active.y")\r\n', |
| 197 | 'elif arg=="login":\r\n', |
| 198 | ' #-------------------- LOGIN PAGE redirect\r\n', |
| 199 | ' self.Yredirect("login.y")\r\n', |
| 200 | 'elif arg=="date":\r\n', |
| 201 | ' #-------------------- ARTICLES OF A SPECIFIC DATE\r\n', |
| 202 | ' showdate = self.Request.getParameter("date")\r\n', |
| 203 | ' self.write("<h2>Articles written on %s</h2>"% frog.util.mediumdatestr(showdate))\r\n', |
| 204 | ' entries = readArticlesFromDate(showdate)\r\n', |
| 205 | 'else:\r\n', |
| 206 | ' #-------------------- RECENT ARTICLES\r\n', |
| 207 | ' self.write("<h2>Recent articles</h2>")\r\n', |
| 208 | ' dates=storageEngine.listBlogEntryDates()\r\n', |
| 209 | ' if dates:\r\n', |
| 210 | ' entries=[]\r\n', |
| 211 | ' SHOWAMOUNT=10\r\n', |
| 212 | ' for showdate in dates:\r\n', |
| 213 | ' entries.extend( readArticlesFromDate(showdate, SHOWAMOUNT-len(entries)) )\r\n', |
| 214 | ' if len(entries)>=SHOWAMOUNT:\r\n', |
| 215 | ' break\r\n', |
| 216 | ' \r\n', |
| 217 | ] |
Walter Dörwald | c3ab0a7 | 2007-05-10 15:02:49 +0000 | [diff] [blame] | 218 | stream = io.BytesIO("".join(s).encode(self.encoding)) |
Walter Dörwald | 7a6dc13 | 2005-04-04 21:38:47 +0000 | [diff] [blame] | 219 | reader = codecs.getreader(self.encoding)(stream) |
| 220 | for (i, line) in enumerate(reader): |
| 221 | self.assertEqual(line, s[i]) |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 222 | |
| 223 | def test_readlinequeue(self): |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 224 | q = Queue(b"") |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 225 | writer = codecs.getwriter(self.encoding)(q) |
| 226 | reader = codecs.getreader(self.encoding)(q) |
| 227 | |
| 228 | # No lineends |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 229 | writer.write("foo\r") |
| 230 | self.assertEqual(reader.readline(keepends=False), "foo") |
| 231 | writer.write("\nbar\r") |
| 232 | self.assertEqual(reader.readline(keepends=False), "") |
| 233 | self.assertEqual(reader.readline(keepends=False), "bar") |
| 234 | writer.write("baz") |
| 235 | self.assertEqual(reader.readline(keepends=False), "baz") |
| 236 | self.assertEqual(reader.readline(keepends=False), "") |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 237 | |
| 238 | # Lineends |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 239 | writer.write("foo\r") |
| 240 | self.assertEqual(reader.readline(keepends=True), "foo\r") |
| 241 | writer.write("\nbar\r") |
| 242 | self.assertEqual(reader.readline(keepends=True), "\n") |
| 243 | self.assertEqual(reader.readline(keepends=True), "bar\r") |
| 244 | writer.write("baz") |
| 245 | self.assertEqual(reader.readline(keepends=True), "baz") |
| 246 | self.assertEqual(reader.readline(keepends=True), "") |
| 247 | writer.write("foo\r\n") |
| 248 | self.assertEqual(reader.readline(keepends=True), "foo\r\n") |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 249 | |
Walter Dörwald | 9fa0946 | 2005-01-10 12:01:39 +0000 | [diff] [blame] | 250 | def test_bug1098990_a(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 251 | s1 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\r\n" |
| 252 | s2 = "offending line: ladfj askldfj klasdj fskla dfzaskdj fasklfj laskd fjasklfzzzzaa%whereisthis!!!\r\n" |
| 253 | s3 = "next line.\r\n" |
Walter Dörwald | 9fa0946 | 2005-01-10 12:01:39 +0000 | [diff] [blame] | 254 | |
| 255 | s = (s1+s2+s3).encode(self.encoding) |
Walter Dörwald | c3ab0a7 | 2007-05-10 15:02:49 +0000 | [diff] [blame] | 256 | stream = io.BytesIO(s) |
Walter Dörwald | 9fa0946 | 2005-01-10 12:01:39 +0000 | [diff] [blame] | 257 | reader = codecs.getreader(self.encoding)(stream) |
| 258 | self.assertEqual(reader.readline(), s1) |
| 259 | self.assertEqual(reader.readline(), s2) |
| 260 | self.assertEqual(reader.readline(), s3) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 261 | self.assertEqual(reader.readline(), "") |
Walter Dörwald | 9fa0946 | 2005-01-10 12:01:39 +0000 | [diff] [blame] | 262 | |
| 263 | def test_bug1098990_b(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 264 | s1 = "aaaaaaaaaaaaaaaaaaaaaaaa\r\n" |
| 265 | s2 = "bbbbbbbbbbbbbbbbbbbbbbbb\r\n" |
| 266 | s3 = "stillokay:bbbbxx\r\n" |
| 267 | s4 = "broken!!!!badbad\r\n" |
| 268 | s5 = "againokay.\r\n" |
Walter Dörwald | 9fa0946 | 2005-01-10 12:01:39 +0000 | [diff] [blame] | 269 | |
| 270 | s = (s1+s2+s3+s4+s5).encode(self.encoding) |
Walter Dörwald | c3ab0a7 | 2007-05-10 15:02:49 +0000 | [diff] [blame] | 271 | stream = io.BytesIO(s) |
Walter Dörwald | 9fa0946 | 2005-01-10 12:01:39 +0000 | [diff] [blame] | 272 | reader = codecs.getreader(self.encoding)(stream) |
| 273 | self.assertEqual(reader.readline(), s1) |
| 274 | self.assertEqual(reader.readline(), s2) |
| 275 | self.assertEqual(reader.readline(), s3) |
| 276 | self.assertEqual(reader.readline(), s4) |
| 277 | self.assertEqual(reader.readline(), s5) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 278 | self.assertEqual(reader.readline(), "") |
Walter Dörwald | 9fa0946 | 2005-01-10 12:01:39 +0000 | [diff] [blame] | 279 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 280 | class UTF32Test(ReadTest): |
| 281 | encoding = "utf-32" |
| 282 | |
| 283 | spamle = (b'\xff\xfe\x00\x00' |
| 284 | b's\x00\x00\x00p\x00\x00\x00a\x00\x00\x00m\x00\x00\x00' |
| 285 | b's\x00\x00\x00p\x00\x00\x00a\x00\x00\x00m\x00\x00\x00') |
| 286 | spambe = (b'\x00\x00\xfe\xff' |
| 287 | b'\x00\x00\x00s\x00\x00\x00p\x00\x00\x00a\x00\x00\x00m' |
| 288 | b'\x00\x00\x00s\x00\x00\x00p\x00\x00\x00a\x00\x00\x00m') |
| 289 | |
| 290 | def test_only_one_bom(self): |
| 291 | _,_,reader,writer = codecs.lookup(self.encoding) |
| 292 | # encode some stream |
| 293 | s = io.BytesIO() |
| 294 | f = writer(s) |
| 295 | f.write("spam") |
| 296 | f.write("spam") |
| 297 | d = s.getvalue() |
| 298 | # check whether there is exactly one BOM in it |
| 299 | self.assert_(d == self.spamle or d == self.spambe) |
| 300 | # try to read it back |
| 301 | s = io.BytesIO(d) |
| 302 | f = reader(s) |
| 303 | self.assertEquals(f.read(), "spamspam") |
| 304 | |
| 305 | def test_badbom(self): |
| 306 | s = io.BytesIO(4*b"\xff") |
| 307 | f = codecs.getreader(self.encoding)(s) |
| 308 | self.assertRaises(UnicodeError, f.read) |
| 309 | |
| 310 | s = io.BytesIO(8*b"\xff") |
| 311 | f = codecs.getreader(self.encoding)(s) |
| 312 | self.assertRaises(UnicodeError, f.read) |
| 313 | |
| 314 | def test_partial(self): |
| 315 | self.check_partial( |
| 316 | "\x00\xff\u0100\uffff", |
| 317 | [ |
| 318 | "", # first byte of BOM read |
| 319 | "", # second byte of BOM read |
| 320 | "", # third byte of BOM read |
| 321 | "", # fourth byte of BOM read => byteorder known |
| 322 | "", |
| 323 | "", |
| 324 | "", |
| 325 | "\x00", |
| 326 | "\x00", |
| 327 | "\x00", |
| 328 | "\x00", |
| 329 | "\x00\xff", |
| 330 | "\x00\xff", |
| 331 | "\x00\xff", |
| 332 | "\x00\xff", |
| 333 | "\x00\xff\u0100", |
| 334 | "\x00\xff\u0100", |
| 335 | "\x00\xff\u0100", |
| 336 | "\x00\xff\u0100", |
| 337 | "\x00\xff\u0100\uffff", |
| 338 | ] |
| 339 | ) |
| 340 | |
| 341 | def test_errors(self): |
| 342 | self.assertRaises(UnicodeDecodeError, codecs.utf_32_decode, |
| 343 | b"\xff", "strict", True) |
| 344 | |
| 345 | def test_decoder_state(self): |
| 346 | self.check_state_handling_decode(self.encoding, |
| 347 | "spamspam", self.spamle) |
| 348 | self.check_state_handling_decode(self.encoding, |
| 349 | "spamspam", self.spambe) |
| 350 | |
| 351 | class UTF32LETest(ReadTest): |
| 352 | encoding = "utf-32-le" |
| 353 | |
| 354 | def test_partial(self): |
| 355 | self.check_partial( |
| 356 | "\x00\xff\u0100\uffff", |
| 357 | [ |
| 358 | "", |
| 359 | "", |
| 360 | "", |
| 361 | "\x00", |
| 362 | "\x00", |
| 363 | "\x00", |
| 364 | "\x00", |
| 365 | "\x00\xff", |
| 366 | "\x00\xff", |
| 367 | "\x00\xff", |
| 368 | "\x00\xff", |
| 369 | "\x00\xff\u0100", |
| 370 | "\x00\xff\u0100", |
| 371 | "\x00\xff\u0100", |
| 372 | "\x00\xff\u0100", |
| 373 | "\x00\xff\u0100\uffff", |
| 374 | ] |
| 375 | ) |
| 376 | |
| 377 | def test_simple(self): |
| 378 | self.assertEqual("\U00010203".encode(self.encoding), b"\x03\x02\x01\x00") |
| 379 | |
| 380 | def test_errors(self): |
| 381 | self.assertRaises(UnicodeDecodeError, codecs.utf_32_le_decode, |
| 382 | b"\xff", "strict", True) |
| 383 | |
| 384 | class UTF32BETest(ReadTest): |
| 385 | encoding = "utf-32-be" |
| 386 | |
| 387 | def test_partial(self): |
| 388 | self.check_partial( |
| 389 | "\x00\xff\u0100\uffff", |
| 390 | [ |
| 391 | "", |
| 392 | "", |
| 393 | "", |
| 394 | "\x00", |
| 395 | "\x00", |
| 396 | "\x00", |
| 397 | "\x00", |
| 398 | "\x00\xff", |
| 399 | "\x00\xff", |
| 400 | "\x00\xff", |
| 401 | "\x00\xff", |
| 402 | "\x00\xff\u0100", |
| 403 | "\x00\xff\u0100", |
| 404 | "\x00\xff\u0100", |
| 405 | "\x00\xff\u0100", |
| 406 | "\x00\xff\u0100\uffff", |
| 407 | ] |
| 408 | ) |
| 409 | |
| 410 | def test_simple(self): |
| 411 | self.assertEqual("\U00010203".encode(self.encoding), b"\x00\x01\x02\x03") |
| 412 | |
| 413 | def test_errors(self): |
| 414 | self.assertRaises(UnicodeDecodeError, codecs.utf_32_be_decode, |
| 415 | b"\xff", "strict", True) |
| 416 | |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 417 | class UTF16Test(ReadTest): |
| 418 | encoding = "utf-16" |
Marc-André Lemburg | a37171d | 2001-06-19 20:09:28 +0000 | [diff] [blame] | 419 | |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 420 | spamle = b'\xff\xfes\x00p\x00a\x00m\x00s\x00p\x00a\x00m\x00' |
| 421 | spambe = b'\xfe\xff\x00s\x00p\x00a\x00m\x00s\x00p\x00a\x00m' |
Marc-André Lemburg | a37171d | 2001-06-19 20:09:28 +0000 | [diff] [blame] | 422 | |
| 423 | def test_only_one_bom(self): |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 424 | _,_,reader,writer = codecs.lookup(self.encoding) |
Marc-André Lemburg | a37171d | 2001-06-19 20:09:28 +0000 | [diff] [blame] | 425 | # encode some stream |
Walter Dörwald | c3ab0a7 | 2007-05-10 15:02:49 +0000 | [diff] [blame] | 426 | s = io.BytesIO() |
Marc-André Lemburg | a37171d | 2001-06-19 20:09:28 +0000 | [diff] [blame] | 427 | f = writer(s) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 428 | f.write("spam") |
| 429 | f.write("spam") |
Marc-André Lemburg | a37171d | 2001-06-19 20:09:28 +0000 | [diff] [blame] | 430 | d = s.getvalue() |
| 431 | # check whether there is exactly one BOM in it |
| 432 | self.assert_(d == self.spamle or d == self.spambe) |
| 433 | # try to read it back |
Walter Dörwald | c3ab0a7 | 2007-05-10 15:02:49 +0000 | [diff] [blame] | 434 | s = io.BytesIO(d) |
Marc-André Lemburg | a37171d | 2001-06-19 20:09:28 +0000 | [diff] [blame] | 435 | f = reader(s) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 436 | self.assertEquals(f.read(), "spamspam") |
Marc-André Lemburg | a37171d | 2001-06-19 20:09:28 +0000 | [diff] [blame] | 437 | |
Walter Dörwald | 1f1d252 | 2005-02-04 14:15:34 +0000 | [diff] [blame] | 438 | def test_badbom(self): |
Walter Dörwald | c3ab0a7 | 2007-05-10 15:02:49 +0000 | [diff] [blame] | 439 | s = io.BytesIO(b"\xff\xff") |
Walter Dörwald | a9620d1 | 2005-02-08 10:10:01 +0000 | [diff] [blame] | 440 | f = codecs.getreader(self.encoding)(s) |
Walter Dörwald | 1f1d252 | 2005-02-04 14:15:34 +0000 | [diff] [blame] | 441 | self.assertRaises(UnicodeError, f.read) |
| 442 | |
Walter Dörwald | c3ab0a7 | 2007-05-10 15:02:49 +0000 | [diff] [blame] | 443 | s = io.BytesIO(b"\xff\xff\xff\xff") |
Walter Dörwald | a9620d1 | 2005-02-08 10:10:01 +0000 | [diff] [blame] | 444 | f = codecs.getreader(self.encoding)(s) |
Walter Dörwald | 1f1d252 | 2005-02-04 14:15:34 +0000 | [diff] [blame] | 445 | self.assertRaises(UnicodeError, f.read) |
| 446 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 447 | def test_partial(self): |
| 448 | self.check_partial( |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 449 | "\x00\xff\u0100\uffff", |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 450 | [ |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 451 | "", # first byte of BOM read |
| 452 | "", # second byte of BOM read => byteorder known |
| 453 | "", |
| 454 | "\x00", |
| 455 | "\x00", |
| 456 | "\x00\xff", |
| 457 | "\x00\xff", |
| 458 | "\x00\xff\u0100", |
| 459 | "\x00\xff\u0100", |
| 460 | "\x00\xff\u0100\uffff", |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 461 | ] |
| 462 | ) |
| 463 | |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 464 | def test_errors(self): |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 465 | self.assertRaises(UnicodeDecodeError, codecs.utf_16_decode, |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 466 | b"\xff", "strict", True) |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 467 | |
| 468 | def test_decoder_state(self): |
| 469 | self.check_state_handling_decode(self.encoding, |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 470 | "spamspam", self.spamle) |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 471 | self.check_state_handling_decode(self.encoding, |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 472 | "spamspam", self.spambe) |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 473 | |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 474 | class UTF16LETest(ReadTest): |
| 475 | encoding = "utf-16-le" |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 476 | |
| 477 | def test_partial(self): |
| 478 | self.check_partial( |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 479 | "\x00\xff\u0100\uffff", |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 480 | [ |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 481 | "", |
| 482 | "\x00", |
| 483 | "\x00", |
| 484 | "\x00\xff", |
| 485 | "\x00\xff", |
| 486 | "\x00\xff\u0100", |
| 487 | "\x00\xff\u0100", |
| 488 | "\x00\xff\u0100\uffff", |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 489 | ] |
| 490 | ) |
| 491 | |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 492 | def test_errors(self): |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 493 | self.assertRaises(UnicodeDecodeError, codecs.utf_16_le_decode, |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 494 | b"\xff", "strict", True) |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 495 | |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 496 | class UTF16BETest(ReadTest): |
| 497 | encoding = "utf-16-be" |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 498 | |
| 499 | def test_partial(self): |
| 500 | self.check_partial( |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 501 | "\x00\xff\u0100\uffff", |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 502 | [ |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 503 | "", |
| 504 | "\x00", |
| 505 | "\x00", |
| 506 | "\x00\xff", |
| 507 | "\x00\xff", |
| 508 | "\x00\xff\u0100", |
| 509 | "\x00\xff\u0100", |
| 510 | "\x00\xff\u0100\uffff", |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 511 | ] |
| 512 | ) |
| 513 | |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 514 | def test_errors(self): |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 515 | self.assertRaises(UnicodeDecodeError, codecs.utf_16_be_decode, |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 516 | b"\xff", "strict", True) |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 517 | |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 518 | class UTF8Test(ReadTest): |
| 519 | encoding = "utf-8" |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 520 | |
| 521 | def test_partial(self): |
| 522 | self.check_partial( |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 523 | "\x00\xff\u07ff\u0800\uffff", |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 524 | [ |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 525 | "\x00", |
| 526 | "\x00", |
| 527 | "\x00\xff", |
| 528 | "\x00\xff", |
| 529 | "\x00\xff\u07ff", |
| 530 | "\x00\xff\u07ff", |
| 531 | "\x00\xff\u07ff", |
| 532 | "\x00\xff\u07ff\u0800", |
| 533 | "\x00\xff\u07ff\u0800", |
| 534 | "\x00\xff\u07ff\u0800", |
| 535 | "\x00\xff\u07ff\u0800\uffff", |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 536 | ] |
| 537 | ) |
| 538 | |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 539 | def test_decoder_state(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 540 | u = "\x00\x7f\x80\xff\u0100\u07ff\u0800\uffff\U0010ffff" |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 541 | self.check_state_handling_decode(self.encoding, |
| 542 | u, u.encode(self.encoding)) |
| 543 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 544 | def test_lone_surrogates(self): |
| 545 | self.assertRaises(UnicodeEncodeError, "\ud800".encode, "utf-8") |
| 546 | self.assertRaises(UnicodeDecodeError, b"\xed\xa0\x80".decode, "utf-8") |
| 547 | |
Martin v. Löwis | e0a2b72 | 2009-05-10 08:08:56 +0000 | [diff] [blame] | 548 | def test_surrogatepass_handler(self): |
| 549 | self.assertEquals("abc\ud800def".encode("utf-8", "surrogatepass"), |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 550 | b"abc\xed\xa0\x80def") |
Martin v. Löwis | e0a2b72 | 2009-05-10 08:08:56 +0000 | [diff] [blame] | 551 | self.assertEquals(b"abc\xed\xa0\x80def".decode("utf-8", "surrogatepass"), |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 552 | "abc\ud800def") |
Martin v. Löwis | e0a2b72 | 2009-05-10 08:08:56 +0000 | [diff] [blame] | 553 | self.assertTrue(codecs.lookup_error("surrogatepass")) |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 554 | |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 555 | class UTF7Test(ReadTest): |
| 556 | encoding = "utf-7" |
| 557 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 558 | def test_partial(self): |
| 559 | self.check_partial( |
| 560 | "a+-b", |
| 561 | [ |
| 562 | "a", |
| 563 | "a", |
| 564 | "a+", |
| 565 | "a+-", |
| 566 | "a+-b", |
| 567 | ] |
| 568 | ) |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 569 | |
| 570 | class UTF16ExTest(unittest.TestCase): |
| 571 | |
| 572 | def test_errors(self): |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 573 | self.assertRaises(UnicodeDecodeError, codecs.utf_16_ex_decode, b"\xff", "strict", 0, True) |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 574 | |
| 575 | def test_bad_args(self): |
| 576 | self.assertRaises(TypeError, codecs.utf_16_ex_decode) |
| 577 | |
| 578 | class ReadBufferTest(unittest.TestCase): |
| 579 | |
| 580 | def test_array(self): |
| 581 | import array |
| 582 | self.assertEqual( |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 583 | codecs.readbuffer_encode(array.array("b", b"spam")), |
Walter Dörwald | 2233d27 | 2007-06-22 12:17:08 +0000 | [diff] [blame] | 584 | (b"spam", 4) |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 585 | ) |
| 586 | |
| 587 | def test_empty(self): |
Walter Dörwald | 2233d27 | 2007-06-22 12:17:08 +0000 | [diff] [blame] | 588 | self.assertEqual(codecs.readbuffer_encode(""), (b"", 0)) |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 589 | |
| 590 | def test_bad_args(self): |
| 591 | self.assertRaises(TypeError, codecs.readbuffer_encode) |
| 592 | self.assertRaises(TypeError, codecs.readbuffer_encode, 42) |
| 593 | |
| 594 | class CharBufferTest(unittest.TestCase): |
| 595 | |
| 596 | def test_string(self): |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 597 | self.assertEqual(codecs.charbuffer_encode(b"spam"), (b"spam", 4)) |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 598 | |
| 599 | def test_empty(self): |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 600 | self.assertEqual(codecs.charbuffer_encode(b""), (b"", 0)) |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 601 | |
| 602 | def test_bad_args(self): |
| 603 | self.assertRaises(TypeError, codecs.charbuffer_encode) |
| 604 | self.assertRaises(TypeError, codecs.charbuffer_encode, 42) |
| 605 | |
Martin v. Löwis | 412ed3b | 2006-01-08 10:45:39 +0000 | [diff] [blame] | 606 | class UTF8SigTest(ReadTest): |
| 607 | encoding = "utf-8-sig" |
| 608 | |
| 609 | def test_partial(self): |
| 610 | self.check_partial( |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 611 | "\ufeff\x00\xff\u07ff\u0800\uffff", |
Martin v. Löwis | 412ed3b | 2006-01-08 10:45:39 +0000 | [diff] [blame] | 612 | [ |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 613 | "", |
| 614 | "", |
| 615 | "", # First BOM has been read and skipped |
| 616 | "", |
| 617 | "", |
| 618 | "\ufeff", # Second BOM has been read and emitted |
| 619 | "\ufeff\x00", # "\x00" read and emitted |
Walter Dörwald | 32a4c71 | 2007-06-20 09:25:34 +0000 | [diff] [blame] | 620 | "\ufeff\x00", # First byte of encoded "\xff" read |
| 621 | "\ufeff\x00\xff", # Second byte of encoded "\xff" read |
| 622 | "\ufeff\x00\xff", # First byte of encoded "\u07ff" read |
| 623 | "\ufeff\x00\xff\u07ff", # Second byte of encoded "\u07ff" read |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 624 | "\ufeff\x00\xff\u07ff", |
| 625 | "\ufeff\x00\xff\u07ff", |
| 626 | "\ufeff\x00\xff\u07ff\u0800", |
| 627 | "\ufeff\x00\xff\u07ff\u0800", |
| 628 | "\ufeff\x00\xff\u07ff\u0800", |
| 629 | "\ufeff\x00\xff\u07ff\u0800\uffff", |
Martin v. Löwis | 412ed3b | 2006-01-08 10:45:39 +0000 | [diff] [blame] | 630 | ] |
| 631 | ) |
| 632 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 633 | def test_bug1601501(self): |
| 634 | # SF bug #1601501: check that the codec works with a buffer |
Antoine Pitrou | 616d285 | 2008-08-19 22:09:34 +0000 | [diff] [blame] | 635 | self.assertEquals(str(b"\xef\xbb\xbf", "utf-8-sig"), "") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 636 | |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 637 | def test_bom(self): |
| 638 | d = codecs.getincrementaldecoder("utf-8-sig")() |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 639 | s = "spam" |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 640 | self.assertEqual(d.decode(s.encode("utf-8-sig")), s) |
| 641 | |
Guido van Rossum | 87c0f1d | 2007-11-19 18:03:44 +0000 | [diff] [blame] | 642 | def test_stream_bom(self): |
| 643 | unistring = "ABC\u00A1\u2200XYZ" |
| 644 | bytestring = codecs.BOM_UTF8 + b"ABC\xC2\xA1\xE2\x88\x80XYZ" |
| 645 | |
| 646 | reader = codecs.getreader("utf-8-sig") |
| 647 | for sizehint in [None] + list(range(1, 11)) + \ |
| 648 | [64, 128, 256, 512, 1024]: |
| 649 | istream = reader(io.BytesIO(bytestring)) |
| 650 | ostream = io.StringIO() |
| 651 | while 1: |
| 652 | if sizehint is not None: |
| 653 | data = istream.read(sizehint) |
| 654 | else: |
| 655 | data = istream.read() |
| 656 | |
| 657 | if not data: |
| 658 | break |
| 659 | ostream.write(data) |
| 660 | |
| 661 | got = ostream.getvalue() |
| 662 | self.assertEqual(got, unistring) |
| 663 | |
| 664 | def test_stream_bare(self): |
| 665 | unistring = "ABC\u00A1\u2200XYZ" |
| 666 | bytestring = b"ABC\xC2\xA1\xE2\x88\x80XYZ" |
| 667 | |
| 668 | reader = codecs.getreader("utf-8-sig") |
| 669 | for sizehint in [None] + list(range(1, 11)) + \ |
| 670 | [64, 128, 256, 512, 1024]: |
| 671 | istream = reader(io.BytesIO(bytestring)) |
| 672 | ostream = io.StringIO() |
| 673 | while 1: |
| 674 | if sizehint is not None: |
| 675 | data = istream.read(sizehint) |
| 676 | else: |
| 677 | data = istream.read() |
| 678 | |
| 679 | if not data: |
| 680 | break |
| 681 | ostream.write(data) |
| 682 | |
| 683 | got = ostream.getvalue() |
| 684 | self.assertEqual(got, unistring) |
| 685 | |
| 686 | class EscapeDecodeTest(unittest.TestCase): |
| 687 | def test_empty(self): |
| 688 | self.assertEquals(codecs.escape_decode(""), ("", 0)) |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 689 | |
Marc-André Lemburg | 29273c8 | 2003-02-04 19:35:03 +0000 | [diff] [blame] | 690 | class RecodingTest(unittest.TestCase): |
| 691 | def test_recoding(self): |
Guido van Rossum | f4cfc8f | 2007-05-17 21:52:23 +0000 | [diff] [blame] | 692 | f = io.BytesIO() |
Marc-André Lemburg | 29273c8 | 2003-02-04 19:35:03 +0000 | [diff] [blame] | 693 | f2 = codecs.EncodedFile(f, "unicode_internal", "utf-8") |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 694 | f2.write("a") |
Marc-André Lemburg | 29273c8 | 2003-02-04 19:35:03 +0000 | [diff] [blame] | 695 | f2.close() |
| 696 | # Python used to crash on this at exit because of a refcount |
| 697 | # bug in _codecsmodule.c |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 698 | |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 699 | # From RFC 3492 |
| 700 | punycode_testcases = [ |
| 701 | # A Arabic (Egyptian): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 702 | ("\u0644\u064A\u0647\u0645\u0627\u0628\u062A\u0643\u0644" |
| 703 | "\u0645\u0648\u0634\u0639\u0631\u0628\u064A\u061F", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 704 | b"egbpdaj6bu4bxfgehfvwxn"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 705 | # B Chinese (simplified): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 706 | ("\u4ED6\u4EEC\u4E3A\u4EC0\u4E48\u4E0D\u8BF4\u4E2D\u6587", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 707 | b"ihqwcrb4cv8a8dqg056pqjye"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 708 | # C Chinese (traditional): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 709 | ("\u4ED6\u5011\u7232\u4EC0\u9EBD\u4E0D\u8AAA\u4E2D\u6587", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 710 | b"ihqwctvzc91f659drss3x8bo0yb"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 711 | # D Czech: Pro<ccaron>prost<ecaron>nemluv<iacute><ccaron>esky |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 712 | ("\u0050\u0072\u006F\u010D\u0070\u0072\u006F\u0073\u0074" |
| 713 | "\u011B\u006E\u0065\u006D\u006C\u0075\u0076\u00ED\u010D" |
| 714 | "\u0065\u0073\u006B\u0079", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 715 | b"Proprostnemluvesky-uyb24dma41a"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 716 | # E Hebrew: |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 717 | ("\u05DC\u05DE\u05D4\u05D4\u05DD\u05E4\u05E9\u05D5\u05D8" |
| 718 | "\u05DC\u05D0\u05DE\u05D3\u05D1\u05E8\u05D9\u05DD\u05E2" |
| 719 | "\u05D1\u05E8\u05D9\u05EA", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 720 | b"4dbcagdahymbxekheh6e0a7fei0b"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 721 | # F Hindi (Devanagari): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 722 | ("\u092F\u0939\u0932\u094B\u0917\u0939\u093F\u0928\u094D" |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 723 | "\u0926\u0940\u0915\u094D\u092F\u094B\u0902\u0928\u0939" |
| 724 | "\u0940\u0902\u092C\u094B\u0932\u0938\u0915\u0924\u0947" |
| 725 | "\u0939\u0948\u0902", |
| 726 | b"i1baa7eci9glrd9b2ae1bj0hfcgg6iyaf8o0a1dig0cd"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 727 | |
| 728 | #(G) Japanese (kanji and hiragana): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 729 | ("\u306A\u305C\u307F\u3093\u306A\u65E5\u672C\u8A9E\u3092" |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 730 | "\u8A71\u3057\u3066\u304F\u308C\u306A\u3044\u306E\u304B", |
| 731 | b"n8jok5ay5dzabd5bym9f0cm5685rrjetr6pdxa"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 732 | |
| 733 | # (H) Korean (Hangul syllables): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 734 | ("\uC138\uACC4\uC758\uBAA8\uB4E0\uC0AC\uB78C\uB4E4\uC774" |
| 735 | "\uD55C\uAD6D\uC5B4\uB97C\uC774\uD574\uD55C\uB2E4\uBA74" |
| 736 | "\uC5BC\uB9C8\uB098\uC88B\uC744\uAE4C", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 737 | b"989aomsvi5e83db1d2a355cv1e0vak1dwrv93d5xbh15a0dt30a5j" |
| 738 | b"psd879ccm6fea98c"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 739 | |
| 740 | # (I) Russian (Cyrillic): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 741 | ("\u043F\u043E\u0447\u0435\u043C\u0443\u0436\u0435\u043E" |
| 742 | "\u043D\u0438\u043D\u0435\u0433\u043E\u0432\u043E\u0440" |
| 743 | "\u044F\u0442\u043F\u043E\u0440\u0443\u0441\u0441\u043A" |
| 744 | "\u0438", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 745 | b"b1abfaaepdrnnbgefbaDotcwatmq2g4l"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 746 | |
| 747 | # (J) Spanish: Porqu<eacute>nopuedensimplementehablarenEspa<ntilde>ol |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 748 | ("\u0050\u006F\u0072\u0071\u0075\u00E9\u006E\u006F\u0070" |
| 749 | "\u0075\u0065\u0064\u0065\u006E\u0073\u0069\u006D\u0070" |
| 750 | "\u006C\u0065\u006D\u0065\u006E\u0074\u0065\u0068\u0061" |
| 751 | "\u0062\u006C\u0061\u0072\u0065\u006E\u0045\u0073\u0070" |
| 752 | "\u0061\u00F1\u006F\u006C", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 753 | b"PorqunopuedensimplementehablarenEspaol-fmd56a"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 754 | |
| 755 | # (K) Vietnamese: |
| 756 | # T<adotbelow>isaoh<odotbelow>kh<ocirc>ngth<ecirchookabove>ch\ |
| 757 | # <ihookabove>n<oacute>iti<ecircacute>ngVi<ecircdotbelow>t |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 758 | ("\u0054\u1EA1\u0069\u0073\u0061\u006F\u0068\u1ECD\u006B" |
| 759 | "\u0068\u00F4\u006E\u0067\u0074\u0068\u1EC3\u0063\u0068" |
| 760 | "\u1EC9\u006E\u00F3\u0069\u0074\u0069\u1EBF\u006E\u0067" |
| 761 | "\u0056\u0069\u1EC7\u0074", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 762 | b"TisaohkhngthchnitingVit-kjcr8268qyxafd2f1b9g"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 763 | |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 764 | #(L) 3<nen>B<gumi><kinpachi><sensei> |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 765 | ("\u0033\u5E74\u0042\u7D44\u91D1\u516B\u5148\u751F", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 766 | b"3B-ww4c5e180e575a65lsy2b"), |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 767 | |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 768 | # (M) <amuro><namie>-with-SUPER-MONKEYS |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 769 | ("\u5B89\u5BA4\u5948\u7F8E\u6075\u002D\u0077\u0069\u0074" |
| 770 | "\u0068\u002D\u0053\u0055\u0050\u0045\u0052\u002D\u004D" |
| 771 | "\u004F\u004E\u004B\u0045\u0059\u0053", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 772 | b"-with-SUPER-MONKEYS-pc58ag80a8qai00g7n9n"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 773 | |
| 774 | # (N) Hello-Another-Way-<sorezore><no><basho> |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 775 | ("\u0048\u0065\u006C\u006C\u006F\u002D\u0041\u006E\u006F" |
| 776 | "\u0074\u0068\u0065\u0072\u002D\u0057\u0061\u0079\u002D" |
| 777 | "\u305D\u308C\u305E\u308C\u306E\u5834\u6240", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 778 | b"Hello-Another-Way--fc4qua05auwb3674vfr0b"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 779 | |
| 780 | # (O) <hitotsu><yane><no><shita>2 |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 781 | ("\u3072\u3068\u3064\u5C4B\u6839\u306E\u4E0B\u0032", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 782 | b"2-u9tlzr9756bt3uc0v"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 783 | |
| 784 | # (P) Maji<de>Koi<suru>5<byou><mae> |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 785 | ("\u004D\u0061\u006A\u0069\u3067\u004B\u006F\u0069\u3059" |
| 786 | "\u308B\u0035\u79D2\u524D", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 787 | b"MajiKoi5-783gue6qz075azm5e"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 788 | |
| 789 | # (Q) <pafii>de<runba> |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 790 | ("\u30D1\u30D5\u30A3\u30FC\u0064\u0065\u30EB\u30F3\u30D0", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 791 | b"de-jg4avhby1noc0d"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 792 | |
| 793 | # (R) <sono><supiido><de> |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 794 | ("\u305D\u306E\u30B9\u30D4\u30FC\u30C9\u3067", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 795 | b"d9juau41awczczp"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 796 | |
| 797 | # (S) -> $1.00 <- |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 798 | ("\u002D\u003E\u0020\u0024\u0031\u002E\u0030\u0030\u0020" |
| 799 | "\u003C\u002D", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 800 | b"-> $1.00 <--") |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 801 | ] |
| 802 | |
| 803 | for i in punycode_testcases: |
| 804 | if len(i)!=2: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 805 | print(repr(i)) |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 806 | |
| 807 | class PunycodeTest(unittest.TestCase): |
| 808 | def test_encode(self): |
| 809 | for uni, puny in punycode_testcases: |
| 810 | # Need to convert both strings to lower case, since |
| 811 | # some of the extended encodings use upper case, but our |
| 812 | # code produces only lower case. Converting just puny to |
| 813 | # lower is also insufficient, since some of the input characters |
| 814 | # are upper case. |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 815 | self.assertEquals( |
| 816 | str(uni.encode("punycode"), "ascii").lower(), |
| 817 | str(puny, "ascii").lower() |
| 818 | ) |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 819 | |
| 820 | def test_decode(self): |
| 821 | for uni, puny in punycode_testcases: |
| 822 | self.assertEquals(uni, puny.decode("punycode")) |
Guido van Rossum | 04c70ad | 2007-08-29 14:04:40 +0000 | [diff] [blame] | 823 | puny = puny.decode("ascii").encode("ascii") |
| 824 | self.assertEquals(uni, puny.decode("punycode")) |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 825 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 826 | class UnicodeInternalTest(unittest.TestCase): |
| 827 | def test_bug1251300(self): |
| 828 | # Decoding with unicode_internal used to not correctly handle "code |
| 829 | # points" above 0x10ffff on UCS-4 builds. |
| 830 | if sys.maxunicode > 0xffff: |
| 831 | ok = [ |
Walter Dörwald | 092a225 | 2007-06-07 11:26:16 +0000 | [diff] [blame] | 832 | (b"\x00\x10\xff\xff", "\U0010ffff"), |
| 833 | (b"\x00\x00\x01\x01", "\U00000101"), |
| 834 | (b"", ""), |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 835 | ] |
| 836 | not_ok = [ |
Walter Dörwald | 092a225 | 2007-06-07 11:26:16 +0000 | [diff] [blame] | 837 | b"\x7f\xff\xff\xff", |
| 838 | b"\x80\x00\x00\x00", |
| 839 | b"\x81\x00\x00\x00", |
| 840 | b"\x00", |
| 841 | b"\x00\x00\x00\x00\x00", |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 842 | ] |
| 843 | for internal, uni in ok: |
| 844 | if sys.byteorder == "little": |
Walter Dörwald | 092a225 | 2007-06-07 11:26:16 +0000 | [diff] [blame] | 845 | internal = bytes(reversed(internal)) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 846 | self.assertEquals(uni, internal.decode("unicode_internal")) |
| 847 | for internal in not_ok: |
| 848 | if sys.byteorder == "little": |
Walter Dörwald | 092a225 | 2007-06-07 11:26:16 +0000 | [diff] [blame] | 849 | internal = bytes(reversed(internal)) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 850 | self.assertRaises(UnicodeDecodeError, internal.decode, |
| 851 | "unicode_internal") |
| 852 | |
| 853 | def test_decode_error_attributes(self): |
| 854 | if sys.maxunicode > 0xffff: |
| 855 | try: |
Walter Dörwald | 092a225 | 2007-06-07 11:26:16 +0000 | [diff] [blame] | 856 | b"\x00\x00\x00\x00\x00\x11\x11\x00".decode("unicode_internal") |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 857 | except UnicodeDecodeError as ex: |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 858 | self.assertEquals("unicode_internal", ex.encoding) |
Walter Dörwald | 092a225 | 2007-06-07 11:26:16 +0000 | [diff] [blame] | 859 | self.assertEquals(b"\x00\x00\x00\x00\x00\x11\x11\x00", ex.object) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 860 | self.assertEquals(4, ex.start) |
| 861 | self.assertEquals(8, ex.end) |
| 862 | else: |
| 863 | self.fail() |
| 864 | |
| 865 | def test_decode_callback(self): |
| 866 | if sys.maxunicode > 0xffff: |
| 867 | codecs.register_error("UnicodeInternalTest", codecs.ignore_errors) |
| 868 | decoder = codecs.getdecoder("unicode_internal") |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 869 | ab = "ab".encode("unicode_internal").decode() |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 870 | ignored = decoder(bytes("%s\x22\x22\x22\x22%s" % (ab[:4], ab[4:]), |
| 871 | "ascii"), |
| 872 | "UnicodeInternalTest") |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 873 | self.assertEquals(("ab", 12), ignored) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 874 | |
Walter Dörwald | 8dc33d5 | 2009-05-06 14:41:26 +0000 | [diff] [blame] | 875 | def test_encode_length(self): |
| 876 | # Issue 3739 |
| 877 | encoder = codecs.getencoder("unicode_internal") |
| 878 | self.assertEquals(encoder("a")[1], 1) |
| 879 | self.assertEquals(encoder("\xe9\u0142")[1], 2) |
| 880 | |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 881 | # From http://www.gnu.org/software/libidn/draft-josefsson-idn-test-vectors.html |
| 882 | nameprep_tests = [ |
| 883 | # 3.1 Map to nothing. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 884 | (b'foo\xc2\xad\xcd\x8f\xe1\xa0\x86\xe1\xa0\x8bbar' |
| 885 | b'\xe2\x80\x8b\xe2\x81\xa0baz\xef\xb8\x80\xef\xb8\x88\xef' |
| 886 | b'\xb8\x8f\xef\xbb\xbf', |
| 887 | b'foobarbaz'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 888 | # 3.2 Case folding ASCII U+0043 U+0041 U+0046 U+0045. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 889 | (b'CAFE', |
| 890 | b'cafe'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 891 | # 3.3 Case folding 8bit U+00DF (german sharp s). |
| 892 | # The original test case is bogus; it says \xc3\xdf |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 893 | (b'\xc3\x9f', |
| 894 | b'ss'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 895 | # 3.4 Case folding U+0130 (turkish capital I with dot). |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 896 | (b'\xc4\xb0', |
| 897 | b'i\xcc\x87'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 898 | # 3.5 Case folding multibyte U+0143 U+037A. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 899 | (b'\xc5\x83\xcd\xba', |
| 900 | b'\xc5\x84 \xce\xb9'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 901 | # 3.6 Case folding U+2121 U+33C6 U+1D7BB. |
| 902 | # XXX: skip this as it fails in UCS-2 mode |
| 903 | #('\xe2\x84\xa1\xe3\x8f\x86\xf0\x9d\x9e\xbb', |
| 904 | # 'telc\xe2\x88\x95kg\xcf\x83'), |
| 905 | (None, None), |
| 906 | # 3.7 Normalization of U+006a U+030c U+00A0 U+00AA. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 907 | (b'j\xcc\x8c\xc2\xa0\xc2\xaa', |
| 908 | b'\xc7\xb0 a'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 909 | # 3.8 Case folding U+1FB7 and normalization. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 910 | (b'\xe1\xbe\xb7', |
| 911 | b'\xe1\xbe\xb6\xce\xb9'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 912 | # 3.9 Self-reverting case folding U+01F0 and normalization. |
| 913 | # The original test case is bogus, it says `\xc7\xf0' |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 914 | (b'\xc7\xb0', |
| 915 | b'\xc7\xb0'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 916 | # 3.10 Self-reverting case folding U+0390 and normalization. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 917 | (b'\xce\x90', |
| 918 | b'\xce\x90'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 919 | # 3.11 Self-reverting case folding U+03B0 and normalization. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 920 | (b'\xce\xb0', |
| 921 | b'\xce\xb0'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 922 | # 3.12 Self-reverting case folding U+1E96 and normalization. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 923 | (b'\xe1\xba\x96', |
| 924 | b'\xe1\xba\x96'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 925 | # 3.13 Self-reverting case folding U+1F56 and normalization. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 926 | (b'\xe1\xbd\x96', |
| 927 | b'\xe1\xbd\x96'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 928 | # 3.14 ASCII space character U+0020. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 929 | (b' ', |
| 930 | b' '), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 931 | # 3.15 Non-ASCII 8bit space character U+00A0. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 932 | (b'\xc2\xa0', |
| 933 | b' '), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 934 | # 3.16 Non-ASCII multibyte space character U+1680. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 935 | (b'\xe1\x9a\x80', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 936 | None), |
| 937 | # 3.17 Non-ASCII multibyte space character U+2000. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 938 | (b'\xe2\x80\x80', |
| 939 | b' '), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 940 | # 3.18 Zero Width Space U+200b. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 941 | (b'\xe2\x80\x8b', |
| 942 | b''), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 943 | # 3.19 Non-ASCII multibyte space character U+3000. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 944 | (b'\xe3\x80\x80', |
| 945 | b' '), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 946 | # 3.20 ASCII control characters U+0010 U+007F. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 947 | (b'\x10\x7f', |
| 948 | b'\x10\x7f'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 949 | # 3.21 Non-ASCII 8bit control character U+0085. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 950 | (b'\xc2\x85', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 951 | None), |
| 952 | # 3.22 Non-ASCII multibyte control character U+180E. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 953 | (b'\xe1\xa0\x8e', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 954 | None), |
| 955 | # 3.23 Zero Width No-Break Space U+FEFF. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 956 | (b'\xef\xbb\xbf', |
| 957 | b''), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 958 | # 3.24 Non-ASCII control character U+1D175. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 959 | (b'\xf0\x9d\x85\xb5', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 960 | None), |
| 961 | # 3.25 Plane 0 private use character U+F123. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 962 | (b'\xef\x84\xa3', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 963 | None), |
| 964 | # 3.26 Plane 15 private use character U+F1234. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 965 | (b'\xf3\xb1\x88\xb4', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 966 | None), |
| 967 | # 3.27 Plane 16 private use character U+10F234. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 968 | (b'\xf4\x8f\x88\xb4', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 969 | None), |
| 970 | # 3.28 Non-character code point U+8FFFE. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 971 | (b'\xf2\x8f\xbf\xbe', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 972 | None), |
| 973 | # 3.29 Non-character code point U+10FFFF. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 974 | (b'\xf4\x8f\xbf\xbf', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 975 | None), |
| 976 | # 3.30 Surrogate code U+DF42. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 977 | (b'\xed\xbd\x82', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 978 | None), |
| 979 | # 3.31 Non-plain text character U+FFFD. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 980 | (b'\xef\xbf\xbd', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 981 | None), |
| 982 | # 3.32 Ideographic description character U+2FF5. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 983 | (b'\xe2\xbf\xb5', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 984 | None), |
| 985 | # 3.33 Display property character U+0341. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 986 | (b'\xcd\x81', |
| 987 | b'\xcc\x81'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 988 | # 3.34 Left-to-right mark U+200E. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 989 | (b'\xe2\x80\x8e', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 990 | None), |
| 991 | # 3.35 Deprecated U+202A. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 992 | (b'\xe2\x80\xaa', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 993 | None), |
| 994 | # 3.36 Language tagging character U+E0001. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 995 | (b'\xf3\xa0\x80\x81', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 996 | None), |
| 997 | # 3.37 Language tagging character U+E0042. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 998 | (b'\xf3\xa0\x81\x82', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 999 | None), |
| 1000 | # 3.38 Bidi: RandALCat character U+05BE and LCat characters. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1001 | (b'foo\xd6\xbebar', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1002 | None), |
| 1003 | # 3.39 Bidi: RandALCat character U+FD50 and LCat characters. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1004 | (b'foo\xef\xb5\x90bar', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1005 | None), |
| 1006 | # 3.40 Bidi: RandALCat character U+FB38 and LCat characters. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1007 | (b'foo\xef\xb9\xb6bar', |
| 1008 | b'foo \xd9\x8ebar'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1009 | # 3.41 Bidi: RandALCat without trailing RandALCat U+0627 U+0031. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1010 | (b'\xd8\xa71', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1011 | None), |
| 1012 | # 3.42 Bidi: RandALCat character U+0627 U+0031 U+0628. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1013 | (b'\xd8\xa71\xd8\xa8', |
| 1014 | b'\xd8\xa71\xd8\xa8'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1015 | # 3.43 Unassigned code point U+E0002. |
Martin v. Löwis | b5c4b7b | 2003-04-18 20:21:00 +0000 | [diff] [blame] | 1016 | # Skip this test as we allow unassigned |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1017 | #(b'\xf3\xa0\x80\x82', |
Martin v. Löwis | b5c4b7b | 2003-04-18 20:21:00 +0000 | [diff] [blame] | 1018 | # None), |
| 1019 | (None, None), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1020 | # 3.44 Larger test (shrinking). |
| 1021 | # Original test case reads \xc3\xdf |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1022 | (b'X\xc2\xad\xc3\x9f\xc4\xb0\xe2\x84\xa1j\xcc\x8c\xc2\xa0\xc2' |
| 1023 | b'\xaa\xce\xb0\xe2\x80\x80', |
| 1024 | b'xssi\xcc\x87tel\xc7\xb0 a\xce\xb0 '), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1025 | # 3.45 Larger test (expanding). |
| 1026 | # Original test case reads \xc3\x9f |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1027 | (b'X\xc3\x9f\xe3\x8c\x96\xc4\xb0\xe2\x84\xa1\xe2\x92\x9f\xe3\x8c' |
| 1028 | b'\x80', |
| 1029 | b'xss\xe3\x82\xad\xe3\x83\xad\xe3\x83\xa1\xe3\x83\xbc\xe3' |
| 1030 | b'\x83\x88\xe3\x83\xabi\xcc\x87tel\x28d\x29\xe3\x82' |
| 1031 | b'\xa2\xe3\x83\x91\xe3\x83\xbc\xe3\x83\x88') |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1032 | ] |
| 1033 | |
| 1034 | |
| 1035 | class NameprepTest(unittest.TestCase): |
| 1036 | def test_nameprep(self): |
| 1037 | from encodings.idna import nameprep |
| 1038 | for pos, (orig, prepped) in enumerate(nameprep_tests): |
| 1039 | if orig is None: |
| 1040 | # Skipped |
| 1041 | continue |
| 1042 | # The Unicode strings are given in UTF-8 |
Martin v. Löwis | e0a2b72 | 2009-05-10 08:08:56 +0000 | [diff] [blame] | 1043 | orig = str(orig, "utf-8", "surrogatepass") |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1044 | if prepped is None: |
| 1045 | # Input contains prohibited characters |
| 1046 | self.assertRaises(UnicodeError, nameprep, orig) |
| 1047 | else: |
Martin v. Löwis | e0a2b72 | 2009-05-10 08:08:56 +0000 | [diff] [blame] | 1048 | prepped = str(prepped, "utf-8", "surrogatepass") |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1049 | try: |
| 1050 | self.assertEquals(nameprep(orig), prepped) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 1051 | except Exception as e: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1052 | raise support.TestFailed("Test 3.%d: %s" % (pos+1, str(e))) |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1053 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1054 | class IDNACodecTest(unittest.TestCase): |
| 1055 | def test_builtin_decode(self): |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1056 | self.assertEquals(str(b"python.org", "idna"), "python.org") |
| 1057 | self.assertEquals(str(b"python.org.", "idna"), "python.org.") |
| 1058 | self.assertEquals(str(b"xn--pythn-mua.org", "idna"), "pyth\xf6n.org") |
| 1059 | self.assertEquals(str(b"xn--pythn-mua.org.", "idna"), "pyth\xf6n.org.") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1060 | |
| 1061 | def test_builtin_encode(self): |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1062 | self.assertEquals("python.org".encode("idna"), b"python.org") |
| 1063 | self.assertEquals("python.org.".encode("idna"), b"python.org.") |
| 1064 | self.assertEquals("pyth\xf6n.org".encode("idna"), b"xn--pythn-mua.org") |
| 1065 | self.assertEquals("pyth\xf6n.org.".encode("idna"), b"xn--pythn-mua.org.") |
Martin v. Löwis | a1dde13 | 2004-03-24 16:48:24 +0000 | [diff] [blame] | 1066 | |
Martin v. Löwis | 8b59514 | 2005-08-25 11:03:38 +0000 | [diff] [blame] | 1067 | def test_stream(self): |
Walter Dörwald | c3ab0a7 | 2007-05-10 15:02:49 +0000 | [diff] [blame] | 1068 | r = codecs.getreader("idna")(io.BytesIO(b"abc")) |
Martin v. Löwis | 8b59514 | 2005-08-25 11:03:38 +0000 | [diff] [blame] | 1069 | r.read(3) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1070 | self.assertEquals(r.read(), "") |
Martin v. Löwis | 8b59514 | 2005-08-25 11:03:38 +0000 | [diff] [blame] | 1071 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1072 | def test_incremental_decode(self): |
| 1073 | self.assertEquals( |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 1074 | "".join(codecs.iterdecode((bytes([c]) for c in b"python.org"), "idna")), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1075 | "python.org" |
| 1076 | ) |
| 1077 | self.assertEquals( |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 1078 | "".join(codecs.iterdecode((bytes([c]) for c in b"python.org."), "idna")), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1079 | "python.org." |
| 1080 | ) |
| 1081 | self.assertEquals( |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 1082 | "".join(codecs.iterdecode((bytes([c]) for c in b"xn--pythn-mua.org."), "idna")), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1083 | "pyth\xf6n.org." |
| 1084 | ) |
| 1085 | self.assertEquals( |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 1086 | "".join(codecs.iterdecode((bytes([c]) for c in b"xn--pythn-mua.org."), "idna")), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1087 | "pyth\xf6n.org." |
| 1088 | ) |
| 1089 | |
| 1090 | decoder = codecs.getincrementaldecoder("idna")() |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1091 | self.assertEquals(decoder.decode(b"xn--xam", ), "") |
| 1092 | self.assertEquals(decoder.decode(b"ple-9ta.o", ), "\xe4xample.") |
| 1093 | self.assertEquals(decoder.decode(b"rg"), "") |
| 1094 | self.assertEquals(decoder.decode(b"", True), "org") |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1095 | |
| 1096 | decoder.reset() |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1097 | self.assertEquals(decoder.decode(b"xn--xam", ), "") |
| 1098 | self.assertEquals(decoder.decode(b"ple-9ta.o", ), "\xe4xample.") |
| 1099 | self.assertEquals(decoder.decode(b"rg."), "org.") |
| 1100 | self.assertEquals(decoder.decode(b"", True), "") |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1101 | |
| 1102 | def test_incremental_encode(self): |
| 1103 | self.assertEquals( |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1104 | b"".join(codecs.iterencode("python.org", "idna")), |
| 1105 | b"python.org" |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1106 | ) |
| 1107 | self.assertEquals( |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1108 | b"".join(codecs.iterencode("python.org.", "idna")), |
| 1109 | b"python.org." |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1110 | ) |
| 1111 | self.assertEquals( |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1112 | b"".join(codecs.iterencode("pyth\xf6n.org.", "idna")), |
| 1113 | b"xn--pythn-mua.org." |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1114 | ) |
| 1115 | self.assertEquals( |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1116 | b"".join(codecs.iterencode("pyth\xf6n.org.", "idna")), |
| 1117 | b"xn--pythn-mua.org." |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1118 | ) |
| 1119 | |
| 1120 | encoder = codecs.getincrementalencoder("idna")() |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1121 | self.assertEquals(encoder.encode("\xe4x"), b"") |
| 1122 | self.assertEquals(encoder.encode("ample.org"), b"xn--xample-9ta.") |
| 1123 | self.assertEquals(encoder.encode("", True), b"org") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1124 | |
| 1125 | encoder.reset() |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1126 | self.assertEquals(encoder.encode("\xe4x"), b"") |
| 1127 | self.assertEquals(encoder.encode("ample.org."), b"xn--xample-9ta.org.") |
| 1128 | self.assertEquals(encoder.encode("", True), b"") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1129 | |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 1130 | class CodecsModuleTest(unittest.TestCase): |
| 1131 | |
| 1132 | def test_decode(self): |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1133 | self.assertEquals(codecs.decode(b'\xe4\xf6\xfc', 'latin-1'), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1134 | '\xe4\xf6\xfc') |
Walter Dörwald | 063e1e8 | 2004-10-28 13:04:26 +0000 | [diff] [blame] | 1135 | self.assertRaises(TypeError, codecs.decode) |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1136 | self.assertEquals(codecs.decode(b'abc'), 'abc') |
| 1137 | self.assertRaises(UnicodeDecodeError, codecs.decode, b'\xff', 'ascii') |
Walter Dörwald | 063e1e8 | 2004-10-28 13:04:26 +0000 | [diff] [blame] | 1138 | |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 1139 | def test_encode(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1140 | self.assertEquals(codecs.encode('\xe4\xf6\xfc', 'latin-1'), |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1141 | b'\xe4\xf6\xfc') |
Walter Dörwald | 063e1e8 | 2004-10-28 13:04:26 +0000 | [diff] [blame] | 1142 | self.assertRaises(TypeError, codecs.encode) |
Walter Dörwald | 690402f | 2005-11-17 18:51:34 +0000 | [diff] [blame] | 1143 | self.assertRaises(LookupError, codecs.encode, "foo", "__spam__") |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1144 | self.assertEquals(codecs.encode('abc'), b'abc') |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1145 | self.assertRaises(UnicodeEncodeError, codecs.encode, '\xffff', 'ascii') |
Walter Dörwald | 063e1e8 | 2004-10-28 13:04:26 +0000 | [diff] [blame] | 1146 | |
| 1147 | def test_register(self): |
| 1148 | self.assertRaises(TypeError, codecs.register) |
Walter Dörwald | 690402f | 2005-11-17 18:51:34 +0000 | [diff] [blame] | 1149 | self.assertRaises(TypeError, codecs.register, 42) |
Walter Dörwald | 063e1e8 | 2004-10-28 13:04:26 +0000 | [diff] [blame] | 1150 | |
| 1151 | def test_lookup(self): |
| 1152 | self.assertRaises(TypeError, codecs.lookup) |
| 1153 | self.assertRaises(LookupError, codecs.lookup, "__spam__") |
Walter Dörwald | 690402f | 2005-11-17 18:51:34 +0000 | [diff] [blame] | 1154 | self.assertRaises(LookupError, codecs.lookup, " ") |
| 1155 | |
| 1156 | def test_getencoder(self): |
| 1157 | self.assertRaises(TypeError, codecs.getencoder) |
| 1158 | self.assertRaises(LookupError, codecs.getencoder, "__spam__") |
| 1159 | |
| 1160 | def test_getdecoder(self): |
| 1161 | self.assertRaises(TypeError, codecs.getdecoder) |
| 1162 | self.assertRaises(LookupError, codecs.getdecoder, "__spam__") |
| 1163 | |
| 1164 | def test_getreader(self): |
| 1165 | self.assertRaises(TypeError, codecs.getreader) |
| 1166 | self.assertRaises(LookupError, codecs.getreader, "__spam__") |
| 1167 | |
| 1168 | def test_getwriter(self): |
| 1169 | self.assertRaises(TypeError, codecs.getwriter) |
| 1170 | self.assertRaises(LookupError, codecs.getwriter, "__spam__") |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 1171 | |
Hye-Shik Chang | af5c7cf | 2004-10-17 23:51:21 +0000 | [diff] [blame] | 1172 | class StreamReaderTest(unittest.TestCase): |
| 1173 | |
| 1174 | def setUp(self): |
| 1175 | self.reader = codecs.getreader('utf-8') |
Walter Dörwald | c3ab0a7 | 2007-05-10 15:02:49 +0000 | [diff] [blame] | 1176 | self.stream = io.BytesIO(b'\xed\x95\x9c\n\xea\xb8\x80') |
Hye-Shik Chang | af5c7cf | 2004-10-17 23:51:21 +0000 | [diff] [blame] | 1177 | |
| 1178 | def test_readlines(self): |
| 1179 | f = self.reader(self.stream) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1180 | self.assertEquals(f.readlines(), ['\ud55c\n', '\uae00']) |
Hye-Shik Chang | af5c7cf | 2004-10-17 23:51:21 +0000 | [diff] [blame] | 1181 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1182 | class EncodedFileTest(unittest.TestCase): |
| 1183 | |
| 1184 | def test_basic(self): |
Walter Dörwald | c3ab0a7 | 2007-05-10 15:02:49 +0000 | [diff] [blame] | 1185 | f = io.BytesIO(b'\xed\x95\x9c\n\xea\xb8\x80') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1186 | ef = codecs.EncodedFile(f, 'utf-16-le', 'utf-8') |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1187 | self.assertEquals(ef.read(), b'\\\xd5\n\x00\x00\xae') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1188 | |
Walter Dörwald | c3ab0a7 | 2007-05-10 15:02:49 +0000 | [diff] [blame] | 1189 | f = io.BytesIO() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1190 | ef = codecs.EncodedFile(f, 'utf-8', 'latin1') |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1191 | ef.write(b'\xc3\xbc') |
| 1192 | self.assertEquals(f.getvalue(), b'\xfc') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1193 | |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1194 | all_unicode_encodings = [ |
| 1195 | "ascii", |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1196 | "big5", |
| 1197 | "big5hkscs", |
| 1198 | "charmap", |
| 1199 | "cp037", |
| 1200 | "cp1006", |
| 1201 | "cp1026", |
| 1202 | "cp1140", |
| 1203 | "cp1250", |
| 1204 | "cp1251", |
| 1205 | "cp1252", |
| 1206 | "cp1253", |
| 1207 | "cp1254", |
| 1208 | "cp1255", |
| 1209 | "cp1256", |
| 1210 | "cp1257", |
| 1211 | "cp1258", |
| 1212 | "cp424", |
| 1213 | "cp437", |
| 1214 | "cp500", |
| 1215 | "cp737", |
| 1216 | "cp775", |
| 1217 | "cp850", |
| 1218 | "cp852", |
| 1219 | "cp855", |
| 1220 | "cp856", |
| 1221 | "cp857", |
| 1222 | "cp860", |
| 1223 | "cp861", |
| 1224 | "cp862", |
| 1225 | "cp863", |
| 1226 | "cp864", |
| 1227 | "cp865", |
| 1228 | "cp866", |
| 1229 | "cp869", |
| 1230 | "cp874", |
| 1231 | "cp875", |
| 1232 | "cp932", |
| 1233 | "cp949", |
| 1234 | "cp950", |
| 1235 | "euc_jis_2004", |
| 1236 | "euc_jisx0213", |
| 1237 | "euc_jp", |
| 1238 | "euc_kr", |
| 1239 | "gb18030", |
| 1240 | "gb2312", |
| 1241 | "gbk", |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1242 | "hp_roman8", |
| 1243 | "hz", |
| 1244 | "idna", |
| 1245 | "iso2022_jp", |
| 1246 | "iso2022_jp_1", |
| 1247 | "iso2022_jp_2", |
| 1248 | "iso2022_jp_2004", |
| 1249 | "iso2022_jp_3", |
| 1250 | "iso2022_jp_ext", |
| 1251 | "iso2022_kr", |
| 1252 | "iso8859_1", |
| 1253 | "iso8859_10", |
| 1254 | "iso8859_11", |
| 1255 | "iso8859_13", |
| 1256 | "iso8859_14", |
| 1257 | "iso8859_15", |
| 1258 | "iso8859_16", |
| 1259 | "iso8859_2", |
| 1260 | "iso8859_3", |
| 1261 | "iso8859_4", |
| 1262 | "iso8859_5", |
| 1263 | "iso8859_6", |
| 1264 | "iso8859_7", |
| 1265 | "iso8859_8", |
| 1266 | "iso8859_9", |
| 1267 | "johab", |
| 1268 | "koi8_r", |
| 1269 | "koi8_u", |
| 1270 | "latin_1", |
| 1271 | "mac_cyrillic", |
| 1272 | "mac_greek", |
| 1273 | "mac_iceland", |
| 1274 | "mac_latin2", |
| 1275 | "mac_roman", |
| 1276 | "mac_turkish", |
| 1277 | "palmos", |
| 1278 | "ptcp154", |
| 1279 | "punycode", |
| 1280 | "raw_unicode_escape", |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1281 | "shift_jis", |
| 1282 | "shift_jis_2004", |
| 1283 | "shift_jisx0213", |
| 1284 | "tis_620", |
| 1285 | "unicode_escape", |
| 1286 | "unicode_internal", |
| 1287 | "utf_16", |
| 1288 | "utf_16_be", |
| 1289 | "utf_16_le", |
| 1290 | "utf_7", |
| 1291 | "utf_8", |
| 1292 | ] |
| 1293 | |
| 1294 | if hasattr(codecs, "mbcs_encode"): |
| 1295 | all_unicode_encodings.append("mbcs") |
| 1296 | |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1297 | # The following encoding is not tested, because it's not supposed |
| 1298 | # to work: |
| 1299 | # "undefined" |
| 1300 | |
| 1301 | # The following encodings don't work in stateful mode |
| 1302 | broken_unicode_with_streams = [ |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1303 | "punycode", |
| 1304 | "unicode_internal" |
| 1305 | ] |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 1306 | broken_incremental_coders = broken_unicode_with_streams + [ |
| 1307 | "idna", |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 1308 | ] |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1309 | |
| 1310 | # The following encodings only support "strict" mode |
| 1311 | only_strict_mode = [ |
| 1312 | "idna", |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1313 | ] |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1314 | |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 1315 | class BasicUnicodeTest(unittest.TestCase, MixInCheckStateHandling): |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1316 | def test_basics(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1317 | s = "abc123" # all codecs should be able to encode these |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1318 | for encoding in all_unicode_encodings: |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1319 | name = codecs.lookup(encoding).name |
| 1320 | if encoding.endswith("_codec"): |
| 1321 | name += "_codec" |
| 1322 | elif encoding == "latin_1": |
| 1323 | name = "latin_1" |
| 1324 | self.assertEqual(encoding.replace("_", "-"), name.replace("_", "-")) |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1325 | (b, size) = codecs.getencoder(encoding)(s) |
Walter Dörwald | 8dc33d5 | 2009-05-06 14:41:26 +0000 | [diff] [blame] | 1326 | self.assertEqual(size, len(s), "%r != %r (encoding=%r)" % (size, len(s), encoding)) |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1327 | (chars, size) = codecs.getdecoder(encoding)(b) |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1328 | self.assertEqual(chars, s, "%r != %r (encoding=%r)" % (chars, s, encoding)) |
| 1329 | |
| 1330 | if encoding not in broken_unicode_with_streams: |
| 1331 | # check stream reader/writer |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1332 | q = Queue(b"") |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1333 | writer = codecs.getwriter(encoding)(q) |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1334 | encodedresult = b"" |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1335 | for c in s: |
| 1336 | writer.write(c) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1337 | chunk = q.read() |
| 1338 | self.assert_(type(chunk) is bytes, type(chunk)) |
| 1339 | encodedresult += chunk |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1340 | q = Queue(b"") |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1341 | reader = codecs.getreader(encoding)(q) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1342 | decodedresult = "" |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1343 | for c in encodedresult: |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1344 | q.write(bytes([c])) |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1345 | decodedresult += reader.read() |
| 1346 | self.assertEqual(decodedresult, s, "%r != %r (encoding=%r)" % (decodedresult, s, encoding)) |
| 1347 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1348 | if encoding not in broken_incremental_coders: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1349 | # check incremental decoder/encoder (fetched via the Python |
| 1350 | # and C API) and iterencode()/iterdecode() |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1351 | try: |
| 1352 | encoder = codecs.getincrementalencoder(encoding)() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1353 | cencoder = _testcapi.codec_incrementalencoder(encoding) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1354 | except LookupError: # no IncrementalEncoder |
| 1355 | pass |
| 1356 | else: |
| 1357 | # check incremental decoder/encoder |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1358 | encodedresult = b"" |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1359 | for c in s: |
| 1360 | encodedresult += encoder.encode(c) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1361 | encodedresult += encoder.encode("", True) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1362 | decoder = codecs.getincrementaldecoder(encoding)() |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1363 | decodedresult = "" |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1364 | for c in encodedresult: |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1365 | decodedresult += decoder.decode(bytes([c])) |
Guido van Rossum | f4cfc8f | 2007-05-17 21:52:23 +0000 | [diff] [blame] | 1366 | decodedresult += decoder.decode(b"", True) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1367 | self.assertEqual(decodedresult, s, "%r != %r (encoding=%r)" % (decodedresult, s, encoding)) |
| 1368 | |
| 1369 | # check C API |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1370 | encodedresult = b"" |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1371 | for c in s: |
| 1372 | encodedresult += cencoder.encode(c) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1373 | encodedresult += cencoder.encode("", True) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1374 | cdecoder = _testcapi.codec_incrementaldecoder(encoding) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1375 | decodedresult = "" |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1376 | for c in encodedresult: |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1377 | decodedresult += cdecoder.decode(bytes([c])) |
Guido van Rossum | f4cfc8f | 2007-05-17 21:52:23 +0000 | [diff] [blame] | 1378 | decodedresult += cdecoder.decode(b"", True) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1379 | self.assertEqual(decodedresult, s, "%r != %r (encoding=%r)" % (decodedresult, s, encoding)) |
| 1380 | |
| 1381 | # check iterencode()/iterdecode() |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1382 | result = "".join(codecs.iterdecode(codecs.iterencode(s, encoding), encoding)) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1383 | self.assertEqual(result, s, "%r != %r (encoding=%r)" % (result, s, encoding)) |
| 1384 | |
| 1385 | # check iterencode()/iterdecode() with empty string |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1386 | result = "".join(codecs.iterdecode(codecs.iterencode("", encoding), encoding)) |
| 1387 | self.assertEqual(result, "") |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1388 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1389 | if encoding not in only_strict_mode: |
| 1390 | # check incremental decoder/encoder with errors argument |
| 1391 | try: |
| 1392 | encoder = codecs.getincrementalencoder(encoding)("ignore") |
| 1393 | cencoder = _testcapi.codec_incrementalencoder(encoding, "ignore") |
| 1394 | except LookupError: # no IncrementalEncoder |
| 1395 | pass |
| 1396 | else: |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1397 | encodedresult = b"".join(encoder.encode(c) for c in s) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1398 | decoder = codecs.getincrementaldecoder(encoding)("ignore") |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1399 | decodedresult = "".join(decoder.decode(bytes([c])) for c in encodedresult) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1400 | self.assertEqual(decodedresult, s, "%r != %r (encoding=%r)" % (decodedresult, s, encoding)) |
| 1401 | |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1402 | encodedresult = b"".join(cencoder.encode(c) for c in s) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1403 | cdecoder = _testcapi.codec_incrementaldecoder(encoding, "ignore") |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1404 | decodedresult = "".join(cdecoder.decode(bytes([c])) for c in encodedresult) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1405 | self.assertEqual(decodedresult, s, "%r != %r (encoding=%r)" % (decodedresult, s, encoding)) |
| 1406 | |
Walter Dörwald | 729c31f | 2005-03-14 19:06:30 +0000 | [diff] [blame] | 1407 | def test_seek(self): |
| 1408 | # all codecs should be able to encode these |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1409 | s = "%s\n%s\n" % (100*"abc123", 100*"def456") |
Walter Dörwald | 729c31f | 2005-03-14 19:06:30 +0000 | [diff] [blame] | 1410 | for encoding in all_unicode_encodings: |
| 1411 | if encoding == "idna": # FIXME: See SF bug #1163178 |
| 1412 | continue |
| 1413 | if encoding in broken_unicode_with_streams: |
| 1414 | continue |
Walter Dörwald | c3ab0a7 | 2007-05-10 15:02:49 +0000 | [diff] [blame] | 1415 | reader = codecs.getreader(encoding)(io.BytesIO(s.encode(encoding))) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1416 | for t in range(5): |
Walter Dörwald | 729c31f | 2005-03-14 19:06:30 +0000 | [diff] [blame] | 1417 | # Test that calling seek resets the internal codec state and buffers |
| 1418 | reader.seek(0, 0) |
Guido van Rossum | f4cfc8f | 2007-05-17 21:52:23 +0000 | [diff] [blame] | 1419 | data = reader.read() |
| 1420 | self.assertEqual(s, data) |
Walter Dörwald | 729c31f | 2005-03-14 19:06:30 +0000 | [diff] [blame] | 1421 | |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 1422 | def test_bad_decode_args(self): |
| 1423 | for encoding in all_unicode_encodings: |
| 1424 | decoder = codecs.getdecoder(encoding) |
| 1425 | self.assertRaises(TypeError, decoder) |
| 1426 | if encoding not in ("idna", "punycode"): |
| 1427 | self.assertRaises(TypeError, decoder, 42) |
| 1428 | |
| 1429 | def test_bad_encode_args(self): |
| 1430 | for encoding in all_unicode_encodings: |
| 1431 | encoder = codecs.getencoder(encoding) |
| 1432 | self.assertRaises(TypeError, encoder) |
| 1433 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1434 | def test_encoding_map_type_initialized(self): |
| 1435 | from encodings import cp1140 |
| 1436 | # This used to crash, we are only verifying there's no crash. |
| 1437 | table_type = type(cp1140.encoding_table) |
| 1438 | self.assertEqual(table_type, table_type) |
| 1439 | |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 1440 | def test_decoder_state(self): |
| 1441 | # Check that getstate() and setstate() handle the state properly |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1442 | u = "abc123" |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 1443 | for encoding in all_unicode_encodings: |
| 1444 | if encoding not in broken_incremental_coders: |
| 1445 | self.check_state_handling_decode(encoding, u, u.encode(encoding)) |
| 1446 | self.check_state_handling_encode(encoding, u, u.encode(encoding)) |
| 1447 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 1448 | class CharmapTest(unittest.TestCase): |
| 1449 | def test_decode_with_string_map(self): |
| 1450 | self.assertEquals( |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1451 | codecs.charmap_decode(b"\x00\x01\x02", "strict", "abc"), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1452 | ("abc", 3) |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 1453 | ) |
| 1454 | |
| 1455 | self.assertEquals( |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1456 | codecs.charmap_decode(b"\x00\x01\x02", "replace", "ab"), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1457 | ("ab\ufffd", 3) |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 1458 | ) |
| 1459 | |
| 1460 | self.assertEquals( |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1461 | codecs.charmap_decode(b"\x00\x01\x02", "replace", "ab\ufffe"), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1462 | ("ab\ufffd", 3) |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 1463 | ) |
| 1464 | |
| 1465 | self.assertEquals( |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1466 | codecs.charmap_decode(b"\x00\x01\x02", "ignore", "ab"), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1467 | ("ab", 3) |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 1468 | ) |
| 1469 | |
| 1470 | self.assertEquals( |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1471 | codecs.charmap_decode(b"\x00\x01\x02", "ignore", "ab\ufffe"), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1472 | ("ab", 3) |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 1473 | ) |
| 1474 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1475 | allbytes = bytes(range(256)) |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 1476 | self.assertEquals( |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1477 | codecs.charmap_decode(allbytes, "ignore", ""), |
| 1478 | ("", len(allbytes)) |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 1479 | ) |
| 1480 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1481 | class WithStmtTest(unittest.TestCase): |
| 1482 | def test_encodedfile(self): |
Walter Dörwald | c3ab0a7 | 2007-05-10 15:02:49 +0000 | [diff] [blame] | 1483 | f = io.BytesIO(b"\xc3\xbc") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1484 | with codecs.EncodedFile(f, "latin-1", "utf-8") as ef: |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1485 | self.assertEquals(ef.read(), b"\xfc") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1486 | |
| 1487 | def test_streamreaderwriter(self): |
Walter Dörwald | c3ab0a7 | 2007-05-10 15:02:49 +0000 | [diff] [blame] | 1488 | f = io.BytesIO(b"\xc3\xbc") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1489 | info = codecs.lookup("utf-8") |
| 1490 | with codecs.StreamReaderWriter(f, info.streamreader, |
| 1491 | info.streamwriter, 'strict') as srw: |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1492 | self.assertEquals(srw.read(), "\xfc") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1493 | |
Antoine Pitrou | 81fabdb | 2009-01-22 10:11:36 +0000 | [diff] [blame] | 1494 | class TypesTest(unittest.TestCase): |
| 1495 | def test_decode_unicode(self): |
| 1496 | # Most decoders don't accept unicode input |
| 1497 | decoders = [ |
| 1498 | codecs.utf_7_decode, |
| 1499 | codecs.utf_8_decode, |
| 1500 | codecs.utf_16_le_decode, |
| 1501 | codecs.utf_16_be_decode, |
| 1502 | codecs.utf_16_ex_decode, |
| 1503 | codecs.utf_32_decode, |
| 1504 | codecs.utf_32_le_decode, |
| 1505 | codecs.utf_32_be_decode, |
| 1506 | codecs.utf_32_ex_decode, |
| 1507 | codecs.latin_1_decode, |
| 1508 | codecs.ascii_decode, |
| 1509 | codecs.charmap_decode, |
| 1510 | ] |
| 1511 | if hasattr(codecs, "mbcs_decode"): |
| 1512 | decoders.append(codecs.mbcs_decode) |
| 1513 | for decoder in decoders: |
| 1514 | self.assertRaises(TypeError, decoder, "xxx") |
| 1515 | |
| 1516 | def test_unicode_escape(self): |
| 1517 | # Escape-decoding an unicode string is supported ang gives the same |
| 1518 | # result as decoding the equivalent ASCII bytes string. |
| 1519 | self.assertEquals(codecs.unicode_escape_decode(r"\u1234"), ("\u1234", 6)) |
| 1520 | self.assertEquals(codecs.unicode_escape_decode(br"\u1234"), ("\u1234", 6)) |
| 1521 | self.assertEquals(codecs.raw_unicode_escape_decode(r"\u1234"), ("\u1234", 6)) |
| 1522 | self.assertEquals(codecs.raw_unicode_escape_decode(br"\u1234"), ("\u1234", 6)) |
| 1523 | |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 1524 | class SurrogateEscapeTest(unittest.TestCase): |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1525 | |
| 1526 | def test_utf8(self): |
| 1527 | # Bad byte |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 1528 | self.assertEqual(b"foo\x80bar".decode("utf-8", "surrogateescape"), |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1529 | "foo\udc80bar") |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 1530 | self.assertEqual("foo\udc80bar".encode("utf-8", "surrogateescape"), |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1531 | b"foo\x80bar") |
| 1532 | # bad-utf-8 encoded surrogate |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 1533 | self.assertEqual(b"\xed\xb0\x80".decode("utf-8", "surrogateescape"), |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1534 | "\udced\udcb0\udc80") |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 1535 | self.assertEqual("\udced\udcb0\udc80".encode("utf-8", "surrogateescape"), |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1536 | b"\xed\xb0\x80") |
| 1537 | |
| 1538 | def test_ascii(self): |
| 1539 | # bad byte |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 1540 | self.assertEqual(b"foo\x80bar".decode("ascii", "surrogateescape"), |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1541 | "foo\udc80bar") |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 1542 | self.assertEqual("foo\udc80bar".encode("ascii", "surrogateescape"), |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1543 | b"foo\x80bar") |
| 1544 | |
| 1545 | def test_charmap(self): |
| 1546 | # bad byte: \xa5 is unmapped in iso-8859-3 |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 1547 | self.assertEqual(b"foo\xa5bar".decode("iso-8859-3", "surrogateescape"), |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1548 | "foo\udca5bar") |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 1549 | self.assertEqual("foo\udca5bar".encode("iso-8859-3", "surrogateescape"), |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 1550 | b"foo\xa5bar") |
| 1551 | |
Amaury Forgeot d'Arc | e5344d6 | 2009-06-29 22:38:54 +0000 | [diff] [blame^] | 1552 | def test_latin1(self): |
| 1553 | # Issue6373 |
| 1554 | self.assertEqual("\udce4\udceb\udcef\udcf6\udcfc".encode("latin1", "surrogateescape"), |
| 1555 | b"\xe4\xeb\xef\xf6\xfc") |
| 1556 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 1557 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 1558 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1559 | support.run_unittest( |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 1560 | UTF32Test, |
| 1561 | UTF32LETest, |
| 1562 | UTF32BETest, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 1563 | UTF16Test, |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 1564 | UTF16LETest, |
| 1565 | UTF16BETest, |
| 1566 | UTF8Test, |
Martin v. Löwis | 412ed3b | 2006-01-08 10:45:39 +0000 | [diff] [blame] | 1567 | UTF8SigTest, |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 1568 | UTF7Test, |
| 1569 | UTF16ExTest, |
| 1570 | ReadBufferTest, |
| 1571 | CharBufferTest, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 1572 | RecodingTest, |
| 1573 | PunycodeTest, |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 1574 | UnicodeInternalTest, |
Martin v. Löwis | a1dde13 | 2004-03-24 16:48:24 +0000 | [diff] [blame] | 1575 | NameprepTest, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1576 | IDNACodecTest, |
Hye-Shik Chang | af5c7cf | 2004-10-17 23:51:21 +0000 | [diff] [blame] | 1577 | CodecsModuleTest, |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1578 | StreamReaderTest, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1579 | EncodedFileTest, |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1580 | BasicUnicodeTest, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1581 | CharmapTest, |
| 1582 | WithStmtTest, |
Antoine Pitrou | 81fabdb | 2009-01-22 10:11:36 +0000 | [diff] [blame] | 1583 | TypesTest, |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 1584 | SurrogateEscapeTest, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 1585 | ) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 1586 | |
| 1587 | |
| 1588 | if __name__ == "__main__": |
| 1589 | test_main() |