Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 1 | """Unit tests for io.py.""" |
| 2 | |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 3 | import os |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 4 | import sys |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 5 | import time |
Guido van Rossum | d410395 | 2007-04-12 05:44:49 +0000 | [diff] [blame] | 6 | import array |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 7 | import unittest |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 8 | from itertools import chain |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 9 | from test import test_support |
| 10 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 11 | import io # The module under test |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 14 | class MockRawIO(io.RawIOBase): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 16 | def __init__(self, read_stack=()): |
| 17 | self._read_stack = list(read_stack) |
| 18 | self._write_stack = [] |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 19 | |
| 20 | def read(self, n=None): |
| 21 | try: |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 22 | return self._read_stack.pop(0) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 23 | except: |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 24 | return b"" |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 26 | def write(self, b): |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 27 | self._write_stack.append(b[:]) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 28 | return len(b) |
| 29 | |
| 30 | def writable(self): |
| 31 | return True |
| 32 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 33 | def fileno(self): |
| 34 | return 42 |
| 35 | |
| 36 | def readable(self): |
| 37 | return True |
| 38 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 39 | def seekable(self): |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 40 | return True |
| 41 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 42 | def seek(self, pos, whence): |
| 43 | pass |
| 44 | |
| 45 | def tell(self): |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 46 | return 42 |
| 47 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 48 | |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 49 | class MockFileIO(io.BytesIO): |
| 50 | |
| 51 | def __init__(self, data): |
| 52 | self.read_history = [] |
| 53 | io.BytesIO.__init__(self, data) |
| 54 | |
| 55 | def read(self, n=None): |
| 56 | res = io.BytesIO.read(self, n) |
| 57 | self.read_history.append(None if res is None else len(res)) |
| 58 | return res |
| 59 | |
| 60 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 61 | class MockNonBlockWriterIO(io.RawIOBase): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 62 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 63 | def __init__(self, blocking_script): |
| 64 | self._blocking_script = list(blocking_script) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 65 | self._write_stack = [] |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 66 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 67 | def write(self, b): |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 68 | self._write_stack.append(b[:]) |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 69 | n = self._blocking_script.pop(0) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 70 | if (n < 0): |
Guido van Rossum | 141f767 | 2007-04-10 00:22:16 +0000 | [diff] [blame] | 71 | raise io.BlockingIOError(0, "test blocking", -n) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 72 | else: |
| 73 | return n |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 75 | def writable(self): |
| 76 | return True |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 77 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 78 | |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 79 | class IOTest(unittest.TestCase): |
| 80 | |
Guido van Rossum | 4d0f5a4 | 2007-03-07 22:59:39 +0000 | [diff] [blame] | 81 | def tearDown(self): |
| 82 | test_support.unlink(test_support.TESTFN) |
| 83 | |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 84 | def write_ops(self, f): |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 85 | self.assertEqual(f.write(b"blah."), 5) |
| 86 | self.assertEqual(f.seek(0), 0) |
| 87 | self.assertEqual(f.write(b"Hello."), 6) |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 88 | self.assertEqual(f.tell(), 6) |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 89 | self.assertEqual(f.seek(-1, 1), 5) |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 90 | self.assertEqual(f.tell(), 5) |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 91 | self.assertEqual(f.write(bytearray(b" world\n\n\n")), 9) |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 92 | self.assertEqual(f.seek(0), 0) |
Guido van Rossum | 2b08b38 | 2007-05-08 20:18:39 +0000 | [diff] [blame] | 93 | self.assertEqual(f.write(b"h"), 1) |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 94 | self.assertEqual(f.seek(-1, 2), 13) |
| 95 | self.assertEqual(f.tell(), 13) |
| 96 | self.assertEqual(f.truncate(12), 12) |
Guido van Rossum | dc0b1a1 | 2007-04-12 22:55:07 +0000 | [diff] [blame] | 97 | self.assertEqual(f.tell(), 13) |
Christian Heimes | 8e42a0a | 2007-11-08 18:04:45 +0000 | [diff] [blame] | 98 | self.assertRaises(TypeError, f.seek, 0.0) |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 99 | |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 100 | def read_ops(self, f, buffered=False): |
| 101 | data = f.read(5) |
| 102 | self.assertEqual(data, b"hello") |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 103 | data = bytearray(data) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 104 | self.assertEqual(f.readinto(data), 5) |
| 105 | self.assertEqual(data, b" worl") |
| 106 | self.assertEqual(f.readinto(data), 2) |
| 107 | self.assertEqual(len(data), 5) |
| 108 | self.assertEqual(data[:2], b"d\n") |
| 109 | self.assertEqual(f.seek(0), 0) |
| 110 | self.assertEqual(f.read(20), b"hello world\n") |
| 111 | self.assertEqual(f.read(1), b"") |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 112 | self.assertEqual(f.readinto(bytearray(b"x")), 0) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 113 | self.assertEqual(f.seek(-6, 2), 6) |
| 114 | self.assertEqual(f.read(5), b"world") |
| 115 | self.assertEqual(f.read(0), b"") |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 116 | self.assertEqual(f.readinto(bytearray()), 0) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 117 | self.assertEqual(f.seek(-6, 1), 5) |
| 118 | self.assertEqual(f.read(5), b" worl") |
| 119 | self.assertEqual(f.tell(), 10) |
Christian Heimes | 8e42a0a | 2007-11-08 18:04:45 +0000 | [diff] [blame] | 120 | self.assertRaises(TypeError, f.seek, 0.0) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 121 | if buffered: |
| 122 | f.seek(0) |
| 123 | self.assertEqual(f.read(), b"hello world\n") |
| 124 | f.seek(6) |
| 125 | self.assertEqual(f.read(), b"world\n") |
| 126 | self.assertEqual(f.read(), b"") |
| 127 | |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 128 | LARGE = 2**31 |
| 129 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 130 | def large_file_ops(self, f): |
| 131 | assert f.readable() |
| 132 | assert f.writable() |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 133 | self.assertEqual(f.seek(self.LARGE), self.LARGE) |
| 134 | self.assertEqual(f.tell(), self.LARGE) |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 135 | self.assertEqual(f.write(b"xxx"), 3) |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 136 | self.assertEqual(f.tell(), self.LARGE + 3) |
| 137 | self.assertEqual(f.seek(-1, 1), self.LARGE + 2) |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 138 | self.assertEqual(f.truncate(), self.LARGE + 2) |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 139 | self.assertEqual(f.tell(), self.LARGE + 2) |
| 140 | self.assertEqual(f.seek(0, 2), self.LARGE + 2) |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 141 | self.assertEqual(f.truncate(self.LARGE + 1), self.LARGE + 1) |
Guido van Rossum | dc0b1a1 | 2007-04-12 22:55:07 +0000 | [diff] [blame] | 142 | self.assertEqual(f.tell(), self.LARGE + 2) |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 143 | self.assertEqual(f.seek(0, 2), self.LARGE + 1) |
| 144 | self.assertEqual(f.seek(-1, 2), self.LARGE) |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 145 | self.assertEqual(f.read(2), b"x") |
| 146 | |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 147 | def test_raw_file_io(self): |
| 148 | f = io.open(test_support.TESTFN, "wb", buffering=0) |
| 149 | self.assertEqual(f.readable(), False) |
| 150 | self.assertEqual(f.writable(), True) |
| 151 | self.assertEqual(f.seekable(), True) |
| 152 | self.write_ops(f) |
| 153 | f.close() |
| 154 | f = io.open(test_support.TESTFN, "rb", buffering=0) |
| 155 | self.assertEqual(f.readable(), True) |
| 156 | self.assertEqual(f.writable(), False) |
| 157 | self.assertEqual(f.seekable(), True) |
| 158 | self.read_ops(f) |
| 159 | f.close() |
| 160 | |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 161 | def test_buffered_file_io(self): |
| 162 | f = io.open(test_support.TESTFN, "wb") |
| 163 | self.assertEqual(f.readable(), False) |
| 164 | self.assertEqual(f.writable(), True) |
| 165 | self.assertEqual(f.seekable(), True) |
| 166 | self.write_ops(f) |
| 167 | f.close() |
| 168 | f = io.open(test_support.TESTFN, "rb") |
| 169 | self.assertEqual(f.readable(), True) |
| 170 | self.assertEqual(f.writable(), False) |
| 171 | self.assertEqual(f.seekable(), True) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 172 | self.read_ops(f, True) |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 173 | f.close() |
| 174 | |
Guido van Rossum | 48fc58a | 2007-06-07 23:45:37 +0000 | [diff] [blame] | 175 | def test_readline(self): |
| 176 | f = io.open(test_support.TESTFN, "wb") |
| 177 | f.write(b"abc\ndef\nxyzzy\nfoo") |
| 178 | f.close() |
| 179 | f = io.open(test_support.TESTFN, "rb") |
| 180 | self.assertEqual(f.readline(), b"abc\n") |
| 181 | self.assertEqual(f.readline(10), b"def\n") |
| 182 | self.assertEqual(f.readline(2), b"xy") |
| 183 | self.assertEqual(f.readline(4), b"zzy\n") |
| 184 | self.assertEqual(f.readline(), b"foo") |
| 185 | f.close() |
| 186 | |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 187 | def test_raw_bytes_io(self): |
| 188 | f = io.BytesIO() |
| 189 | self.write_ops(f) |
| 190 | data = f.getvalue() |
| 191 | self.assertEqual(data, b"hello world\n") |
| 192 | f = io.BytesIO(data) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 193 | self.read_ops(f, True) |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 194 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 195 | def test_large_file_ops(self): |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 196 | # On Windows and Mac OSX this test comsumes large resources; It takes |
| 197 | # a long time to build the >2GB file and takes >2GB of disk space |
| 198 | # therefore the resource must be enabled to run this test. |
| 199 | if sys.platform[:3] == 'win' or sys.platform == 'darwin': |
| 200 | if not test_support.is_resource_enabled("largefile"): |
| 201 | print("\nTesting large file ops skipped on %s." % sys.platform, |
| 202 | file=sys.stderr) |
| 203 | print("It requires %d bytes and a long time." % self.LARGE, |
| 204 | file=sys.stderr) |
| 205 | print("Use 'regrtest.py -u largefile test_io' to run it.", |
| 206 | file=sys.stderr) |
| 207 | return |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 208 | f = io.open(test_support.TESTFN, "w+b", 0) |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 209 | self.large_file_ops(f) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 210 | f.close() |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 211 | f = io.open(test_support.TESTFN, "w+b") |
| 212 | self.large_file_ops(f) |
| 213 | f.close() |
| 214 | |
| 215 | def test_with_open(self): |
| 216 | for bufsize in (0, 1, 100): |
| 217 | f = None |
| 218 | with open(test_support.TESTFN, "wb", bufsize) as f: |
Guido van Rossum | 1f2ca56 | 2007-08-27 20:44:15 +0000 | [diff] [blame] | 219 | f.write(b"xxx") |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 220 | self.assertEqual(f.closed, True) |
| 221 | f = None |
| 222 | try: |
| 223 | with open(test_support.TESTFN, "wb", bufsize) as f: |
| 224 | 1/0 |
| 225 | except ZeroDivisionError: |
| 226 | self.assertEqual(f.closed, True) |
| 227 | else: |
| 228 | self.fail("1/0 didn't raise an exception") |
| 229 | |
| 230 | def test_destructor(self): |
| 231 | record = [] |
| 232 | class MyFileIO(io.FileIO): |
| 233 | def __del__(self): |
| 234 | record.append(1) |
| 235 | io.FileIO.__del__(self) |
| 236 | def close(self): |
| 237 | record.append(2) |
| 238 | io.FileIO.close(self) |
| 239 | def flush(self): |
| 240 | record.append(3) |
| 241 | io.FileIO.flush(self) |
| 242 | f = MyFileIO(test_support.TESTFN, "w") |
| 243 | f.write("xxx") |
| 244 | del f |
| 245 | self.assertEqual(record, [1, 2, 3]) |
| 246 | |
| 247 | def test_close_flushes(self): |
| 248 | f = io.open(test_support.TESTFN, "wb") |
Guido van Rossum | 2b08b38 | 2007-05-08 20:18:39 +0000 | [diff] [blame] | 249 | f.write(b"xxx") |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 250 | f.close() |
| 251 | f = io.open(test_support.TESTFN, "rb") |
| 252 | self.assertEqual(f.read(), b"xxx") |
| 253 | f.close() |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 254 | |
Guido van Rossum | d410395 | 2007-04-12 05:44:49 +0000 | [diff] [blame] | 255 | def test_array_writes(self): |
| 256 | a = array.array('i', range(10)) |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 257 | n = len(memoryview(a)) |
Guido van Rossum | d410395 | 2007-04-12 05:44:49 +0000 | [diff] [blame] | 258 | f = io.open(test_support.TESTFN, "wb", 0) |
| 259 | self.assertEqual(f.write(a), n) |
| 260 | f.close() |
| 261 | f = io.open(test_support.TESTFN, "wb") |
| 262 | self.assertEqual(f.write(a), n) |
| 263 | f.close() |
| 264 | |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 265 | def test_closefd(self): |
| 266 | self.assertRaises(ValueError, io.open, test_support.TESTFN, 'w', |
| 267 | closefd=False) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 268 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 269 | class MemorySeekTestMixin: |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 270 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 271 | def testInit(self): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 272 | buf = self.buftype("1234567890") |
| 273 | bytesIo = self.ioclass(buf) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 274 | |
| 275 | def testRead(self): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 276 | buf = self.buftype("1234567890") |
| 277 | bytesIo = self.ioclass(buf) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 278 | |
| 279 | self.assertEquals(buf[:1], bytesIo.read(1)) |
| 280 | self.assertEquals(buf[1:5], bytesIo.read(4)) |
| 281 | self.assertEquals(buf[5:], bytesIo.read(900)) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 282 | self.assertEquals(self.EOF, bytesIo.read()) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 283 | |
| 284 | def testReadNoArgs(self): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 285 | buf = self.buftype("1234567890") |
| 286 | bytesIo = self.ioclass(buf) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 287 | |
| 288 | self.assertEquals(buf, bytesIo.read()) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 289 | self.assertEquals(self.EOF, bytesIo.read()) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 290 | |
| 291 | def testSeek(self): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 292 | buf = self.buftype("1234567890") |
| 293 | bytesIo = self.ioclass(buf) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 294 | |
| 295 | bytesIo.read(5) |
| 296 | bytesIo.seek(0) |
| 297 | self.assertEquals(buf, bytesIo.read()) |
| 298 | |
| 299 | bytesIo.seek(3) |
| 300 | self.assertEquals(buf[3:], bytesIo.read()) |
Christian Heimes | 8e42a0a | 2007-11-08 18:04:45 +0000 | [diff] [blame] | 301 | self.assertRaises(TypeError, bytesIo.seek, 0.0) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 302 | |
| 303 | def testTell(self): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 304 | buf = self.buftype("1234567890") |
| 305 | bytesIo = self.ioclass(buf) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 306 | |
| 307 | self.assertEquals(0, bytesIo.tell()) |
| 308 | bytesIo.seek(5) |
| 309 | self.assertEquals(5, bytesIo.tell()) |
| 310 | bytesIo.seek(10000) |
| 311 | self.assertEquals(10000, bytesIo.tell()) |
| 312 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 313 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 314 | class BytesIOTest(MemorySeekTestMixin, unittest.TestCase): |
Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 315 | @staticmethod |
| 316 | def buftype(s): |
| 317 | return s.encode("utf-8") |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 318 | ioclass = io.BytesIO |
| 319 | EOF = b"" |
| 320 | |
| 321 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 322 | class StringIOTest(MemorySeekTestMixin, unittest.TestCase): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 323 | buftype = str |
| 324 | ioclass = io.StringIO |
| 325 | EOF = "" |
| 326 | |
| 327 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 328 | class BufferedReaderTest(unittest.TestCase): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 329 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 330 | def testRead(self): |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 331 | rawio = MockRawIO((b"abc", b"d", b"efg")) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 332 | bufio = io.BufferedReader(rawio) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 333 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 334 | self.assertEquals(b"abcdef", bufio.read(6)) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 335 | |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 336 | def testBuffering(self): |
| 337 | data = b"abcdefghi" |
| 338 | dlen = len(data) |
| 339 | |
| 340 | tests = [ |
| 341 | [ 100, [ 3, 1, 4, 8 ], [ dlen, 0 ] ], |
| 342 | [ 100, [ 3, 3, 3], [ dlen ] ], |
| 343 | [ 4, [ 1, 2, 4, 2 ], [ 4, 4, 1 ] ], |
| 344 | ] |
| 345 | |
| 346 | for bufsize, buf_read_sizes, raw_read_sizes in tests: |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 347 | rawio = MockFileIO(data) |
| 348 | bufio = io.BufferedReader(rawio, buffer_size=bufsize) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 349 | pos = 0 |
| 350 | for nbytes in buf_read_sizes: |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 351 | self.assertEquals(bufio.read(nbytes), data[pos:pos+nbytes]) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 352 | pos += nbytes |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 353 | self.assertEquals(rawio.read_history, raw_read_sizes) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 354 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 355 | def testReadNonBlocking(self): |
| 356 | # Inject some None's in there to simulate EWOULDBLOCK |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 357 | rawio = MockRawIO((b"abc", b"d", None, b"efg", None, None)) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 358 | bufio = io.BufferedReader(rawio) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 359 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 360 | self.assertEquals(b"abcd", bufio.read(6)) |
| 361 | self.assertEquals(b"e", bufio.read(1)) |
| 362 | self.assertEquals(b"fg", bufio.read()) |
| 363 | self.assert_(None is bufio.read()) |
| 364 | self.assertEquals(b"", bufio.read()) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 365 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 366 | def testReadToEof(self): |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 367 | rawio = MockRawIO((b"abc", b"d", b"efg")) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 368 | bufio = io.BufferedReader(rawio) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 369 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 370 | self.assertEquals(b"abcdefg", bufio.read(9000)) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 371 | |
| 372 | def testReadNoArgs(self): |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 373 | rawio = MockRawIO((b"abc", b"d", b"efg")) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 374 | bufio = io.BufferedReader(rawio) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 375 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 376 | self.assertEquals(b"abcdefg", bufio.read()) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 377 | |
| 378 | def testFileno(self): |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 379 | rawio = MockRawIO((b"abc", b"d", b"efg")) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 380 | bufio = io.BufferedReader(rawio) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 381 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 382 | self.assertEquals(42, bufio.fileno()) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 383 | |
| 384 | def testFilenoNoFileno(self): |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 385 | # XXX will we always have fileno() function? If so, kill |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 386 | # this test. Else, write it. |
| 387 | pass |
| 388 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 389 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 390 | class BufferedWriterTest(unittest.TestCase): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 391 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 392 | def testWrite(self): |
| 393 | # Write to the buffered IO but don't overflow the buffer. |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 394 | writer = MockRawIO() |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 395 | bufio = io.BufferedWriter(writer, 8) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 396 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 397 | bufio.write(b"abc") |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 398 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 399 | self.assertFalse(writer._write_stack) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 400 | |
| 401 | def testWriteOverflow(self): |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 402 | writer = MockRawIO() |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 403 | bufio = io.BufferedWriter(writer, 8) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 404 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 405 | bufio.write(b"abc") |
| 406 | bufio.write(b"defghijkl") |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 407 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 408 | self.assertEquals(b"abcdefghijkl", writer._write_stack[0]) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 409 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 410 | def testWriteNonBlocking(self): |
| 411 | raw = MockNonBlockWriterIO((9, 2, 22, -6, 10, 12, 12)) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 412 | bufio = io.BufferedWriter(raw, 8, 16) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 413 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 414 | bufio.write(b"asdf") |
| 415 | bufio.write(b"asdfa") |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 416 | self.assertEquals(b"asdfasdfa", raw._write_stack[0]) |
| 417 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 418 | bufio.write(b"asdfasdfasdf") |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 419 | self.assertEquals(b"asdfasdfasdf", raw._write_stack[1]) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 420 | bufio.write(b"asdfasdfasdf") |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 421 | self.assertEquals(b"dfasdfasdf", raw._write_stack[2]) |
| 422 | self.assertEquals(b"asdfasdfasdf", raw._write_stack[3]) |
| 423 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 424 | bufio.write(b"asdfasdfasdf") |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 425 | |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 426 | # XXX I don't like this test. It relies too heavily on how the |
| 427 | # algorithm actually works, which we might change. Refactor |
| 428 | # later. |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 429 | |
| 430 | def testFileno(self): |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 431 | rawio = MockRawIO((b"abc", b"d", b"efg")) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 432 | bufio = io.BufferedWriter(rawio) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 433 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 434 | self.assertEquals(42, bufio.fileno()) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 435 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 436 | def testFlush(self): |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 437 | writer = MockRawIO() |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 438 | bufio = io.BufferedWriter(writer, 8) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 439 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 440 | bufio.write(b"abc") |
| 441 | bufio.flush() |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 442 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 443 | self.assertEquals(b"abc", writer._write_stack[0]) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 444 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 445 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 446 | class BufferedRWPairTest(unittest.TestCase): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 447 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 448 | def testRWPair(self): |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 449 | r = MockRawIO(()) |
| 450 | w = MockRawIO() |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 451 | pair = io.BufferedRWPair(r, w) |
| 452 | |
| 453 | # XXX need implementation |
| 454 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 455 | |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 456 | class BufferedRandomTest(unittest.TestCase): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 457 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 458 | def testReadAndWrite(self): |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 459 | raw = MockRawIO((b"asdf", b"ghjk")) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 460 | rw = io.BufferedRandom(raw, 8, 12) |
| 461 | |
| 462 | self.assertEqual(b"as", rw.read(2)) |
| 463 | rw.write(b"ddd") |
| 464 | rw.write(b"eee") |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 465 | self.assertFalse(raw._write_stack) # Buffer writes |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 466 | self.assertEqual(b"ghjk", rw.read()) # This read forces write flush |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 467 | self.assertEquals(b"dddeee", raw._write_stack[0]) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 468 | |
| 469 | def testSeekAndTell(self): |
| 470 | raw = io.BytesIO(b"asdfghjkl") |
| 471 | rw = io.BufferedRandom(raw) |
| 472 | |
| 473 | self.assertEquals(b"as", rw.read(2)) |
| 474 | self.assertEquals(2, rw.tell()) |
| 475 | rw.seek(0, 0) |
| 476 | self.assertEquals(b"asdf", rw.read(4)) |
| 477 | |
| 478 | rw.write(b"asdf") |
| 479 | rw.seek(0, 0) |
| 480 | self.assertEquals(b"asdfasdfl", rw.read()) |
| 481 | self.assertEquals(9, rw.tell()) |
| 482 | rw.seek(-4, 2) |
| 483 | self.assertEquals(5, rw.tell()) |
| 484 | rw.seek(2, 1) |
| 485 | self.assertEquals(7, rw.tell()) |
| 486 | self.assertEquals(b"fl", rw.read(11)) |
Christian Heimes | 8e42a0a | 2007-11-08 18:04:45 +0000 | [diff] [blame] | 487 | self.assertRaises(TypeError, rw.seek, 0.0) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 488 | |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 489 | |
| 490 | class TextIOWrapperTest(unittest.TestCase): |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 491 | |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 492 | def setUp(self): |
| 493 | self.testdata = b"AAA\r\nBBB\rCCC\r\nDDD\nEEE\r\n" |
| 494 | self.normalized = b"AAA\nBBB\nCCC\nDDD\nEEE\n".decode("ascii") |
| 495 | |
Guido van Rossum | d071281 | 2007-04-11 16:32:43 +0000 | [diff] [blame] | 496 | def tearDown(self): |
| 497 | test_support.unlink(test_support.TESTFN) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 498 | |
Guido van Rossum | e7fc50f | 2007-12-03 22:54:21 +0000 | [diff] [blame^] | 499 | def testEncodingErrorsReading(self): |
| 500 | # (1) default |
| 501 | b = io.BytesIO(b"abc\n\xff\n") |
| 502 | t = io.TextIOWrapper(b, encoding="ascii") |
| 503 | self.assertRaises(UnicodeError, t.read) |
| 504 | # (2) explicit strict |
| 505 | b = io.BytesIO(b"abc\n\xff\n") |
| 506 | t = io.TextIOWrapper(b, encoding="ascii", errors="strict") |
| 507 | self.assertRaises(UnicodeError, t.read) |
| 508 | # (3) ignore |
| 509 | b = io.BytesIO(b"abc\n\xff\n") |
| 510 | t = io.TextIOWrapper(b, encoding="ascii", errors="ignore") |
| 511 | self.assertEquals(t.read(), "abc\n\n") |
| 512 | # (4) replace |
| 513 | b = io.BytesIO(b"abc\n\xff\n") |
| 514 | t = io.TextIOWrapper(b, encoding="ascii", errors="replace") |
| 515 | self.assertEquals(t.read(), "abc\n\ufffd\n") |
| 516 | |
| 517 | def testEncodingErrorsWriting(self): |
| 518 | # (1) default |
| 519 | b = io.BytesIO() |
| 520 | t = io.TextIOWrapper(b, encoding="ascii") |
| 521 | self.assertRaises(UnicodeError, t.write, "\xff") |
| 522 | # (2) explicit strict |
| 523 | b = io.BytesIO() |
| 524 | t = io.TextIOWrapper(b, encoding="ascii", errors="strict") |
| 525 | self.assertRaises(UnicodeError, t.write, "\xff") |
| 526 | # (3) ignore |
| 527 | b = io.BytesIO() |
| 528 | t = io.TextIOWrapper(b, encoding="ascii", errors="ignore") |
| 529 | t.write("abc\xffdef\n") |
| 530 | t.flush() |
| 531 | self.assertEquals(b.getvalue(), b"abcdef\n") |
| 532 | # (4) replace |
| 533 | b = io.BytesIO() |
| 534 | t = io.TextIOWrapper(b, encoding="ascii", errors="replace") |
| 535 | t.write("abc\xffdef\n") |
| 536 | t.flush() |
| 537 | self.assertEquals(b.getvalue(), b"abc?def\n") |
| 538 | |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 539 | def testNewlinesInput(self): |
| 540 | testdata = b"AAA\nBBB\nCCC\rDDD\rEEE\r\nFFF\r\nGGG" |
| 541 | normalized = testdata.replace(b"\r\n", b"\n").replace(b"\r", b"\n") |
| 542 | for newline, expected in [ |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 543 | (None, normalized.decode("ascii").splitlines(True)), |
| 544 | ("", testdata.decode("ascii").splitlines(True)), |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 545 | ("\n", ["AAA\n", "BBB\n", "CCC\rDDD\rEEE\r\n", "FFF\r\n", "GGG"]), |
| 546 | ("\r\n", ["AAA\nBBB\nCCC\rDDD\rEEE\r\n", "FFF\r\n", "GGG"]), |
| 547 | ("\r", ["AAA\nBBB\nCCC\r", "DDD\r", "EEE\r", "\nFFF\r", "\nGGG"]), |
| 548 | ]: |
| 549 | buf = io.BytesIO(testdata) |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 550 | txt = io.TextIOWrapper(buf, encoding="ascii", newline=newline) |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 551 | self.assertEquals(txt.readlines(), expected) |
| 552 | txt.seek(0) |
| 553 | self.assertEquals(txt.read(), "".join(expected)) |
| 554 | |
| 555 | def testNewlinesOutput(self): |
| 556 | testdict = { |
| 557 | "": b"AAA\nBBB\nCCC\nX\rY\r\nZ", |
| 558 | "\n": b"AAA\nBBB\nCCC\nX\rY\r\nZ", |
| 559 | "\r": b"AAA\rBBB\rCCC\rX\rY\r\rZ", |
| 560 | "\r\n": b"AAA\r\nBBB\r\nCCC\r\nX\rY\r\r\nZ", |
| 561 | } |
| 562 | tests = [(None, testdict[os.linesep])] + sorted(testdict.items()) |
| 563 | for newline, expected in tests: |
| 564 | buf = io.BytesIO() |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 565 | txt = io.TextIOWrapper(buf, encoding="ascii", newline=newline) |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 566 | txt.write("AAA\nB") |
| 567 | txt.write("BB\nCCC\n") |
| 568 | txt.write("X\rY\r\nZ") |
| 569 | txt.flush() |
| 570 | self.assertEquals(buf.getvalue(), expected) |
| 571 | |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 572 | def testNewlines(self): |
| 573 | input_lines = [ "unix\n", "windows\r\n", "os9\r", "last\n", "nonl" ] |
| 574 | |
| 575 | tests = [ |
| 576 | [ None, [ 'unix\n', 'windows\n', 'os9\n', 'last\n', 'nonl' ] ], |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 577 | [ '', input_lines ], |
| 578 | [ '\n', [ "unix\n", "windows\r\n", "os9\rlast\n", "nonl" ] ], |
| 579 | [ '\r\n', [ "unix\nwindows\r\n", "os9\rlast\nnonl" ] ], |
| 580 | [ '\r', [ "unix\nwindows\r", "\nos9\r", "last\nnonl" ] ], |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 581 | ] |
| 582 | |
Guido van Rossum | c2f93dc | 2007-05-24 00:50:02 +0000 | [diff] [blame] | 583 | encodings = ('utf-8', 'latin-1') |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 584 | |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 585 | # Try a range of buffer sizes to test the case where \r is the last |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 586 | # character in TextIOWrapper._pending_line. |
| 587 | for encoding in encodings: |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 588 | # XXX: str.encode() should return bytes |
| 589 | data = bytes(''.join(input_lines).encode(encoding)) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 590 | for do_reads in (False, True): |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 591 | for bufsize in range(1, 10): |
| 592 | for newline, exp_lines in tests: |
| 593 | bufio = io.BufferedReader(io.BytesIO(data), bufsize) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 594 | textio = io.TextIOWrapper(bufio, newline=newline, |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 595 | encoding=encoding) |
| 596 | if do_reads: |
| 597 | got_lines = [] |
| 598 | while True: |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 599 | c2 = textio.read(2) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 600 | if c2 == '': |
| 601 | break |
| 602 | self.assertEquals(len(c2), 2) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 603 | got_lines.append(c2 + textio.readline()) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 604 | else: |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 605 | got_lines = list(textio) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 606 | |
| 607 | for got_line, exp_line in zip(got_lines, exp_lines): |
| 608 | self.assertEquals(got_line, exp_line) |
| 609 | self.assertEquals(len(got_lines), len(exp_lines)) |
| 610 | |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 611 | def testNewlinesInput(self): |
| 612 | testdata = b"AAA\nBBB\nCCC\rDDD\rEEE\r\nFFF\r\nGGG" |
| 613 | normalized = testdata.replace(b"\r\n", b"\n").replace(b"\r", b"\n") |
| 614 | for newline, expected in [ |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 615 | (None, normalized.decode("ascii").splitlines(True)), |
| 616 | ("", testdata.decode("ascii").splitlines(True)), |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 617 | ("\n", ["AAA\n", "BBB\n", "CCC\rDDD\rEEE\r\n", "FFF\r\n", "GGG"]), |
| 618 | ("\r\n", ["AAA\nBBB\nCCC\rDDD\rEEE\r\n", "FFF\r\n", "GGG"]), |
| 619 | ("\r", ["AAA\nBBB\nCCC\r", "DDD\r", "EEE\r", "\nFFF\r", "\nGGG"]), |
| 620 | ]: |
| 621 | buf = io.BytesIO(testdata) |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 622 | txt = io.TextIOWrapper(buf, encoding="ascii", newline=newline) |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 623 | self.assertEquals(txt.readlines(), expected) |
| 624 | txt.seek(0) |
| 625 | self.assertEquals(txt.read(), "".join(expected)) |
| 626 | |
| 627 | def testNewlinesOutput(self): |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 628 | data = "AAA\nBBB\rCCC\n" |
| 629 | data_lf = b"AAA\nBBB\rCCC\n" |
| 630 | data_cr = b"AAA\rBBB\rCCC\r" |
| 631 | data_crlf = b"AAA\r\nBBB\rCCC\r\n" |
Guido van Rossum | 1836358 | 2007-08-18 22:05:58 +0000 | [diff] [blame] | 632 | save_linesep = os.linesep |
| 633 | try: |
| 634 | for os.linesep, newline, expected in [ |
| 635 | ("\n", None, data_lf), |
| 636 | ("\r\n", None, data_crlf), |
| 637 | ("\n", "", data_lf), |
| 638 | ("\r\n", "", data_lf), |
| 639 | ("\n", "\n", data_lf), |
| 640 | ("\r\n", "\n", data_lf), |
| 641 | ("\n", "\r", data_cr), |
| 642 | ("\r\n", "\r", data_cr), |
| 643 | ("\n", "\r\n", data_crlf), |
| 644 | ("\r\n", "\r\n", data_crlf), |
| 645 | ]: |
| 646 | buf = io.BytesIO() |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 647 | txt = io.TextIOWrapper(buf, encoding="ascii", newline=newline) |
Guido van Rossum | 1836358 | 2007-08-18 22:05:58 +0000 | [diff] [blame] | 648 | txt.write(data) |
| 649 | txt.close() |
| 650 | self.assertEquals(buf.getvalue(), expected) |
| 651 | finally: |
| 652 | os.linesep = save_linesep |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 653 | |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 654 | # Systematic tests of the text I/O API |
| 655 | |
| 656 | def testBasicIO(self): |
| 657 | for chunksize in (1, 2, 3, 4, 5, 15, 16, 17, 31, 32, 33, 63, 64, 65): |
| 658 | for enc in "ascii", "latin1", "utf8" :# , "utf-16-be", "utf-16-le": |
| 659 | f = io.open(test_support.TESTFN, "w+", encoding=enc) |
| 660 | f._CHUNK_SIZE = chunksize |
| 661 | self.assertEquals(f.write("abc"), 3) |
| 662 | f.close() |
| 663 | f = io.open(test_support.TESTFN, "r+", encoding=enc) |
| 664 | f._CHUNK_SIZE = chunksize |
| 665 | self.assertEquals(f.tell(), 0) |
| 666 | self.assertEquals(f.read(), "abc") |
| 667 | cookie = f.tell() |
| 668 | self.assertEquals(f.seek(0), 0) |
| 669 | self.assertEquals(f.read(2), "ab") |
| 670 | self.assertEquals(f.read(1), "c") |
| 671 | self.assertEquals(f.read(1), "") |
| 672 | self.assertEquals(f.read(), "") |
| 673 | self.assertEquals(f.tell(), cookie) |
| 674 | self.assertEquals(f.seek(0), 0) |
| 675 | self.assertEquals(f.seek(0, 2), cookie) |
| 676 | self.assertEquals(f.write("def"), 3) |
| 677 | self.assertEquals(f.seek(cookie), cookie) |
| 678 | self.assertEquals(f.read(), "def") |
| 679 | if enc.startswith("utf"): |
| 680 | self.multi_line_test(f, enc) |
| 681 | f.close() |
| 682 | |
| 683 | def multi_line_test(self, f, enc): |
| 684 | f.seek(0) |
| 685 | f.truncate() |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 686 | sample = "s\xff\u0fff\uffff" |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 687 | wlines = [] |
Guido van Rossum | cba608c | 2007-04-11 14:19:59 +0000 | [diff] [blame] | 688 | for size in (0, 1, 2, 3, 4, 5, 30, 31, 32, 33, 62, 63, 64, 65, 1000): |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 689 | chars = [] |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 690 | for i in range(size): |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 691 | chars.append(sample[i % len(sample)]) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 692 | line = "".join(chars) + "\n" |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 693 | wlines.append((f.tell(), line)) |
| 694 | f.write(line) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 695 | f.seek(0) |
| 696 | rlines = [] |
| 697 | while True: |
| 698 | pos = f.tell() |
| 699 | line = f.readline() |
| 700 | if not line: |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 701 | break |
| 702 | rlines.append((pos, line)) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 703 | self.assertEquals(rlines, wlines) |
| 704 | |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 705 | def testTelling(self): |
| 706 | f = io.open(test_support.TESTFN, "w+", encoding="utf8") |
| 707 | p0 = f.tell() |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 708 | f.write("\xff\n") |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 709 | p1 = f.tell() |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 710 | f.write("\xff\n") |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 711 | p2 = f.tell() |
| 712 | f.seek(0) |
| 713 | self.assertEquals(f.tell(), p0) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 714 | self.assertEquals(f.readline(), "\xff\n") |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 715 | self.assertEquals(f.tell(), p1) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 716 | self.assertEquals(f.readline(), "\xff\n") |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 717 | self.assertEquals(f.tell(), p2) |
| 718 | f.seek(0) |
| 719 | for line in f: |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 720 | self.assertEquals(line, "\xff\n") |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 721 | self.assertRaises(IOError, f.tell) |
| 722 | self.assertEquals(f.tell(), p2) |
| 723 | f.close() |
| 724 | |
Guido van Rossum | d76e779 | 2007-04-17 02:38:04 +0000 | [diff] [blame] | 725 | def testSeeking(self): |
| 726 | chunk_size = io.TextIOWrapper._CHUNK_SIZE |
| 727 | prefix_size = chunk_size - 2 |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 728 | u_prefix = "a" * prefix_size |
Guido van Rossum | d76e779 | 2007-04-17 02:38:04 +0000 | [diff] [blame] | 729 | prefix = bytes(u_prefix.encode("utf-8")) |
| 730 | self.assertEquals(len(u_prefix), len(prefix)) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 731 | u_suffix = "\u8888\n" |
Guido van Rossum | d76e779 | 2007-04-17 02:38:04 +0000 | [diff] [blame] | 732 | suffix = bytes(u_suffix.encode("utf-8")) |
| 733 | line = prefix + suffix |
| 734 | f = io.open(test_support.TESTFN, "wb") |
| 735 | f.write(line*2) |
| 736 | f.close() |
| 737 | f = io.open(test_support.TESTFN, "r", encoding="utf-8") |
| 738 | s = f.read(prefix_size) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 739 | self.assertEquals(s, str(prefix, "ascii")) |
Guido van Rossum | d76e779 | 2007-04-17 02:38:04 +0000 | [diff] [blame] | 740 | self.assertEquals(f.tell(), prefix_size) |
| 741 | self.assertEquals(f.readline(), u_suffix) |
| 742 | |
| 743 | def testSeekingToo(self): |
| 744 | # Regression test for a specific bug |
| 745 | data = b'\xe0\xbf\xbf\n' |
| 746 | f = io.open(test_support.TESTFN, "wb") |
| 747 | f.write(data) |
| 748 | f.close() |
| 749 | f = io.open(test_support.TESTFN, "r", encoding="utf-8") |
| 750 | f._CHUNK_SIZE # Just test that it exists |
| 751 | f._CHUNK_SIZE = 2 |
| 752 | f.readline() |
| 753 | f.tell() |
| 754 | |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 755 | def timingTest(self): |
| 756 | timer = time.time |
| 757 | enc = "utf8" |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 758 | line = "\0\x0f\xff\u0fff\uffff\U000fffff\U0010ffff"*3 + "\n" |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 759 | nlines = 10000 |
| 760 | nchars = len(line) |
| 761 | nbytes = len(line.encode(enc)) |
| 762 | for chunk_size in (32, 64, 128, 256): |
| 763 | f = io.open(test_support.TESTFN, "w+", encoding=enc) |
| 764 | f._CHUNK_SIZE = chunk_size |
| 765 | t0 = timer() |
| 766 | for i in range(nlines): |
| 767 | f.write(line) |
| 768 | f.flush() |
| 769 | t1 = timer() |
| 770 | f.seek(0) |
| 771 | for line in f: |
| 772 | pass |
| 773 | t2 = timer() |
| 774 | f.seek(0) |
| 775 | while f.readline(): |
| 776 | pass |
| 777 | t3 = timer() |
| 778 | f.seek(0) |
| 779 | while f.readline(): |
| 780 | f.tell() |
| 781 | t4 = timer() |
| 782 | f.close() |
| 783 | if test_support.verbose: |
| 784 | print("\nTiming test: %d lines of %d characters (%d bytes)" % |
| 785 | (nlines, nchars, nbytes)) |
| 786 | print("File chunk size: %6s" % f._CHUNK_SIZE) |
| 787 | print("Writing: %6.3f seconds" % (t1-t0)) |
| 788 | print("Reading using iteration: %6.3f seconds" % (t2-t1)) |
| 789 | print("Reading using readline(): %6.3f seconds" % (t3-t2)) |
| 790 | print("Using readline()+tell(): %6.3f seconds" % (t4-t3)) |
| 791 | |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 792 | def testReadOneByOne(self): |
| 793 | txt = io.TextIOWrapper(io.BytesIO(b"AA\r\nBB")) |
| 794 | reads = "" |
| 795 | while True: |
| 796 | c = txt.read(1) |
| 797 | if not c: |
| 798 | break |
| 799 | reads += c |
| 800 | self.assertEquals(reads, "AA\nBB") |
| 801 | |
| 802 | # read in amounts equal to TextIOWrapper._CHUNK_SIZE which is 128. |
| 803 | def testReadByChunk(self): |
| 804 | # make sure "\r\n" straddles 128 char boundary. |
| 805 | txt = io.TextIOWrapper(io.BytesIO(b"A" * 127 + b"\r\nB")) |
| 806 | reads = "" |
| 807 | while True: |
| 808 | c = txt.read(128) |
| 809 | if not c: |
| 810 | break |
| 811 | reads += c |
| 812 | self.assertEquals(reads, "A"*127+"\nB") |
| 813 | |
| 814 | def test_issue1395_1(self): |
| 815 | txt = io.TextIOWrapper(io.BytesIO(self.testdata), encoding="ascii") |
| 816 | |
| 817 | # read one char at a time |
| 818 | reads = "" |
| 819 | while True: |
| 820 | c = txt.read(1) |
| 821 | if not c: |
| 822 | break |
| 823 | reads += c |
| 824 | self.assertEquals(reads, self.normalized) |
| 825 | |
| 826 | def test_issue1395_2(self): |
| 827 | txt = io.TextIOWrapper(io.BytesIO(self.testdata), encoding="ascii") |
| 828 | txt._CHUNK_SIZE = 4 |
| 829 | |
| 830 | reads = "" |
| 831 | while True: |
| 832 | c = txt.read(4) |
| 833 | if not c: |
| 834 | break |
| 835 | reads += c |
| 836 | self.assertEquals(reads, self.normalized) |
| 837 | |
| 838 | def test_issue1395_3(self): |
| 839 | txt = io.TextIOWrapper(io.BytesIO(self.testdata), encoding="ascii") |
| 840 | txt._CHUNK_SIZE = 4 |
| 841 | |
| 842 | reads = txt.read(4) |
| 843 | reads += txt.read(4) |
| 844 | reads += txt.readline() |
| 845 | reads += txt.readline() |
| 846 | reads += txt.readline() |
| 847 | self.assertEquals(reads, self.normalized) |
| 848 | |
| 849 | def test_issue1395_4(self): |
| 850 | txt = io.TextIOWrapper(io.BytesIO(self.testdata), encoding="ascii") |
| 851 | txt._CHUNK_SIZE = 4 |
| 852 | |
| 853 | reads = txt.read(4) |
| 854 | reads += txt.read() |
| 855 | self.assertEquals(reads, self.normalized) |
| 856 | |
| 857 | def test_issue1395_5(self): |
| 858 | txt = io.TextIOWrapper(io.BytesIO(self.testdata), encoding="ascii") |
| 859 | txt._CHUNK_SIZE = 4 |
| 860 | |
| 861 | reads = txt.read(4) |
| 862 | pos = txt.tell() |
| 863 | txt.seek(0) |
| 864 | txt.seek(pos) |
| 865 | self.assertEquals(txt.read(4), "BBB\n") |
| 866 | |
| 867 | def test_newline_decoder(self): |
| 868 | import codecs |
| 869 | decoder = codecs.getincrementaldecoder("utf-8")() |
| 870 | decoder = io.IncrementalNewlineDecoder(decoder, translate=True) |
| 871 | |
| 872 | self.assertEquals(decoder.decode(b'\xe8\xa2\x88'), "\u8888") |
| 873 | |
| 874 | self.assertEquals(decoder.decode(b'\xe8'), "") |
| 875 | self.assertEquals(decoder.decode(b'\xa2'), "") |
| 876 | self.assertEquals(decoder.decode(b'\x88'), "\u8888") |
| 877 | |
| 878 | self.assertEquals(decoder.decode(b'\xe8'), "") |
| 879 | self.assertRaises(UnicodeDecodeError, decoder.decode, b'', final=True) |
| 880 | |
| 881 | decoder.setstate((b'', 0)) |
| 882 | self.assertEquals(decoder.decode(b'\n'), "\n") |
| 883 | self.assertEquals(decoder.decode(b'\r'), "") |
| 884 | self.assertEquals(decoder.decode(b'', final=True), "\n") |
| 885 | self.assertEquals(decoder.decode(b'\r', final=True), "\n") |
| 886 | |
| 887 | self.assertEquals(decoder.decode(b'\r'), "") |
| 888 | self.assertEquals(decoder.decode(b'a'), "\na") |
| 889 | |
| 890 | self.assertEquals(decoder.decode(b'\r\r\n'), "\n\n") |
| 891 | self.assertEquals(decoder.decode(b'\r'), "") |
| 892 | self.assertEquals(decoder.decode(b'\r'), "\n") |
| 893 | self.assertEquals(decoder.decode(b'\na'), "\na") |
| 894 | |
| 895 | self.assertEquals(decoder.decode(b'\xe8\xa2\x88\r\n'), "\u8888\n") |
| 896 | self.assertEquals(decoder.decode(b'\xe8\xa2\x88'), "\u8888") |
| 897 | self.assertEquals(decoder.decode(b'\n'), "\n") |
| 898 | self.assertEquals(decoder.decode(b'\xe8\xa2\x88\r'), "\u8888") |
| 899 | self.assertEquals(decoder.decode(b'\n'), "\n") |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 900 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 901 | # XXX Tests for open() |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 902 | |
Guido van Rossum | 5abbf75 | 2007-08-27 17:39:33 +0000 | [diff] [blame] | 903 | class MiscIOTest(unittest.TestCase): |
| 904 | |
| 905 | def testImport__all__(self): |
| 906 | for name in io.__all__: |
| 907 | obj = getattr(io, name, None) |
| 908 | self.assert_(obj is not None, name) |
| 909 | if name == "open": |
| 910 | continue |
| 911 | elif "error" in name.lower(): |
| 912 | self.assert_(issubclass(obj, Exception), name) |
| 913 | else: |
| 914 | self.assert_(issubclass(obj, io.IOBase)) |
| 915 | |
| 916 | |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 917 | def test_main(): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 918 | test_support.run_unittest(IOTest, BytesIOTest, StringIOTest, |
| 919 | BufferedReaderTest, |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 920 | BufferedWriterTest, BufferedRWPairTest, |
Guido van Rossum | 5abbf75 | 2007-08-27 17:39:33 +0000 | [diff] [blame] | 921 | BufferedRandomTest, TextIOWrapperTest, |
| 922 | MiscIOTest) |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 923 | |
| 924 | if __name__ == "__main__": |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 925 | unittest.main() |