Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 1 | import codecs |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2 | import contextlib |
Victor Stinner | 040e16e | 2011-11-15 22:44:05 +0100 | [diff] [blame] | 3 | import io |
Antoine Pitrou | cf9d3c0 | 2011-07-24 02:27:04 +0200 | [diff] [blame] | 4 | import locale |
Victor Stinner | 040e16e | 2011-11-15 22:44:05 +0100 | [diff] [blame] | 5 | import sys |
| 6 | import unittest |
| 7 | import warnings |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 8 | import encodings |
Victor Stinner | 040e16e | 2011-11-15 22:44:05 +0100 | [diff] [blame] | 9 | |
| 10 | from test import support |
Victor Stinner | 182d90d | 2011-09-29 19:53:55 +0200 | [diff] [blame] | 11 | |
Victor Stinner | 2f3ca9f | 2011-10-27 01:38:56 +0200 | [diff] [blame] | 12 | if sys.platform == 'win32': |
| 13 | VISTA_OR_LATER = (sys.getwindowsversion().major >= 6) |
| 14 | else: |
| 15 | VISTA_OR_LATER = False |
| 16 | |
Antoine Pitrou | 00b2c86 | 2011-10-05 13:01:41 +0200 | [diff] [blame] | 17 | try: |
| 18 | import ctypes |
| 19 | except ImportError: |
| 20 | ctypes = None |
| 21 | SIZEOF_WCHAR_T = -1 |
| 22 | else: |
| 23 | SIZEOF_WCHAR_T = ctypes.sizeof(ctypes.c_wchar) |
Marc-André Lemburg | a37171d | 2001-06-19 20:09:28 +0000 | [diff] [blame] | 24 | |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 25 | def coding_checker(self, coder): |
| 26 | def check(input, expect): |
| 27 | self.assertEqual(coder(input), (expect, len(input))) |
| 28 | return check |
| 29 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 30 | class Queue(object): |
| 31 | """ |
| 32 | queue: write bytes at one end, read bytes from the other end |
| 33 | """ |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 34 | def __init__(self, buffer): |
| 35 | self._buffer = buffer |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 36 | |
| 37 | def write(self, chars): |
| 38 | self._buffer += chars |
| 39 | |
| 40 | def read(self, size=-1): |
| 41 | if size<0: |
| 42 | s = self._buffer |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 43 | self._buffer = self._buffer[:0] # make empty |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 44 | return s |
| 45 | else: |
| 46 | s = self._buffer[:size] |
| 47 | self._buffer = self._buffer[size:] |
| 48 | return s |
| 49 | |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 50 | class MixInCheckStateHandling: |
| 51 | def check_state_handling_decode(self, encoding, u, s): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 52 | for i in range(len(s)+1): |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 53 | d = codecs.getincrementaldecoder(encoding)() |
| 54 | part1 = d.decode(s[:i]) |
| 55 | state = d.getstate() |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 56 | self.assertIsInstance(state[1], int) |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 57 | # Check that the condition stated in the documentation for |
| 58 | # IncrementalDecoder.getstate() holds |
| 59 | if not state[1]: |
| 60 | # reset decoder to the default state without anything buffered |
| 61 | d.setstate((state[0][:0], 0)) |
| 62 | # Feeding the previous input may not produce any output |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 63 | self.assertTrue(not d.decode(state[0])) |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 64 | # The decoder must return to the same state |
| 65 | self.assertEqual(state, d.getstate()) |
| 66 | # Create a new decoder and set it to the state |
| 67 | # we extracted from the old one |
| 68 | d = codecs.getincrementaldecoder(encoding)() |
| 69 | d.setstate(state) |
| 70 | part2 = d.decode(s[i:], True) |
| 71 | self.assertEqual(u, part1+part2) |
| 72 | |
| 73 | def check_state_handling_encode(self, encoding, u, s): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 74 | for i in range(len(u)+1): |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 75 | d = codecs.getincrementalencoder(encoding)() |
| 76 | part1 = d.encode(u[:i]) |
| 77 | state = d.getstate() |
| 78 | d = codecs.getincrementalencoder(encoding)() |
| 79 | d.setstate(state) |
| 80 | part2 = d.encode(u[i:], True) |
| 81 | self.assertEqual(s, part1+part2) |
| 82 | |
Ezio Melotti | 5d3dba0 | 2013-01-11 06:02:07 +0200 | [diff] [blame] | 83 | class ReadTest(MixInCheckStateHandling): |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 84 | def check_partial(self, input, partialresults): |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 85 | # get a StreamReader for the encoding and feed the bytestring version |
Guido van Rossum | 87c0f1d | 2007-11-19 18:03:44 +0000 | [diff] [blame] | 86 | # 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] | 87 | # the StreamReader and check that the results equal the appropriate |
| 88 | # entries from partialresults. |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 89 | q = Queue(b"") |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 90 | r = codecs.getreader(self.encoding)(q) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 91 | result = "" |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 92 | for (c, partialresult) in zip(input.encode(self.encoding), partialresults): |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 93 | q.write(bytes([c])) |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 94 | result += r.read() |
| 95 | self.assertEqual(result, partialresult) |
| 96 | # check that there's nothing left in the buffers |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 97 | self.assertEqual(r.read(), "") |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 98 | self.assertEqual(r.bytebuffer, b"") |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 99 | |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 100 | # do the check again, this time using an incremental decoder |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 101 | d = codecs.getincrementaldecoder(self.encoding)() |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 102 | result = "" |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 103 | for (c, partialresult) in zip(input.encode(self.encoding), partialresults): |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 104 | result += d.decode(bytes([c])) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 105 | self.assertEqual(result, partialresult) |
| 106 | # check that there's nothing left in the buffers |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 107 | self.assertEqual(d.decode(b"", True), "") |
| 108 | self.assertEqual(d.buffer, b"") |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 109 | |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 110 | # Check whether the reset method works properly |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 111 | d.reset() |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 112 | result = "" |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 113 | for (c, partialresult) in zip(input.encode(self.encoding), partialresults): |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 114 | result += d.decode(bytes([c])) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 115 | self.assertEqual(result, partialresult) |
| 116 | # check that there's nothing left in the buffers |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 117 | self.assertEqual(d.decode(b"", True), "") |
| 118 | self.assertEqual(d.buffer, b"") |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 119 | |
| 120 | # check iterdecode() |
| 121 | encoded = input.encode(self.encoding) |
| 122 | self.assertEqual( |
| 123 | input, |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 124 | "".join(codecs.iterdecode([bytes([c]) for c in encoded], self.encoding)) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 125 | ) |
| 126 | |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 127 | def test_readline(self): |
| 128 | def getreader(input): |
Walter Dörwald | c3ab0a7 | 2007-05-10 15:02:49 +0000 | [diff] [blame] | 129 | stream = io.BytesIO(input.encode(self.encoding)) |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 130 | return codecs.getreader(self.encoding)(stream) |
| 131 | |
Walter Dörwald | ca19943 | 2006-03-06 22:39:12 +0000 | [diff] [blame] | 132 | def readalllines(input, keepends=True, size=None): |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 133 | reader = getreader(input) |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 134 | lines = [] |
| 135 | while True: |
Walter Dörwald | ca19943 | 2006-03-06 22:39:12 +0000 | [diff] [blame] | 136 | line = reader.readline(size=size, keepends=keepends) |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 137 | if not line: |
| 138 | break |
| 139 | lines.append(line) |
Walter Dörwald | ca19943 | 2006-03-06 22:39:12 +0000 | [diff] [blame] | 140 | return "|".join(lines) |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 141 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 142 | s = "foo\nbar\r\nbaz\rspam\u2028eggs" |
| 143 | sexpected = "foo\n|bar\r\n|baz\r|spam\u2028|eggs" |
| 144 | sexpectednoends = "foo|bar|baz|spam|eggs" |
Walter Dörwald | ca19943 | 2006-03-06 22:39:12 +0000 | [diff] [blame] | 145 | self.assertEqual(readalllines(s, True), sexpected) |
| 146 | self.assertEqual(readalllines(s, False), sexpectednoends) |
| 147 | self.assertEqual(readalllines(s, True, 10), sexpected) |
| 148 | self.assertEqual(readalllines(s, False, 10), sexpectednoends) |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 149 | |
Serhiy Storchaka | 5b4fab1 | 2014-02-06 09:26:56 +0200 | [diff] [blame] | 150 | lineends = ("\n", "\r\n", "\r", "\u2028") |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 151 | # Test long lines (multiple calls to read() in readline()) |
| 152 | vw = [] |
| 153 | vwo = [] |
Serhiy Storchaka | 5b4fab1 | 2014-02-06 09:26:56 +0200 | [diff] [blame] | 154 | for (i, lineend) in enumerate(lineends): |
| 155 | vw.append((i*200+200)*"\u3042" + lineend) |
| 156 | vwo.append((i*200+200)*"\u3042") |
| 157 | self.assertEqual(readalllines("".join(vw), True), "|".join(vw)) |
| 158 | self.assertEqual(readalllines("".join(vw), False), "|".join(vwo)) |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 159 | |
| 160 | # Test lines where the first read might end with \r, so the |
| 161 | # 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] | 162 | for size in range(80): |
Serhiy Storchaka | 5b4fab1 | 2014-02-06 09:26:56 +0200 | [diff] [blame] | 163 | for lineend in lineends: |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 164 | s = 10*(size*"a" + lineend + "xxx\n") |
Walter Dörwald | 7a6dc13 | 2005-04-04 21:38:47 +0000 | [diff] [blame] | 165 | reader = getreader(s) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 166 | for i in range(10): |
Walter Dörwald | 7a6dc13 | 2005-04-04 21:38:47 +0000 | [diff] [blame] | 167 | self.assertEqual( |
| 168 | reader.readline(keepends=True), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 169 | size*"a" + lineend, |
Walter Dörwald | 7a6dc13 | 2005-04-04 21:38:47 +0000 | [diff] [blame] | 170 | ) |
Serhiy Storchaka | 5b4fab1 | 2014-02-06 09:26:56 +0200 | [diff] [blame] | 171 | self.assertEqual( |
| 172 | reader.readline(keepends=True), |
| 173 | "xxx\n", |
| 174 | ) |
Walter Dörwald | 7a6dc13 | 2005-04-04 21:38:47 +0000 | [diff] [blame] | 175 | reader = getreader(s) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 176 | for i in range(10): |
Walter Dörwald | 7a6dc13 | 2005-04-04 21:38:47 +0000 | [diff] [blame] | 177 | self.assertEqual( |
| 178 | reader.readline(keepends=False), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 179 | size*"a", |
Walter Dörwald | 7a6dc13 | 2005-04-04 21:38:47 +0000 | [diff] [blame] | 180 | ) |
Serhiy Storchaka | 5b4fab1 | 2014-02-06 09:26:56 +0200 | [diff] [blame] | 181 | self.assertEqual( |
| 182 | reader.readline(keepends=False), |
| 183 | "xxx", |
| 184 | ) |
Walter Dörwald | 7a6dc13 | 2005-04-04 21:38:47 +0000 | [diff] [blame] | 185 | |
Serhiy Storchaka | dbe0982 | 2014-01-26 19:27:56 +0200 | [diff] [blame] | 186 | def test_mixed_readline_and_read(self): |
| 187 | lines = ["Humpty Dumpty sat on a wall,\n", |
| 188 | "Humpty Dumpty had a great fall.\r\n", |
| 189 | "All the king's horses and all the king's men\r", |
| 190 | "Couldn't put Humpty together again."] |
| 191 | data = ''.join(lines) |
| 192 | def getreader(): |
| 193 | stream = io.BytesIO(data.encode(self.encoding)) |
| 194 | return codecs.getreader(self.encoding)(stream) |
| 195 | |
| 196 | # Issue #8260: Test readline() followed by read() |
| 197 | f = getreader() |
| 198 | self.assertEqual(f.readline(), lines[0]) |
| 199 | self.assertEqual(f.read(), ''.join(lines[1:])) |
| 200 | self.assertEqual(f.read(), '') |
| 201 | |
| 202 | # Issue #16636: Test readline() followed by readlines() |
| 203 | f = getreader() |
| 204 | self.assertEqual(f.readline(), lines[0]) |
| 205 | self.assertEqual(f.readlines(), lines[1:]) |
| 206 | self.assertEqual(f.read(), '') |
| 207 | |
| 208 | # Test read() followed by read() |
| 209 | f = getreader() |
| 210 | self.assertEqual(f.read(size=40, chars=5), data[:5]) |
| 211 | self.assertEqual(f.read(), data[5:]) |
| 212 | self.assertEqual(f.read(), '') |
| 213 | |
| 214 | # Issue #12446: Test read() followed by readlines() |
| 215 | f = getreader() |
| 216 | self.assertEqual(f.read(size=40, chars=5), data[:5]) |
| 217 | self.assertEqual(f.readlines(), [lines[0][5:]] + lines[1:]) |
| 218 | self.assertEqual(f.read(), '') |
| 219 | |
Walter Dörwald | 7a6dc13 | 2005-04-04 21:38:47 +0000 | [diff] [blame] | 220 | def test_bug1175396(self): |
| 221 | s = [ |
| 222 | '<%!--===================================================\r\n', |
| 223 | ' BLOG index page: show recent articles,\r\n', |
| 224 | ' today\'s articles, or articles of a specific date.\r\n', |
| 225 | '========================================================--%>\r\n', |
| 226 | '<%@inputencoding="ISO-8859-1"%>\r\n', |
| 227 | '<%@pagetemplate=TEMPLATE.y%>\r\n', |
| 228 | '<%@import=import frog.util, frog%>\r\n', |
| 229 | '<%@import=import frog.objects%>\r\n', |
| 230 | '<%@import=from frog.storageerrors import StorageError%>\r\n', |
| 231 | '<%\r\n', |
| 232 | '\r\n', |
| 233 | 'import logging\r\n', |
| 234 | 'log=logging.getLogger("Snakelets.logger")\r\n', |
| 235 | '\r\n', |
| 236 | '\r\n', |
| 237 | 'user=self.SessionCtx.user\r\n', |
| 238 | 'storageEngine=self.SessionCtx.storageEngine\r\n', |
| 239 | '\r\n', |
| 240 | '\r\n', |
| 241 | 'def readArticlesFromDate(date, count=None):\r\n', |
| 242 | ' entryids=storageEngine.listBlogEntries(date)\r\n', |
| 243 | ' entryids.reverse() # descending\r\n', |
| 244 | ' if count:\r\n', |
| 245 | ' entryids=entryids[:count]\r\n', |
| 246 | ' try:\r\n', |
| 247 | ' return [ frog.objects.BlogEntry.load(storageEngine, date, Id) for Id in entryids ]\r\n', |
| 248 | ' except StorageError,x:\r\n', |
| 249 | ' log.error("Error loading articles: "+str(x))\r\n', |
| 250 | ' self.abort("cannot load articles")\r\n', |
| 251 | '\r\n', |
| 252 | 'showdate=None\r\n', |
| 253 | '\r\n', |
| 254 | 'arg=self.Request.getArg()\r\n', |
| 255 | 'if arg=="today":\r\n', |
| 256 | ' #-------------------- TODAY\'S ARTICLES\r\n', |
| 257 | ' self.write("<h2>Today\'s articles</h2>")\r\n', |
| 258 | ' showdate = frog.util.isodatestr() \r\n', |
| 259 | ' entries = readArticlesFromDate(showdate)\r\n', |
| 260 | 'elif arg=="active":\r\n', |
| 261 | ' #-------------------- ACTIVE ARTICLES redirect\r\n', |
| 262 | ' self.Yredirect("active.y")\r\n', |
| 263 | 'elif arg=="login":\r\n', |
| 264 | ' #-------------------- LOGIN PAGE redirect\r\n', |
| 265 | ' self.Yredirect("login.y")\r\n', |
| 266 | 'elif arg=="date":\r\n', |
| 267 | ' #-------------------- ARTICLES OF A SPECIFIC DATE\r\n', |
| 268 | ' showdate = self.Request.getParameter("date")\r\n', |
| 269 | ' self.write("<h2>Articles written on %s</h2>"% frog.util.mediumdatestr(showdate))\r\n', |
| 270 | ' entries = readArticlesFromDate(showdate)\r\n', |
| 271 | 'else:\r\n', |
| 272 | ' #-------------------- RECENT ARTICLES\r\n', |
| 273 | ' self.write("<h2>Recent articles</h2>")\r\n', |
| 274 | ' dates=storageEngine.listBlogEntryDates()\r\n', |
| 275 | ' if dates:\r\n', |
| 276 | ' entries=[]\r\n', |
| 277 | ' SHOWAMOUNT=10\r\n', |
| 278 | ' for showdate in dates:\r\n', |
| 279 | ' entries.extend( readArticlesFromDate(showdate, SHOWAMOUNT-len(entries)) )\r\n', |
| 280 | ' if len(entries)>=SHOWAMOUNT:\r\n', |
| 281 | ' break\r\n', |
| 282 | ' \r\n', |
| 283 | ] |
Walter Dörwald | c3ab0a7 | 2007-05-10 15:02:49 +0000 | [diff] [blame] | 284 | stream = io.BytesIO("".join(s).encode(self.encoding)) |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 285 | reader = codecs.getreader(self.encoding)(stream) |
Walter Dörwald | 7a6dc13 | 2005-04-04 21:38:47 +0000 | [diff] [blame] | 286 | for (i, line) in enumerate(reader): |
| 287 | self.assertEqual(line, s[i]) |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 288 | |
| 289 | def test_readlinequeue(self): |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 290 | q = Queue(b"") |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 291 | writer = codecs.getwriter(self.encoding)(q) |
| 292 | reader = codecs.getreader(self.encoding)(q) |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 293 | |
| 294 | # No lineends |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 295 | writer.write("foo\r") |
| 296 | self.assertEqual(reader.readline(keepends=False), "foo") |
| 297 | writer.write("\nbar\r") |
| 298 | self.assertEqual(reader.readline(keepends=False), "") |
| 299 | self.assertEqual(reader.readline(keepends=False), "bar") |
| 300 | writer.write("baz") |
| 301 | self.assertEqual(reader.readline(keepends=False), "baz") |
| 302 | self.assertEqual(reader.readline(keepends=False), "") |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 303 | |
| 304 | # Lineends |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 305 | writer.write("foo\r") |
| 306 | self.assertEqual(reader.readline(keepends=True), "foo\r") |
| 307 | writer.write("\nbar\r") |
| 308 | self.assertEqual(reader.readline(keepends=True), "\n") |
| 309 | self.assertEqual(reader.readline(keepends=True), "bar\r") |
| 310 | writer.write("baz") |
| 311 | self.assertEqual(reader.readline(keepends=True), "baz") |
| 312 | self.assertEqual(reader.readline(keepends=True), "") |
| 313 | writer.write("foo\r\n") |
| 314 | self.assertEqual(reader.readline(keepends=True), "foo\r\n") |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 315 | |
Walter Dörwald | 9fa0946 | 2005-01-10 12:01:39 +0000 | [diff] [blame] | 316 | def test_bug1098990_a(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 317 | s1 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\r\n" |
| 318 | s2 = "offending line: ladfj askldfj klasdj fskla dfzaskdj fasklfj laskd fjasklfzzzzaa%whereisthis!!!\r\n" |
| 319 | s3 = "next line.\r\n" |
Walter Dörwald | 9fa0946 | 2005-01-10 12:01:39 +0000 | [diff] [blame] | 320 | |
| 321 | s = (s1+s2+s3).encode(self.encoding) |
Walter Dörwald | c3ab0a7 | 2007-05-10 15:02:49 +0000 | [diff] [blame] | 322 | stream = io.BytesIO(s) |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 323 | reader = codecs.getreader(self.encoding)(stream) |
Walter Dörwald | 9fa0946 | 2005-01-10 12:01:39 +0000 | [diff] [blame] | 324 | self.assertEqual(reader.readline(), s1) |
| 325 | self.assertEqual(reader.readline(), s2) |
| 326 | self.assertEqual(reader.readline(), s3) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 327 | self.assertEqual(reader.readline(), "") |
Walter Dörwald | 9fa0946 | 2005-01-10 12:01:39 +0000 | [diff] [blame] | 328 | |
| 329 | def test_bug1098990_b(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 330 | s1 = "aaaaaaaaaaaaaaaaaaaaaaaa\r\n" |
| 331 | s2 = "bbbbbbbbbbbbbbbbbbbbbbbb\r\n" |
| 332 | s3 = "stillokay:bbbbxx\r\n" |
| 333 | s4 = "broken!!!!badbad\r\n" |
| 334 | s5 = "againokay.\r\n" |
Walter Dörwald | 9fa0946 | 2005-01-10 12:01:39 +0000 | [diff] [blame] | 335 | |
| 336 | s = (s1+s2+s3+s4+s5).encode(self.encoding) |
Walter Dörwald | c3ab0a7 | 2007-05-10 15:02:49 +0000 | [diff] [blame] | 337 | stream = io.BytesIO(s) |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 338 | reader = codecs.getreader(self.encoding)(stream) |
Walter Dörwald | 9fa0946 | 2005-01-10 12:01:39 +0000 | [diff] [blame] | 339 | self.assertEqual(reader.readline(), s1) |
| 340 | self.assertEqual(reader.readline(), s2) |
| 341 | self.assertEqual(reader.readline(), s3) |
| 342 | self.assertEqual(reader.readline(), s4) |
| 343 | self.assertEqual(reader.readline(), s5) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 344 | self.assertEqual(reader.readline(), "") |
Walter Dörwald | 9fa0946 | 2005-01-10 12:01:39 +0000 | [diff] [blame] | 345 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 346 | ill_formed_sequence_replace = "\ufffd" |
| 347 | |
| 348 | def test_lone_surrogates(self): |
| 349 | self.assertRaises(UnicodeEncodeError, "\ud800".encode, self.encoding) |
| 350 | self.assertEqual("[\uDC80]".encode(self.encoding, "backslashreplace"), |
| 351 | "[\\udc80]".encode(self.encoding)) |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 352 | self.assertEqual("[\uDC80]".encode(self.encoding, "namereplace"), |
| 353 | "[\\udc80]".encode(self.encoding)) |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 354 | self.assertEqual("[\uDC80]".encode(self.encoding, "xmlcharrefreplace"), |
| 355 | "[�]".encode(self.encoding)) |
| 356 | self.assertEqual("[\uDC80]".encode(self.encoding, "ignore"), |
| 357 | "[]".encode(self.encoding)) |
| 358 | self.assertEqual("[\uDC80]".encode(self.encoding, "replace"), |
| 359 | "[?]".encode(self.encoding)) |
| 360 | |
| 361 | bom = "".encode(self.encoding) |
| 362 | for before, after in [("\U00010fff", "A"), ("[", "]"), |
| 363 | ("A", "\U00010fff")]: |
| 364 | before_sequence = before.encode(self.encoding)[len(bom):] |
| 365 | after_sequence = after.encode(self.encoding)[len(bom):] |
| 366 | test_string = before + "\uDC80" + after |
| 367 | test_sequence = (bom + before_sequence + |
| 368 | self.ill_formed_sequence + after_sequence) |
| 369 | self.assertRaises(UnicodeDecodeError, test_sequence.decode, |
| 370 | self.encoding) |
| 371 | self.assertEqual(test_string.encode(self.encoding, |
| 372 | "surrogatepass"), |
| 373 | test_sequence) |
| 374 | self.assertEqual(test_sequence.decode(self.encoding, |
| 375 | "surrogatepass"), |
| 376 | test_string) |
| 377 | self.assertEqual(test_sequence.decode(self.encoding, "ignore"), |
| 378 | before + after) |
| 379 | self.assertEqual(test_sequence.decode(self.encoding, "replace"), |
| 380 | before + self.ill_formed_sequence_replace + after) |
Serhiy Storchaka | 07985ef | 2015-01-25 22:56:57 +0200 | [diff] [blame] | 381 | backslashreplace = ''.join('\\x%02x' % b |
| 382 | for b in self.ill_formed_sequence) |
| 383 | self.assertEqual(test_sequence.decode(self.encoding, "backslashreplace"), |
| 384 | before + backslashreplace + after) |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 385 | |
Ezio Melotti | 5d3dba0 | 2013-01-11 06:02:07 +0200 | [diff] [blame] | 386 | class UTF32Test(ReadTest, unittest.TestCase): |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 387 | encoding = "utf-32" |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 388 | if sys.byteorder == 'little': |
| 389 | ill_formed_sequence = b"\x80\xdc\x00\x00" |
| 390 | else: |
| 391 | ill_formed_sequence = b"\x00\x00\xdc\x80" |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 392 | |
| 393 | spamle = (b'\xff\xfe\x00\x00' |
| 394 | b's\x00\x00\x00p\x00\x00\x00a\x00\x00\x00m\x00\x00\x00' |
| 395 | b's\x00\x00\x00p\x00\x00\x00a\x00\x00\x00m\x00\x00\x00') |
| 396 | spambe = (b'\x00\x00\xfe\xff' |
| 397 | b'\x00\x00\x00s\x00\x00\x00p\x00\x00\x00a\x00\x00\x00m' |
| 398 | b'\x00\x00\x00s\x00\x00\x00p\x00\x00\x00a\x00\x00\x00m') |
| 399 | |
| 400 | def test_only_one_bom(self): |
| 401 | _,_,reader,writer = codecs.lookup(self.encoding) |
| 402 | # encode some stream |
| 403 | s = io.BytesIO() |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 404 | f = writer(s) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 405 | f.write("spam") |
| 406 | f.write("spam") |
| 407 | d = s.getvalue() |
| 408 | # check whether there is exactly one BOM in it |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 409 | self.assertTrue(d == self.spamle or d == self.spambe) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 410 | # try to read it back |
| 411 | s = io.BytesIO(d) |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 412 | f = reader(s) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 413 | self.assertEqual(f.read(), "spamspam") |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 414 | |
| 415 | def test_badbom(self): |
| 416 | s = io.BytesIO(4*b"\xff") |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 417 | f = codecs.getreader(self.encoding)(s) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 418 | self.assertRaises(UnicodeError, f.read) |
| 419 | |
| 420 | s = io.BytesIO(8*b"\xff") |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 421 | f = codecs.getreader(self.encoding)(s) |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 422 | self.assertRaises(UnicodeError, f.read) |
| 423 | |
| 424 | def test_partial(self): |
| 425 | self.check_partial( |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 426 | "\x00\xff\u0100\uffff\U00010000", |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 427 | [ |
| 428 | "", # first byte of BOM read |
| 429 | "", # second byte of BOM read |
| 430 | "", # third byte of BOM read |
| 431 | "", # fourth byte of BOM read => byteorder known |
| 432 | "", |
| 433 | "", |
| 434 | "", |
| 435 | "\x00", |
| 436 | "\x00", |
| 437 | "\x00", |
| 438 | "\x00", |
| 439 | "\x00\xff", |
| 440 | "\x00\xff", |
| 441 | "\x00\xff", |
| 442 | "\x00\xff", |
| 443 | "\x00\xff\u0100", |
| 444 | "\x00\xff\u0100", |
| 445 | "\x00\xff\u0100", |
| 446 | "\x00\xff\u0100", |
| 447 | "\x00\xff\u0100\uffff", |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 448 | "\x00\xff\u0100\uffff", |
| 449 | "\x00\xff\u0100\uffff", |
| 450 | "\x00\xff\u0100\uffff", |
| 451 | "\x00\xff\u0100\uffff\U00010000", |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 452 | ] |
| 453 | ) |
| 454 | |
Georg Brandl | 791f4e1 | 2009-09-17 11:41:24 +0000 | [diff] [blame] | 455 | def test_handlers(self): |
| 456 | self.assertEqual(('\ufffd', 1), |
| 457 | codecs.utf_32_decode(b'\x01', 'replace', True)) |
| 458 | self.assertEqual(('', 1), |
| 459 | codecs.utf_32_decode(b'\x01', 'ignore', True)) |
| 460 | |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 461 | def test_errors(self): |
| 462 | self.assertRaises(UnicodeDecodeError, codecs.utf_32_decode, |
| 463 | b"\xff", "strict", True) |
| 464 | |
| 465 | def test_decoder_state(self): |
| 466 | self.check_state_handling_decode(self.encoding, |
| 467 | "spamspam", self.spamle) |
| 468 | self.check_state_handling_decode(self.encoding, |
| 469 | "spamspam", self.spambe) |
| 470 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 471 | def test_issue8941(self): |
| 472 | # Issue #8941: insufficient result allocation when decoding into |
| 473 | # surrogate pairs on UCS-2 builds. |
| 474 | encoded_le = b'\xff\xfe\x00\x00' + b'\x00\x00\x01\x00' * 1024 |
| 475 | self.assertEqual('\U00010000' * 1024, |
| 476 | codecs.utf_32_decode(encoded_le)[0]) |
| 477 | encoded_be = b'\x00\x00\xfe\xff' + b'\x00\x01\x00\x00' * 1024 |
| 478 | self.assertEqual('\U00010000' * 1024, |
| 479 | codecs.utf_32_decode(encoded_be)[0]) |
| 480 | |
Ezio Melotti | 5d3dba0 | 2013-01-11 06:02:07 +0200 | [diff] [blame] | 481 | class UTF32LETest(ReadTest, unittest.TestCase): |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 482 | encoding = "utf-32-le" |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 483 | ill_formed_sequence = b"\x80\xdc\x00\x00" |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 484 | |
| 485 | def test_partial(self): |
| 486 | self.check_partial( |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 487 | "\x00\xff\u0100\uffff\U00010000", |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 488 | [ |
| 489 | "", |
| 490 | "", |
| 491 | "", |
| 492 | "\x00", |
| 493 | "\x00", |
| 494 | "\x00", |
| 495 | "\x00", |
| 496 | "\x00\xff", |
| 497 | "\x00\xff", |
| 498 | "\x00\xff", |
| 499 | "\x00\xff", |
| 500 | "\x00\xff\u0100", |
| 501 | "\x00\xff\u0100", |
| 502 | "\x00\xff\u0100", |
| 503 | "\x00\xff\u0100", |
| 504 | "\x00\xff\u0100\uffff", |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 505 | "\x00\xff\u0100\uffff", |
| 506 | "\x00\xff\u0100\uffff", |
| 507 | "\x00\xff\u0100\uffff", |
| 508 | "\x00\xff\u0100\uffff\U00010000", |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 509 | ] |
| 510 | ) |
| 511 | |
| 512 | def test_simple(self): |
| 513 | self.assertEqual("\U00010203".encode(self.encoding), b"\x03\x02\x01\x00") |
| 514 | |
| 515 | def test_errors(self): |
| 516 | self.assertRaises(UnicodeDecodeError, codecs.utf_32_le_decode, |
| 517 | b"\xff", "strict", True) |
| 518 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 519 | def test_issue8941(self): |
| 520 | # Issue #8941: insufficient result allocation when decoding into |
| 521 | # surrogate pairs on UCS-2 builds. |
| 522 | encoded = b'\x00\x00\x01\x00' * 1024 |
| 523 | self.assertEqual('\U00010000' * 1024, |
| 524 | codecs.utf_32_le_decode(encoded)[0]) |
| 525 | |
Ezio Melotti | 5d3dba0 | 2013-01-11 06:02:07 +0200 | [diff] [blame] | 526 | class UTF32BETest(ReadTest, unittest.TestCase): |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 527 | encoding = "utf-32-be" |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 528 | ill_formed_sequence = b"\x00\x00\xdc\x80" |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 529 | |
| 530 | def test_partial(self): |
| 531 | self.check_partial( |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 532 | "\x00\xff\u0100\uffff\U00010000", |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 533 | [ |
| 534 | "", |
| 535 | "", |
| 536 | "", |
| 537 | "\x00", |
| 538 | "\x00", |
| 539 | "\x00", |
| 540 | "\x00", |
| 541 | "\x00\xff", |
| 542 | "\x00\xff", |
| 543 | "\x00\xff", |
| 544 | "\x00\xff", |
| 545 | "\x00\xff\u0100", |
| 546 | "\x00\xff\u0100", |
| 547 | "\x00\xff\u0100", |
| 548 | "\x00\xff\u0100", |
| 549 | "\x00\xff\u0100\uffff", |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 550 | "\x00\xff\u0100\uffff", |
| 551 | "\x00\xff\u0100\uffff", |
| 552 | "\x00\xff\u0100\uffff", |
| 553 | "\x00\xff\u0100\uffff\U00010000", |
Walter Dörwald | 41980ca | 2007-08-16 21:55:45 +0000 | [diff] [blame] | 554 | ] |
| 555 | ) |
| 556 | |
| 557 | def test_simple(self): |
| 558 | self.assertEqual("\U00010203".encode(self.encoding), b"\x00\x01\x02\x03") |
| 559 | |
| 560 | def test_errors(self): |
| 561 | self.assertRaises(UnicodeDecodeError, codecs.utf_32_be_decode, |
| 562 | b"\xff", "strict", True) |
| 563 | |
Antoine Pitrou | cc0cfd3 | 2010-06-11 21:46:32 +0000 | [diff] [blame] | 564 | def test_issue8941(self): |
| 565 | # Issue #8941: insufficient result allocation when decoding into |
| 566 | # surrogate pairs on UCS-2 builds. |
| 567 | encoded = b'\x00\x01\x00\x00' * 1024 |
| 568 | self.assertEqual('\U00010000' * 1024, |
| 569 | codecs.utf_32_be_decode(encoded)[0]) |
| 570 | |
| 571 | |
Ezio Melotti | 5d3dba0 | 2013-01-11 06:02:07 +0200 | [diff] [blame] | 572 | class UTF16Test(ReadTest, unittest.TestCase): |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 573 | encoding = "utf-16" |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 574 | if sys.byteorder == 'little': |
| 575 | ill_formed_sequence = b"\x80\xdc" |
| 576 | else: |
| 577 | ill_formed_sequence = b"\xdc\x80" |
Marc-André Lemburg | a37171d | 2001-06-19 20:09:28 +0000 | [diff] [blame] | 578 | |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 579 | spamle = b'\xff\xfes\x00p\x00a\x00m\x00s\x00p\x00a\x00m\x00' |
| 580 | 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] | 581 | |
| 582 | def test_only_one_bom(self): |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 583 | _,_,reader,writer = codecs.lookup(self.encoding) |
Marc-André Lemburg | a37171d | 2001-06-19 20:09:28 +0000 | [diff] [blame] | 584 | # encode some stream |
Walter Dörwald | c3ab0a7 | 2007-05-10 15:02:49 +0000 | [diff] [blame] | 585 | s = io.BytesIO() |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 586 | f = writer(s) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 587 | f.write("spam") |
| 588 | f.write("spam") |
Marc-André Lemburg | a37171d | 2001-06-19 20:09:28 +0000 | [diff] [blame] | 589 | d = s.getvalue() |
| 590 | # check whether there is exactly one BOM in it |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 591 | self.assertTrue(d == self.spamle or d == self.spambe) |
Marc-André Lemburg | a37171d | 2001-06-19 20:09:28 +0000 | [diff] [blame] | 592 | # try to read it back |
Walter Dörwald | c3ab0a7 | 2007-05-10 15:02:49 +0000 | [diff] [blame] | 593 | s = io.BytesIO(d) |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 594 | f = reader(s) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 595 | self.assertEqual(f.read(), "spamspam") |
Marc-André Lemburg | a37171d | 2001-06-19 20:09:28 +0000 | [diff] [blame] | 596 | |
Walter Dörwald | 1f1d252 | 2005-02-04 14:15:34 +0000 | [diff] [blame] | 597 | def test_badbom(self): |
Walter Dörwald | c3ab0a7 | 2007-05-10 15:02:49 +0000 | [diff] [blame] | 598 | s = io.BytesIO(b"\xff\xff") |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 599 | f = codecs.getreader(self.encoding)(s) |
Walter Dörwald | 1f1d252 | 2005-02-04 14:15:34 +0000 | [diff] [blame] | 600 | self.assertRaises(UnicodeError, f.read) |
| 601 | |
Walter Dörwald | c3ab0a7 | 2007-05-10 15:02:49 +0000 | [diff] [blame] | 602 | s = io.BytesIO(b"\xff\xff\xff\xff") |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 603 | f = codecs.getreader(self.encoding)(s) |
Walter Dörwald | 1f1d252 | 2005-02-04 14:15:34 +0000 | [diff] [blame] | 604 | self.assertRaises(UnicodeError, f.read) |
| 605 | |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 606 | def test_partial(self): |
| 607 | self.check_partial( |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 608 | "\x00\xff\u0100\uffff\U00010000", |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 609 | [ |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 610 | "", # first byte of BOM read |
| 611 | "", # second byte of BOM read => byteorder known |
| 612 | "", |
| 613 | "\x00", |
| 614 | "\x00", |
| 615 | "\x00\xff", |
| 616 | "\x00\xff", |
| 617 | "\x00\xff\u0100", |
| 618 | "\x00\xff\u0100", |
| 619 | "\x00\xff\u0100\uffff", |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 620 | "\x00\xff\u0100\uffff", |
| 621 | "\x00\xff\u0100\uffff", |
| 622 | "\x00\xff\u0100\uffff", |
| 623 | "\x00\xff\u0100\uffff\U00010000", |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 624 | ] |
| 625 | ) |
| 626 | |
Georg Brandl | 791f4e1 | 2009-09-17 11:41:24 +0000 | [diff] [blame] | 627 | def test_handlers(self): |
| 628 | self.assertEqual(('\ufffd', 1), |
| 629 | codecs.utf_16_decode(b'\x01', 'replace', True)) |
| 630 | self.assertEqual(('', 1), |
| 631 | codecs.utf_16_decode(b'\x01', 'ignore', True)) |
| 632 | |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 633 | def test_errors(self): |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 634 | self.assertRaises(UnicodeDecodeError, codecs.utf_16_decode, |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 635 | b"\xff", "strict", True) |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 636 | |
| 637 | def test_decoder_state(self): |
| 638 | self.check_state_handling_decode(self.encoding, |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 639 | "spamspam", self.spamle) |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 640 | self.check_state_handling_decode(self.encoding, |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 641 | "spamspam", self.spambe) |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 642 | |
Florent Xicluna | c1c415f | 2010-02-26 11:12:33 +0000 | [diff] [blame] | 643 | def test_bug691291(self): |
| 644 | # Files are always opened in binary mode, even if no binary mode was |
| 645 | # specified. This means that no automatic conversion of '\n' is done |
| 646 | # on reading and writing. |
| 647 | s1 = 'Hello\r\nworld\r\n' |
| 648 | |
| 649 | s = s1.encode(self.encoding) |
Victor Stinner | 2cca057 | 2011-05-23 14:51:42 +0200 | [diff] [blame] | 650 | self.addCleanup(support.unlink, support.TESTFN) |
| 651 | with open(support.TESTFN, 'wb') as fp: |
| 652 | fp.write(s) |
Serhiy Storchaka | 2480c2e | 2013-11-24 23:13:26 +0200 | [diff] [blame] | 653 | with support.check_warnings(('', DeprecationWarning)): |
| 654 | reader = codecs.open(support.TESTFN, 'U', encoding=self.encoding) |
| 655 | with reader: |
Victor Stinner | 2cca057 | 2011-05-23 14:51:42 +0200 | [diff] [blame] | 656 | self.assertEqual(reader.read(), s1) |
Florent Xicluna | c1c415f | 2010-02-26 11:12:33 +0000 | [diff] [blame] | 657 | |
Ezio Melotti | 5d3dba0 | 2013-01-11 06:02:07 +0200 | [diff] [blame] | 658 | class UTF16LETest(ReadTest, unittest.TestCase): |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 659 | encoding = "utf-16-le" |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 660 | ill_formed_sequence = b"\x80\xdc" |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 661 | |
| 662 | def test_partial(self): |
| 663 | self.check_partial( |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 664 | "\x00\xff\u0100\uffff\U00010000", |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 665 | [ |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 666 | "", |
| 667 | "\x00", |
| 668 | "\x00", |
| 669 | "\x00\xff", |
| 670 | "\x00\xff", |
| 671 | "\x00\xff\u0100", |
| 672 | "\x00\xff\u0100", |
| 673 | "\x00\xff\u0100\uffff", |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 674 | "\x00\xff\u0100\uffff", |
| 675 | "\x00\xff\u0100\uffff", |
| 676 | "\x00\xff\u0100\uffff", |
| 677 | "\x00\xff\u0100\uffff\U00010000", |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 678 | ] |
| 679 | ) |
| 680 | |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 681 | def test_errors(self): |
Antoine Pitrou | b4bbee2 | 2012-07-21 00:45:14 +0200 | [diff] [blame] | 682 | tests = [ |
| 683 | (b'\xff', '\ufffd'), |
| 684 | (b'A\x00Z', 'A\ufffd'), |
| 685 | (b'A\x00B\x00C\x00D\x00Z', 'ABCD\ufffd'), |
| 686 | (b'\x00\xd8', '\ufffd'), |
| 687 | (b'\x00\xd8A', '\ufffd'), |
| 688 | (b'\x00\xd8A\x00', '\ufffdA'), |
| 689 | (b'\x00\xdcA\x00', '\ufffdA'), |
| 690 | ] |
| 691 | for raw, expected in tests: |
| 692 | self.assertRaises(UnicodeDecodeError, codecs.utf_16_le_decode, |
| 693 | raw, 'strict', True) |
| 694 | self.assertEqual(raw.decode('utf-16le', 'replace'), expected) |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 695 | |
Victor Stinner | 53a9dd7 | 2010-12-08 22:25:45 +0000 | [diff] [blame] | 696 | def test_nonbmp(self): |
| 697 | self.assertEqual("\U00010203".encode(self.encoding), |
| 698 | b'\x00\xd8\x03\xde') |
| 699 | self.assertEqual(b'\x00\xd8\x03\xde'.decode(self.encoding), |
| 700 | "\U00010203") |
| 701 | |
Ezio Melotti | 5d3dba0 | 2013-01-11 06:02:07 +0200 | [diff] [blame] | 702 | class UTF16BETest(ReadTest, unittest.TestCase): |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 703 | encoding = "utf-16-be" |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 704 | ill_formed_sequence = b"\xdc\x80" |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 705 | |
| 706 | def test_partial(self): |
| 707 | self.check_partial( |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 708 | "\x00\xff\u0100\uffff\U00010000", |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 709 | [ |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 710 | "", |
| 711 | "\x00", |
| 712 | "\x00", |
| 713 | "\x00\xff", |
| 714 | "\x00\xff", |
| 715 | "\x00\xff\u0100", |
| 716 | "\x00\xff\u0100", |
| 717 | "\x00\xff\u0100\uffff", |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 718 | "\x00\xff\u0100\uffff", |
| 719 | "\x00\xff\u0100\uffff", |
| 720 | "\x00\xff\u0100\uffff", |
| 721 | "\x00\xff\u0100\uffff\U00010000", |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 722 | ] |
| 723 | ) |
| 724 | |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 725 | def test_errors(self): |
Antoine Pitrou | b4bbee2 | 2012-07-21 00:45:14 +0200 | [diff] [blame] | 726 | tests = [ |
| 727 | (b'\xff', '\ufffd'), |
| 728 | (b'\x00A\xff', 'A\ufffd'), |
| 729 | (b'\x00A\x00B\x00C\x00DZ', 'ABCD\ufffd'), |
| 730 | (b'\xd8\x00', '\ufffd'), |
| 731 | (b'\xd8\x00\xdc', '\ufffd'), |
| 732 | (b'\xd8\x00\x00A', '\ufffdA'), |
| 733 | (b'\xdc\x00\x00A', '\ufffdA'), |
| 734 | ] |
| 735 | for raw, expected in tests: |
| 736 | self.assertRaises(UnicodeDecodeError, codecs.utf_16_be_decode, |
| 737 | raw, 'strict', True) |
| 738 | self.assertEqual(raw.decode('utf-16be', 'replace'), expected) |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 739 | |
Victor Stinner | 53a9dd7 | 2010-12-08 22:25:45 +0000 | [diff] [blame] | 740 | def test_nonbmp(self): |
| 741 | self.assertEqual("\U00010203".encode(self.encoding), |
| 742 | b'\xd8\x00\xde\x03') |
| 743 | self.assertEqual(b'\xd8\x00\xde\x03'.decode(self.encoding), |
| 744 | "\U00010203") |
| 745 | |
Ezio Melotti | 5d3dba0 | 2013-01-11 06:02:07 +0200 | [diff] [blame] | 746 | class UTF8Test(ReadTest, unittest.TestCase): |
Walter Dörwald | e57d7b1 | 2004-12-21 22:24:00 +0000 | [diff] [blame] | 747 | encoding = "utf-8" |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 748 | ill_formed_sequence = b"\xed\xb2\x80" |
| 749 | ill_formed_sequence_replace = "\ufffd" * 3 |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 750 | |
| 751 | def test_partial(self): |
| 752 | self.check_partial( |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 753 | "\x00\xff\u07ff\u0800\uffff\U00010000", |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 754 | [ |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 755 | "\x00", |
| 756 | "\x00", |
| 757 | "\x00\xff", |
| 758 | "\x00\xff", |
| 759 | "\x00\xff\u07ff", |
| 760 | "\x00\xff\u07ff", |
| 761 | "\x00\xff\u07ff", |
| 762 | "\x00\xff\u07ff\u0800", |
| 763 | "\x00\xff\u07ff\u0800", |
| 764 | "\x00\xff\u07ff\u0800", |
| 765 | "\x00\xff\u07ff\u0800\uffff", |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 766 | "\x00\xff\u07ff\u0800\uffff", |
| 767 | "\x00\xff\u07ff\u0800\uffff", |
| 768 | "\x00\xff\u07ff\u0800\uffff", |
| 769 | "\x00\xff\u07ff\u0800\uffff\U00010000", |
Walter Dörwald | 6965203 | 2004-09-07 20:24:22 +0000 | [diff] [blame] | 770 | ] |
| 771 | ) |
| 772 | |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 773 | def test_decoder_state(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 774 | u = "\x00\x7f\x80\xff\u0100\u07ff\u0800\uffff\U0010ffff" |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 775 | self.check_state_handling_decode(self.encoding, |
| 776 | u, u.encode(self.encoding)) |
| 777 | |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 778 | def test_lone_surrogates(self): |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 779 | super().test_lone_surrogates() |
| 780 | # not sure if this is making sense for |
| 781 | # UTF-16 and UTF-32 |
| 782 | self.assertEqual("[\uDC80]".encode('utf-8', "surrogateescape"), |
Victor Stinner | 31be90b | 2010-04-22 19:38:16 +0000 | [diff] [blame] | 783 | b'[\x80]') |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 784 | |
Martin v. Löwis | e0a2b72 | 2009-05-10 08:08:56 +0000 | [diff] [blame] | 785 | def test_surrogatepass_handler(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 786 | self.assertEqual("abc\ud800def".encode("utf-8", "surrogatepass"), |
| 787 | b"abc\xed\xa0\x80def") |
| 788 | self.assertEqual(b"abc\xed\xa0\x80def".decode("utf-8", "surrogatepass"), |
| 789 | "abc\ud800def") |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 790 | self.assertEqual("\U00010fff\uD800".encode("utf-8", "surrogatepass"), |
| 791 | b"\xf0\x90\xbf\xbf\xed\xa0\x80") |
| 792 | self.assertEqual(b"\xf0\x90\xbf\xbf\xed\xa0\x80".decode("utf-8", "surrogatepass"), |
| 793 | "\U00010fff\uD800") |
Martin v. Löwis | e0a2b72 | 2009-05-10 08:08:56 +0000 | [diff] [blame] | 794 | self.assertTrue(codecs.lookup_error("surrogatepass")) |
Philip Jenvey | 45c4149 | 2012-10-26 17:01:53 -0700 | [diff] [blame] | 795 | with self.assertRaises(UnicodeDecodeError): |
| 796 | b"abc\xed\xa0".decode("utf-8", "surrogatepass") |
Ezio Melotti | 540da76 | 2012-11-03 23:03:39 +0200 | [diff] [blame] | 797 | with self.assertRaises(UnicodeDecodeError): |
| 798 | b"abc\xed\xa0z".decode("utf-8", "surrogatepass") |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 799 | |
Victor Stinner | 2f3ca9f | 2011-10-27 01:38:56 +0200 | [diff] [blame] | 800 | @unittest.skipUnless(sys.platform == 'win32', |
| 801 | 'cp65001 is a Windows-only codec') |
Ezio Melotti | 5d3dba0 | 2013-01-11 06:02:07 +0200 | [diff] [blame] | 802 | class CP65001Test(ReadTest, unittest.TestCase): |
Victor Stinner | 2f3ca9f | 2011-10-27 01:38:56 +0200 | [diff] [blame] | 803 | encoding = "cp65001" |
| 804 | |
| 805 | def test_encode(self): |
| 806 | tests = [ |
| 807 | ('abc', 'strict', b'abc'), |
| 808 | ('\xe9\u20ac', 'strict', b'\xc3\xa9\xe2\x82\xac'), |
| 809 | ('\U0010ffff', 'strict', b'\xf4\x8f\xbf\xbf'), |
| 810 | ] |
| 811 | if VISTA_OR_LATER: |
| 812 | tests.extend(( |
| 813 | ('\udc80', 'strict', None), |
| 814 | ('\udc80', 'ignore', b''), |
| 815 | ('\udc80', 'replace', b'?'), |
| 816 | ('\udc80', 'backslashreplace', b'\\udc80'), |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 817 | ('\udc80', 'namereplace', b'\\udc80'), |
Victor Stinner | 2f3ca9f | 2011-10-27 01:38:56 +0200 | [diff] [blame] | 818 | ('\udc80', 'surrogatepass', b'\xed\xb2\x80'), |
| 819 | )) |
| 820 | else: |
| 821 | tests.append(('\udc80', 'strict', b'\xed\xb2\x80')) |
| 822 | for text, errors, expected in tests: |
| 823 | if expected is not None: |
| 824 | try: |
| 825 | encoded = text.encode('cp65001', errors) |
| 826 | except UnicodeEncodeError as err: |
| 827 | self.fail('Unable to encode %a to cp65001 with ' |
| 828 | 'errors=%r: %s' % (text, errors, err)) |
| 829 | self.assertEqual(encoded, expected, |
| 830 | '%a.encode("cp65001", %r)=%a != %a' |
| 831 | % (text, errors, encoded, expected)) |
| 832 | else: |
| 833 | self.assertRaises(UnicodeEncodeError, |
| 834 | text.encode, "cp65001", errors) |
| 835 | |
| 836 | def test_decode(self): |
| 837 | tests = [ |
| 838 | (b'abc', 'strict', 'abc'), |
| 839 | (b'\xc3\xa9\xe2\x82\xac', 'strict', '\xe9\u20ac'), |
| 840 | (b'\xf4\x8f\xbf\xbf', 'strict', '\U0010ffff'), |
| 841 | (b'\xef\xbf\xbd', 'strict', '\ufffd'), |
| 842 | (b'[\xc3\xa9]', 'strict', '[\xe9]'), |
| 843 | # invalid bytes |
| 844 | (b'[\xff]', 'strict', None), |
| 845 | (b'[\xff]', 'ignore', '[]'), |
| 846 | (b'[\xff]', 'replace', '[\ufffd]'), |
| 847 | (b'[\xff]', 'surrogateescape', '[\udcff]'), |
| 848 | ] |
| 849 | if VISTA_OR_LATER: |
| 850 | tests.extend(( |
| 851 | (b'[\xed\xb2\x80]', 'strict', None), |
| 852 | (b'[\xed\xb2\x80]', 'ignore', '[]'), |
| 853 | (b'[\xed\xb2\x80]', 'replace', '[\ufffd\ufffd\ufffd]'), |
| 854 | )) |
| 855 | else: |
| 856 | tests.extend(( |
| 857 | (b'[\xed\xb2\x80]', 'strict', '[\udc80]'), |
| 858 | )) |
| 859 | for raw, errors, expected in tests: |
| 860 | if expected is not None: |
| 861 | try: |
| 862 | decoded = raw.decode('cp65001', errors) |
| 863 | except UnicodeDecodeError as err: |
| 864 | self.fail('Unable to decode %a from cp65001 with ' |
| 865 | 'errors=%r: %s' % (raw, errors, err)) |
| 866 | self.assertEqual(decoded, expected, |
| 867 | '%a.decode("cp65001", %r)=%a != %a' |
| 868 | % (raw, errors, decoded, expected)) |
| 869 | else: |
| 870 | self.assertRaises(UnicodeDecodeError, |
| 871 | raw.decode, 'cp65001', errors) |
| 872 | |
| 873 | @unittest.skipUnless(VISTA_OR_LATER, 'require Windows Vista or later') |
| 874 | def test_lone_surrogates(self): |
| 875 | self.assertRaises(UnicodeEncodeError, "\ud800".encode, "cp65001") |
| 876 | self.assertRaises(UnicodeDecodeError, b"\xed\xa0\x80".decode, "cp65001") |
| 877 | self.assertEqual("[\uDC80]".encode("cp65001", "backslashreplace"), |
| 878 | b'[\\udc80]') |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 879 | self.assertEqual("[\uDC80]".encode("cp65001", "namereplace"), |
| 880 | b'[\\udc80]') |
Victor Stinner | 2f3ca9f | 2011-10-27 01:38:56 +0200 | [diff] [blame] | 881 | self.assertEqual("[\uDC80]".encode("cp65001", "xmlcharrefreplace"), |
| 882 | b'[�]') |
| 883 | self.assertEqual("[\uDC80]".encode("cp65001", "surrogateescape"), |
| 884 | b'[\x80]') |
| 885 | self.assertEqual("[\uDC80]".encode("cp65001", "ignore"), |
| 886 | b'[]') |
| 887 | self.assertEqual("[\uDC80]".encode("cp65001", "replace"), |
| 888 | b'[?]') |
| 889 | |
| 890 | @unittest.skipUnless(VISTA_OR_LATER, 'require Windows Vista or later') |
| 891 | def test_surrogatepass_handler(self): |
| 892 | self.assertEqual("abc\ud800def".encode("cp65001", "surrogatepass"), |
| 893 | b"abc\xed\xa0\x80def") |
| 894 | self.assertEqual(b"abc\xed\xa0\x80def".decode("cp65001", "surrogatepass"), |
| 895 | "abc\ud800def") |
| 896 | self.assertEqual("\U00010fff\uD800".encode("cp65001", "surrogatepass"), |
| 897 | b"\xf0\x90\xbf\xbf\xed\xa0\x80") |
| 898 | self.assertEqual(b"\xf0\x90\xbf\xbf\xed\xa0\x80".decode("cp65001", "surrogatepass"), |
| 899 | "\U00010fff\uD800") |
| 900 | self.assertTrue(codecs.lookup_error("surrogatepass")) |
| 901 | |
Victor Stinner | 2f3ca9f | 2011-10-27 01:38:56 +0200 | [diff] [blame] | 902 | |
Ezio Melotti | 5d3dba0 | 2013-01-11 06:02:07 +0200 | [diff] [blame] | 903 | class UTF7Test(ReadTest, unittest.TestCase): |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 904 | encoding = "utf-7" |
| 905 | |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 906 | def test_ascii(self): |
| 907 | # Set D (directly encoded characters) |
| 908 | set_d = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
| 909 | 'abcdefghijklmnopqrstuvwxyz' |
| 910 | '0123456789' |
| 911 | '\'(),-./:?') |
| 912 | self.assertEqual(set_d.encode(self.encoding), set_d.encode('ascii')) |
| 913 | self.assertEqual(set_d.encode('ascii').decode(self.encoding), set_d) |
| 914 | # Set O (optional direct characters) |
| 915 | set_o = ' !"#$%&*;<=>@[]^_`{|}' |
| 916 | self.assertEqual(set_o.encode(self.encoding), set_o.encode('ascii')) |
| 917 | self.assertEqual(set_o.encode('ascii').decode(self.encoding), set_o) |
| 918 | # + |
| 919 | self.assertEqual('a+b'.encode(self.encoding), b'a+-b') |
| 920 | self.assertEqual(b'a+-b'.decode(self.encoding), 'a+b') |
| 921 | # White spaces |
| 922 | ws = ' \t\n\r' |
| 923 | self.assertEqual(ws.encode(self.encoding), ws.encode('ascii')) |
| 924 | self.assertEqual(ws.encode('ascii').decode(self.encoding), ws) |
| 925 | # Other ASCII characters |
| 926 | other_ascii = ''.join(sorted(set(bytes(range(0x80)).decode()) - |
| 927 | set(set_d + set_o + '+' + ws))) |
| 928 | self.assertEqual(other_ascii.encode(self.encoding), |
| 929 | b'+AAAAAQACAAMABAAFAAYABwAIAAsADAAOAA8AEAARABIAEwAU' |
| 930 | b'ABUAFgAXABgAGQAaABsAHAAdAB4AHwBcAH4Afw-') |
| 931 | |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 932 | def test_partial(self): |
| 933 | self.check_partial( |
Serhiy Storchaka | 016a3f3 | 2014-02-08 14:01:29 +0200 | [diff] [blame] | 934 | 'a+-b\x00c\x80d\u0100e\U00010000f', |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 935 | [ |
Serhiy Storchaka | 016a3f3 | 2014-02-08 14:01:29 +0200 | [diff] [blame] | 936 | 'a', |
| 937 | 'a', |
| 938 | 'a+', |
| 939 | 'a+-', |
| 940 | 'a+-b', |
| 941 | 'a+-b', |
| 942 | 'a+-b', |
| 943 | 'a+-b', |
| 944 | 'a+-b', |
| 945 | 'a+-b\x00', |
| 946 | 'a+-b\x00c', |
| 947 | 'a+-b\x00c', |
| 948 | 'a+-b\x00c', |
| 949 | 'a+-b\x00c', |
| 950 | 'a+-b\x00c', |
| 951 | 'a+-b\x00c\x80', |
| 952 | 'a+-b\x00c\x80d', |
| 953 | 'a+-b\x00c\x80d', |
| 954 | 'a+-b\x00c\x80d', |
| 955 | 'a+-b\x00c\x80d', |
| 956 | 'a+-b\x00c\x80d', |
| 957 | 'a+-b\x00c\x80d\u0100', |
| 958 | 'a+-b\x00c\x80d\u0100e', |
| 959 | 'a+-b\x00c\x80d\u0100e', |
| 960 | 'a+-b\x00c\x80d\u0100e', |
| 961 | 'a+-b\x00c\x80d\u0100e', |
| 962 | 'a+-b\x00c\x80d\u0100e', |
| 963 | 'a+-b\x00c\x80d\u0100e', |
| 964 | 'a+-b\x00c\x80d\u0100e', |
| 965 | 'a+-b\x00c\x80d\u0100e', |
| 966 | 'a+-b\x00c\x80d\u0100e\U00010000', |
| 967 | 'a+-b\x00c\x80d\u0100e\U00010000f', |
Christian Heimes | 5d14c2b | 2007-11-20 23:38:09 +0000 | [diff] [blame] | 968 | ] |
| 969 | ) |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 970 | |
Serhiy Storchaka | 35804e4 | 2013-10-19 20:38:19 +0300 | [diff] [blame] | 971 | def test_errors(self): |
| 972 | tests = [ |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 973 | (b'\xffb', '\ufffdb'), |
Serhiy Storchaka | 35804e4 | 2013-10-19 20:38:19 +0300 | [diff] [blame] | 974 | (b'a\xffb', 'a\ufffdb'), |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 975 | (b'a\xff\xffb', 'a\ufffd\ufffdb'), |
Serhiy Storchaka | 35804e4 | 2013-10-19 20:38:19 +0300 | [diff] [blame] | 976 | (b'a+IK', 'a\ufffd'), |
| 977 | (b'a+IK-b', 'a\ufffdb'), |
| 978 | (b'a+IK,b', 'a\ufffdb'), |
| 979 | (b'a+IKx', 'a\u20ac\ufffd'), |
| 980 | (b'a+IKx-b', 'a\u20ac\ufffdb'), |
| 981 | (b'a+IKwgr', 'a\u20ac\ufffd'), |
| 982 | (b'a+IKwgr-b', 'a\u20ac\ufffdb'), |
| 983 | (b'a+IKwgr,', 'a\u20ac\ufffd'), |
| 984 | (b'a+IKwgr,-b', 'a\u20ac\ufffd-b'), |
| 985 | (b'a+IKwgrB', 'a\u20ac\u20ac\ufffd'), |
| 986 | (b'a+IKwgrB-b', 'a\u20ac\u20ac\ufffdb'), |
| 987 | (b'a+/,+IKw-b', 'a\ufffd\u20acb'), |
| 988 | (b'a+//,+IKw-b', 'a\ufffd\u20acb'), |
| 989 | (b'a+///,+IKw-b', 'a\uffff\ufffd\u20acb'), |
| 990 | (b'a+////,+IKw-b', 'a\uffff\ufffd\u20acb'), |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 991 | (b'a+IKw-b\xff', 'a\u20acb\ufffd'), |
| 992 | (b'a+IKw\xffb', 'a\u20ac\ufffdb'), |
Serhiy Storchaka | 35804e4 | 2013-10-19 20:38:19 +0300 | [diff] [blame] | 993 | ] |
| 994 | for raw, expected in tests: |
| 995 | with self.subTest(raw=raw): |
| 996 | self.assertRaises(UnicodeDecodeError, codecs.utf_7_decode, |
| 997 | raw, 'strict', True) |
| 998 | self.assertEqual(raw.decode('utf-7', 'replace'), expected) |
| 999 | |
| 1000 | def test_nonbmp(self): |
| 1001 | self.assertEqual('\U000104A0'.encode(self.encoding), b'+2AHcoA-') |
| 1002 | self.assertEqual('\ud801\udca0'.encode(self.encoding), b'+2AHcoA-') |
| 1003 | self.assertEqual(b'+2AHcoA-'.decode(self.encoding), '\U000104A0') |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 1004 | self.assertEqual(b'+2AHcoA'.decode(self.encoding), '\U000104A0') |
| 1005 | self.assertEqual('\u20ac\U000104A0'.encode(self.encoding), b'+IKzYAdyg-') |
| 1006 | self.assertEqual(b'+IKzYAdyg-'.decode(self.encoding), '\u20ac\U000104A0') |
| 1007 | self.assertEqual(b'+IKzYAdyg'.decode(self.encoding), '\u20ac\U000104A0') |
| 1008 | self.assertEqual('\u20ac\u20ac\U000104A0'.encode(self.encoding), |
| 1009 | b'+IKwgrNgB3KA-') |
| 1010 | self.assertEqual(b'+IKwgrNgB3KA-'.decode(self.encoding), |
| 1011 | '\u20ac\u20ac\U000104A0') |
| 1012 | self.assertEqual(b'+IKwgrNgB3KA'.decode(self.encoding), |
| 1013 | '\u20ac\u20ac\U000104A0') |
Serhiy Storchaka | 35804e4 | 2013-10-19 20:38:19 +0300 | [diff] [blame] | 1014 | |
Serhiy Storchaka | 28b21e5 | 2015-10-02 13:07:28 +0300 | [diff] [blame] | 1015 | def test_lone_surrogates(self): |
| 1016 | tests = [ |
| 1017 | (b'a+2AE-b', 'a\ud801b'), |
| 1018 | (b'a+2AE\xffb', 'a\ufffdb'), |
| 1019 | (b'a+2AE', 'a\ufffd'), |
| 1020 | (b'a+2AEA-b', 'a\ufffdb'), |
| 1021 | (b'a+2AH-b', 'a\ufffdb'), |
| 1022 | (b'a+IKzYAQ-b', 'a\u20ac\ud801b'), |
| 1023 | (b'a+IKzYAQ\xffb', 'a\u20ac\ufffdb'), |
| 1024 | (b'a+IKzYAQA-b', 'a\u20ac\ufffdb'), |
| 1025 | (b'a+IKzYAd-b', 'a\u20ac\ufffdb'), |
| 1026 | (b'a+IKwgrNgB-b', 'a\u20ac\u20ac\ud801b'), |
| 1027 | (b'a+IKwgrNgB\xffb', 'a\u20ac\u20ac\ufffdb'), |
| 1028 | (b'a+IKwgrNgB', 'a\u20ac\u20ac\ufffd'), |
| 1029 | (b'a+IKwgrNgBA-b', 'a\u20ac\u20ac\ufffdb'), |
| 1030 | ] |
| 1031 | for raw, expected in tests: |
| 1032 | with self.subTest(raw=raw): |
| 1033 | self.assertEqual(raw.decode('utf-7', 'replace'), expected) |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 1034 | |
| 1035 | |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 1036 | class UTF16ExTest(unittest.TestCase): |
| 1037 | |
| 1038 | def test_errors(self): |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1039 | 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] | 1040 | |
| 1041 | def test_bad_args(self): |
| 1042 | self.assertRaises(TypeError, codecs.utf_16_ex_decode) |
| 1043 | |
| 1044 | class ReadBufferTest(unittest.TestCase): |
| 1045 | |
| 1046 | def test_array(self): |
| 1047 | import array |
| 1048 | self.assertEqual( |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 1049 | codecs.readbuffer_encode(array.array("b", b"spam")), |
Walter Dörwald | 2233d27 | 2007-06-22 12:17:08 +0000 | [diff] [blame] | 1050 | (b"spam", 4) |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 1051 | ) |
| 1052 | |
| 1053 | def test_empty(self): |
Walter Dörwald | 2233d27 | 2007-06-22 12:17:08 +0000 | [diff] [blame] | 1054 | self.assertEqual(codecs.readbuffer_encode(""), (b"", 0)) |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 1055 | |
| 1056 | def test_bad_args(self): |
| 1057 | self.assertRaises(TypeError, codecs.readbuffer_encode) |
| 1058 | self.assertRaises(TypeError, codecs.readbuffer_encode, 42) |
| 1059 | |
Serhiy Storchaka | 58cf607 | 2013-11-19 11:32:41 +0200 | [diff] [blame] | 1060 | class UTF8SigTest(UTF8Test, unittest.TestCase): |
Martin v. Löwis | 412ed3b | 2006-01-08 10:45:39 +0000 | [diff] [blame] | 1061 | encoding = "utf-8-sig" |
| 1062 | |
| 1063 | def test_partial(self): |
| 1064 | self.check_partial( |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 1065 | "\ufeff\x00\xff\u07ff\u0800\uffff\U00010000", |
Martin v. Löwis | 412ed3b | 2006-01-08 10:45:39 +0000 | [diff] [blame] | 1066 | [ |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1067 | "", |
| 1068 | "", |
| 1069 | "", # First BOM has been read and skipped |
| 1070 | "", |
| 1071 | "", |
| 1072 | "\ufeff", # Second BOM has been read and emitted |
| 1073 | "\ufeff\x00", # "\x00" read and emitted |
Walter Dörwald | 32a4c71 | 2007-06-20 09:25:34 +0000 | [diff] [blame] | 1074 | "\ufeff\x00", # First byte of encoded "\xff" read |
| 1075 | "\ufeff\x00\xff", # Second byte of encoded "\xff" read |
| 1076 | "\ufeff\x00\xff", # First byte of encoded "\u07ff" read |
| 1077 | "\ufeff\x00\xff\u07ff", # Second byte of encoded "\u07ff" read |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1078 | "\ufeff\x00\xff\u07ff", |
| 1079 | "\ufeff\x00\xff\u07ff", |
| 1080 | "\ufeff\x00\xff\u07ff\u0800", |
| 1081 | "\ufeff\x00\xff\u07ff\u0800", |
| 1082 | "\ufeff\x00\xff\u07ff\u0800", |
| 1083 | "\ufeff\x00\xff\u07ff\u0800\uffff", |
Serhiy Storchaka | 48e188e | 2013-01-08 23:14:24 +0200 | [diff] [blame] | 1084 | "\ufeff\x00\xff\u07ff\u0800\uffff", |
| 1085 | "\ufeff\x00\xff\u07ff\u0800\uffff", |
| 1086 | "\ufeff\x00\xff\u07ff\u0800\uffff", |
| 1087 | "\ufeff\x00\xff\u07ff\u0800\uffff\U00010000", |
Martin v. Löwis | 412ed3b | 2006-01-08 10:45:39 +0000 | [diff] [blame] | 1088 | ] |
| 1089 | ) |
| 1090 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1091 | def test_bug1601501(self): |
| 1092 | # SF bug #1601501: check that the codec works with a buffer |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1093 | self.assertEqual(str(b"\xef\xbb\xbf", "utf-8-sig"), "") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1094 | |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 1095 | def test_bom(self): |
| 1096 | d = codecs.getincrementaldecoder("utf-8-sig")() |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1097 | s = "spam" |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 1098 | self.assertEqual(d.decode(s.encode("utf-8-sig")), s) |
| 1099 | |
Guido van Rossum | 87c0f1d | 2007-11-19 18:03:44 +0000 | [diff] [blame] | 1100 | def test_stream_bom(self): |
| 1101 | unistring = "ABC\u00A1\u2200XYZ" |
| 1102 | bytestring = codecs.BOM_UTF8 + b"ABC\xC2\xA1\xE2\x88\x80XYZ" |
| 1103 | |
| 1104 | reader = codecs.getreader("utf-8-sig") |
| 1105 | for sizehint in [None] + list(range(1, 11)) + \ |
| 1106 | [64, 128, 256, 512, 1024]: |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 1107 | istream = reader(io.BytesIO(bytestring)) |
Guido van Rossum | 87c0f1d | 2007-11-19 18:03:44 +0000 | [diff] [blame] | 1108 | ostream = io.StringIO() |
| 1109 | while 1: |
| 1110 | if sizehint is not None: |
| 1111 | data = istream.read(sizehint) |
| 1112 | else: |
| 1113 | data = istream.read() |
| 1114 | |
| 1115 | if not data: |
| 1116 | break |
| 1117 | ostream.write(data) |
| 1118 | |
| 1119 | got = ostream.getvalue() |
| 1120 | self.assertEqual(got, unistring) |
| 1121 | |
| 1122 | def test_stream_bare(self): |
| 1123 | unistring = "ABC\u00A1\u2200XYZ" |
| 1124 | bytestring = b"ABC\xC2\xA1\xE2\x88\x80XYZ" |
| 1125 | |
| 1126 | reader = codecs.getreader("utf-8-sig") |
| 1127 | for sizehint in [None] + list(range(1, 11)) + \ |
| 1128 | [64, 128, 256, 512, 1024]: |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 1129 | istream = reader(io.BytesIO(bytestring)) |
Guido van Rossum | 87c0f1d | 2007-11-19 18:03:44 +0000 | [diff] [blame] | 1130 | ostream = io.StringIO() |
| 1131 | while 1: |
| 1132 | if sizehint is not None: |
| 1133 | data = istream.read(sizehint) |
| 1134 | else: |
| 1135 | data = istream.read() |
| 1136 | |
| 1137 | if not data: |
| 1138 | break |
| 1139 | ostream.write(data) |
| 1140 | |
| 1141 | got = ostream.getvalue() |
| 1142 | self.assertEqual(got, unistring) |
| 1143 | |
| 1144 | class EscapeDecodeTest(unittest.TestCase): |
| 1145 | def test_empty(self): |
Serhiy Storchaka | 077cb34 | 2013-01-29 11:06:53 +0200 | [diff] [blame] | 1146 | self.assertEqual(codecs.escape_decode(b""), (b"", 0)) |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 1147 | self.assertEqual(codecs.escape_decode(bytearray()), (b"", 0)) |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 1148 | |
Serhiy Storchaka | ace3ad3 | 2013-01-25 23:31:43 +0200 | [diff] [blame] | 1149 | def test_raw(self): |
Serhiy Storchaka | 077cb34 | 2013-01-29 11:06:53 +0200 | [diff] [blame] | 1150 | decode = codecs.escape_decode |
Serhiy Storchaka | ace3ad3 | 2013-01-25 23:31:43 +0200 | [diff] [blame] | 1151 | for b in range(256): |
Serhiy Storchaka | 077cb34 | 2013-01-29 11:06:53 +0200 | [diff] [blame] | 1152 | b = bytes([b]) |
| 1153 | if b != b'\\': |
| 1154 | self.assertEqual(decode(b + b'0'), (b + b'0', 2)) |
Serhiy Storchaka | ace3ad3 | 2013-01-25 23:31:43 +0200 | [diff] [blame] | 1155 | |
| 1156 | def test_escape(self): |
Serhiy Storchaka | 077cb34 | 2013-01-29 11:06:53 +0200 | [diff] [blame] | 1157 | decode = codecs.escape_decode |
| 1158 | check = coding_checker(self, decode) |
| 1159 | check(b"[\\\n]", b"[]") |
| 1160 | check(br'[\"]', b'["]') |
| 1161 | check(br"[\']", b"[']") |
| 1162 | check(br"[\\]", br"[\]") |
| 1163 | check(br"[\a]", b"[\x07]") |
| 1164 | check(br"[\b]", b"[\x08]") |
| 1165 | check(br"[\t]", b"[\x09]") |
| 1166 | check(br"[\n]", b"[\x0a]") |
| 1167 | check(br"[\v]", b"[\x0b]") |
| 1168 | check(br"[\f]", b"[\x0c]") |
| 1169 | check(br"[\r]", b"[\x0d]") |
| 1170 | check(br"[\7]", b"[\x07]") |
| 1171 | check(br"[\8]", br"[\8]") |
| 1172 | check(br"[\78]", b"[\x078]") |
| 1173 | check(br"[\41]", b"[!]") |
| 1174 | check(br"[\418]", b"[!8]") |
| 1175 | check(br"[\101]", b"[A]") |
| 1176 | check(br"[\1010]", b"[A0]") |
| 1177 | check(br"[\501]", b"[A]") |
| 1178 | check(br"[\x41]", b"[A]") |
| 1179 | check(br"[\X41]", br"[\X41]") |
| 1180 | check(br"[\x410]", b"[A0]") |
Serhiy Storchaka | ace3ad3 | 2013-01-25 23:31:43 +0200 | [diff] [blame] | 1181 | for b in range(256): |
| 1182 | if b not in b'\n"\'\\abtnvfr01234567x': |
Serhiy Storchaka | 077cb34 | 2013-01-29 11:06:53 +0200 | [diff] [blame] | 1183 | b = bytes([b]) |
| 1184 | check(b'\\' + b, b'\\' + b) |
Serhiy Storchaka | ace3ad3 | 2013-01-25 23:31:43 +0200 | [diff] [blame] | 1185 | |
| 1186 | def test_errors(self): |
Serhiy Storchaka | 077cb34 | 2013-01-29 11:06:53 +0200 | [diff] [blame] | 1187 | decode = codecs.escape_decode |
| 1188 | self.assertRaises(ValueError, decode, br"\x") |
| 1189 | self.assertRaises(ValueError, decode, br"[\x]") |
| 1190 | self.assertEqual(decode(br"[\x]\x", "ignore"), (b"[]", 6)) |
| 1191 | self.assertEqual(decode(br"[\x]\x", "replace"), (b"[?]?", 6)) |
| 1192 | self.assertRaises(ValueError, decode, br"\x0") |
| 1193 | self.assertRaises(ValueError, decode, br"[\x0]") |
| 1194 | self.assertEqual(decode(br"[\x0]\x0", "ignore"), (b"[]", 8)) |
| 1195 | self.assertEqual(decode(br"[\x0]\x0", "replace"), (b"[?]?", 8)) |
Serhiy Storchaka | ace3ad3 | 2013-01-25 23:31:43 +0200 | [diff] [blame] | 1196 | |
Marc-André Lemburg | 29273c8 | 2003-02-04 19:35:03 +0000 | [diff] [blame] | 1197 | class RecodingTest(unittest.TestCase): |
| 1198 | def test_recoding(self): |
Guido van Rossum | f4cfc8f | 2007-05-17 21:52:23 +0000 | [diff] [blame] | 1199 | f = io.BytesIO() |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 1200 | f2 = codecs.EncodedFile(f, "unicode_internal", "utf-8") |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1201 | f2.write("a") |
Marc-André Lemburg | 29273c8 | 2003-02-04 19:35:03 +0000 | [diff] [blame] | 1202 | f2.close() |
| 1203 | # Python used to crash on this at exit because of a refcount |
| 1204 | # bug in _codecsmodule.c |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 1205 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 1206 | self.assertTrue(f.closed) |
| 1207 | |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1208 | # From RFC 3492 |
| 1209 | punycode_testcases = [ |
| 1210 | # A Arabic (Egyptian): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1211 | ("\u0644\u064A\u0647\u0645\u0627\u0628\u062A\u0643\u0644" |
| 1212 | "\u0645\u0648\u0634\u0639\u0631\u0628\u064A\u061F", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 1213 | b"egbpdaj6bu4bxfgehfvwxn"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1214 | # B Chinese (simplified): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1215 | ("\u4ED6\u4EEC\u4E3A\u4EC0\u4E48\u4E0D\u8BF4\u4E2D\u6587", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 1216 | b"ihqwcrb4cv8a8dqg056pqjye"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1217 | # C Chinese (traditional): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1218 | ("\u4ED6\u5011\u7232\u4EC0\u9EBD\u4E0D\u8AAA\u4E2D\u6587", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 1219 | b"ihqwctvzc91f659drss3x8bo0yb"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1220 | # D Czech: Pro<ccaron>prost<ecaron>nemluv<iacute><ccaron>esky |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1221 | ("\u0050\u0072\u006F\u010D\u0070\u0072\u006F\u0073\u0074" |
| 1222 | "\u011B\u006E\u0065\u006D\u006C\u0075\u0076\u00ED\u010D" |
| 1223 | "\u0065\u0073\u006B\u0079", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 1224 | b"Proprostnemluvesky-uyb24dma41a"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1225 | # E Hebrew: |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1226 | ("\u05DC\u05DE\u05D4\u05D4\u05DD\u05E4\u05E9\u05D5\u05D8" |
| 1227 | "\u05DC\u05D0\u05DE\u05D3\u05D1\u05E8\u05D9\u05DD\u05E2" |
| 1228 | "\u05D1\u05E8\u05D9\u05EA", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 1229 | b"4dbcagdahymbxekheh6e0a7fei0b"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1230 | # F Hindi (Devanagari): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1231 | ("\u092F\u0939\u0932\u094B\u0917\u0939\u093F\u0928\u094D" |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 1232 | "\u0926\u0940\u0915\u094D\u092F\u094B\u0902\u0928\u0939" |
| 1233 | "\u0940\u0902\u092C\u094B\u0932\u0938\u0915\u0924\u0947" |
| 1234 | "\u0939\u0948\u0902", |
| 1235 | b"i1baa7eci9glrd9b2ae1bj0hfcgg6iyaf8o0a1dig0cd"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1236 | |
| 1237 | #(G) Japanese (kanji and hiragana): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1238 | ("\u306A\u305C\u307F\u3093\u306A\u65E5\u672C\u8A9E\u3092" |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 1239 | "\u8A71\u3057\u3066\u304F\u308C\u306A\u3044\u306E\u304B", |
| 1240 | b"n8jok5ay5dzabd5bym9f0cm5685rrjetr6pdxa"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1241 | |
| 1242 | # (H) Korean (Hangul syllables): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1243 | ("\uC138\uACC4\uC758\uBAA8\uB4E0\uC0AC\uB78C\uB4E4\uC774" |
| 1244 | "\uD55C\uAD6D\uC5B4\uB97C\uC774\uD574\uD55C\uB2E4\uBA74" |
| 1245 | "\uC5BC\uB9C8\uB098\uC88B\uC744\uAE4C", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 1246 | b"989aomsvi5e83db1d2a355cv1e0vak1dwrv93d5xbh15a0dt30a5j" |
| 1247 | b"psd879ccm6fea98c"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1248 | |
| 1249 | # (I) Russian (Cyrillic): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1250 | ("\u043F\u043E\u0447\u0435\u043C\u0443\u0436\u0435\u043E" |
| 1251 | "\u043D\u0438\u043D\u0435\u0433\u043E\u0432\u043E\u0440" |
| 1252 | "\u044F\u0442\u043F\u043E\u0440\u0443\u0441\u0441\u043A" |
| 1253 | "\u0438", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 1254 | b"b1abfaaepdrnnbgefbaDotcwatmq2g4l"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1255 | |
| 1256 | # (J) Spanish: Porqu<eacute>nopuedensimplementehablarenEspa<ntilde>ol |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1257 | ("\u0050\u006F\u0072\u0071\u0075\u00E9\u006E\u006F\u0070" |
| 1258 | "\u0075\u0065\u0064\u0065\u006E\u0073\u0069\u006D\u0070" |
| 1259 | "\u006C\u0065\u006D\u0065\u006E\u0074\u0065\u0068\u0061" |
| 1260 | "\u0062\u006C\u0061\u0072\u0065\u006E\u0045\u0073\u0070" |
| 1261 | "\u0061\u00F1\u006F\u006C", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 1262 | b"PorqunopuedensimplementehablarenEspaol-fmd56a"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1263 | |
| 1264 | # (K) Vietnamese: |
| 1265 | # T<adotbelow>isaoh<odotbelow>kh<ocirc>ngth<ecirchookabove>ch\ |
| 1266 | # <ihookabove>n<oacute>iti<ecircacute>ngVi<ecircdotbelow>t |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1267 | ("\u0054\u1EA1\u0069\u0073\u0061\u006F\u0068\u1ECD\u006B" |
| 1268 | "\u0068\u00F4\u006E\u0067\u0074\u0068\u1EC3\u0063\u0068" |
| 1269 | "\u1EC9\u006E\u00F3\u0069\u0074\u0069\u1EBF\u006E\u0067" |
| 1270 | "\u0056\u0069\u1EC7\u0074", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 1271 | b"TisaohkhngthchnitingVit-kjcr8268qyxafd2f1b9g"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1272 | |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1273 | #(L) 3<nen>B<gumi><kinpachi><sensei> |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1274 | ("\u0033\u5E74\u0042\u7D44\u91D1\u516B\u5148\u751F", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 1275 | b"3B-ww4c5e180e575a65lsy2b"), |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 1276 | |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1277 | # (M) <amuro><namie>-with-SUPER-MONKEYS |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1278 | ("\u5B89\u5BA4\u5948\u7F8E\u6075\u002D\u0077\u0069\u0074" |
| 1279 | "\u0068\u002D\u0053\u0055\u0050\u0045\u0052\u002D\u004D" |
| 1280 | "\u004F\u004E\u004B\u0045\u0059\u0053", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 1281 | b"-with-SUPER-MONKEYS-pc58ag80a8qai00g7n9n"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1282 | |
| 1283 | # (N) Hello-Another-Way-<sorezore><no><basho> |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1284 | ("\u0048\u0065\u006C\u006C\u006F\u002D\u0041\u006E\u006F" |
| 1285 | "\u0074\u0068\u0065\u0072\u002D\u0057\u0061\u0079\u002D" |
| 1286 | "\u305D\u308C\u305E\u308C\u306E\u5834\u6240", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 1287 | b"Hello-Another-Way--fc4qua05auwb3674vfr0b"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1288 | |
| 1289 | # (O) <hitotsu><yane><no><shita>2 |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1290 | ("\u3072\u3068\u3064\u5C4B\u6839\u306E\u4E0B\u0032", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 1291 | b"2-u9tlzr9756bt3uc0v"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1292 | |
| 1293 | # (P) Maji<de>Koi<suru>5<byou><mae> |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1294 | ("\u004D\u0061\u006A\u0069\u3067\u004B\u006F\u0069\u3059" |
| 1295 | "\u308B\u0035\u79D2\u524D", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 1296 | b"MajiKoi5-783gue6qz075azm5e"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1297 | |
| 1298 | # (Q) <pafii>de<runba> |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1299 | ("\u30D1\u30D5\u30A3\u30FC\u0064\u0065\u30EB\u30F3\u30D0", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 1300 | b"de-jg4avhby1noc0d"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1301 | |
| 1302 | # (R) <sono><supiido><de> |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1303 | ("\u305D\u306E\u30B9\u30D4\u30FC\u30C9\u3067", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 1304 | b"d9juau41awczczp"), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1305 | |
| 1306 | # (S) -> $1.00 <- |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1307 | ("\u002D\u003E\u0020\u0024\u0031\u002E\u0030\u0030\u0020" |
| 1308 | "\u003C\u002D", |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 1309 | b"-> $1.00 <--") |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1310 | ] |
| 1311 | |
| 1312 | for i in punycode_testcases: |
| 1313 | if len(i)!=2: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1314 | print(repr(i)) |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1315 | |
| 1316 | class PunycodeTest(unittest.TestCase): |
| 1317 | def test_encode(self): |
| 1318 | for uni, puny in punycode_testcases: |
| 1319 | # Need to convert both strings to lower case, since |
| 1320 | # some of the extended encodings use upper case, but our |
| 1321 | # code produces only lower case. Converting just puny to |
| 1322 | # lower is also insufficient, since some of the input characters |
| 1323 | # are upper case. |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1324 | self.assertEqual( |
Walter Dörwald | a4c6128 | 2007-05-10 12:36:25 +0000 | [diff] [blame] | 1325 | str(uni.encode("punycode"), "ascii").lower(), |
| 1326 | str(puny, "ascii").lower() |
| 1327 | ) |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1328 | |
| 1329 | def test_decode(self): |
| 1330 | for uni, puny in punycode_testcases: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1331 | self.assertEqual(uni, puny.decode("punycode")) |
Guido van Rossum | 04c70ad | 2007-08-29 14:04:40 +0000 | [diff] [blame] | 1332 | puny = puny.decode("ascii").encode("ascii") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1333 | self.assertEqual(uni, puny.decode("punycode")) |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1334 | |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 1335 | class UnicodeInternalTest(unittest.TestCase): |
Victor Stinner | 182d90d | 2011-09-29 19:53:55 +0200 | [diff] [blame] | 1336 | @unittest.skipUnless(SIZEOF_WCHAR_T == 4, 'specific to 32-bit wchar_t') |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 1337 | def test_bug1251300(self): |
| 1338 | # Decoding with unicode_internal used to not correctly handle "code |
| 1339 | # points" above 0x10ffff on UCS-4 builds. |
Victor Stinner | 182d90d | 2011-09-29 19:53:55 +0200 | [diff] [blame] | 1340 | ok = [ |
| 1341 | (b"\x00\x10\xff\xff", "\U0010ffff"), |
| 1342 | (b"\x00\x00\x01\x01", "\U00000101"), |
| 1343 | (b"", ""), |
| 1344 | ] |
| 1345 | not_ok = [ |
| 1346 | b"\x7f\xff\xff\xff", |
| 1347 | b"\x80\x00\x00\x00", |
| 1348 | b"\x81\x00\x00\x00", |
| 1349 | b"\x00", |
| 1350 | b"\x00\x00\x00\x00\x00", |
| 1351 | ] |
| 1352 | for internal, uni in ok: |
| 1353 | if sys.byteorder == "little": |
| 1354 | internal = bytes(reversed(internal)) |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 1355 | with support.check_warnings(): |
| 1356 | self.assertEqual(uni, internal.decode("unicode_internal")) |
Victor Stinner | 182d90d | 2011-09-29 19:53:55 +0200 | [diff] [blame] | 1357 | for internal in not_ok: |
| 1358 | if sys.byteorder == "little": |
| 1359 | internal = bytes(reversed(internal)) |
Ezio Melotti | 345379a | 2011-11-16 09:54:19 +0200 | [diff] [blame] | 1360 | with support.check_warnings(('unicode_internal codec has been ' |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 1361 | 'deprecated', DeprecationWarning)): |
| 1362 | self.assertRaises(UnicodeDecodeError, internal.decode, |
| 1363 | "unicode_internal") |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1364 | if sys.byteorder == "little": |
| 1365 | invalid = b"\x00\x00\x11\x00" |
Serhiy Storchaka | 07985ef | 2015-01-25 22:56:57 +0200 | [diff] [blame] | 1366 | invalid_backslashreplace = r"\x00\x00\x11\x00" |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1367 | else: |
| 1368 | invalid = b"\x00\x11\x00\x00" |
Serhiy Storchaka | 07985ef | 2015-01-25 22:56:57 +0200 | [diff] [blame] | 1369 | invalid_backslashreplace = r"\x00\x11\x00\x00" |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1370 | with support.check_warnings(): |
| 1371 | self.assertRaises(UnicodeDecodeError, |
| 1372 | invalid.decode, "unicode_internal") |
| 1373 | with support.check_warnings(): |
| 1374 | self.assertEqual(invalid.decode("unicode_internal", "replace"), |
| 1375 | '\ufffd') |
Serhiy Storchaka | 07985ef | 2015-01-25 22:56:57 +0200 | [diff] [blame] | 1376 | with support.check_warnings(): |
| 1377 | self.assertEqual(invalid.decode("unicode_internal", "backslashreplace"), |
| 1378 | invalid_backslashreplace) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 1379 | |
Victor Stinner | 182d90d | 2011-09-29 19:53:55 +0200 | [diff] [blame] | 1380 | @unittest.skipUnless(SIZEOF_WCHAR_T == 4, 'specific to 32-bit wchar_t') |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 1381 | def test_decode_error_attributes(self): |
Victor Stinner | 182d90d | 2011-09-29 19:53:55 +0200 | [diff] [blame] | 1382 | try: |
Ezio Melotti | 345379a | 2011-11-16 09:54:19 +0200 | [diff] [blame] | 1383 | with support.check_warnings(('unicode_internal codec has been ' |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 1384 | 'deprecated', DeprecationWarning)): |
| 1385 | b"\x00\x00\x00\x00\x00\x11\x11\x00".decode("unicode_internal") |
Victor Stinner | 182d90d | 2011-09-29 19:53:55 +0200 | [diff] [blame] | 1386 | except UnicodeDecodeError as ex: |
| 1387 | self.assertEqual("unicode_internal", ex.encoding) |
| 1388 | self.assertEqual(b"\x00\x00\x00\x00\x00\x11\x11\x00", ex.object) |
| 1389 | self.assertEqual(4, ex.start) |
| 1390 | self.assertEqual(8, ex.end) |
| 1391 | else: |
| 1392 | self.fail() |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 1393 | |
Victor Stinner | 182d90d | 2011-09-29 19:53:55 +0200 | [diff] [blame] | 1394 | @unittest.skipUnless(SIZEOF_WCHAR_T == 4, 'specific to 32-bit wchar_t') |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 1395 | def test_decode_callback(self): |
Victor Stinner | 182d90d | 2011-09-29 19:53:55 +0200 | [diff] [blame] | 1396 | codecs.register_error("UnicodeInternalTest", codecs.ignore_errors) |
| 1397 | decoder = codecs.getdecoder("unicode_internal") |
Ezio Melotti | 345379a | 2011-11-16 09:54:19 +0200 | [diff] [blame] | 1398 | with support.check_warnings(('unicode_internal codec has been ' |
Ezio Melotti | 11060a4 | 2011-11-16 09:39:10 +0200 | [diff] [blame] | 1399 | 'deprecated', DeprecationWarning)): |
| 1400 | ab = "ab".encode("unicode_internal").decode() |
| 1401 | ignored = decoder(bytes("%s\x22\x22\x22\x22%s" % (ab[:4], ab[4:]), |
| 1402 | "ascii"), |
| 1403 | "UnicodeInternalTest") |
Victor Stinner | 182d90d | 2011-09-29 19:53:55 +0200 | [diff] [blame] | 1404 | self.assertEqual(("ab", 12), ignored) |
Walter Dörwald | a47d1c0 | 2005-08-30 10:23:14 +0000 | [diff] [blame] | 1405 | |
Walter Dörwald | 8dc33d5 | 2009-05-06 14:41:26 +0000 | [diff] [blame] | 1406 | def test_encode_length(self): |
Ezio Melotti | adc417c | 2011-11-17 12:23:34 +0200 | [diff] [blame] | 1407 | with support.check_warnings(('unicode_internal codec has been ' |
| 1408 | 'deprecated', DeprecationWarning)): |
Victor Stinner | 040e16e | 2011-11-15 22:44:05 +0100 | [diff] [blame] | 1409 | # Issue 3739 |
| 1410 | encoder = codecs.getencoder("unicode_internal") |
| 1411 | self.assertEqual(encoder("a")[1], 1) |
| 1412 | self.assertEqual(encoder("\xe9\u0142")[1], 2) |
| 1413 | |
| 1414 | self.assertEqual(codecs.escape_encode(br'\x00')[1], 4) |
Philip Jenvey | 66a1bd5 | 2010-04-05 03:05:24 +0000 | [diff] [blame] | 1415 | |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1416 | # From http://www.gnu.org/software/libidn/draft-josefsson-idn-test-vectors.html |
| 1417 | nameprep_tests = [ |
| 1418 | # 3.1 Map to nothing. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1419 | (b'foo\xc2\xad\xcd\x8f\xe1\xa0\x86\xe1\xa0\x8bbar' |
| 1420 | b'\xe2\x80\x8b\xe2\x81\xa0baz\xef\xb8\x80\xef\xb8\x88\xef' |
| 1421 | b'\xb8\x8f\xef\xbb\xbf', |
| 1422 | b'foobarbaz'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1423 | # 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] | 1424 | (b'CAFE', |
| 1425 | b'cafe'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1426 | # 3.3 Case folding 8bit U+00DF (german sharp s). |
| 1427 | # The original test case is bogus; it says \xc3\xdf |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1428 | (b'\xc3\x9f', |
| 1429 | b'ss'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1430 | # 3.4 Case folding U+0130 (turkish capital I with dot). |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1431 | (b'\xc4\xb0', |
| 1432 | b'i\xcc\x87'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1433 | # 3.5 Case folding multibyte U+0143 U+037A. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1434 | (b'\xc5\x83\xcd\xba', |
| 1435 | b'\xc5\x84 \xce\xb9'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1436 | # 3.6 Case folding U+2121 U+33C6 U+1D7BB. |
| 1437 | # XXX: skip this as it fails in UCS-2 mode |
| 1438 | #('\xe2\x84\xa1\xe3\x8f\x86\xf0\x9d\x9e\xbb', |
| 1439 | # 'telc\xe2\x88\x95kg\xcf\x83'), |
| 1440 | (None, None), |
| 1441 | # 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] | 1442 | (b'j\xcc\x8c\xc2\xa0\xc2\xaa', |
| 1443 | b'\xc7\xb0 a'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1444 | # 3.8 Case folding U+1FB7 and normalization. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1445 | (b'\xe1\xbe\xb7', |
| 1446 | b'\xe1\xbe\xb6\xce\xb9'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1447 | # 3.9 Self-reverting case folding U+01F0 and normalization. |
| 1448 | # The original test case is bogus, it says `\xc7\xf0' |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1449 | (b'\xc7\xb0', |
| 1450 | b'\xc7\xb0'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1451 | # 3.10 Self-reverting case folding U+0390 and normalization. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1452 | (b'\xce\x90', |
| 1453 | b'\xce\x90'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1454 | # 3.11 Self-reverting case folding U+03B0 and normalization. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1455 | (b'\xce\xb0', |
| 1456 | b'\xce\xb0'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1457 | # 3.12 Self-reverting case folding U+1E96 and normalization. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1458 | (b'\xe1\xba\x96', |
| 1459 | b'\xe1\xba\x96'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1460 | # 3.13 Self-reverting case folding U+1F56 and normalization. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1461 | (b'\xe1\xbd\x96', |
| 1462 | b'\xe1\xbd\x96'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1463 | # 3.14 ASCII space character U+0020. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1464 | (b' ', |
| 1465 | b' '), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1466 | # 3.15 Non-ASCII 8bit space character U+00A0. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1467 | (b'\xc2\xa0', |
| 1468 | b' '), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1469 | # 3.16 Non-ASCII multibyte space character U+1680. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1470 | (b'\xe1\x9a\x80', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1471 | None), |
| 1472 | # 3.17 Non-ASCII multibyte space character U+2000. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1473 | (b'\xe2\x80\x80', |
| 1474 | b' '), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1475 | # 3.18 Zero Width Space U+200b. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1476 | (b'\xe2\x80\x8b', |
| 1477 | b''), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1478 | # 3.19 Non-ASCII multibyte space character U+3000. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1479 | (b'\xe3\x80\x80', |
| 1480 | b' '), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1481 | # 3.20 ASCII control characters U+0010 U+007F. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1482 | (b'\x10\x7f', |
| 1483 | b'\x10\x7f'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1484 | # 3.21 Non-ASCII 8bit control character U+0085. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1485 | (b'\xc2\x85', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1486 | None), |
| 1487 | # 3.22 Non-ASCII multibyte control character U+180E. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1488 | (b'\xe1\xa0\x8e', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1489 | None), |
| 1490 | # 3.23 Zero Width No-Break Space U+FEFF. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1491 | (b'\xef\xbb\xbf', |
| 1492 | b''), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1493 | # 3.24 Non-ASCII control character U+1D175. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1494 | (b'\xf0\x9d\x85\xb5', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1495 | None), |
| 1496 | # 3.25 Plane 0 private use character U+F123. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1497 | (b'\xef\x84\xa3', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1498 | None), |
| 1499 | # 3.26 Plane 15 private use character U+F1234. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1500 | (b'\xf3\xb1\x88\xb4', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1501 | None), |
| 1502 | # 3.27 Plane 16 private use character U+10F234. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1503 | (b'\xf4\x8f\x88\xb4', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1504 | None), |
| 1505 | # 3.28 Non-character code point U+8FFFE. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1506 | (b'\xf2\x8f\xbf\xbe', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1507 | None), |
| 1508 | # 3.29 Non-character code point U+10FFFF. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1509 | (b'\xf4\x8f\xbf\xbf', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1510 | None), |
| 1511 | # 3.30 Surrogate code U+DF42. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1512 | (b'\xed\xbd\x82', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1513 | None), |
| 1514 | # 3.31 Non-plain text character U+FFFD. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1515 | (b'\xef\xbf\xbd', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1516 | None), |
| 1517 | # 3.32 Ideographic description character U+2FF5. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1518 | (b'\xe2\xbf\xb5', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1519 | None), |
| 1520 | # 3.33 Display property character U+0341. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1521 | (b'\xcd\x81', |
| 1522 | b'\xcc\x81'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1523 | # 3.34 Left-to-right mark U+200E. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1524 | (b'\xe2\x80\x8e', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1525 | None), |
| 1526 | # 3.35 Deprecated U+202A. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1527 | (b'\xe2\x80\xaa', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1528 | None), |
| 1529 | # 3.36 Language tagging character U+E0001. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1530 | (b'\xf3\xa0\x80\x81', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1531 | None), |
| 1532 | # 3.37 Language tagging character U+E0042. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1533 | (b'\xf3\xa0\x81\x82', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1534 | None), |
| 1535 | # 3.38 Bidi: RandALCat character U+05BE and LCat characters. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1536 | (b'foo\xd6\xbebar', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1537 | None), |
| 1538 | # 3.39 Bidi: RandALCat character U+FD50 and LCat characters. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1539 | (b'foo\xef\xb5\x90bar', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1540 | None), |
| 1541 | # 3.40 Bidi: RandALCat character U+FB38 and LCat characters. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1542 | (b'foo\xef\xb9\xb6bar', |
| 1543 | b'foo \xd9\x8ebar'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1544 | # 3.41 Bidi: RandALCat without trailing RandALCat U+0627 U+0031. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1545 | (b'\xd8\xa71', |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1546 | None), |
| 1547 | # 3.42 Bidi: RandALCat character U+0627 U+0031 U+0628. |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1548 | (b'\xd8\xa71\xd8\xa8', |
| 1549 | b'\xd8\xa71\xd8\xa8'), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1550 | # 3.43 Unassigned code point U+E0002. |
Martin v. Löwis | b5c4b7b | 2003-04-18 20:21:00 +0000 | [diff] [blame] | 1551 | # Skip this test as we allow unassigned |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1552 | #(b'\xf3\xa0\x80\x82', |
Martin v. Löwis | b5c4b7b | 2003-04-18 20:21:00 +0000 | [diff] [blame] | 1553 | # None), |
| 1554 | (None, None), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1555 | # 3.44 Larger test (shrinking). |
| 1556 | # Original test case reads \xc3\xdf |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1557 | (b'X\xc2\xad\xc3\x9f\xc4\xb0\xe2\x84\xa1j\xcc\x8c\xc2\xa0\xc2' |
| 1558 | b'\xaa\xce\xb0\xe2\x80\x80', |
| 1559 | b'xssi\xcc\x87tel\xc7\xb0 a\xce\xb0 '), |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1560 | # 3.45 Larger test (expanding). |
| 1561 | # Original test case reads \xc3\x9f |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1562 | (b'X\xc3\x9f\xe3\x8c\x96\xc4\xb0\xe2\x84\xa1\xe2\x92\x9f\xe3\x8c' |
| 1563 | b'\x80', |
| 1564 | b'xss\xe3\x82\xad\xe3\x83\xad\xe3\x83\xa1\xe3\x83\xbc\xe3' |
| 1565 | b'\x83\x88\xe3\x83\xabi\xcc\x87tel\x28d\x29\xe3\x82' |
| 1566 | 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] | 1567 | ] |
| 1568 | |
| 1569 | |
| 1570 | class NameprepTest(unittest.TestCase): |
| 1571 | def test_nameprep(self): |
| 1572 | from encodings.idna import nameprep |
| 1573 | for pos, (orig, prepped) in enumerate(nameprep_tests): |
| 1574 | if orig is None: |
| 1575 | # Skipped |
| 1576 | continue |
| 1577 | # The Unicode strings are given in UTF-8 |
Martin v. Löwis | e0a2b72 | 2009-05-10 08:08:56 +0000 | [diff] [blame] | 1578 | orig = str(orig, "utf-8", "surrogatepass") |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1579 | if prepped is None: |
| 1580 | # Input contains prohibited characters |
| 1581 | self.assertRaises(UnicodeError, nameprep, orig) |
| 1582 | else: |
Martin v. Löwis | e0a2b72 | 2009-05-10 08:08:56 +0000 | [diff] [blame] | 1583 | prepped = str(prepped, "utf-8", "surrogatepass") |
Martin v. Löwis | 2548c73 | 2003-04-18 10:39:54 +0000 | [diff] [blame] | 1584 | try: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1585 | self.assertEqual(nameprep(orig), prepped) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 1586 | except Exception as e: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1587 | 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] | 1588 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1589 | class IDNACodecTest(unittest.TestCase): |
| 1590 | def test_builtin_decode(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1591 | self.assertEqual(str(b"python.org", "idna"), "python.org") |
| 1592 | self.assertEqual(str(b"python.org.", "idna"), "python.org.") |
| 1593 | self.assertEqual(str(b"xn--pythn-mua.org", "idna"), "pyth\xf6n.org") |
| 1594 | self.assertEqual(str(b"xn--pythn-mua.org.", "idna"), "pyth\xf6n.org.") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1595 | |
| 1596 | def test_builtin_encode(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1597 | self.assertEqual("python.org".encode("idna"), b"python.org") |
| 1598 | self.assertEqual("python.org.".encode("idna"), b"python.org.") |
| 1599 | self.assertEqual("pyth\xf6n.org".encode("idna"), b"xn--pythn-mua.org") |
| 1600 | 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] | 1601 | |
Martin v. Löwis | 8b59514 | 2005-08-25 11:03:38 +0000 | [diff] [blame] | 1602 | def test_stream(self): |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 1603 | r = codecs.getreader("idna")(io.BytesIO(b"abc")) |
Martin v. Löwis | 8b59514 | 2005-08-25 11:03:38 +0000 | [diff] [blame] | 1604 | r.read(3) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1605 | self.assertEqual(r.read(), "") |
Martin v. Löwis | 8b59514 | 2005-08-25 11:03:38 +0000 | [diff] [blame] | 1606 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1607 | def test_incremental_decode(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1608 | self.assertEqual( |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 1609 | "".join(codecs.iterdecode((bytes([c]) for c in b"python.org"), "idna")), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1610 | "python.org" |
| 1611 | ) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1612 | self.assertEqual( |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 1613 | "".join(codecs.iterdecode((bytes([c]) for c in b"python.org."), "idna")), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1614 | "python.org." |
| 1615 | ) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1616 | self.assertEqual( |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 1617 | "".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] | 1618 | "pyth\xf6n.org." |
| 1619 | ) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1620 | self.assertEqual( |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 1621 | "".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] | 1622 | "pyth\xf6n.org." |
| 1623 | ) |
| 1624 | |
| 1625 | decoder = codecs.getincrementaldecoder("idna")() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1626 | self.assertEqual(decoder.decode(b"xn--xam", ), "") |
| 1627 | self.assertEqual(decoder.decode(b"ple-9ta.o", ), "\xe4xample.") |
| 1628 | self.assertEqual(decoder.decode(b"rg"), "") |
| 1629 | self.assertEqual(decoder.decode(b"", True), "org") |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1630 | |
| 1631 | decoder.reset() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1632 | self.assertEqual(decoder.decode(b"xn--xam", ), "") |
| 1633 | self.assertEqual(decoder.decode(b"ple-9ta.o", ), "\xe4xample.") |
| 1634 | self.assertEqual(decoder.decode(b"rg."), "org.") |
| 1635 | self.assertEqual(decoder.decode(b"", True), "") |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1636 | |
| 1637 | def test_incremental_encode(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1638 | self.assertEqual( |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1639 | b"".join(codecs.iterencode("python.org", "idna")), |
| 1640 | b"python.org" |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1641 | ) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1642 | self.assertEqual( |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1643 | b"".join(codecs.iterencode("python.org.", "idna")), |
| 1644 | b"python.org." |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1645 | ) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1646 | self.assertEqual( |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1647 | b"".join(codecs.iterencode("pyth\xf6n.org.", "idna")), |
| 1648 | b"xn--pythn-mua.org." |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1649 | ) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1650 | self.assertEqual( |
Walter Dörwald | 0ac30f8 | 2007-05-11 10:32:57 +0000 | [diff] [blame] | 1651 | b"".join(codecs.iterencode("pyth\xf6n.org.", "idna")), |
| 1652 | b"xn--pythn-mua.org." |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1653 | ) |
| 1654 | |
| 1655 | encoder = codecs.getincrementalencoder("idna")() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1656 | self.assertEqual(encoder.encode("\xe4x"), b"") |
| 1657 | self.assertEqual(encoder.encode("ample.org"), b"xn--xample-9ta.") |
| 1658 | self.assertEqual(encoder.encode("", True), b"org") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1659 | |
| 1660 | encoder.reset() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1661 | self.assertEqual(encoder.encode("\xe4x"), b"") |
| 1662 | self.assertEqual(encoder.encode("ample.org."), b"xn--xample-9ta.org.") |
| 1663 | self.assertEqual(encoder.encode("", True), b"") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1664 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 1665 | def test_errors(self): |
| 1666 | """Only supports "strict" error handler""" |
| 1667 | "python.org".encode("idna", "strict") |
| 1668 | b"python.org".decode("idna", "strict") |
| 1669 | for errors in ("ignore", "replace", "backslashreplace", |
| 1670 | "surrogateescape"): |
| 1671 | self.assertRaises(Exception, "python.org".encode, "idna", errors) |
| 1672 | self.assertRaises(Exception, |
| 1673 | b"python.org".decode, "idna", errors) |
| 1674 | |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 1675 | class CodecsModuleTest(unittest.TestCase): |
| 1676 | |
| 1677 | def test_decode(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1678 | self.assertEqual(codecs.decode(b'\xe4\xf6\xfc', 'latin-1'), |
| 1679 | '\xe4\xf6\xfc') |
Walter Dörwald | 063e1e8 | 2004-10-28 13:04:26 +0000 | [diff] [blame] | 1680 | self.assertRaises(TypeError, codecs.decode) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1681 | self.assertEqual(codecs.decode(b'abc'), 'abc') |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1682 | self.assertRaises(UnicodeDecodeError, codecs.decode, b'\xff', 'ascii') |
Walter Dörwald | 063e1e8 | 2004-10-28 13:04:26 +0000 | [diff] [blame] | 1683 | |
Victor Stinner | a57dfd0 | 2014-05-14 17:13:14 +0200 | [diff] [blame] | 1684 | # test keywords |
| 1685 | self.assertEqual(codecs.decode(obj=b'\xe4\xf6\xfc', encoding='latin-1'), |
| 1686 | '\xe4\xf6\xfc') |
| 1687 | self.assertEqual(codecs.decode(b'[\xff]', 'ascii', errors='ignore'), |
| 1688 | '[]') |
| 1689 | |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 1690 | def test_encode(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1691 | self.assertEqual(codecs.encode('\xe4\xf6\xfc', 'latin-1'), |
| 1692 | b'\xe4\xf6\xfc') |
Walter Dörwald | 063e1e8 | 2004-10-28 13:04:26 +0000 | [diff] [blame] | 1693 | self.assertRaises(TypeError, codecs.encode) |
Walter Dörwald | 690402f | 2005-11-17 18:51:34 +0000 | [diff] [blame] | 1694 | self.assertRaises(LookupError, codecs.encode, "foo", "__spam__") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1695 | self.assertEqual(codecs.encode('abc'), b'abc') |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1696 | self.assertRaises(UnicodeEncodeError, codecs.encode, '\xffff', 'ascii') |
Walter Dörwald | 063e1e8 | 2004-10-28 13:04:26 +0000 | [diff] [blame] | 1697 | |
Victor Stinner | a57dfd0 | 2014-05-14 17:13:14 +0200 | [diff] [blame] | 1698 | # test keywords |
| 1699 | self.assertEqual(codecs.encode(obj='\xe4\xf6\xfc', encoding='latin-1'), |
| 1700 | b'\xe4\xf6\xfc') |
| 1701 | self.assertEqual(codecs.encode('[\xff]', 'ascii', errors='ignore'), |
| 1702 | b'[]') |
| 1703 | |
Walter Dörwald | 063e1e8 | 2004-10-28 13:04:26 +0000 | [diff] [blame] | 1704 | def test_register(self): |
| 1705 | self.assertRaises(TypeError, codecs.register) |
Walter Dörwald | 690402f | 2005-11-17 18:51:34 +0000 | [diff] [blame] | 1706 | self.assertRaises(TypeError, codecs.register, 42) |
Walter Dörwald | 063e1e8 | 2004-10-28 13:04:26 +0000 | [diff] [blame] | 1707 | |
| 1708 | def test_lookup(self): |
| 1709 | self.assertRaises(TypeError, codecs.lookup) |
| 1710 | self.assertRaises(LookupError, codecs.lookup, "__spam__") |
Walter Dörwald | 690402f | 2005-11-17 18:51:34 +0000 | [diff] [blame] | 1711 | self.assertRaises(LookupError, codecs.lookup, " ") |
| 1712 | |
| 1713 | def test_getencoder(self): |
| 1714 | self.assertRaises(TypeError, codecs.getencoder) |
| 1715 | self.assertRaises(LookupError, codecs.getencoder, "__spam__") |
| 1716 | |
| 1717 | def test_getdecoder(self): |
| 1718 | self.assertRaises(TypeError, codecs.getdecoder) |
| 1719 | self.assertRaises(LookupError, codecs.getdecoder, "__spam__") |
| 1720 | |
| 1721 | def test_getreader(self): |
| 1722 | self.assertRaises(TypeError, codecs.getreader) |
| 1723 | self.assertRaises(LookupError, codecs.getreader, "__spam__") |
| 1724 | |
| 1725 | def test_getwriter(self): |
| 1726 | self.assertRaises(TypeError, codecs.getwriter) |
| 1727 | self.assertRaises(LookupError, codecs.getwriter, "__spam__") |
Marc-André Lemburg | 3f41974 | 2004-07-10 12:06:10 +0000 | [diff] [blame] | 1728 | |
Antoine Pitrou | cf9d3c0 | 2011-07-24 02:27:04 +0200 | [diff] [blame] | 1729 | def test_lookup_issue1813(self): |
| 1730 | # Issue #1813: under Turkish locales, lookup of some codecs failed |
| 1731 | # because 'I' is lowercased as "ı" (dotless i) |
Antoine Pitrou | d05066d | 2011-07-26 23:55:33 +0200 | [diff] [blame] | 1732 | oldlocale = locale.setlocale(locale.LC_CTYPE) |
Antoine Pitrou | cf9d3c0 | 2011-07-24 02:27:04 +0200 | [diff] [blame] | 1733 | self.addCleanup(locale.setlocale, locale.LC_CTYPE, oldlocale) |
| 1734 | try: |
| 1735 | locale.setlocale(locale.LC_CTYPE, 'tr_TR') |
| 1736 | except locale.Error: |
| 1737 | # Unsupported locale on this system |
| 1738 | self.skipTest('test needs Turkish locale') |
| 1739 | c = codecs.lookup('ASCII') |
| 1740 | self.assertEqual(c.name, 'ascii') |
| 1741 | |
Serhiy Storchaka | de3ee5b | 2014-12-20 17:42:38 +0200 | [diff] [blame] | 1742 | def test_all(self): |
| 1743 | api = ( |
| 1744 | "encode", "decode", |
| 1745 | "register", "CodecInfo", "Codec", "IncrementalEncoder", |
| 1746 | "IncrementalDecoder", "StreamReader", "StreamWriter", "lookup", |
| 1747 | "getencoder", "getdecoder", "getincrementalencoder", |
| 1748 | "getincrementaldecoder", "getreader", "getwriter", |
| 1749 | "register_error", "lookup_error", |
| 1750 | "strict_errors", "replace_errors", "ignore_errors", |
| 1751 | "xmlcharrefreplace_errors", "backslashreplace_errors", |
| 1752 | "namereplace_errors", |
| 1753 | "open", "EncodedFile", |
| 1754 | "iterencode", "iterdecode", |
| 1755 | "BOM", "BOM_BE", "BOM_LE", |
| 1756 | "BOM_UTF8", "BOM_UTF16", "BOM_UTF16_BE", "BOM_UTF16_LE", |
| 1757 | "BOM_UTF32", "BOM_UTF32_BE", "BOM_UTF32_LE", |
| 1758 | "BOM32_BE", "BOM32_LE", "BOM64_BE", "BOM64_LE", # Undocumented |
| 1759 | "StreamReaderWriter", "StreamRecoder", |
| 1760 | ) |
| 1761 | self.assertCountEqual(api, codecs.__all__) |
| 1762 | for api in codecs.__all__: |
| 1763 | getattr(codecs, api) |
| 1764 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 1765 | def test_open(self): |
| 1766 | self.addCleanup(support.unlink, support.TESTFN) |
| 1767 | for mode in ('w', 'r', 'r+', 'w+', 'a', 'a+'): |
| 1768 | with self.subTest(mode), \ |
| 1769 | codecs.open(support.TESTFN, mode, 'ascii') as file: |
| 1770 | self.assertIsInstance(file, codecs.StreamReaderWriter) |
| 1771 | |
| 1772 | def test_undefined(self): |
| 1773 | self.assertRaises(UnicodeError, codecs.encode, 'abc', 'undefined') |
| 1774 | self.assertRaises(UnicodeError, codecs.decode, b'abc', 'undefined') |
| 1775 | self.assertRaises(UnicodeError, codecs.encode, '', 'undefined') |
| 1776 | self.assertRaises(UnicodeError, codecs.decode, b'', 'undefined') |
| 1777 | for errors in ('strict', 'ignore', 'replace', 'backslashreplace'): |
| 1778 | self.assertRaises(UnicodeError, |
| 1779 | codecs.encode, 'abc', 'undefined', errors) |
| 1780 | self.assertRaises(UnicodeError, |
| 1781 | codecs.decode, b'abc', 'undefined', errors) |
| 1782 | |
Hye-Shik Chang | af5c7cf | 2004-10-17 23:51:21 +0000 | [diff] [blame] | 1783 | class StreamReaderTest(unittest.TestCase): |
| 1784 | |
| 1785 | def setUp(self): |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 1786 | self.reader = codecs.getreader('utf-8') |
Walter Dörwald | c3ab0a7 | 2007-05-10 15:02:49 +0000 | [diff] [blame] | 1787 | 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] | 1788 | |
| 1789 | def test_readlines(self): |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 1790 | f = self.reader(self.stream) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1791 | self.assertEqual(f.readlines(), ['\ud55c\n', '\uae00']) |
Hye-Shik Chang | af5c7cf | 2004-10-17 23:51:21 +0000 | [diff] [blame] | 1792 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1793 | class EncodedFileTest(unittest.TestCase): |
| 1794 | |
| 1795 | def test_basic(self): |
Walter Dörwald | c3ab0a7 | 2007-05-10 15:02:49 +0000 | [diff] [blame] | 1796 | f = io.BytesIO(b'\xed\x95\x9c\n\xea\xb8\x80') |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 1797 | ef = codecs.EncodedFile(f, 'utf-16-le', 'utf-8') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1798 | self.assertEqual(ef.read(), b'\\\xd5\n\x00\x00\xae') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1799 | |
Walter Dörwald | c3ab0a7 | 2007-05-10 15:02:49 +0000 | [diff] [blame] | 1800 | f = io.BytesIO() |
Marc-André Lemburg | 8f36af7 | 2011-02-25 15:42:01 +0000 | [diff] [blame] | 1801 | ef = codecs.EncodedFile(f, 'utf-8', 'latin-1') |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1802 | ef.write(b'\xc3\xbc') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1803 | self.assertEqual(f.getvalue(), b'\xfc') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1804 | |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1805 | all_unicode_encodings = [ |
| 1806 | "ascii", |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1807 | "big5", |
| 1808 | "big5hkscs", |
| 1809 | "charmap", |
| 1810 | "cp037", |
| 1811 | "cp1006", |
| 1812 | "cp1026", |
Serhiy Storchaka | be0c325 | 2013-11-23 18:52:23 +0200 | [diff] [blame] | 1813 | "cp1125", |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1814 | "cp1140", |
| 1815 | "cp1250", |
| 1816 | "cp1251", |
| 1817 | "cp1252", |
| 1818 | "cp1253", |
| 1819 | "cp1254", |
| 1820 | "cp1255", |
| 1821 | "cp1256", |
| 1822 | "cp1257", |
| 1823 | "cp1258", |
| 1824 | "cp424", |
| 1825 | "cp437", |
| 1826 | "cp500", |
Benjamin Peterson | 5a6214a | 2010-06-27 22:41:29 +0000 | [diff] [blame] | 1827 | "cp720", |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1828 | "cp737", |
| 1829 | "cp775", |
| 1830 | "cp850", |
| 1831 | "cp852", |
| 1832 | "cp855", |
| 1833 | "cp856", |
| 1834 | "cp857", |
Benjamin Peterson | 5a6214a | 2010-06-27 22:41:29 +0000 | [diff] [blame] | 1835 | "cp858", |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1836 | "cp860", |
| 1837 | "cp861", |
| 1838 | "cp862", |
| 1839 | "cp863", |
| 1840 | "cp864", |
| 1841 | "cp865", |
| 1842 | "cp866", |
| 1843 | "cp869", |
| 1844 | "cp874", |
| 1845 | "cp875", |
| 1846 | "cp932", |
| 1847 | "cp949", |
| 1848 | "cp950", |
| 1849 | "euc_jis_2004", |
| 1850 | "euc_jisx0213", |
| 1851 | "euc_jp", |
| 1852 | "euc_kr", |
| 1853 | "gb18030", |
| 1854 | "gb2312", |
| 1855 | "gbk", |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1856 | "hp_roman8", |
| 1857 | "hz", |
| 1858 | "idna", |
| 1859 | "iso2022_jp", |
| 1860 | "iso2022_jp_1", |
| 1861 | "iso2022_jp_2", |
| 1862 | "iso2022_jp_2004", |
| 1863 | "iso2022_jp_3", |
| 1864 | "iso2022_jp_ext", |
| 1865 | "iso2022_kr", |
| 1866 | "iso8859_1", |
| 1867 | "iso8859_10", |
| 1868 | "iso8859_11", |
| 1869 | "iso8859_13", |
| 1870 | "iso8859_14", |
| 1871 | "iso8859_15", |
| 1872 | "iso8859_16", |
| 1873 | "iso8859_2", |
| 1874 | "iso8859_3", |
| 1875 | "iso8859_4", |
| 1876 | "iso8859_5", |
| 1877 | "iso8859_6", |
| 1878 | "iso8859_7", |
| 1879 | "iso8859_8", |
| 1880 | "iso8859_9", |
| 1881 | "johab", |
| 1882 | "koi8_r", |
Serhiy Storchaka | f0eeedf | 2015-05-12 23:24:19 +0300 | [diff] [blame] | 1883 | "koi8_t", |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1884 | "koi8_u", |
Serhiy Storchaka | ad8a1c3 | 2015-05-12 23:16:55 +0300 | [diff] [blame] | 1885 | "kz1048", |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1886 | "latin_1", |
| 1887 | "mac_cyrillic", |
| 1888 | "mac_greek", |
| 1889 | "mac_iceland", |
| 1890 | "mac_latin2", |
| 1891 | "mac_roman", |
| 1892 | "mac_turkish", |
| 1893 | "palmos", |
| 1894 | "ptcp154", |
| 1895 | "punycode", |
| 1896 | "raw_unicode_escape", |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1897 | "shift_jis", |
| 1898 | "shift_jis_2004", |
| 1899 | "shift_jisx0213", |
| 1900 | "tis_620", |
| 1901 | "unicode_escape", |
| 1902 | "unicode_internal", |
| 1903 | "utf_16", |
| 1904 | "utf_16_be", |
| 1905 | "utf_16_le", |
| 1906 | "utf_7", |
| 1907 | "utf_8", |
| 1908 | ] |
| 1909 | |
| 1910 | if hasattr(codecs, "mbcs_encode"): |
| 1911 | all_unicode_encodings.append("mbcs") |
| 1912 | |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1913 | # The following encoding is not tested, because it's not supposed |
| 1914 | # to work: |
| 1915 | # "undefined" |
| 1916 | |
| 1917 | # The following encodings don't work in stateful mode |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 1918 | broken_unicode_with_stateful = [ |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1919 | "punycode", |
| 1920 | "unicode_internal" |
| 1921 | ] |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1922 | |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 1923 | class BasicUnicodeTest(unittest.TestCase, MixInCheckStateHandling): |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1924 | def test_basics(self): |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 1925 | s = "abc123" # all codecs should be able to encode these |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1926 | for encoding in all_unicode_encodings: |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1927 | name = codecs.lookup(encoding).name |
| 1928 | if encoding.endswith("_codec"): |
| 1929 | name += "_codec" |
| 1930 | elif encoding == "latin_1": |
| 1931 | name = "latin_1" |
| 1932 | self.assertEqual(encoding.replace("_", "-"), name.replace("_", "-")) |
Victor Stinner | 040e16e | 2011-11-15 22:44:05 +0100 | [diff] [blame] | 1933 | |
Ezio Melotti | adc417c | 2011-11-17 12:23:34 +0200 | [diff] [blame] | 1934 | with support.check_warnings(): |
Victor Stinner | 040e16e | 2011-11-15 22:44:05 +0100 | [diff] [blame] | 1935 | # unicode-internal has been deprecated |
Victor Stinner | 040e16e | 2011-11-15 22:44:05 +0100 | [diff] [blame] | 1936 | (b, size) = codecs.getencoder(encoding)(s) |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 1937 | self.assertEqual(size, len(s), "encoding=%r" % encoding) |
Victor Stinner | 040e16e | 2011-11-15 22:44:05 +0100 | [diff] [blame] | 1938 | (chars, size) = codecs.getdecoder(encoding)(b) |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 1939 | self.assertEqual(chars, s, "encoding=%r" % encoding) |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1940 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 1941 | if encoding not in broken_unicode_with_stateful: |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1942 | # check stream reader/writer |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1943 | q = Queue(b"") |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 1944 | writer = codecs.getwriter(encoding)(q) |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1945 | encodedresult = b"" |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1946 | for c in s: |
| 1947 | writer.write(c) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1948 | chunk = q.read() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1949 | self.assertTrue(type(chunk) is bytes, type(chunk)) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1950 | encodedresult += chunk |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1951 | q = Queue(b"") |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 1952 | reader = codecs.getreader(encoding)(q) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1953 | decodedresult = "" |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1954 | for c in encodedresult: |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1955 | q.write(bytes([c])) |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1956 | decodedresult += reader.read() |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 1957 | self.assertEqual(decodedresult, s, "encoding=%r" % encoding) |
Walter Dörwald | ee1d247 | 2004-12-29 16:04:38 +0000 | [diff] [blame] | 1958 | |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 1959 | if encoding not in broken_unicode_with_stateful: |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 1960 | # check incremental decoder/encoder and iterencode()/iterdecode() |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1961 | try: |
| 1962 | encoder = codecs.getincrementalencoder(encoding)() |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 1963 | except LookupError: # no IncrementalEncoder |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1964 | pass |
| 1965 | else: |
| 1966 | # check incremental decoder/encoder |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1967 | encodedresult = b"" |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1968 | for c in s: |
| 1969 | encodedresult += encoder.encode(c) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1970 | encodedresult += encoder.encode("", True) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1971 | decoder = codecs.getincrementaldecoder(encoding)() |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1972 | decodedresult = "" |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1973 | for c in encodedresult: |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1974 | decodedresult += decoder.decode(bytes([c])) |
Guido van Rossum | f4cfc8f | 2007-05-17 21:52:23 +0000 | [diff] [blame] | 1975 | decodedresult += decoder.decode(b"", True) |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 1976 | self.assertEqual(decodedresult, s, |
| 1977 | "encoding=%r" % encoding) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1978 | |
| 1979 | # check iterencode()/iterdecode() |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 1980 | result = "".join(codecs.iterdecode( |
| 1981 | codecs.iterencode(s, encoding), encoding)) |
| 1982 | self.assertEqual(result, s, "encoding=%r" % encoding) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1983 | |
| 1984 | # check iterencode()/iterdecode() with empty string |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 1985 | result = "".join(codecs.iterdecode( |
| 1986 | codecs.iterencode("", encoding), encoding)) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1987 | self.assertEqual(result, "") |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1988 | |
Victor Stinner | 554f3f0 | 2010-06-16 23:33:54 +0000 | [diff] [blame] | 1989 | if encoding not in ("idna", "mbcs"): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1990 | # check incremental decoder/encoder with errors argument |
| 1991 | try: |
| 1992 | encoder = codecs.getincrementalencoder(encoding)("ignore") |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 1993 | except LookupError: # no IncrementalEncoder |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1994 | pass |
| 1995 | else: |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 1996 | encodedresult = b"".join(encoder.encode(c) for c in s) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1997 | decoder = codecs.getincrementaldecoder(encoding)("ignore") |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 1998 | decodedresult = "".join(decoder.decode(bytes([c])) |
| 1999 | for c in encodedresult) |
| 2000 | self.assertEqual(decodedresult, s, |
| 2001 | "encoding=%r" % encoding) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2002 | |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 2003 | @support.cpython_only |
| 2004 | def test_basics_capi(self): |
| 2005 | from _testcapi import codec_incrementalencoder, codec_incrementaldecoder |
| 2006 | s = "abc123" # all codecs should be able to encode these |
| 2007 | for encoding in all_unicode_encodings: |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 2008 | if encoding not in broken_unicode_with_stateful: |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 2009 | # check incremental decoder/encoder (fetched via the C API) |
| 2010 | try: |
| 2011 | cencoder = codec_incrementalencoder(encoding) |
| 2012 | except LookupError: # no IncrementalEncoder |
| 2013 | pass |
| 2014 | else: |
| 2015 | # check C API |
| 2016 | encodedresult = b"" |
| 2017 | for c in s: |
| 2018 | encodedresult += cencoder.encode(c) |
| 2019 | encodedresult += cencoder.encode("", True) |
| 2020 | cdecoder = codec_incrementaldecoder(encoding) |
| 2021 | decodedresult = "" |
| 2022 | for c in encodedresult: |
| 2023 | decodedresult += cdecoder.decode(bytes([c])) |
| 2024 | decodedresult += cdecoder.decode(b"", True) |
| 2025 | self.assertEqual(decodedresult, s, |
| 2026 | "encoding=%r" % encoding) |
| 2027 | |
| 2028 | if encoding not in ("idna", "mbcs"): |
| 2029 | # check incremental decoder/encoder with errors argument |
| 2030 | try: |
| 2031 | cencoder = codec_incrementalencoder(encoding, "ignore") |
| 2032 | except LookupError: # no IncrementalEncoder |
| 2033 | pass |
| 2034 | else: |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 2035 | encodedresult = b"".join(cencoder.encode(c) for c in s) |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 2036 | cdecoder = codec_incrementaldecoder(encoding, "ignore") |
| 2037 | decodedresult = "".join(cdecoder.decode(bytes([c])) |
| 2038 | for c in encodedresult) |
| 2039 | self.assertEqual(decodedresult, s, |
| 2040 | "encoding=%r" % encoding) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2041 | |
Walter Dörwald | 729c31f | 2005-03-14 19:06:30 +0000 | [diff] [blame] | 2042 | def test_seek(self): |
| 2043 | # all codecs should be able to encode these |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 2044 | s = "%s\n%s\n" % (100*"abc123", 100*"def456") |
Walter Dörwald | 729c31f | 2005-03-14 19:06:30 +0000 | [diff] [blame] | 2045 | for encoding in all_unicode_encodings: |
| 2046 | if encoding == "idna": # FIXME: See SF bug #1163178 |
| 2047 | continue |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 2048 | if encoding in broken_unicode_with_stateful: |
Walter Dörwald | 729c31f | 2005-03-14 19:06:30 +0000 | [diff] [blame] | 2049 | continue |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 2050 | reader = codecs.getreader(encoding)(io.BytesIO(s.encode(encoding))) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 2051 | for t in range(5): |
Walter Dörwald | 729c31f | 2005-03-14 19:06:30 +0000 | [diff] [blame] | 2052 | # Test that calling seek resets the internal codec state and buffers |
| 2053 | reader.seek(0, 0) |
Guido van Rossum | f4cfc8f | 2007-05-17 21:52:23 +0000 | [diff] [blame] | 2054 | data = reader.read() |
| 2055 | self.assertEqual(s, data) |
Walter Dörwald | 729c31f | 2005-03-14 19:06:30 +0000 | [diff] [blame] | 2056 | |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 2057 | def test_bad_decode_args(self): |
| 2058 | for encoding in all_unicode_encodings: |
| 2059 | decoder = codecs.getdecoder(encoding) |
| 2060 | self.assertRaises(TypeError, decoder) |
| 2061 | if encoding not in ("idna", "punycode"): |
| 2062 | self.assertRaises(TypeError, decoder, 42) |
| 2063 | |
| 2064 | def test_bad_encode_args(self): |
| 2065 | for encoding in all_unicode_encodings: |
| 2066 | encoder = codecs.getencoder(encoding) |
Ezio Melotti | adc417c | 2011-11-17 12:23:34 +0200 | [diff] [blame] | 2067 | with support.check_warnings(): |
| 2068 | # unicode-internal has been deprecated |
| 2069 | self.assertRaises(TypeError, encoder) |
Walter Dörwald | e22d339 | 2005-11-17 08:52:34 +0000 | [diff] [blame] | 2070 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2071 | def test_encoding_map_type_initialized(self): |
| 2072 | from encodings import cp1140 |
| 2073 | # This used to crash, we are only verifying there's no crash. |
| 2074 | table_type = type(cp1140.encoding_table) |
| 2075 | self.assertEqual(table_type, table_type) |
| 2076 | |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 2077 | def test_decoder_state(self): |
| 2078 | # Check that getstate() and setstate() handle the state properly |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 2079 | u = "abc123" |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 2080 | for encoding in all_unicode_encodings: |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 2081 | if encoding not in broken_unicode_with_stateful: |
Walter Dörwald | 3abcb01 | 2007-04-16 22:10:50 +0000 | [diff] [blame] | 2082 | self.check_state_handling_decode(encoding, u, u.encode(encoding)) |
| 2083 | self.check_state_handling_encode(encoding, u, u.encode(encoding)) |
| 2084 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2085 | class CharmapTest(unittest.TestCase): |
| 2086 | def test_decode_with_string_map(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2087 | self.assertEqual( |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 2088 | codecs.charmap_decode(b"\x00\x01\x02", "strict", "abc"), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 2089 | ("abc", 3) |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2090 | ) |
| 2091 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2092 | self.assertEqual( |
Antoine Pitrou | a1f7655 | 2012-09-23 20:00:04 +0200 | [diff] [blame] | 2093 | codecs.charmap_decode(b"\x00\x01\x02", "strict", "\U0010FFFFbc"), |
| 2094 | ("\U0010FFFFbc", 3) |
| 2095 | ) |
| 2096 | |
Antoine Pitrou | 6f80f5d | 2012-09-23 19:55:21 +0200 | [diff] [blame] | 2097 | self.assertRaises(UnicodeDecodeError, |
| 2098 | codecs.charmap_decode, b"\x00\x01\x02", "strict", "ab" |
| 2099 | ) |
| 2100 | |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 2101 | self.assertRaises(UnicodeDecodeError, |
| 2102 | codecs.charmap_decode, b"\x00\x01\x02", "strict", "ab\ufffe" |
| 2103 | ) |
| 2104 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2105 | self.assertEqual( |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 2106 | codecs.charmap_decode(b"\x00\x01\x02", "replace", "ab"), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 2107 | ("ab\ufffd", 3) |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2108 | ) |
| 2109 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2110 | self.assertEqual( |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 2111 | codecs.charmap_decode(b"\x00\x01\x02", "replace", "ab\ufffe"), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 2112 | ("ab\ufffd", 3) |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2113 | ) |
| 2114 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2115 | self.assertEqual( |
Serhiy Storchaka | 07985ef | 2015-01-25 22:56:57 +0200 | [diff] [blame] | 2116 | codecs.charmap_decode(b"\x00\x01\x02", "backslashreplace", "ab"), |
| 2117 | ("ab\\x02", 3) |
| 2118 | ) |
| 2119 | |
| 2120 | self.assertEqual( |
| 2121 | codecs.charmap_decode(b"\x00\x01\x02", "backslashreplace", "ab\ufffe"), |
| 2122 | ("ab\\x02", 3) |
| 2123 | ) |
| 2124 | |
| 2125 | self.assertEqual( |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 2126 | codecs.charmap_decode(b"\x00\x01\x02", "ignore", "ab"), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 2127 | ("ab", 3) |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2128 | ) |
| 2129 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2130 | self.assertEqual( |
Walter Dörwald | ca8a8d0 | 2007-05-04 13:05:09 +0000 | [diff] [blame] | 2131 | codecs.charmap_decode(b"\x00\x01\x02", "ignore", "ab\ufffe"), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 2132 | ("ab", 3) |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2133 | ) |
| 2134 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 2135 | allbytes = bytes(range(256)) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2136 | self.assertEqual( |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 2137 | codecs.charmap_decode(allbytes, "ignore", ""), |
| 2138 | ("", len(allbytes)) |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2139 | ) |
| 2140 | |
Antoine Pitrou | 6f80f5d | 2012-09-23 19:55:21 +0200 | [diff] [blame] | 2141 | def test_decode_with_int2str_map(self): |
| 2142 | self.assertEqual( |
| 2143 | codecs.charmap_decode(b"\x00\x01\x02", "strict", |
| 2144 | {0: 'a', 1: 'b', 2: 'c'}), |
| 2145 | ("abc", 3) |
| 2146 | ) |
| 2147 | |
| 2148 | self.assertEqual( |
| 2149 | codecs.charmap_decode(b"\x00\x01\x02", "strict", |
| 2150 | {0: 'Aa', 1: 'Bb', 2: 'Cc'}), |
| 2151 | ("AaBbCc", 3) |
| 2152 | ) |
| 2153 | |
| 2154 | self.assertEqual( |
| 2155 | codecs.charmap_decode(b"\x00\x01\x02", "strict", |
| 2156 | {0: '\U0010FFFF', 1: 'b', 2: 'c'}), |
| 2157 | ("\U0010FFFFbc", 3) |
| 2158 | ) |
| 2159 | |
| 2160 | self.assertEqual( |
| 2161 | codecs.charmap_decode(b"\x00\x01\x02", "strict", |
| 2162 | {0: 'a', 1: 'b', 2: ''}), |
| 2163 | ("ab", 3) |
| 2164 | ) |
| 2165 | |
| 2166 | self.assertRaises(UnicodeDecodeError, |
| 2167 | codecs.charmap_decode, b"\x00\x01\x02", "strict", |
| 2168 | {0: 'a', 1: 'b'} |
| 2169 | ) |
| 2170 | |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 2171 | self.assertRaises(UnicodeDecodeError, |
| 2172 | codecs.charmap_decode, b"\x00\x01\x02", "strict", |
| 2173 | {0: 'a', 1: 'b', 2: None} |
| 2174 | ) |
| 2175 | |
| 2176 | # Issue #14850 |
| 2177 | self.assertRaises(UnicodeDecodeError, |
| 2178 | codecs.charmap_decode, b"\x00\x01\x02", "strict", |
| 2179 | {0: 'a', 1: 'b', 2: '\ufffe'} |
| 2180 | ) |
| 2181 | |
Antoine Pitrou | 6f80f5d | 2012-09-23 19:55:21 +0200 | [diff] [blame] | 2182 | self.assertEqual( |
| 2183 | codecs.charmap_decode(b"\x00\x01\x02", "replace", |
| 2184 | {0: 'a', 1: 'b'}), |
| 2185 | ("ab\ufffd", 3) |
| 2186 | ) |
| 2187 | |
| 2188 | self.assertEqual( |
| 2189 | codecs.charmap_decode(b"\x00\x01\x02", "replace", |
| 2190 | {0: 'a', 1: 'b', 2: None}), |
| 2191 | ("ab\ufffd", 3) |
| 2192 | ) |
| 2193 | |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 2194 | # Issue #14850 |
| 2195 | self.assertEqual( |
| 2196 | codecs.charmap_decode(b"\x00\x01\x02", "replace", |
| 2197 | {0: 'a', 1: 'b', 2: '\ufffe'}), |
| 2198 | ("ab\ufffd", 3) |
| 2199 | ) |
| 2200 | |
Antoine Pitrou | 6f80f5d | 2012-09-23 19:55:21 +0200 | [diff] [blame] | 2201 | self.assertEqual( |
Serhiy Storchaka | 07985ef | 2015-01-25 22:56:57 +0200 | [diff] [blame] | 2202 | codecs.charmap_decode(b"\x00\x01\x02", "backslashreplace", |
| 2203 | {0: 'a', 1: 'b'}), |
| 2204 | ("ab\\x02", 3) |
| 2205 | ) |
| 2206 | |
| 2207 | self.assertEqual( |
| 2208 | codecs.charmap_decode(b"\x00\x01\x02", "backslashreplace", |
| 2209 | {0: 'a', 1: 'b', 2: None}), |
| 2210 | ("ab\\x02", 3) |
| 2211 | ) |
| 2212 | |
| 2213 | # Issue #14850 |
| 2214 | self.assertEqual( |
| 2215 | codecs.charmap_decode(b"\x00\x01\x02", "backslashreplace", |
| 2216 | {0: 'a', 1: 'b', 2: '\ufffe'}), |
| 2217 | ("ab\\x02", 3) |
| 2218 | ) |
| 2219 | |
| 2220 | self.assertEqual( |
Antoine Pitrou | 6f80f5d | 2012-09-23 19:55:21 +0200 | [diff] [blame] | 2221 | codecs.charmap_decode(b"\x00\x01\x02", "ignore", |
| 2222 | {0: 'a', 1: 'b'}), |
| 2223 | ("ab", 3) |
| 2224 | ) |
| 2225 | |
| 2226 | self.assertEqual( |
| 2227 | codecs.charmap_decode(b"\x00\x01\x02", "ignore", |
| 2228 | {0: 'a', 1: 'b', 2: None}), |
| 2229 | ("ab", 3) |
| 2230 | ) |
| 2231 | |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 2232 | # Issue #14850 |
| 2233 | self.assertEqual( |
| 2234 | codecs.charmap_decode(b"\x00\x01\x02", "ignore", |
| 2235 | {0: 'a', 1: 'b', 2: '\ufffe'}), |
| 2236 | ("ab", 3) |
| 2237 | ) |
| 2238 | |
Antoine Pitrou | 6f80f5d | 2012-09-23 19:55:21 +0200 | [diff] [blame] | 2239 | allbytes = bytes(range(256)) |
| 2240 | self.assertEqual( |
| 2241 | codecs.charmap_decode(allbytes, "ignore", {}), |
| 2242 | ("", len(allbytes)) |
| 2243 | ) |
| 2244 | |
| 2245 | def test_decode_with_int2int_map(self): |
| 2246 | a = ord('a') |
| 2247 | b = ord('b') |
| 2248 | c = ord('c') |
| 2249 | |
| 2250 | self.assertEqual( |
| 2251 | codecs.charmap_decode(b"\x00\x01\x02", "strict", |
| 2252 | {0: a, 1: b, 2: c}), |
| 2253 | ("abc", 3) |
| 2254 | ) |
| 2255 | |
| 2256 | # Issue #15379 |
| 2257 | self.assertEqual( |
| 2258 | codecs.charmap_decode(b"\x00\x01\x02", "strict", |
| 2259 | {0: 0x10FFFF, 1: b, 2: c}), |
| 2260 | ("\U0010FFFFbc", 3) |
| 2261 | ) |
| 2262 | |
Antoine Pitrou | a1f7655 | 2012-09-23 20:00:04 +0200 | [diff] [blame] | 2263 | self.assertEqual( |
| 2264 | codecs.charmap_decode(b"\x00\x01\x02", "strict", |
| 2265 | {0: sys.maxunicode, 1: b, 2: c}), |
| 2266 | (chr(sys.maxunicode) + "bc", 3) |
| 2267 | ) |
| 2268 | |
Antoine Pitrou | 6f80f5d | 2012-09-23 19:55:21 +0200 | [diff] [blame] | 2269 | self.assertRaises(TypeError, |
| 2270 | codecs.charmap_decode, b"\x00\x01\x02", "strict", |
Antoine Pitrou | a1f7655 | 2012-09-23 20:00:04 +0200 | [diff] [blame] | 2271 | {0: sys.maxunicode + 1, 1: b, 2: c} |
Antoine Pitrou | 6f80f5d | 2012-09-23 19:55:21 +0200 | [diff] [blame] | 2272 | ) |
| 2273 | |
| 2274 | self.assertRaises(UnicodeDecodeError, |
| 2275 | codecs.charmap_decode, b"\x00\x01\x02", "strict", |
| 2276 | {0: a, 1: b}, |
| 2277 | ) |
| 2278 | |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 2279 | self.assertRaises(UnicodeDecodeError, |
| 2280 | codecs.charmap_decode, b"\x00\x01\x02", "strict", |
| 2281 | {0: a, 1: b, 2: 0xFFFE}, |
| 2282 | ) |
| 2283 | |
Antoine Pitrou | 6f80f5d | 2012-09-23 19:55:21 +0200 | [diff] [blame] | 2284 | self.assertEqual( |
| 2285 | codecs.charmap_decode(b"\x00\x01\x02", "replace", |
| 2286 | {0: a, 1: b}), |
| 2287 | ("ab\ufffd", 3) |
| 2288 | ) |
| 2289 | |
| 2290 | self.assertEqual( |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 2291 | codecs.charmap_decode(b"\x00\x01\x02", "replace", |
| 2292 | {0: a, 1: b, 2: 0xFFFE}), |
| 2293 | ("ab\ufffd", 3) |
| 2294 | ) |
| 2295 | |
| 2296 | self.assertEqual( |
Serhiy Storchaka | 07985ef | 2015-01-25 22:56:57 +0200 | [diff] [blame] | 2297 | codecs.charmap_decode(b"\x00\x01\x02", "backslashreplace", |
| 2298 | {0: a, 1: b}), |
| 2299 | ("ab\\x02", 3) |
| 2300 | ) |
| 2301 | |
| 2302 | self.assertEqual( |
| 2303 | codecs.charmap_decode(b"\x00\x01\x02", "backslashreplace", |
| 2304 | {0: a, 1: b, 2: 0xFFFE}), |
| 2305 | ("ab\\x02", 3) |
| 2306 | ) |
| 2307 | |
| 2308 | self.assertEqual( |
Antoine Pitrou | 6f80f5d | 2012-09-23 19:55:21 +0200 | [diff] [blame] | 2309 | codecs.charmap_decode(b"\x00\x01\x02", "ignore", |
| 2310 | {0: a, 1: b}), |
| 2311 | ("ab", 3) |
| 2312 | ) |
| 2313 | |
Serhiy Storchaka | 4fb8cae | 2013-01-15 14:43:21 +0200 | [diff] [blame] | 2314 | self.assertEqual( |
| 2315 | codecs.charmap_decode(b"\x00\x01\x02", "ignore", |
| 2316 | {0: a, 1: b, 2: 0xFFFE}), |
| 2317 | ("ab", 3) |
| 2318 | ) |
| 2319 | |
Antoine Pitrou | 6f80f5d | 2012-09-23 19:55:21 +0200 | [diff] [blame] | 2320 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2321 | class WithStmtTest(unittest.TestCase): |
| 2322 | def test_encodedfile(self): |
Walter Dörwald | c3ab0a7 | 2007-05-10 15:02:49 +0000 | [diff] [blame] | 2323 | f = io.BytesIO(b"\xc3\xbc") |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 2324 | with codecs.EncodedFile(f, "latin-1", "utf-8") as ef: |
| 2325 | self.assertEqual(ef.read(), b"\xfc") |
Nick Coghlan | b9fdb7a | 2015-01-07 00:22:00 +1000 | [diff] [blame] | 2326 | self.assertTrue(f.closed) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2327 | |
| 2328 | def test_streamreaderwriter(self): |
Walter Dörwald | c3ab0a7 | 2007-05-10 15:02:49 +0000 | [diff] [blame] | 2329 | f = io.BytesIO(b"\xc3\xbc") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2330 | info = codecs.lookup("utf-8") |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 2331 | with codecs.StreamReaderWriter(f, info.streamreader, |
| 2332 | info.streamwriter, 'strict') as srw: |
| 2333 | self.assertEqual(srw.read(), "\xfc") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2334 | |
Antoine Pitrou | 81fabdb | 2009-01-22 10:11:36 +0000 | [diff] [blame] | 2335 | class TypesTest(unittest.TestCase): |
| 2336 | def test_decode_unicode(self): |
| 2337 | # Most decoders don't accept unicode input |
| 2338 | decoders = [ |
| 2339 | codecs.utf_7_decode, |
| 2340 | codecs.utf_8_decode, |
| 2341 | codecs.utf_16_le_decode, |
| 2342 | codecs.utf_16_be_decode, |
| 2343 | codecs.utf_16_ex_decode, |
| 2344 | codecs.utf_32_decode, |
| 2345 | codecs.utf_32_le_decode, |
| 2346 | codecs.utf_32_be_decode, |
| 2347 | codecs.utf_32_ex_decode, |
| 2348 | codecs.latin_1_decode, |
| 2349 | codecs.ascii_decode, |
| 2350 | codecs.charmap_decode, |
| 2351 | ] |
| 2352 | if hasattr(codecs, "mbcs_decode"): |
| 2353 | decoders.append(codecs.mbcs_decode) |
| 2354 | for decoder in decoders: |
| 2355 | self.assertRaises(TypeError, decoder, "xxx") |
| 2356 | |
| 2357 | def test_unicode_escape(self): |
Martin Panter | 119e502 | 2016-04-16 09:28:57 +0000 | [diff] [blame] | 2358 | # Escape-decoding a unicode string is supported and gives the same |
Antoine Pitrou | 81fabdb | 2009-01-22 10:11:36 +0000 | [diff] [blame] | 2359 | # result as decoding the equivalent ASCII bytes string. |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2360 | self.assertEqual(codecs.unicode_escape_decode(r"\u1234"), ("\u1234", 6)) |
| 2361 | self.assertEqual(codecs.unicode_escape_decode(br"\u1234"), ("\u1234", 6)) |
| 2362 | self.assertEqual(codecs.raw_unicode_escape_decode(r"\u1234"), ("\u1234", 6)) |
| 2363 | self.assertEqual(codecs.raw_unicode_escape_decode(br"\u1234"), ("\u1234", 6)) |
Antoine Pitrou | 81fabdb | 2009-01-22 10:11:36 +0000 | [diff] [blame] | 2364 | |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 2365 | self.assertRaises(UnicodeDecodeError, codecs.unicode_escape_decode, br"\U00110000") |
| 2366 | self.assertEqual(codecs.unicode_escape_decode(r"\U00110000", "replace"), ("\ufffd", 10)) |
Serhiy Storchaka | 07985ef | 2015-01-25 22:56:57 +0200 | [diff] [blame] | 2367 | self.assertEqual(codecs.unicode_escape_decode(r"\U00110000", "backslashreplace"), |
| 2368 | (r"\x5c\x55\x30\x30\x31\x31\x30\x30\x30\x30", 10)) |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 2369 | |
| 2370 | self.assertRaises(UnicodeDecodeError, codecs.raw_unicode_escape_decode, br"\U00110000") |
| 2371 | self.assertEqual(codecs.raw_unicode_escape_decode(r"\U00110000", "replace"), ("\ufffd", 10)) |
Serhiy Storchaka | 07985ef | 2015-01-25 22:56:57 +0200 | [diff] [blame] | 2372 | self.assertEqual(codecs.raw_unicode_escape_decode(r"\U00110000", "backslashreplace"), |
| 2373 | (r"\x5c\x55\x30\x30\x31\x31\x30\x30\x30\x30", 10)) |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 2374 | |
Serhiy Storchaka | d679377 | 2013-01-29 10:20:44 +0200 | [diff] [blame] | 2375 | |
| 2376 | class UnicodeEscapeTest(unittest.TestCase): |
| 2377 | def test_empty(self): |
| 2378 | self.assertEqual(codecs.unicode_escape_encode(""), (b"", 0)) |
| 2379 | self.assertEqual(codecs.unicode_escape_decode(b""), ("", 0)) |
| 2380 | |
| 2381 | def test_raw_encode(self): |
| 2382 | encode = codecs.unicode_escape_encode |
| 2383 | for b in range(32, 127): |
| 2384 | if b != b'\\'[0]: |
| 2385 | self.assertEqual(encode(chr(b)), (bytes([b]), 1)) |
| 2386 | |
| 2387 | def test_raw_decode(self): |
| 2388 | decode = codecs.unicode_escape_decode |
| 2389 | for b in range(256): |
| 2390 | if b != b'\\'[0]: |
| 2391 | self.assertEqual(decode(bytes([b]) + b'0'), (chr(b) + '0', 2)) |
| 2392 | |
| 2393 | def test_escape_encode(self): |
| 2394 | encode = codecs.unicode_escape_encode |
| 2395 | check = coding_checker(self, encode) |
| 2396 | check('\t', br'\t') |
| 2397 | check('\n', br'\n') |
| 2398 | check('\r', br'\r') |
| 2399 | check('\\', br'\\') |
| 2400 | for b in range(32): |
| 2401 | if chr(b) not in '\t\n\r': |
| 2402 | check(chr(b), ('\\x%02x' % b).encode()) |
| 2403 | for b in range(127, 256): |
| 2404 | check(chr(b), ('\\x%02x' % b).encode()) |
| 2405 | check('\u20ac', br'\u20ac') |
| 2406 | check('\U0001d120', br'\U0001d120') |
| 2407 | |
| 2408 | def test_escape_decode(self): |
| 2409 | decode = codecs.unicode_escape_decode |
| 2410 | check = coding_checker(self, decode) |
| 2411 | check(b"[\\\n]", "[]") |
| 2412 | check(br'[\"]', '["]') |
| 2413 | check(br"[\']", "[']") |
| 2414 | check(br"[\\]", r"[\]") |
| 2415 | check(br"[\a]", "[\x07]") |
| 2416 | check(br"[\b]", "[\x08]") |
| 2417 | check(br"[\t]", "[\x09]") |
| 2418 | check(br"[\n]", "[\x0a]") |
| 2419 | check(br"[\v]", "[\x0b]") |
| 2420 | check(br"[\f]", "[\x0c]") |
| 2421 | check(br"[\r]", "[\x0d]") |
| 2422 | check(br"[\7]", "[\x07]") |
| 2423 | check(br"[\8]", r"[\8]") |
| 2424 | check(br"[\78]", "[\x078]") |
| 2425 | check(br"[\41]", "[!]") |
| 2426 | check(br"[\418]", "[!8]") |
| 2427 | check(br"[\101]", "[A]") |
| 2428 | check(br"[\1010]", "[A0]") |
| 2429 | check(br"[\x41]", "[A]") |
| 2430 | check(br"[\x410]", "[A0]") |
| 2431 | check(br"\u20ac", "\u20ac") |
| 2432 | check(br"\U0001d120", "\U0001d120") |
| 2433 | for b in range(256): |
| 2434 | if b not in b'\n"\'\\abtnvfr01234567xuUN': |
| 2435 | check(b'\\' + bytes([b]), '\\' + chr(b)) |
| 2436 | |
| 2437 | def test_decode_errors(self): |
| 2438 | decode = codecs.unicode_escape_decode |
| 2439 | for c, d in (b'x', 2), (b'u', 4), (b'U', 4): |
| 2440 | for i in range(d): |
| 2441 | self.assertRaises(UnicodeDecodeError, decode, |
| 2442 | b"\\" + c + b"0"*i) |
| 2443 | self.assertRaises(UnicodeDecodeError, decode, |
| 2444 | b"[\\" + c + b"0"*i + b"]") |
| 2445 | data = b"[\\" + c + b"0"*i + b"]\\" + c + b"0"*i |
| 2446 | self.assertEqual(decode(data, "ignore"), ("[]", len(data))) |
| 2447 | self.assertEqual(decode(data, "replace"), |
| 2448 | ("[\ufffd]\ufffd", len(data))) |
| 2449 | self.assertRaises(UnicodeDecodeError, decode, br"\U00110000") |
| 2450 | self.assertEqual(decode(br"\U00110000", "ignore"), ("", 10)) |
| 2451 | self.assertEqual(decode(br"\U00110000", "replace"), ("\ufffd", 10)) |
| 2452 | |
| 2453 | |
Serhiy Storchaka | c9c4338 | 2013-01-29 11:40:00 +0200 | [diff] [blame] | 2454 | class RawUnicodeEscapeTest(unittest.TestCase): |
| 2455 | def test_empty(self): |
| 2456 | self.assertEqual(codecs.raw_unicode_escape_encode(""), (b"", 0)) |
| 2457 | self.assertEqual(codecs.raw_unicode_escape_decode(b""), ("", 0)) |
| 2458 | |
| 2459 | def test_raw_encode(self): |
| 2460 | encode = codecs.raw_unicode_escape_encode |
| 2461 | for b in range(256): |
| 2462 | self.assertEqual(encode(chr(b)), (bytes([b]), 1)) |
| 2463 | |
| 2464 | def test_raw_decode(self): |
| 2465 | decode = codecs.raw_unicode_escape_decode |
| 2466 | for b in range(256): |
| 2467 | self.assertEqual(decode(bytes([b]) + b'0'), (chr(b) + '0', 2)) |
| 2468 | |
| 2469 | def test_escape_encode(self): |
| 2470 | encode = codecs.raw_unicode_escape_encode |
| 2471 | check = coding_checker(self, encode) |
| 2472 | for b in range(256): |
| 2473 | if b not in b'uU': |
| 2474 | check('\\' + chr(b), b'\\' + bytes([b])) |
| 2475 | check('\u20ac', br'\u20ac') |
| 2476 | check('\U0001d120', br'\U0001d120') |
| 2477 | |
| 2478 | def test_escape_decode(self): |
| 2479 | decode = codecs.raw_unicode_escape_decode |
| 2480 | check = coding_checker(self, decode) |
| 2481 | for b in range(256): |
| 2482 | if b not in b'uU': |
| 2483 | check(b'\\' + bytes([b]), '\\' + chr(b)) |
| 2484 | check(br"\u20ac", "\u20ac") |
| 2485 | check(br"\U0001d120", "\U0001d120") |
| 2486 | |
| 2487 | def test_decode_errors(self): |
| 2488 | decode = codecs.raw_unicode_escape_decode |
| 2489 | for c, d in (b'u', 4), (b'U', 4): |
| 2490 | for i in range(d): |
| 2491 | self.assertRaises(UnicodeDecodeError, decode, |
| 2492 | b"\\" + c + b"0"*i) |
| 2493 | self.assertRaises(UnicodeDecodeError, decode, |
| 2494 | b"[\\" + c + b"0"*i + b"]") |
| 2495 | data = b"[\\" + c + b"0"*i + b"]\\" + c + b"0"*i |
| 2496 | self.assertEqual(decode(data, "ignore"), ("[]", len(data))) |
| 2497 | self.assertEqual(decode(data, "replace"), |
| 2498 | ("[\ufffd]\ufffd", len(data))) |
| 2499 | self.assertRaises(UnicodeDecodeError, decode, br"\U00110000") |
| 2500 | self.assertEqual(decode(br"\U00110000", "ignore"), ("", 10)) |
| 2501 | self.assertEqual(decode(br"\U00110000", "replace"), ("\ufffd", 10)) |
| 2502 | |
| 2503 | |
Berker Peksag | 4a72a7b | 2016-09-16 17:31:06 +0300 | [diff] [blame^] | 2504 | class EscapeEncodeTest(unittest.TestCase): |
| 2505 | |
| 2506 | def test_escape_encode(self): |
| 2507 | tests = [ |
| 2508 | (b'', (b'', 0)), |
| 2509 | (b'foobar', (b'foobar', 6)), |
| 2510 | (b'spam\0eggs', (b'spam\\x00eggs', 9)), |
| 2511 | (b'a\'b', (b"a\\'b", 3)), |
| 2512 | (b'b\\c', (b'b\\\\c', 3)), |
| 2513 | (b'c\nd', (b'c\\nd', 3)), |
| 2514 | (b'd\re', (b'd\\re', 3)), |
| 2515 | (b'f\x7fg', (b'f\\x7fg', 3)), |
| 2516 | ] |
| 2517 | for data, output in tests: |
| 2518 | with self.subTest(data=data): |
| 2519 | self.assertEqual(codecs.escape_encode(data), output) |
| 2520 | self.assertRaises(TypeError, codecs.escape_encode, 'spam') |
| 2521 | self.assertRaises(TypeError, codecs.escape_encode, bytearray(b'spam')) |
| 2522 | |
| 2523 | |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 2524 | class SurrogateEscapeTest(unittest.TestCase): |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2525 | |
| 2526 | def test_utf8(self): |
| 2527 | # Bad byte |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 2528 | self.assertEqual(b"foo\x80bar".decode("utf-8", "surrogateescape"), |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2529 | "foo\udc80bar") |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 2530 | self.assertEqual("foo\udc80bar".encode("utf-8", "surrogateescape"), |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2531 | b"foo\x80bar") |
| 2532 | # bad-utf-8 encoded surrogate |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 2533 | self.assertEqual(b"\xed\xb0\x80".decode("utf-8", "surrogateescape"), |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2534 | "\udced\udcb0\udc80") |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 2535 | self.assertEqual("\udced\udcb0\udc80".encode("utf-8", "surrogateescape"), |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2536 | b"\xed\xb0\x80") |
| 2537 | |
| 2538 | def test_ascii(self): |
| 2539 | # bad byte |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 2540 | self.assertEqual(b"foo\x80bar".decode("ascii", "surrogateescape"), |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2541 | "foo\udc80bar") |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 2542 | self.assertEqual("foo\udc80bar".encode("ascii", "surrogateescape"), |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2543 | b"foo\x80bar") |
| 2544 | |
| 2545 | def test_charmap(self): |
| 2546 | # bad byte: \xa5 is unmapped in iso-8859-3 |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 2547 | self.assertEqual(b"foo\xa5bar".decode("iso-8859-3", "surrogateescape"), |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2548 | "foo\udca5bar") |
Martin v. Löwis | 43c5778 | 2009-05-10 08:15:24 +0000 | [diff] [blame] | 2549 | self.assertEqual("foo\udca5bar".encode("iso-8859-3", "surrogateescape"), |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 2550 | b"foo\xa5bar") |
| 2551 | |
Amaury Forgeot d'Arc | 84ec8d9 | 2009-06-29 22:36:49 +0000 | [diff] [blame] | 2552 | def test_latin1(self): |
| 2553 | # Issue6373 |
Marc-André Lemburg | 8f36af7 | 2011-02-25 15:42:01 +0000 | [diff] [blame] | 2554 | 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] | 2555 | b"\xe4\xeb\xef\xf6\xfc") |
| 2556 | |
Walter Dörwald | d1c1e10 | 2005-10-06 20:29:57 +0000 | [diff] [blame] | 2557 | |
Victor Stinner | 3fed087 | 2010-05-22 02:16:27 +0000 | [diff] [blame] | 2558 | class BomTest(unittest.TestCase): |
| 2559 | def test_seek0(self): |
| 2560 | data = "1234567890" |
| 2561 | tests = ("utf-16", |
| 2562 | "utf-16-le", |
| 2563 | "utf-16-be", |
| 2564 | "utf-32", |
| 2565 | "utf-32-le", |
| 2566 | "utf-32-be") |
Victor Stinner | 2cca057 | 2011-05-23 14:51:42 +0200 | [diff] [blame] | 2567 | self.addCleanup(support.unlink, support.TESTFN) |
Victor Stinner | 3fed087 | 2010-05-22 02:16:27 +0000 | [diff] [blame] | 2568 | for encoding in tests: |
Victor Stinner | a92ad7e | 2010-05-22 16:59:09 +0000 | [diff] [blame] | 2569 | # Check if the BOM is written only once |
| 2570 | with codecs.open(support.TESTFN, 'w+', encoding=encoding) as f: |
Victor Stinner | 3fed087 | 2010-05-22 02:16:27 +0000 | [diff] [blame] | 2571 | f.write(data) |
| 2572 | f.write(data) |
| 2573 | f.seek(0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2574 | self.assertEqual(f.read(), data * 2) |
Victor Stinner | 3fed087 | 2010-05-22 02:16:27 +0000 | [diff] [blame] | 2575 | f.seek(0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2576 | self.assertEqual(f.read(), data * 2) |
Victor Stinner | 3fed087 | 2010-05-22 02:16:27 +0000 | [diff] [blame] | 2577 | |
Victor Stinner | a92ad7e | 2010-05-22 16:59:09 +0000 | [diff] [blame] | 2578 | # Check that the BOM is written after a seek(0) |
| 2579 | with codecs.open(support.TESTFN, 'w+', encoding=encoding) as f: |
| 2580 | f.write(data[0]) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2581 | self.assertNotEqual(f.tell(), 0) |
Victor Stinner | a92ad7e | 2010-05-22 16:59:09 +0000 | [diff] [blame] | 2582 | f.seek(0) |
| 2583 | f.write(data) |
| 2584 | f.seek(0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2585 | self.assertEqual(f.read(), data) |
Victor Stinner | a92ad7e | 2010-05-22 16:59:09 +0000 | [diff] [blame] | 2586 | |
| 2587 | # (StreamWriter) Check that the BOM is written after a seek(0) |
| 2588 | with codecs.open(support.TESTFN, 'w+', encoding=encoding) as f: |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 2589 | f.writer.write(data[0]) |
| 2590 | self.assertNotEqual(f.writer.tell(), 0) |
| 2591 | f.writer.seek(0) |
| 2592 | f.writer.write(data) |
Victor Stinner | a92ad7e | 2010-05-22 16:59:09 +0000 | [diff] [blame] | 2593 | f.seek(0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2594 | self.assertEqual(f.read(), data) |
Victor Stinner | a92ad7e | 2010-05-22 16:59:09 +0000 | [diff] [blame] | 2595 | |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 2596 | # Check that the BOM is not written after a seek() at a position |
| 2597 | # different than the start |
Victor Stinner | a92ad7e | 2010-05-22 16:59:09 +0000 | [diff] [blame] | 2598 | with codecs.open(support.TESTFN, 'w+', encoding=encoding) as f: |
| 2599 | f.write(data) |
| 2600 | f.seek(f.tell()) |
| 2601 | f.write(data) |
| 2602 | f.seek(0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2603 | self.assertEqual(f.read(), data * 2) |
Victor Stinner | a92ad7e | 2010-05-22 16:59:09 +0000 | [diff] [blame] | 2604 | |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 2605 | # (StreamWriter) Check that the BOM is not written after a seek() |
| 2606 | # at a position different than the start |
Victor Stinner | a92ad7e | 2010-05-22 16:59:09 +0000 | [diff] [blame] | 2607 | with codecs.open(support.TESTFN, 'w+', encoding=encoding) as f: |
Victor Stinner | 0501070 | 2011-05-27 16:50:40 +0200 | [diff] [blame] | 2608 | f.writer.write(data) |
| 2609 | f.writer.seek(f.writer.tell()) |
| 2610 | f.writer.write(data) |
Victor Stinner | a92ad7e | 2010-05-22 16:59:09 +0000 | [diff] [blame] | 2611 | f.seek(0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2612 | self.assertEqual(f.read(), data * 2) |
Victor Stinner | a92ad7e | 2010-05-22 16:59:09 +0000 | [diff] [blame] | 2613 | |
Victor Stinner | 3fed087 | 2010-05-22 02:16:27 +0000 | [diff] [blame] | 2614 | |
Georg Brandl | 0252462 | 2010-12-02 18:06:51 +0000 | [diff] [blame] | 2615 | bytes_transform_encodings = [ |
| 2616 | "base64_codec", |
| 2617 | "uu_codec", |
| 2618 | "quopri_codec", |
| 2619 | "hex_codec", |
| 2620 | ] |
Nick Coghlan | 9c1aed8 | 2013-11-23 11:13:36 +1000 | [diff] [blame] | 2621 | |
| 2622 | transform_aliases = { |
| 2623 | "base64_codec": ["base64", "base_64"], |
| 2624 | "uu_codec": ["uu"], |
| 2625 | "quopri_codec": ["quopri", "quoted_printable", "quotedprintable"], |
| 2626 | "hex_codec": ["hex"], |
| 2627 | "rot_13": ["rot13"], |
| 2628 | } |
| 2629 | |
Georg Brandl | 0252462 | 2010-12-02 18:06:51 +0000 | [diff] [blame] | 2630 | try: |
| 2631 | import zlib |
| 2632 | except ImportError: |
Zachary Ware | efa2e04 | 2013-12-30 14:54:11 -0600 | [diff] [blame] | 2633 | zlib = None |
Georg Brandl | 0252462 | 2010-12-02 18:06:51 +0000 | [diff] [blame] | 2634 | else: |
| 2635 | bytes_transform_encodings.append("zlib_codec") |
Nick Coghlan | 9c1aed8 | 2013-11-23 11:13:36 +1000 | [diff] [blame] | 2636 | transform_aliases["zlib_codec"] = ["zip", "zlib"] |
Georg Brandl | 0252462 | 2010-12-02 18:06:51 +0000 | [diff] [blame] | 2637 | try: |
| 2638 | import bz2 |
| 2639 | except ImportError: |
| 2640 | pass |
| 2641 | else: |
| 2642 | bytes_transform_encodings.append("bz2_codec") |
Nick Coghlan | 9c1aed8 | 2013-11-23 11:13:36 +1000 | [diff] [blame] | 2643 | transform_aliases["bz2_codec"] = ["bz2"] |
Georg Brandl | 0252462 | 2010-12-02 18:06:51 +0000 | [diff] [blame] | 2644 | |
| 2645 | class TransformCodecTest(unittest.TestCase): |
Benjamin Peterson | 28a4dce | 2010-12-12 01:33:04 +0000 | [diff] [blame] | 2646 | |
Georg Brandl | 0252462 | 2010-12-02 18:06:51 +0000 | [diff] [blame] | 2647 | def test_basics(self): |
| 2648 | binput = bytes(range(256)) |
Georg Brandl | 0252462 | 2010-12-02 18:06:51 +0000 | [diff] [blame] | 2649 | for encoding in bytes_transform_encodings: |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2650 | with self.subTest(encoding=encoding): |
| 2651 | # generic codecs interface |
| 2652 | (o, size) = codecs.getencoder(encoding)(binput) |
| 2653 | self.assertEqual(size, len(binput)) |
| 2654 | (i, size) = codecs.getdecoder(encoding)(o) |
| 2655 | self.assertEqual(size, len(o)) |
| 2656 | self.assertEqual(i, binput) |
Georg Brandl | 0252462 | 2010-12-02 18:06:51 +0000 | [diff] [blame] | 2657 | |
Georg Brandl | 0252462 | 2010-12-02 18:06:51 +0000 | [diff] [blame] | 2658 | def test_read(self): |
| 2659 | for encoding in bytes_transform_encodings: |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2660 | with self.subTest(encoding=encoding): |
| 2661 | sin = codecs.encode(b"\x80", encoding) |
| 2662 | reader = codecs.getreader(encoding)(io.BytesIO(sin)) |
| 2663 | sout = reader.read() |
| 2664 | self.assertEqual(sout, b"\x80") |
Georg Brandl | 0252462 | 2010-12-02 18:06:51 +0000 | [diff] [blame] | 2665 | |
| 2666 | def test_readline(self): |
| 2667 | for encoding in bytes_transform_encodings: |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2668 | with self.subTest(encoding=encoding): |
| 2669 | sin = codecs.encode(b"\x80", encoding) |
| 2670 | reader = codecs.getreader(encoding)(io.BytesIO(sin)) |
| 2671 | sout = reader.readline() |
| 2672 | self.assertEqual(sout, b"\x80") |
Georg Brandl | 0252462 | 2010-12-02 18:06:51 +0000 | [diff] [blame] | 2673 | |
Nick Coghlan | fdf239a | 2013-10-03 00:43:22 +1000 | [diff] [blame] | 2674 | def test_buffer_api_usage(self): |
| 2675 | # We check all the transform codecs accept memoryview input |
| 2676 | # for encoding and decoding |
| 2677 | # and also that they roundtrip correctly |
| 2678 | original = b"12345\x80" |
| 2679 | for encoding in bytes_transform_encodings: |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2680 | with self.subTest(encoding=encoding): |
| 2681 | data = original |
| 2682 | view = memoryview(data) |
| 2683 | data = codecs.encode(data, encoding) |
| 2684 | view_encoded = codecs.encode(view, encoding) |
| 2685 | self.assertEqual(view_encoded, data) |
| 2686 | view = memoryview(data) |
| 2687 | data = codecs.decode(data, encoding) |
| 2688 | self.assertEqual(data, original) |
| 2689 | view_decoded = codecs.decode(view, encoding) |
| 2690 | self.assertEqual(view_decoded, data) |
Nick Coghlan | fdf239a | 2013-10-03 00:43:22 +1000 | [diff] [blame] | 2691 | |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 2692 | def test_text_to_binary_blacklists_binary_transforms(self): |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2693 | # Check binary -> binary codecs give a good error for str input |
| 2694 | bad_input = "bad input type" |
| 2695 | for encoding in bytes_transform_encodings: |
| 2696 | with self.subTest(encoding=encoding): |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 2697 | fmt = ( "{!r} is not a text encoding; " |
| 2698 | "use codecs.encode\(\) to handle arbitrary codecs") |
| 2699 | msg = fmt.format(encoding) |
| 2700 | with self.assertRaisesRegex(LookupError, msg) as failure: |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2701 | bad_input.encode(encoding) |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 2702 | self.assertIsNone(failure.exception.__cause__) |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2703 | |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 2704 | def test_text_to_binary_blacklists_text_transforms(self): |
| 2705 | # Check str.encode gives a good error message for str -> str codecs |
| 2706 | msg = (r"^'rot_13' is not a text encoding; " |
| 2707 | "use codecs.encode\(\) to handle arbitrary codecs") |
| 2708 | with self.assertRaisesRegex(LookupError, msg): |
| 2709 | "just an example message".encode("rot_13") |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2710 | |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 2711 | def test_binary_to_text_blacklists_binary_transforms(self): |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2712 | # Check bytes.decode and bytearray.decode give a good error |
| 2713 | # message for binary -> binary codecs |
| 2714 | data = b"encode first to ensure we meet any format restrictions" |
| 2715 | for encoding in bytes_transform_encodings: |
| 2716 | with self.subTest(encoding=encoding): |
| 2717 | encoded_data = codecs.encode(data, encoding) |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 2718 | fmt = (r"{!r} is not a text encoding; " |
| 2719 | "use codecs.decode\(\) to handle arbitrary codecs") |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2720 | msg = fmt.format(encoding) |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 2721 | with self.assertRaisesRegex(LookupError, msg): |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2722 | encoded_data.decode(encoding) |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 2723 | with self.assertRaisesRegex(LookupError, msg): |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2724 | bytearray(encoded_data).decode(encoding) |
| 2725 | |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 2726 | def test_binary_to_text_blacklists_text_transforms(self): |
| 2727 | # Check str -> str codec gives a good error for binary input |
| 2728 | for bad_input in (b"immutable", bytearray(b"mutable")): |
| 2729 | with self.subTest(bad_input=bad_input): |
| 2730 | msg = (r"^'rot_13' is not a text encoding; " |
| 2731 | "use codecs.decode\(\) to handle arbitrary codecs") |
| 2732 | with self.assertRaisesRegex(LookupError, msg) as failure: |
| 2733 | bad_input.decode("rot_13") |
| 2734 | self.assertIsNone(failure.exception.__cause__) |
| 2735 | |
Zachary Ware | efa2e04 | 2013-12-30 14:54:11 -0600 | [diff] [blame] | 2736 | @unittest.skipUnless(zlib, "Requires zlib support") |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 2737 | def test_custom_zlib_error_is_wrapped(self): |
| 2738 | # Check zlib codec gives a good error for malformed input |
| 2739 | msg = "^decoding with 'zlib_codec' codec failed" |
| 2740 | with self.assertRaisesRegex(Exception, msg) as failure: |
| 2741 | codecs.decode(b"hello", "zlib_codec") |
| 2742 | self.assertIsInstance(failure.exception.__cause__, |
| 2743 | type(failure.exception)) |
| 2744 | |
| 2745 | def test_custom_hex_error_is_wrapped(self): |
| 2746 | # Check hex codec gives a good error for malformed input |
| 2747 | msg = "^decoding with 'hex_codec' codec failed" |
| 2748 | with self.assertRaisesRegex(Exception, msg) as failure: |
| 2749 | codecs.decode(b"hello", "hex_codec") |
| 2750 | self.assertIsInstance(failure.exception.__cause__, |
| 2751 | type(failure.exception)) |
| 2752 | |
| 2753 | # Unfortunately, the bz2 module throws OSError, which the codec |
| 2754 | # machinery currently can't wrap :( |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2755 | |
Nick Coghlan | 9c1aed8 | 2013-11-23 11:13:36 +1000 | [diff] [blame] | 2756 | # Ensure codec aliases from http://bugs.python.org/issue7475 work |
| 2757 | def test_aliases(self): |
| 2758 | for codec_name, aliases in transform_aliases.items(): |
| 2759 | expected_name = codecs.lookup(codec_name).name |
| 2760 | for alias in aliases: |
| 2761 | with self.subTest(alias=alias): |
| 2762 | info = codecs.lookup(alias) |
| 2763 | self.assertEqual(info.name, expected_name) |
| 2764 | |
Martin Panter | 06171bd | 2015-09-12 00:34:28 +0000 | [diff] [blame] | 2765 | def test_quopri_stateless(self): |
| 2766 | # Should encode with quotetabs=True |
| 2767 | encoded = codecs.encode(b"space tab\teol \n", "quopri-codec") |
| 2768 | self.assertEqual(encoded, b"space=20tab=09eol=20\n") |
| 2769 | # But should still support unescaped tabs and spaces |
| 2770 | unescaped = b"space tab eol\n" |
| 2771 | self.assertEqual(codecs.decode(unescaped, "quopri-codec"), unescaped) |
| 2772 | |
Serhiy Storchaka | 519114d | 2014-11-07 14:04:37 +0200 | [diff] [blame] | 2773 | def test_uu_invalid(self): |
| 2774 | # Missing "begin" line |
| 2775 | self.assertRaises(ValueError, codecs.decode, b"", "uu-codec") |
| 2776 | |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2777 | |
| 2778 | # The codec system tries to wrap exceptions in order to ensure the error |
| 2779 | # mentions the operation being performed and the codec involved. We |
| 2780 | # currently *only* want this to happen for relatively stateless |
| 2781 | # exceptions, where the only significant information they contain is their |
| 2782 | # type and a single str argument. |
Nick Coghlan | 4e553e2 | 2013-11-16 00:35:34 +1000 | [diff] [blame] | 2783 | |
| 2784 | # Use a local codec registry to avoid appearing to leak objects when |
Martin Panter | 119e502 | 2016-04-16 09:28:57 +0000 | [diff] [blame] | 2785 | # registering multiple search functions |
Nick Coghlan | 4e553e2 | 2013-11-16 00:35:34 +1000 | [diff] [blame] | 2786 | _TEST_CODECS = {} |
| 2787 | |
| 2788 | def _get_test_codec(codec_name): |
| 2789 | return _TEST_CODECS.get(codec_name) |
| 2790 | codecs.register(_get_test_codec) # Returns None, not usable as a decorator |
| 2791 | |
Nick Coghlan | 8fad167 | 2014-09-15 23:50:44 +1200 | [diff] [blame] | 2792 | try: |
| 2793 | # Issue #22166: Also need to clear the internal cache in CPython |
| 2794 | from _codecs import _forget_codec |
| 2795 | except ImportError: |
| 2796 | def _forget_codec(codec_name): |
| 2797 | pass |
| 2798 | |
| 2799 | |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2800 | class ExceptionChainingTest(unittest.TestCase): |
| 2801 | |
| 2802 | def setUp(self): |
| 2803 | # There's no way to unregister a codec search function, so we just |
| 2804 | # ensure we render this one fairly harmless after the test |
| 2805 | # case finishes by using the test case repr as the codec name |
| 2806 | # The codecs module normalizes codec names, although this doesn't |
| 2807 | # appear to be formally documented... |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 2808 | # We also make sure we use a truly unique id for the custom codec |
| 2809 | # to avoid issues with the codec cache when running these tests |
| 2810 | # multiple times (e.g. when hunting for refleaks) |
| 2811 | unique_id = repr(self) + str(id(self)) |
| 2812 | self.codec_name = encodings.normalize_encoding(unique_id).lower() |
| 2813 | |
| 2814 | # We store the object to raise on the instance because of a bad |
| 2815 | # interaction between the codec caching (which means we can't |
| 2816 | # recreate the codec entry) and regrtest refleak hunting (which |
| 2817 | # runs the same test instance multiple times). This means we |
| 2818 | # need to ensure the codecs call back in to the instance to find |
| 2819 | # out which exception to raise rather than binding them in a |
| 2820 | # closure to an object that may change on the next run |
| 2821 | self.obj_to_raise = RuntimeError |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2822 | |
Nick Coghlan | 4e553e2 | 2013-11-16 00:35:34 +1000 | [diff] [blame] | 2823 | def tearDown(self): |
| 2824 | _TEST_CODECS.pop(self.codec_name, None) |
Nick Coghlan | 8fad167 | 2014-09-15 23:50:44 +1200 | [diff] [blame] | 2825 | # Issue #22166: Also pop from caches to avoid appearance of ref leaks |
| 2826 | encodings._cache.pop(self.codec_name, None) |
| 2827 | try: |
| 2828 | _forget_codec(self.codec_name) |
| 2829 | except KeyError: |
| 2830 | pass |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2831 | |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 2832 | def set_codec(self, encode, decode): |
| 2833 | codec_info = codecs.CodecInfo(encode, decode, |
Nick Coghlan | 4e553e2 | 2013-11-16 00:35:34 +1000 | [diff] [blame] | 2834 | name=self.codec_name) |
| 2835 | _TEST_CODECS[self.codec_name] = codec_info |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2836 | |
| 2837 | @contextlib.contextmanager |
| 2838 | def assertWrapped(self, operation, exc_type, msg): |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 2839 | full_msg = r"{} with {!r} codec failed \({}: {}\)".format( |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2840 | operation, self.codec_name, exc_type.__name__, msg) |
| 2841 | with self.assertRaisesRegex(exc_type, full_msg) as caught: |
| 2842 | yield caught |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 2843 | self.assertIsInstance(caught.exception.__cause__, exc_type) |
Nick Coghlan | 77b286b | 2014-01-27 00:53:38 +1000 | [diff] [blame] | 2844 | self.assertIsNotNone(caught.exception.__cause__.__traceback__) |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 2845 | |
| 2846 | def raise_obj(self, *args, **kwds): |
| 2847 | # Helper to dynamically change the object raised by a test codec |
| 2848 | raise self.obj_to_raise |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2849 | |
Nick Coghlan | f1de55f | 2013-11-19 22:33:10 +1000 | [diff] [blame] | 2850 | def check_wrapped(self, obj_to_raise, msg, exc_type=RuntimeError): |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 2851 | self.obj_to_raise = obj_to_raise |
| 2852 | self.set_codec(self.raise_obj, self.raise_obj) |
Nick Coghlan | f1de55f | 2013-11-19 22:33:10 +1000 | [diff] [blame] | 2853 | with self.assertWrapped("encoding", exc_type, msg): |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2854 | "str_input".encode(self.codec_name) |
Nick Coghlan | f1de55f | 2013-11-19 22:33:10 +1000 | [diff] [blame] | 2855 | with self.assertWrapped("encoding", exc_type, msg): |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2856 | codecs.encode("str_input", self.codec_name) |
Nick Coghlan | f1de55f | 2013-11-19 22:33:10 +1000 | [diff] [blame] | 2857 | with self.assertWrapped("decoding", exc_type, msg): |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2858 | b"bytes input".decode(self.codec_name) |
Nick Coghlan | f1de55f | 2013-11-19 22:33:10 +1000 | [diff] [blame] | 2859 | with self.assertWrapped("decoding", exc_type, msg): |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2860 | codecs.decode(b"bytes input", self.codec_name) |
| 2861 | |
| 2862 | def test_raise_by_type(self): |
| 2863 | self.check_wrapped(RuntimeError, "") |
| 2864 | |
| 2865 | def test_raise_by_value(self): |
| 2866 | msg = "This should be wrapped" |
| 2867 | self.check_wrapped(RuntimeError(msg), msg) |
| 2868 | |
Nick Coghlan | f1de55f | 2013-11-19 22:33:10 +1000 | [diff] [blame] | 2869 | def test_raise_grandchild_subclass_exact_size(self): |
| 2870 | msg = "This should be wrapped" |
| 2871 | class MyRuntimeError(RuntimeError): |
| 2872 | __slots__ = () |
| 2873 | self.check_wrapped(MyRuntimeError(msg), msg, MyRuntimeError) |
| 2874 | |
| 2875 | def test_raise_subclass_with_weakref_support(self): |
| 2876 | msg = "This should be wrapped" |
| 2877 | class MyRuntimeError(RuntimeError): |
| 2878 | pass |
| 2879 | self.check_wrapped(MyRuntimeError(msg), msg, MyRuntimeError) |
| 2880 | |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 2881 | def check_not_wrapped(self, obj_to_raise, msg): |
| 2882 | def raise_obj(*args, **kwds): |
| 2883 | raise obj_to_raise |
| 2884 | self.set_codec(raise_obj, raise_obj) |
| 2885 | with self.assertRaisesRegex(RuntimeError, msg): |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2886 | "str input".encode(self.codec_name) |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 2887 | with self.assertRaisesRegex(RuntimeError, msg): |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2888 | codecs.encode("str input", self.codec_name) |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 2889 | with self.assertRaisesRegex(RuntimeError, msg): |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2890 | b"bytes input".decode(self.codec_name) |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 2891 | with self.assertRaisesRegex(RuntimeError, msg): |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2892 | codecs.decode(b"bytes input", self.codec_name) |
| 2893 | |
| 2894 | def test_init_override_is_not_wrapped(self): |
| 2895 | class CustomInit(RuntimeError): |
| 2896 | def __init__(self): |
| 2897 | pass |
| 2898 | self.check_not_wrapped(CustomInit, "") |
| 2899 | |
| 2900 | def test_new_override_is_not_wrapped(self): |
| 2901 | class CustomNew(RuntimeError): |
| 2902 | def __new__(cls): |
| 2903 | return super().__new__(cls) |
| 2904 | self.check_not_wrapped(CustomNew, "") |
| 2905 | |
| 2906 | def test_instance_attribute_is_not_wrapped(self): |
| 2907 | msg = "This should NOT be wrapped" |
| 2908 | exc = RuntimeError(msg) |
| 2909 | exc.attr = 1 |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 2910 | self.check_not_wrapped(exc, "^{}$".format(msg)) |
Nick Coghlan | 8b097b4 | 2013-11-13 23:49:21 +1000 | [diff] [blame] | 2911 | |
| 2912 | def test_non_str_arg_is_not_wrapped(self): |
| 2913 | self.check_not_wrapped(RuntimeError(1), "1") |
| 2914 | |
| 2915 | def test_multiple_args_is_not_wrapped(self): |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 2916 | msg_re = r"^\('a', 'b', 'c'\)$" |
| 2917 | self.check_not_wrapped(RuntimeError('a', 'b', 'c'), msg_re) |
Nick Coghlan | c4c2580 | 2013-11-15 21:47:37 +1000 | [diff] [blame] | 2918 | |
| 2919 | # http://bugs.python.org/issue19609 |
| 2920 | def test_codec_lookup_failure_not_wrapped(self): |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 2921 | msg = "^unknown encoding: {}$".format(self.codec_name) |
Nick Coghlan | c4c2580 | 2013-11-15 21:47:37 +1000 | [diff] [blame] | 2922 | # The initial codec lookup should not be wrapped |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 2923 | with self.assertRaisesRegex(LookupError, msg): |
Nick Coghlan | c4c2580 | 2013-11-15 21:47:37 +1000 | [diff] [blame] | 2924 | "str input".encode(self.codec_name) |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 2925 | with self.assertRaisesRegex(LookupError, msg): |
Nick Coghlan | c4c2580 | 2013-11-15 21:47:37 +1000 | [diff] [blame] | 2926 | codecs.encode("str input", self.codec_name) |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 2927 | with self.assertRaisesRegex(LookupError, msg): |
Nick Coghlan | c4c2580 | 2013-11-15 21:47:37 +1000 | [diff] [blame] | 2928 | b"bytes input".decode(self.codec_name) |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 2929 | with self.assertRaisesRegex(LookupError, msg): |
Nick Coghlan | c4c2580 | 2013-11-15 21:47:37 +1000 | [diff] [blame] | 2930 | codecs.decode(b"bytes input", self.codec_name) |
| 2931 | |
Nick Coghlan | c72e4e6 | 2013-11-22 22:39:36 +1000 | [diff] [blame] | 2932 | def test_unflagged_non_text_codec_handling(self): |
| 2933 | # The stdlib non-text codecs are now marked so they're |
| 2934 | # pre-emptively skipped by the text model related methods |
| 2935 | # However, third party codecs won't be flagged, so we still make |
| 2936 | # sure the case where an inappropriate output type is produced is |
| 2937 | # handled appropriately |
| 2938 | def encode_to_str(*args, **kwds): |
| 2939 | return "not bytes!", 0 |
| 2940 | def decode_to_bytes(*args, **kwds): |
| 2941 | return b"not str!", 0 |
| 2942 | self.set_codec(encode_to_str, decode_to_bytes) |
| 2943 | # No input or output type checks on the codecs module functions |
| 2944 | encoded = codecs.encode(None, self.codec_name) |
| 2945 | self.assertEqual(encoded, "not bytes!") |
| 2946 | decoded = codecs.decode(None, self.codec_name) |
| 2947 | self.assertEqual(decoded, b"not str!") |
| 2948 | # Text model methods should complain |
| 2949 | fmt = (r"^{!r} encoder returned 'str' instead of 'bytes'; " |
| 2950 | "use codecs.encode\(\) to encode to arbitrary types$") |
| 2951 | msg = fmt.format(self.codec_name) |
| 2952 | with self.assertRaisesRegex(TypeError, msg): |
| 2953 | "str_input".encode(self.codec_name) |
| 2954 | fmt = (r"^{!r} decoder returned 'bytes' instead of 'str'; " |
| 2955 | "use codecs.decode\(\) to decode to arbitrary types$") |
| 2956 | msg = fmt.format(self.codec_name) |
| 2957 | with self.assertRaisesRegex(TypeError, msg): |
| 2958 | b"bytes input".decode(self.codec_name) |
| 2959 | |
Nick Coghlan | fdf239a | 2013-10-03 00:43:22 +1000 | [diff] [blame] | 2960 | |
Georg Brandl | 0252462 | 2010-12-02 18:06:51 +0000 | [diff] [blame] | 2961 | |
Victor Stinner | 62be4fb | 2011-10-18 21:46:37 +0200 | [diff] [blame] | 2962 | @unittest.skipUnless(sys.platform == 'win32', |
| 2963 | 'code pages are specific to Windows') |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 2964 | class CodePageTest(unittest.TestCase): |
Victor Stinner | 2f3ca9f | 2011-10-27 01:38:56 +0200 | [diff] [blame] | 2965 | # CP_UTF8 is already tested by CP65001Test |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 2966 | CP_UTF8 = 65001 |
Victor Stinner | 62be4fb | 2011-10-18 21:46:37 +0200 | [diff] [blame] | 2967 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 2968 | def test_invalid_code_page(self): |
| 2969 | self.assertRaises(ValueError, codecs.code_page_encode, -1, 'a') |
| 2970 | self.assertRaises(ValueError, codecs.code_page_decode, -1, b'a') |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 2971 | self.assertRaises(OSError, codecs.code_page_encode, 123, 'a') |
| 2972 | self.assertRaises(OSError, codecs.code_page_decode, 123, b'a') |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 2973 | |
| 2974 | def test_code_page_name(self): |
| 2975 | self.assertRaisesRegex(UnicodeEncodeError, 'cp932', |
| 2976 | codecs.code_page_encode, 932, '\xff') |
| 2977 | self.assertRaisesRegex(UnicodeDecodeError, 'cp932', |
Victor Stinner | 7d00cc1 | 2014-03-17 23:08:06 +0100 | [diff] [blame] | 2978 | codecs.code_page_decode, 932, b'\x81\x00', 'strict', True) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 2979 | self.assertRaisesRegex(UnicodeDecodeError, 'CP_UTF8', |
Victor Stinner | 7d00cc1 | 2014-03-17 23:08:06 +0100 | [diff] [blame] | 2980 | codecs.code_page_decode, self.CP_UTF8, b'\xff', 'strict', True) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 2981 | |
| 2982 | def check_decode(self, cp, tests): |
| 2983 | for raw, errors, expected in tests: |
| 2984 | if expected is not None: |
| 2985 | try: |
Victor Stinner | 7d00cc1 | 2014-03-17 23:08:06 +0100 | [diff] [blame] | 2986 | decoded = codecs.code_page_decode(cp, raw, errors, True) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 2987 | except UnicodeDecodeError as err: |
| 2988 | self.fail('Unable to decode %a from "cp%s" with ' |
| 2989 | 'errors=%r: %s' % (raw, cp, errors, err)) |
| 2990 | self.assertEqual(decoded[0], expected, |
| 2991 | '%a.decode("cp%s", %r)=%a != %a' |
| 2992 | % (raw, cp, errors, decoded[0], expected)) |
| 2993 | # assert 0 <= decoded[1] <= len(raw) |
| 2994 | self.assertGreaterEqual(decoded[1], 0) |
| 2995 | self.assertLessEqual(decoded[1], len(raw)) |
| 2996 | else: |
| 2997 | self.assertRaises(UnicodeDecodeError, |
Victor Stinner | 7d00cc1 | 2014-03-17 23:08:06 +0100 | [diff] [blame] | 2998 | codecs.code_page_decode, cp, raw, errors, True) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 2999 | |
| 3000 | def check_encode(self, cp, tests): |
| 3001 | for text, errors, expected in tests: |
| 3002 | if expected is not None: |
| 3003 | try: |
| 3004 | encoded = codecs.code_page_encode(cp, text, errors) |
| 3005 | except UnicodeEncodeError as err: |
| 3006 | self.fail('Unable to encode %a to "cp%s" with ' |
| 3007 | 'errors=%r: %s' % (text, cp, errors, err)) |
| 3008 | self.assertEqual(encoded[0], expected, |
| 3009 | '%a.encode("cp%s", %r)=%a != %a' |
| 3010 | % (text, cp, errors, encoded[0], expected)) |
| 3011 | self.assertEqual(encoded[1], len(text)) |
| 3012 | else: |
| 3013 | self.assertRaises(UnicodeEncodeError, |
| 3014 | codecs.code_page_encode, cp, text, errors) |
| 3015 | |
| 3016 | def test_cp932(self): |
| 3017 | self.check_encode(932, ( |
| 3018 | ('abc', 'strict', b'abc'), |
| 3019 | ('\uff44\u9a3e', 'strict', b'\x82\x84\xe9\x80'), |
Victor Stinner | 2f3ca9f | 2011-10-27 01:38:56 +0200 | [diff] [blame] | 3020 | # test error handlers |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 3021 | ('\xff', 'strict', None), |
| 3022 | ('[\xff]', 'ignore', b'[]'), |
| 3023 | ('[\xff]', 'replace', b'[y]'), |
| 3024 | ('[\u20ac]', 'replace', b'[?]'), |
Victor Stinner | 2f3ca9f | 2011-10-27 01:38:56 +0200 | [diff] [blame] | 3025 | ('[\xff]', 'backslashreplace', b'[\\xff]'), |
Serhiy Storchaka | 166ebc4 | 2014-11-25 13:57:17 +0200 | [diff] [blame] | 3026 | ('[\xff]', 'namereplace', |
| 3027 | b'[\\N{LATIN SMALL LETTER Y WITH DIAERESIS}]'), |
Victor Stinner | 2f3ca9f | 2011-10-27 01:38:56 +0200 | [diff] [blame] | 3028 | ('[\xff]', 'xmlcharrefreplace', b'[ÿ]'), |
Serhiy Storchaka | 88d8fb6 | 2014-05-15 14:37:42 +0300 | [diff] [blame] | 3029 | ('\udcff', 'strict', None), |
| 3030 | ('[\udcff]', 'surrogateescape', b'[\xff]'), |
| 3031 | ('[\udcff]', 'surrogatepass', None), |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 3032 | )) |
Victor Stinner | 9e92188 | 2011-10-18 21:55:25 +0200 | [diff] [blame] | 3033 | self.check_decode(932, ( |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 3034 | (b'abc', 'strict', 'abc'), |
| 3035 | (b'\x82\x84\xe9\x80', 'strict', '\uff44\u9a3e'), |
| 3036 | # invalid bytes |
Victor Stinner | 2f3ca9f | 2011-10-27 01:38:56 +0200 | [diff] [blame] | 3037 | (b'[\xff]', 'strict', None), |
| 3038 | (b'[\xff]', 'ignore', '[]'), |
| 3039 | (b'[\xff]', 'replace', '[\ufffd]'), |
Serhiy Storchaka | 07985ef | 2015-01-25 22:56:57 +0200 | [diff] [blame] | 3040 | (b'[\xff]', 'backslashreplace', '[\\xff]'), |
Victor Stinner | 2f3ca9f | 2011-10-27 01:38:56 +0200 | [diff] [blame] | 3041 | (b'[\xff]', 'surrogateescape', '[\udcff]'), |
Serhiy Storchaka | 88d8fb6 | 2014-05-15 14:37:42 +0300 | [diff] [blame] | 3042 | (b'[\xff]', 'surrogatepass', None), |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 3043 | (b'\x81\x00abc', 'strict', None), |
| 3044 | (b'\x81\x00abc', 'ignore', '\x00abc'), |
Victor Stinner | 9e92188 | 2011-10-18 21:55:25 +0200 | [diff] [blame] | 3045 | (b'\x81\x00abc', 'replace', '\ufffd\x00abc'), |
Victor Stinner | f2be23d | 2015-01-26 23:26:11 +0100 | [diff] [blame] | 3046 | (b'\x81\x00abc', 'backslashreplace', '\\x81\x00abc'), |
Victor Stinner | 9e92188 | 2011-10-18 21:55:25 +0200 | [diff] [blame] | 3047 | )) |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 3048 | |
| 3049 | def test_cp1252(self): |
| 3050 | self.check_encode(1252, ( |
| 3051 | ('abc', 'strict', b'abc'), |
| 3052 | ('\xe9\u20ac', 'strict', b'\xe9\x80'), |
| 3053 | ('\xff', 'strict', b'\xff'), |
Serhiy Storchaka | 88d8fb6 | 2014-05-15 14:37:42 +0300 | [diff] [blame] | 3054 | # test error handlers |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 3055 | ('\u0141', 'strict', None), |
| 3056 | ('\u0141', 'ignore', b''), |
| 3057 | ('\u0141', 'replace', b'L'), |
Serhiy Storchaka | 88d8fb6 | 2014-05-15 14:37:42 +0300 | [diff] [blame] | 3058 | ('\udc98', 'surrogateescape', b'\x98'), |
| 3059 | ('\udc98', 'surrogatepass', None), |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 3060 | )) |
| 3061 | self.check_decode(1252, ( |
| 3062 | (b'abc', 'strict', 'abc'), |
| 3063 | (b'\xe9\x80', 'strict', '\xe9\u20ac'), |
| 3064 | (b'\xff', 'strict', '\xff'), |
| 3065 | )) |
| 3066 | |
| 3067 | def test_cp_utf7(self): |
| 3068 | cp = 65000 |
| 3069 | self.check_encode(cp, ( |
| 3070 | ('abc', 'strict', b'abc'), |
| 3071 | ('\xe9\u20ac', 'strict', b'+AOkgrA-'), |
| 3072 | ('\U0010ffff', 'strict', b'+2//f/w-'), |
| 3073 | ('\udc80', 'strict', b'+3IA-'), |
| 3074 | ('\ufffd', 'strict', b'+//0-'), |
| 3075 | )) |
| 3076 | self.check_decode(cp, ( |
| 3077 | (b'abc', 'strict', 'abc'), |
| 3078 | (b'+AOkgrA-', 'strict', '\xe9\u20ac'), |
| 3079 | (b'+2//f/w-', 'strict', '\U0010ffff'), |
| 3080 | (b'+3IA-', 'strict', '\udc80'), |
| 3081 | (b'+//0-', 'strict', '\ufffd'), |
| 3082 | # invalid bytes |
| 3083 | (b'[+/]', 'strict', '[]'), |
| 3084 | (b'[\xff]', 'strict', '[\xff]'), |
| 3085 | )) |
| 3086 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 3087 | def test_multibyte_encoding(self): |
| 3088 | self.check_decode(932, ( |
| 3089 | (b'\x84\xe9\x80', 'ignore', '\u9a3e'), |
| 3090 | (b'\x84\xe9\x80', 'replace', '\ufffd\u9a3e'), |
| 3091 | )) |
| 3092 | self.check_decode(self.CP_UTF8, ( |
| 3093 | (b'\xff\xf4\x8f\xbf\xbf', 'ignore', '\U0010ffff'), |
| 3094 | (b'\xff\xf4\x8f\xbf\xbf', 'replace', '\ufffd\U0010ffff'), |
| 3095 | )) |
Victor Stinner | 2f3ca9f | 2011-10-27 01:38:56 +0200 | [diff] [blame] | 3096 | if VISTA_OR_LATER: |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 3097 | self.check_encode(self.CP_UTF8, ( |
| 3098 | ('[\U0010ffff\uDC80]', 'ignore', b'[\xf4\x8f\xbf\xbf]'), |
| 3099 | ('[\U0010ffff\uDC80]', 'replace', b'[\xf4\x8f\xbf\xbf?]'), |
| 3100 | )) |
| 3101 | |
| 3102 | def test_incremental(self): |
Victor Stinner | 76a31a6 | 2011-11-04 00:05:13 +0100 | [diff] [blame] | 3103 | decoded = codecs.code_page_decode(932, b'\x82', 'strict', False) |
| 3104 | self.assertEqual(decoded, ('', 0)) |
| 3105 | |
Victor Stinner | 3a50e70 | 2011-10-18 21:21:00 +0200 | [diff] [blame] | 3106 | decoded = codecs.code_page_decode(932, |
| 3107 | b'\xe9\x80\xe9', 'strict', |
| 3108 | False) |
| 3109 | self.assertEqual(decoded, ('\u9a3e', 2)) |
| 3110 | |
| 3111 | decoded = codecs.code_page_decode(932, |
| 3112 | b'\xe9\x80\xe9\x80', 'strict', |
| 3113 | False) |
| 3114 | self.assertEqual(decoded, ('\u9a3e\u9a3e', 4)) |
| 3115 | |
| 3116 | decoded = codecs.code_page_decode(932, |
| 3117 | b'abc', 'strict', |
| 3118 | False) |
| 3119 | self.assertEqual(decoded, ('abc', 3)) |
| 3120 | |
| 3121 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 3122 | if __name__ == "__main__": |
Ezio Melotti | 5d3dba0 | 2013-01-11 06:02:07 +0200 | [diff] [blame] | 3123 | unittest.main() |