Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 1 | """Unit tests for io.py.""" |
| 2 | |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 3 | import sys |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 4 | import time |
Guido van Rossum | d410395 | 2007-04-12 05:44:49 +0000 | [diff] [blame] | 5 | import array |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 6 | import unittest |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 7 | from itertools import chain |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 8 | from test import test_support |
| 9 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 10 | import io # The module under test |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 13 | class MockRawIO(io.RawIOBase): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 15 | def __init__(self, read_stack=()): |
| 16 | self._read_stack = list(read_stack) |
| 17 | self._write_stack = [] |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 18 | |
| 19 | def read(self, n=None): |
| 20 | try: |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 21 | return self._read_stack.pop(0) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 22 | except: |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 23 | return b"" |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 25 | def write(self, b): |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 26 | self._write_stack.append(b[:]) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 27 | return len(b) |
| 28 | |
| 29 | def writable(self): |
| 30 | return True |
| 31 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 32 | def fileno(self): |
| 33 | return 42 |
| 34 | |
| 35 | def readable(self): |
| 36 | return True |
| 37 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 38 | def seekable(self): |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 39 | return True |
| 40 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 41 | def seek(self, pos, whence): |
| 42 | pass |
| 43 | |
| 44 | def tell(self): |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 45 | return 42 |
| 46 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 48 | class MockFileIO(io.BytesIO): |
| 49 | |
| 50 | def __init__(self, data): |
| 51 | self.read_history = [] |
| 52 | io.BytesIO.__init__(self, data) |
| 53 | |
| 54 | def read(self, n=None): |
| 55 | res = io.BytesIO.read(self, n) |
| 56 | self.read_history.append(None if res is None else len(res)) |
| 57 | return res |
| 58 | |
| 59 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 60 | class MockNonBlockWriterIO(io.RawIOBase): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 62 | def __init__(self, blocking_script): |
| 63 | self._blocking_script = list(blocking_script) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 64 | self._write_stack = [] |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 65 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 66 | def write(self, b): |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 67 | self._write_stack.append(b[:]) |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 68 | n = self._blocking_script.pop(0) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 69 | if (n < 0): |
Guido van Rossum | 141f767 | 2007-04-10 00:22:16 +0000 | [diff] [blame] | 70 | raise io.BlockingIOError(0, "test blocking", -n) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 71 | else: |
| 72 | return n |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 74 | def writable(self): |
| 75 | return True |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 76 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 77 | |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 78 | class IOTest(unittest.TestCase): |
| 79 | |
Guido van Rossum | 4d0f5a4 | 2007-03-07 22:59:39 +0000 | [diff] [blame] | 80 | def tearDown(self): |
| 81 | test_support.unlink(test_support.TESTFN) |
| 82 | |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 83 | def write_ops(self, f): |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 84 | self.assertEqual(f.write(b"blah."), 5) |
| 85 | self.assertEqual(f.seek(0), 0) |
| 86 | self.assertEqual(f.write(b"Hello."), 6) |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 87 | self.assertEqual(f.tell(), 6) |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 88 | self.assertEqual(f.seek(-1, 1), 5) |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 89 | self.assertEqual(f.tell(), 5) |
Guido van Rossum | 2b08b38 | 2007-05-08 20:18:39 +0000 | [diff] [blame] | 90 | self.assertEqual(f.write(str8(" world\n\n\n")), 9) |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 91 | self.assertEqual(f.seek(0), 0) |
Guido van Rossum | 2b08b38 | 2007-05-08 20:18:39 +0000 | [diff] [blame] | 92 | self.assertEqual(f.write(b"h"), 1) |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 93 | self.assertEqual(f.seek(-1, 2), 13) |
| 94 | self.assertEqual(f.tell(), 13) |
| 95 | self.assertEqual(f.truncate(12), 12) |
Guido van Rossum | dc0b1a1 | 2007-04-12 22:55:07 +0000 | [diff] [blame] | 96 | self.assertEqual(f.tell(), 13) |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 97 | |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 98 | def read_ops(self, f, buffered=False): |
| 99 | data = f.read(5) |
| 100 | self.assertEqual(data, b"hello") |
| 101 | self.assertEqual(f.readinto(data), 5) |
| 102 | self.assertEqual(data, b" worl") |
| 103 | self.assertEqual(f.readinto(data), 2) |
| 104 | self.assertEqual(len(data), 5) |
| 105 | self.assertEqual(data[:2], b"d\n") |
| 106 | self.assertEqual(f.seek(0), 0) |
| 107 | self.assertEqual(f.read(20), b"hello world\n") |
| 108 | self.assertEqual(f.read(1), b"") |
| 109 | self.assertEqual(f.readinto(b"x"), 0) |
| 110 | self.assertEqual(f.seek(-6, 2), 6) |
| 111 | self.assertEqual(f.read(5), b"world") |
| 112 | self.assertEqual(f.read(0), b"") |
| 113 | self.assertEqual(f.readinto(b""), 0) |
| 114 | self.assertEqual(f.seek(-6, 1), 5) |
| 115 | self.assertEqual(f.read(5), b" worl") |
| 116 | self.assertEqual(f.tell(), 10) |
| 117 | if buffered: |
| 118 | f.seek(0) |
| 119 | self.assertEqual(f.read(), b"hello world\n") |
| 120 | f.seek(6) |
| 121 | self.assertEqual(f.read(), b"world\n") |
| 122 | self.assertEqual(f.read(), b"") |
| 123 | |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 124 | LARGE = 2**31 |
| 125 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 126 | def large_file_ops(self, f): |
| 127 | assert f.readable() |
| 128 | assert f.writable() |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 129 | self.assertEqual(f.seek(self.LARGE), self.LARGE) |
| 130 | self.assertEqual(f.tell(), self.LARGE) |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 131 | self.assertEqual(f.write(b"xxx"), 3) |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 132 | self.assertEqual(f.tell(), self.LARGE + 3) |
| 133 | self.assertEqual(f.seek(-1, 1), self.LARGE + 2) |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 134 | self.assertEqual(f.truncate(), self.LARGE + 2) |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 135 | self.assertEqual(f.tell(), self.LARGE + 2) |
| 136 | self.assertEqual(f.seek(0, 2), self.LARGE + 2) |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 137 | self.assertEqual(f.truncate(self.LARGE + 1), self.LARGE + 1) |
Guido van Rossum | dc0b1a1 | 2007-04-12 22:55:07 +0000 | [diff] [blame] | 138 | self.assertEqual(f.tell(), self.LARGE + 2) |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 139 | self.assertEqual(f.seek(0, 2), self.LARGE + 1) |
| 140 | self.assertEqual(f.seek(-1, 2), self.LARGE) |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 141 | self.assertEqual(f.read(2), b"x") |
| 142 | |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 143 | def test_raw_file_io(self): |
| 144 | f = io.open(test_support.TESTFN, "wb", buffering=0) |
| 145 | self.assertEqual(f.readable(), False) |
| 146 | self.assertEqual(f.writable(), True) |
| 147 | self.assertEqual(f.seekable(), True) |
| 148 | self.write_ops(f) |
| 149 | f.close() |
| 150 | f = io.open(test_support.TESTFN, "rb", buffering=0) |
| 151 | self.assertEqual(f.readable(), True) |
| 152 | self.assertEqual(f.writable(), False) |
| 153 | self.assertEqual(f.seekable(), True) |
| 154 | self.read_ops(f) |
| 155 | f.close() |
| 156 | |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 157 | def test_buffered_file_io(self): |
| 158 | f = io.open(test_support.TESTFN, "wb") |
| 159 | self.assertEqual(f.readable(), False) |
| 160 | self.assertEqual(f.writable(), True) |
| 161 | self.assertEqual(f.seekable(), True) |
| 162 | self.write_ops(f) |
| 163 | f.close() |
| 164 | f = io.open(test_support.TESTFN, "rb") |
| 165 | self.assertEqual(f.readable(), True) |
| 166 | self.assertEqual(f.writable(), False) |
| 167 | self.assertEqual(f.seekable(), True) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 168 | self.read_ops(f, True) |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 169 | f.close() |
| 170 | |
Guido van Rossum | 48fc58a | 2007-06-07 23:45:37 +0000 | [diff] [blame^] | 171 | def test_readline(self): |
| 172 | f = io.open(test_support.TESTFN, "wb") |
| 173 | f.write(b"abc\ndef\nxyzzy\nfoo") |
| 174 | f.close() |
| 175 | f = io.open(test_support.TESTFN, "rb") |
| 176 | self.assertEqual(f.readline(), b"abc\n") |
| 177 | self.assertEqual(f.readline(10), b"def\n") |
| 178 | self.assertEqual(f.readline(2), b"xy") |
| 179 | self.assertEqual(f.readline(4), b"zzy\n") |
| 180 | self.assertEqual(f.readline(), b"foo") |
| 181 | f.close() |
| 182 | |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 183 | def test_raw_bytes_io(self): |
| 184 | f = io.BytesIO() |
| 185 | self.write_ops(f) |
| 186 | data = f.getvalue() |
| 187 | self.assertEqual(data, b"hello world\n") |
| 188 | f = io.BytesIO(data) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 189 | self.read_ops(f, True) |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 190 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 191 | def test_large_file_ops(self): |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 192 | # On Windows and Mac OSX this test comsumes large resources; It takes |
| 193 | # a long time to build the >2GB file and takes >2GB of disk space |
| 194 | # therefore the resource must be enabled to run this test. |
| 195 | if sys.platform[:3] == 'win' or sys.platform == 'darwin': |
| 196 | if not test_support.is_resource_enabled("largefile"): |
| 197 | print("\nTesting large file ops skipped on %s." % sys.platform, |
| 198 | file=sys.stderr) |
| 199 | print("It requires %d bytes and a long time." % self.LARGE, |
| 200 | file=sys.stderr) |
| 201 | print("Use 'regrtest.py -u largefile test_io' to run it.", |
| 202 | file=sys.stderr) |
| 203 | return |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 204 | f = io.open(test_support.TESTFN, "w+b", 0) |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 205 | self.large_file_ops(f) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 206 | f.close() |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 207 | f = io.open(test_support.TESTFN, "w+b") |
| 208 | self.large_file_ops(f) |
| 209 | f.close() |
| 210 | |
| 211 | def test_with_open(self): |
| 212 | for bufsize in (0, 1, 100): |
| 213 | f = None |
| 214 | with open(test_support.TESTFN, "wb", bufsize) as f: |
| 215 | f.write("xxx") |
| 216 | self.assertEqual(f.closed, True) |
| 217 | f = None |
| 218 | try: |
| 219 | with open(test_support.TESTFN, "wb", bufsize) as f: |
| 220 | 1/0 |
| 221 | except ZeroDivisionError: |
| 222 | self.assertEqual(f.closed, True) |
| 223 | else: |
| 224 | self.fail("1/0 didn't raise an exception") |
| 225 | |
| 226 | def test_destructor(self): |
| 227 | record = [] |
| 228 | class MyFileIO(io.FileIO): |
| 229 | def __del__(self): |
| 230 | record.append(1) |
| 231 | io.FileIO.__del__(self) |
| 232 | def close(self): |
| 233 | record.append(2) |
| 234 | io.FileIO.close(self) |
| 235 | def flush(self): |
| 236 | record.append(3) |
| 237 | io.FileIO.flush(self) |
| 238 | f = MyFileIO(test_support.TESTFN, "w") |
| 239 | f.write("xxx") |
| 240 | del f |
| 241 | self.assertEqual(record, [1, 2, 3]) |
| 242 | |
| 243 | def test_close_flushes(self): |
| 244 | f = io.open(test_support.TESTFN, "wb") |
Guido van Rossum | 2b08b38 | 2007-05-08 20:18:39 +0000 | [diff] [blame] | 245 | f.write(b"xxx") |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 246 | f.close() |
| 247 | f = io.open(test_support.TESTFN, "rb") |
| 248 | self.assertEqual(f.read(), b"xxx") |
| 249 | f.close() |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 250 | |
Guido van Rossum | d410395 | 2007-04-12 05:44:49 +0000 | [diff] [blame] | 251 | def test_array_writes(self): |
| 252 | a = array.array('i', range(10)) |
| 253 | n = len(buffer(a)) |
| 254 | f = io.open(test_support.TESTFN, "wb", 0) |
| 255 | self.assertEqual(f.write(a), n) |
| 256 | f.close() |
| 257 | f = io.open(test_support.TESTFN, "wb") |
| 258 | self.assertEqual(f.write(a), n) |
| 259 | f.close() |
| 260 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 261 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 262 | class MemorySeekTestMixin: |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 263 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 264 | def testInit(self): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 265 | buf = self.buftype("1234567890") |
| 266 | bytesIo = self.ioclass(buf) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 267 | |
| 268 | def testRead(self): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 269 | buf = self.buftype("1234567890") |
| 270 | bytesIo = self.ioclass(buf) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 271 | |
| 272 | self.assertEquals(buf[:1], bytesIo.read(1)) |
| 273 | self.assertEquals(buf[1:5], bytesIo.read(4)) |
| 274 | self.assertEquals(buf[5:], bytesIo.read(900)) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 275 | self.assertEquals(self.EOF, bytesIo.read()) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 276 | |
| 277 | def testReadNoArgs(self): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 278 | buf = self.buftype("1234567890") |
| 279 | bytesIo = self.ioclass(buf) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 280 | |
| 281 | self.assertEquals(buf, bytesIo.read()) |
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 testSeek(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 | bytesIo.read(5) |
| 289 | bytesIo.seek(0) |
| 290 | self.assertEquals(buf, bytesIo.read()) |
| 291 | |
| 292 | bytesIo.seek(3) |
| 293 | self.assertEquals(buf[3:], bytesIo.read()) |
| 294 | |
| 295 | def testTell(self): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 296 | buf = self.buftype("1234567890") |
| 297 | bytesIo = self.ioclass(buf) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 298 | |
| 299 | self.assertEquals(0, bytesIo.tell()) |
| 300 | bytesIo.seek(5) |
| 301 | self.assertEquals(5, bytesIo.tell()) |
| 302 | bytesIo.seek(10000) |
| 303 | self.assertEquals(10000, bytesIo.tell()) |
| 304 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 305 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 306 | class BytesIOTest(MemorySeekTestMixin, unittest.TestCase): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 307 | buftype = bytes |
| 308 | ioclass = io.BytesIO |
| 309 | EOF = b"" |
| 310 | |
| 311 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 312 | class StringIOTest(MemorySeekTestMixin, unittest.TestCase): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 313 | buftype = str |
| 314 | ioclass = io.StringIO |
| 315 | EOF = "" |
| 316 | |
| 317 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 318 | class BufferedReaderTest(unittest.TestCase): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 319 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 320 | def testRead(self): |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 321 | rawio = MockRawIO((b"abc", b"d", b"efg")) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 322 | bufio = io.BufferedReader(rawio) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 323 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 324 | self.assertEquals(b"abcdef", bufio.read(6)) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 325 | |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 326 | def testBuffering(self): |
| 327 | data = b"abcdefghi" |
| 328 | dlen = len(data) |
| 329 | |
| 330 | tests = [ |
| 331 | [ 100, [ 3, 1, 4, 8 ], [ dlen, 0 ] ], |
| 332 | [ 100, [ 3, 3, 3], [ dlen ] ], |
| 333 | [ 4, [ 1, 2, 4, 2 ], [ 4, 4, 1 ] ], |
| 334 | ] |
| 335 | |
| 336 | for bufsize, buf_read_sizes, raw_read_sizes in tests: |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 337 | rawio = MockFileIO(data) |
| 338 | bufio = io.BufferedReader(rawio, buffer_size=bufsize) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 339 | pos = 0 |
| 340 | for nbytes in buf_read_sizes: |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 341 | self.assertEquals(bufio.read(nbytes), data[pos:pos+nbytes]) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 342 | pos += nbytes |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 343 | self.assertEquals(rawio.read_history, raw_read_sizes) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 344 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 345 | def testReadNonBlocking(self): |
| 346 | # Inject some None's in there to simulate EWOULDBLOCK |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 347 | rawio = MockRawIO((b"abc", b"d", None, b"efg", None, None)) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 348 | bufio = io.BufferedReader(rawio) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 349 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 350 | self.assertEquals(b"abcd", bufio.read(6)) |
| 351 | self.assertEquals(b"e", bufio.read(1)) |
| 352 | self.assertEquals(b"fg", bufio.read()) |
| 353 | self.assert_(None is bufio.read()) |
| 354 | self.assertEquals(b"", bufio.read()) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 355 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 356 | def testReadToEof(self): |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 357 | rawio = MockRawIO((b"abc", b"d", b"efg")) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 358 | bufio = io.BufferedReader(rawio) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 359 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 360 | self.assertEquals(b"abcdefg", bufio.read(9000)) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 361 | |
| 362 | def testReadNoArgs(self): |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 363 | rawio = MockRawIO((b"abc", b"d", b"efg")) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 364 | bufio = io.BufferedReader(rawio) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 365 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 366 | self.assertEquals(b"abcdefg", bufio.read()) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 367 | |
| 368 | def testFileno(self): |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 369 | rawio = MockRawIO((b"abc", b"d", b"efg")) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 370 | bufio = io.BufferedReader(rawio) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 371 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 372 | self.assertEquals(42, bufio.fileno()) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 373 | |
| 374 | def testFilenoNoFileno(self): |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 375 | # XXX will we always have fileno() function? If so, kill |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 376 | # this test. Else, write it. |
| 377 | pass |
| 378 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 379 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 380 | class BufferedWriterTest(unittest.TestCase): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 381 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 382 | def testWrite(self): |
| 383 | # Write to the buffered IO but don't overflow the buffer. |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 384 | writer = MockRawIO() |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 385 | bufio = io.BufferedWriter(writer, 8) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 386 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 387 | bufio.write(b"abc") |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 388 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 389 | self.assertFalse(writer._write_stack) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 390 | |
| 391 | def testWriteOverflow(self): |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 392 | writer = MockRawIO() |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 393 | bufio = io.BufferedWriter(writer, 8) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 394 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 395 | bufio.write(b"abc") |
| 396 | bufio.write(b"defghijkl") |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 397 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 398 | self.assertEquals(b"abcdefghijkl", writer._write_stack[0]) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 399 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 400 | def testWriteNonBlocking(self): |
| 401 | raw = MockNonBlockWriterIO((9, 2, 22, -6, 10, 12, 12)) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 402 | bufio = io.BufferedWriter(raw, 8, 16) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 403 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 404 | bufio.write(b"asdf") |
| 405 | bufio.write(b"asdfa") |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 406 | self.assertEquals(b"asdfasdfa", raw._write_stack[0]) |
| 407 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 408 | bufio.write(b"asdfasdfasdf") |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 409 | self.assertEquals(b"asdfasdfasdf", raw._write_stack[1]) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 410 | bufio.write(b"asdfasdfasdf") |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 411 | self.assertEquals(b"dfasdfasdf", raw._write_stack[2]) |
| 412 | self.assertEquals(b"asdfasdfasdf", raw._write_stack[3]) |
| 413 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 414 | bufio.write(b"asdfasdfasdf") |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 415 | |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 416 | # XXX I don't like this test. It relies too heavily on how the |
| 417 | # algorithm actually works, which we might change. Refactor |
| 418 | # later. |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 419 | |
| 420 | def testFileno(self): |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 421 | rawio = MockRawIO((b"abc", b"d", b"efg")) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 422 | bufio = io.BufferedWriter(rawio) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 423 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 424 | self.assertEquals(42, bufio.fileno()) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 425 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 426 | def testFlush(self): |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 427 | writer = MockRawIO() |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 428 | bufio = io.BufferedWriter(writer, 8) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 429 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 430 | bufio.write(b"abc") |
| 431 | bufio.flush() |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 432 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 433 | self.assertEquals(b"abc", writer._write_stack[0]) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 434 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 435 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 436 | class BufferedRWPairTest(unittest.TestCase): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 437 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 438 | def testRWPair(self): |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 439 | r = MockRawIO(()) |
| 440 | w = MockRawIO() |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 441 | pair = io.BufferedRWPair(r, w) |
| 442 | |
| 443 | # XXX need implementation |
| 444 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 445 | |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 446 | class BufferedRandomTest(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 testReadAndWrite(self): |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 449 | raw = MockRawIO((b"asdf", b"ghjk")) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 450 | rw = io.BufferedRandom(raw, 8, 12) |
| 451 | |
| 452 | self.assertEqual(b"as", rw.read(2)) |
| 453 | rw.write(b"ddd") |
| 454 | rw.write(b"eee") |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 455 | self.assertFalse(raw._write_stack) # Buffer writes |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 456 | self.assertEqual(b"ghjk", rw.read()) # This read forces write flush |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 457 | self.assertEquals(b"dddeee", raw._write_stack[0]) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 458 | |
| 459 | def testSeekAndTell(self): |
| 460 | raw = io.BytesIO(b"asdfghjkl") |
| 461 | rw = io.BufferedRandom(raw) |
| 462 | |
| 463 | self.assertEquals(b"as", rw.read(2)) |
| 464 | self.assertEquals(2, rw.tell()) |
| 465 | rw.seek(0, 0) |
| 466 | self.assertEquals(b"asdf", rw.read(4)) |
| 467 | |
| 468 | rw.write(b"asdf") |
| 469 | rw.seek(0, 0) |
| 470 | self.assertEquals(b"asdfasdfl", rw.read()) |
| 471 | self.assertEquals(9, rw.tell()) |
| 472 | rw.seek(-4, 2) |
| 473 | self.assertEquals(5, rw.tell()) |
| 474 | rw.seek(2, 1) |
| 475 | self.assertEquals(7, rw.tell()) |
| 476 | self.assertEquals(b"fl", rw.read(11)) |
| 477 | |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 478 | |
| 479 | class TextIOWrapperTest(unittest.TestCase): |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 480 | |
Guido van Rossum | d071281 | 2007-04-11 16:32:43 +0000 | [diff] [blame] | 481 | def tearDown(self): |
| 482 | test_support.unlink(test_support.TESTFN) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 483 | |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 484 | def testNewlines(self): |
| 485 | input_lines = [ "unix\n", "windows\r\n", "os9\r", "last\n", "nonl" ] |
| 486 | |
| 487 | tests = [ |
| 488 | [ None, [ 'unix\n', 'windows\n', 'os9\n', 'last\n', 'nonl' ] ], |
| 489 | [ '\n', input_lines ], |
| 490 | [ '\r\n', input_lines ], |
| 491 | ] |
| 492 | |
Guido van Rossum | c2f93dc | 2007-05-24 00:50:02 +0000 | [diff] [blame] | 493 | encodings = ('utf-8', 'latin-1') |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 494 | |
| 495 | # Try a range of pad sizes to test the case where \r is the last |
| 496 | # character in TextIOWrapper._pending_line. |
| 497 | for encoding in encodings: |
| 498 | for do_reads in (False, True): |
| 499 | for padlen in chain(range(10), range(50, 60)): |
| 500 | pad = '.' * padlen |
| 501 | data_lines = [ pad + line for line in input_lines ] |
| 502 | # XXX: str.encode() should return bytes |
| 503 | data = bytes(''.join(data_lines).encode(encoding)) |
| 504 | |
| 505 | for newline, exp_line_ends in tests: |
| 506 | exp_lines = [ pad + line for line in exp_line_ends ] |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 507 | bufio = io.BufferedReader(io.BytesIO(data)) |
| 508 | textio = io.TextIOWrapper(bufio, newline=newline, |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 509 | encoding=encoding) |
| 510 | if do_reads: |
| 511 | got_lines = [] |
| 512 | while True: |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 513 | c2 = textio.read(2) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 514 | if c2 == '': |
| 515 | break |
| 516 | self.assertEquals(len(c2), 2) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 517 | got_lines.append(c2 + textio.readline()) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 518 | else: |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 519 | got_lines = list(textio) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 520 | |
| 521 | for got_line, exp_line in zip(got_lines, exp_lines): |
| 522 | self.assertEquals(got_line, exp_line) |
| 523 | self.assertEquals(len(got_lines), len(exp_lines)) |
| 524 | |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 525 | # Systematic tests of the text I/O API |
| 526 | |
| 527 | def testBasicIO(self): |
| 528 | for chunksize in (1, 2, 3, 4, 5, 15, 16, 17, 31, 32, 33, 63, 64, 65): |
| 529 | for enc in "ascii", "latin1", "utf8" :# , "utf-16-be", "utf-16-le": |
| 530 | f = io.open(test_support.TESTFN, "w+", encoding=enc) |
| 531 | f._CHUNK_SIZE = chunksize |
| 532 | self.assertEquals(f.write("abc"), 3) |
| 533 | f.close() |
| 534 | f = io.open(test_support.TESTFN, "r+", encoding=enc) |
| 535 | f._CHUNK_SIZE = chunksize |
| 536 | self.assertEquals(f.tell(), 0) |
| 537 | self.assertEquals(f.read(), "abc") |
| 538 | cookie = f.tell() |
| 539 | self.assertEquals(f.seek(0), 0) |
| 540 | self.assertEquals(f.read(2), "ab") |
| 541 | self.assertEquals(f.read(1), "c") |
| 542 | self.assertEquals(f.read(1), "") |
| 543 | self.assertEquals(f.read(), "") |
| 544 | self.assertEquals(f.tell(), cookie) |
| 545 | self.assertEquals(f.seek(0), 0) |
| 546 | self.assertEquals(f.seek(0, 2), cookie) |
| 547 | self.assertEquals(f.write("def"), 3) |
| 548 | self.assertEquals(f.seek(cookie), cookie) |
| 549 | self.assertEquals(f.read(), "def") |
| 550 | if enc.startswith("utf"): |
| 551 | self.multi_line_test(f, enc) |
| 552 | f.close() |
| 553 | |
| 554 | def multi_line_test(self, f, enc): |
| 555 | f.seek(0) |
| 556 | f.truncate() |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 557 | sample = "s\xff\u0fff\uffff" |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 558 | wlines = [] |
Guido van Rossum | cba608c | 2007-04-11 14:19:59 +0000 | [diff] [blame] | 559 | 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] | 560 | chars = [] |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 561 | for i in range(size): |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 562 | chars.append(sample[i % len(sample)]) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 563 | line = "".join(chars) + "\n" |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 564 | wlines.append((f.tell(), line)) |
| 565 | f.write(line) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 566 | f.seek(0) |
| 567 | rlines = [] |
| 568 | while True: |
| 569 | pos = f.tell() |
| 570 | line = f.readline() |
| 571 | if not line: |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 572 | break |
| 573 | rlines.append((pos, line)) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 574 | self.assertEquals(rlines, wlines) |
| 575 | |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 576 | def testTelling(self): |
| 577 | f = io.open(test_support.TESTFN, "w+", encoding="utf8") |
| 578 | p0 = f.tell() |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 579 | f.write("\xff\n") |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 580 | p1 = f.tell() |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 581 | f.write("\xff\n") |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 582 | p2 = f.tell() |
| 583 | f.seek(0) |
| 584 | self.assertEquals(f.tell(), p0) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 585 | self.assertEquals(f.readline(), "\xff\n") |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 586 | self.assertEquals(f.tell(), p1) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 587 | self.assertEquals(f.readline(), "\xff\n") |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 588 | self.assertEquals(f.tell(), p2) |
| 589 | f.seek(0) |
| 590 | for line in f: |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 591 | self.assertEquals(line, "\xff\n") |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 592 | self.assertRaises(IOError, f.tell) |
| 593 | self.assertEquals(f.tell(), p2) |
| 594 | f.close() |
| 595 | |
Guido van Rossum | d76e779 | 2007-04-17 02:38:04 +0000 | [diff] [blame] | 596 | def testSeeking(self): |
| 597 | chunk_size = io.TextIOWrapper._CHUNK_SIZE |
| 598 | prefix_size = chunk_size - 2 |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 599 | u_prefix = "a" * prefix_size |
Guido van Rossum | d76e779 | 2007-04-17 02:38:04 +0000 | [diff] [blame] | 600 | prefix = bytes(u_prefix.encode("utf-8")) |
| 601 | self.assertEquals(len(u_prefix), len(prefix)) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 602 | u_suffix = "\u8888\n" |
Guido van Rossum | d76e779 | 2007-04-17 02:38:04 +0000 | [diff] [blame] | 603 | suffix = bytes(u_suffix.encode("utf-8")) |
| 604 | line = prefix + suffix |
| 605 | f = io.open(test_support.TESTFN, "wb") |
| 606 | f.write(line*2) |
| 607 | f.close() |
| 608 | f = io.open(test_support.TESTFN, "r", encoding="utf-8") |
| 609 | s = f.read(prefix_size) |
Guido van Rossum | 2b08b38 | 2007-05-08 20:18:39 +0000 | [diff] [blame] | 610 | self.assertEquals(s, str(prefix)) |
Guido van Rossum | d76e779 | 2007-04-17 02:38:04 +0000 | [diff] [blame] | 611 | self.assertEquals(f.tell(), prefix_size) |
| 612 | self.assertEquals(f.readline(), u_suffix) |
| 613 | |
| 614 | def testSeekingToo(self): |
| 615 | # Regression test for a specific bug |
| 616 | data = b'\xe0\xbf\xbf\n' |
| 617 | f = io.open(test_support.TESTFN, "wb") |
| 618 | f.write(data) |
| 619 | f.close() |
| 620 | f = io.open(test_support.TESTFN, "r", encoding="utf-8") |
| 621 | f._CHUNK_SIZE # Just test that it exists |
| 622 | f._CHUNK_SIZE = 2 |
| 623 | f.readline() |
| 624 | f.tell() |
| 625 | |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 626 | def timingTest(self): |
| 627 | timer = time.time |
| 628 | enc = "utf8" |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 629 | line = "\0\x0f\xff\u0fff\uffff\U000fffff\U0010ffff"*3 + "\n" |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 630 | nlines = 10000 |
| 631 | nchars = len(line) |
| 632 | nbytes = len(line.encode(enc)) |
| 633 | for chunk_size in (32, 64, 128, 256): |
| 634 | f = io.open(test_support.TESTFN, "w+", encoding=enc) |
| 635 | f._CHUNK_SIZE = chunk_size |
| 636 | t0 = timer() |
| 637 | for i in range(nlines): |
| 638 | f.write(line) |
| 639 | f.flush() |
| 640 | t1 = timer() |
| 641 | f.seek(0) |
| 642 | for line in f: |
| 643 | pass |
| 644 | t2 = timer() |
| 645 | f.seek(0) |
| 646 | while f.readline(): |
| 647 | pass |
| 648 | t3 = timer() |
| 649 | f.seek(0) |
| 650 | while f.readline(): |
| 651 | f.tell() |
| 652 | t4 = timer() |
| 653 | f.close() |
| 654 | if test_support.verbose: |
| 655 | print("\nTiming test: %d lines of %d characters (%d bytes)" % |
| 656 | (nlines, nchars, nbytes)) |
| 657 | print("File chunk size: %6s" % f._CHUNK_SIZE) |
| 658 | print("Writing: %6.3f seconds" % (t1-t0)) |
| 659 | print("Reading using iteration: %6.3f seconds" % (t2-t1)) |
| 660 | print("Reading using readline(): %6.3f seconds" % (t3-t2)) |
| 661 | print("Using readline()+tell(): %6.3f seconds" % (t4-t3)) |
| 662 | |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 663 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 664 | # XXX Tests for open() |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 665 | |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 666 | def test_main(): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 667 | test_support.run_unittest(IOTest, BytesIOTest, StringIOTest, |
| 668 | BufferedReaderTest, |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 669 | BufferedWriterTest, BufferedRWPairTest, |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 670 | BufferedRandomTest, TextIOWrapperTest) |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 671 | |
| 672 | if __name__ == "__main__": |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 673 | unittest.main() |