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