Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1 | """Unit tests for the io module.""" |
| 2 | |
| 3 | # Tests of io are scattered over the test suite: |
| 4 | # * test_bufio - tests file buffering |
| 5 | # * test_memoryio - tests BytesIO and StringIO |
| 6 | # * test_fileio - tests FileIO |
| 7 | # * test_file - tests the file interface |
| 8 | # * test_io - tests everything else in the io module |
| 9 | # * test_univnewlines - tests universal newline support |
| 10 | # * test_largefile - tests operations on a file greater than 2**32 bytes |
| 11 | # (only enabled with -ulargefile) |
| 12 | |
| 13 | ################################################################################ |
| 14 | # ATTENTION TEST WRITERS!!! |
| 15 | ################################################################################ |
| 16 | # When writing tests for io, it's important to test both the C and Python |
| 17 | # implementations. This is usually done by writing a base test that refers to |
| 18 | # the type it is testing as a attribute. Then it provides custom subclasses to |
| 19 | # test both implementations. This file has lots of examples. |
| 20 | ################################################################################ |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 21 | |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 22 | import os |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 23 | import sys |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 24 | import time |
Guido van Rossum | d410395 | 2007-04-12 05:44:49 +0000 | [diff] [blame] | 25 | import array |
Antoine Pitrou | 8769576 | 2008-08-14 22:44:29 +0000 | [diff] [blame] | 26 | import random |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 27 | import unittest |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 28 | import weakref |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 29 | import abc |
Antoine Pitrou | b46b9d5 | 2010-08-21 19:09:32 +0000 | [diff] [blame] | 30 | import signal |
| 31 | import errno |
Antoine Pitrou | e033e06 | 2010-10-29 10:38:18 +0000 | [diff] [blame] | 32 | import warnings |
Antoine Pitrou | 243757e | 2010-11-05 21:15:39 +0000 | [diff] [blame] | 33 | import pickle |
Georg Brandl | 1b37e87 | 2010-03-14 10:45:50 +0000 | [diff] [blame] | 34 | from itertools import cycle, count |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 35 | from collections import deque |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 36 | from test import support |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 37 | |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 38 | import codecs |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 39 | import io # C implementation of io |
| 40 | import _pyio as pyio # Python implementation of io |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 41 | try: |
| 42 | import threading |
| 43 | except ImportError: |
| 44 | threading = None |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 46 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 47 | def _default_chunk_size(): |
| 48 | """Get the default TextIOWrapper chunk size""" |
| 49 | with open(__file__, "r", encoding="latin1") as f: |
| 50 | return f._CHUNK_SIZE |
| 51 | |
| 52 | |
Antoine Pitrou | 328ec74 | 2010-09-14 18:37:24 +0000 | [diff] [blame] | 53 | class MockRawIOWithoutRead: |
| 54 | """A RawIO implementation without read(), so as to exercise the default |
| 55 | RawIO.read() which calls readinto().""" |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 57 | def __init__(self, read_stack=()): |
| 58 | self._read_stack = list(read_stack) |
| 59 | self._write_stack = [] |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 60 | self._reads = 0 |
Antoine Pitrou | 32cfede | 2010-08-11 13:31:33 +0000 | [diff] [blame] | 61 | self._extraneous_reads = 0 |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 62 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 63 | def write(self, b): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 64 | self._write_stack.append(bytes(b)) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 65 | return len(b) |
| 66 | |
| 67 | def writable(self): |
| 68 | return True |
| 69 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 70 | def fileno(self): |
| 71 | return 42 |
| 72 | |
| 73 | def readable(self): |
| 74 | return True |
| 75 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 76 | def seekable(self): |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 77 | return True |
| 78 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 79 | def seek(self, pos, whence): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 80 | return 0 # wrong but we gotta return something |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 81 | |
| 82 | def tell(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 83 | return 0 # same comment as above |
| 84 | |
| 85 | def readinto(self, buf): |
| 86 | self._reads += 1 |
| 87 | max_len = len(buf) |
| 88 | try: |
| 89 | data = self._read_stack[0] |
| 90 | except IndexError: |
Antoine Pitrou | 32cfede | 2010-08-11 13:31:33 +0000 | [diff] [blame] | 91 | self._extraneous_reads += 1 |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 92 | return 0 |
| 93 | if data is None: |
| 94 | del self._read_stack[0] |
| 95 | return None |
| 96 | n = len(data) |
| 97 | if len(data) <= max_len: |
| 98 | del self._read_stack[0] |
| 99 | buf[:n] = data |
| 100 | return n |
| 101 | else: |
| 102 | buf[:] = data[:max_len] |
| 103 | self._read_stack[0] = data[max_len:] |
| 104 | return max_len |
| 105 | |
| 106 | def truncate(self, pos=None): |
| 107 | return pos |
| 108 | |
Antoine Pitrou | 328ec74 | 2010-09-14 18:37:24 +0000 | [diff] [blame] | 109 | class CMockRawIOWithoutRead(MockRawIOWithoutRead, io.RawIOBase): |
| 110 | pass |
| 111 | |
| 112 | class PyMockRawIOWithoutRead(MockRawIOWithoutRead, pyio.RawIOBase): |
| 113 | pass |
| 114 | |
| 115 | |
| 116 | class MockRawIO(MockRawIOWithoutRead): |
| 117 | |
| 118 | def read(self, n=None): |
| 119 | self._reads += 1 |
| 120 | try: |
| 121 | return self._read_stack.pop(0) |
| 122 | except: |
| 123 | self._extraneous_reads += 1 |
| 124 | return b"" |
| 125 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 126 | class CMockRawIO(MockRawIO, io.RawIOBase): |
| 127 | pass |
| 128 | |
| 129 | class PyMockRawIO(MockRawIO, pyio.RawIOBase): |
| 130 | pass |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 131 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 132 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 133 | class MisbehavedRawIO(MockRawIO): |
| 134 | def write(self, b): |
| 135 | return super().write(b) * 2 |
| 136 | |
| 137 | def read(self, n=None): |
| 138 | return super().read(n) * 2 |
| 139 | |
| 140 | def seek(self, pos, whence): |
| 141 | return -123 |
| 142 | |
| 143 | def tell(self): |
| 144 | return -456 |
| 145 | |
| 146 | def readinto(self, buf): |
| 147 | super().readinto(buf) |
| 148 | return len(buf) * 5 |
| 149 | |
| 150 | class CMisbehavedRawIO(MisbehavedRawIO, io.RawIOBase): |
| 151 | pass |
| 152 | |
| 153 | class PyMisbehavedRawIO(MisbehavedRawIO, pyio.RawIOBase): |
| 154 | pass |
| 155 | |
| 156 | |
| 157 | class CloseFailureIO(MockRawIO): |
| 158 | closed = 0 |
| 159 | |
| 160 | def close(self): |
| 161 | if not self.closed: |
| 162 | self.closed = 1 |
| 163 | raise IOError |
| 164 | |
| 165 | class CCloseFailureIO(CloseFailureIO, io.RawIOBase): |
| 166 | pass |
| 167 | |
| 168 | class PyCloseFailureIO(CloseFailureIO, pyio.RawIOBase): |
| 169 | pass |
| 170 | |
| 171 | |
| 172 | class MockFileIO: |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 173 | |
| 174 | def __init__(self, data): |
| 175 | self.read_history = [] |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 176 | super().__init__(data) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 177 | |
| 178 | def read(self, n=None): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 179 | res = super().read(n) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 180 | self.read_history.append(None if res is None else len(res)) |
| 181 | return res |
| 182 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 183 | def readinto(self, b): |
| 184 | res = super().readinto(b) |
| 185 | self.read_history.append(res) |
| 186 | return res |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 187 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 188 | class CMockFileIO(MockFileIO, io.BytesIO): |
| 189 | pass |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 190 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 191 | class PyMockFileIO(MockFileIO, pyio.BytesIO): |
| 192 | pass |
| 193 | |
| 194 | |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 195 | class MockUnseekableIO: |
| 196 | def seekable(self): |
| 197 | return False |
| 198 | |
| 199 | def seek(self, *args): |
| 200 | raise self.UnsupportedOperation("not seekable") |
| 201 | |
| 202 | def tell(self, *args): |
| 203 | raise self.UnsupportedOperation("not seekable") |
| 204 | |
| 205 | class CMockUnseekableIO(MockUnseekableIO, io.BytesIO): |
| 206 | UnsupportedOperation = io.UnsupportedOperation |
| 207 | |
| 208 | class PyMockUnseekableIO(MockUnseekableIO, pyio.BytesIO): |
| 209 | UnsupportedOperation = pyio.UnsupportedOperation |
| 210 | |
| 211 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 212 | class MockNonBlockWriterIO: |
| 213 | |
| 214 | def __init__(self): |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 215 | self._write_stack = [] |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 216 | self._blocker_char = None |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 217 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 218 | def pop_written(self): |
| 219 | s = b"".join(self._write_stack) |
| 220 | self._write_stack[:] = [] |
| 221 | return s |
| 222 | |
| 223 | def block_on(self, char): |
| 224 | """Block when a given char is encountered.""" |
| 225 | self._blocker_char = char |
| 226 | |
| 227 | def readable(self): |
| 228 | return True |
| 229 | |
| 230 | def seekable(self): |
| 231 | return True |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 232 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 233 | def writable(self): |
| 234 | return True |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 235 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 236 | def write(self, b): |
| 237 | b = bytes(b) |
| 238 | n = -1 |
| 239 | if self._blocker_char: |
| 240 | try: |
| 241 | n = b.index(self._blocker_char) |
| 242 | except ValueError: |
| 243 | pass |
| 244 | else: |
| 245 | self._blocker_char = None |
| 246 | self._write_stack.append(b[:n]) |
| 247 | raise self.BlockingIOError(0, "test blocking", n) |
| 248 | self._write_stack.append(b) |
| 249 | return len(b) |
| 250 | |
| 251 | class CMockNonBlockWriterIO(MockNonBlockWriterIO, io.RawIOBase): |
| 252 | BlockingIOError = io.BlockingIOError |
| 253 | |
| 254 | class PyMockNonBlockWriterIO(MockNonBlockWriterIO, pyio.RawIOBase): |
| 255 | BlockingIOError = pyio.BlockingIOError |
| 256 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 257 | |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 258 | class IOTest(unittest.TestCase): |
| 259 | |
Neal Norwitz | e7789b1 | 2008-03-24 06:18:09 +0000 | [diff] [blame] | 260 | def setUp(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 261 | support.unlink(support.TESTFN) |
Neal Norwitz | e7789b1 | 2008-03-24 06:18:09 +0000 | [diff] [blame] | 262 | |
Guido van Rossum | 4d0f5a4 | 2007-03-07 22:59:39 +0000 | [diff] [blame] | 263 | def tearDown(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 264 | support.unlink(support.TESTFN) |
Guido van Rossum | 4d0f5a4 | 2007-03-07 22:59:39 +0000 | [diff] [blame] | 265 | |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 266 | def write_ops(self, f): |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 267 | self.assertEqual(f.write(b"blah."), 5) |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 268 | f.truncate(0) |
| 269 | self.assertEqual(f.tell(), 5) |
| 270 | f.seek(0) |
| 271 | |
| 272 | self.assertEqual(f.write(b"blah."), 5) |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 273 | self.assertEqual(f.seek(0), 0) |
| 274 | self.assertEqual(f.write(b"Hello."), 6) |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 275 | self.assertEqual(f.tell(), 6) |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 276 | self.assertEqual(f.seek(-1, 1), 5) |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 277 | self.assertEqual(f.tell(), 5) |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 278 | self.assertEqual(f.write(bytearray(b" world\n\n\n")), 9) |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 279 | self.assertEqual(f.seek(0), 0) |
Guido van Rossum | 2b08b38 | 2007-05-08 20:18:39 +0000 | [diff] [blame] | 280 | self.assertEqual(f.write(b"h"), 1) |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 281 | self.assertEqual(f.seek(-1, 2), 13) |
| 282 | self.assertEqual(f.tell(), 13) |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 283 | |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 284 | self.assertEqual(f.truncate(12), 12) |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 285 | self.assertEqual(f.tell(), 13) |
Christian Heimes | 8e42a0a | 2007-11-08 18:04:45 +0000 | [diff] [blame] | 286 | self.assertRaises(TypeError, f.seek, 0.0) |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 287 | |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 288 | def read_ops(self, f, buffered=False): |
| 289 | data = f.read(5) |
| 290 | self.assertEqual(data, b"hello") |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 291 | data = bytearray(data) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 292 | self.assertEqual(f.readinto(data), 5) |
| 293 | self.assertEqual(data, b" worl") |
| 294 | self.assertEqual(f.readinto(data), 2) |
| 295 | self.assertEqual(len(data), 5) |
| 296 | self.assertEqual(data[:2], b"d\n") |
| 297 | self.assertEqual(f.seek(0), 0) |
| 298 | self.assertEqual(f.read(20), b"hello world\n") |
| 299 | self.assertEqual(f.read(1), b"") |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 300 | self.assertEqual(f.readinto(bytearray(b"x")), 0) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 301 | self.assertEqual(f.seek(-6, 2), 6) |
| 302 | self.assertEqual(f.read(5), b"world") |
| 303 | self.assertEqual(f.read(0), b"") |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 304 | self.assertEqual(f.readinto(bytearray()), 0) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 305 | self.assertEqual(f.seek(-6, 1), 5) |
| 306 | self.assertEqual(f.read(5), b" worl") |
| 307 | self.assertEqual(f.tell(), 10) |
Christian Heimes | 8e42a0a | 2007-11-08 18:04:45 +0000 | [diff] [blame] | 308 | self.assertRaises(TypeError, f.seek, 0.0) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 309 | if buffered: |
| 310 | f.seek(0) |
| 311 | self.assertEqual(f.read(), b"hello world\n") |
| 312 | f.seek(6) |
| 313 | self.assertEqual(f.read(), b"world\n") |
| 314 | self.assertEqual(f.read(), b"") |
| 315 | |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 316 | LARGE = 2**31 |
| 317 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 318 | def large_file_ops(self, f): |
| 319 | assert f.readable() |
| 320 | assert f.writable() |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 321 | self.assertEqual(f.seek(self.LARGE), self.LARGE) |
| 322 | self.assertEqual(f.tell(), self.LARGE) |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 323 | self.assertEqual(f.write(b"xxx"), 3) |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 324 | self.assertEqual(f.tell(), self.LARGE + 3) |
| 325 | self.assertEqual(f.seek(-1, 1), self.LARGE + 2) |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 326 | self.assertEqual(f.truncate(), self.LARGE + 2) |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 327 | self.assertEqual(f.tell(), self.LARGE + 2) |
| 328 | self.assertEqual(f.seek(0, 2), self.LARGE + 2) |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 329 | self.assertEqual(f.truncate(self.LARGE + 1), self.LARGE + 1) |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 330 | self.assertEqual(f.tell(), self.LARGE + 2) |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 331 | self.assertEqual(f.seek(0, 2), self.LARGE + 1) |
| 332 | self.assertEqual(f.seek(-1, 2), self.LARGE) |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 333 | self.assertEqual(f.read(2), b"x") |
| 334 | |
Benjamin Peterson | 81971ea | 2009-05-14 22:01:31 +0000 | [diff] [blame] | 335 | def test_invalid_operations(self): |
| 336 | # Try writing on a file opened in read mode and vice-versa. |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 337 | exc = self.UnsupportedOperation |
Benjamin Peterson | 81971ea | 2009-05-14 22:01:31 +0000 | [diff] [blame] | 338 | for mode in ("w", "wb"): |
Hirokazu Yamamoto | c7d6aa4 | 2009-06-18 00:07:14 +0000 | [diff] [blame] | 339 | with self.open(support.TESTFN, mode) as fp: |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 340 | self.assertRaises(exc, fp.read) |
| 341 | self.assertRaises(exc, fp.readline) |
| 342 | with self.open(support.TESTFN, "wb", buffering=0) as fp: |
| 343 | self.assertRaises(exc, fp.read) |
| 344 | self.assertRaises(exc, fp.readline) |
| 345 | with self.open(support.TESTFN, "rb", buffering=0) as fp: |
| 346 | self.assertRaises(exc, fp.write, b"blah") |
| 347 | self.assertRaises(exc, fp.writelines, [b"blah\n"]) |
Hirokazu Yamamoto | c7d6aa4 | 2009-06-18 00:07:14 +0000 | [diff] [blame] | 348 | with self.open(support.TESTFN, "rb") as fp: |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 349 | self.assertRaises(exc, fp.write, b"blah") |
| 350 | self.assertRaises(exc, fp.writelines, [b"blah\n"]) |
Hirokazu Yamamoto | c7d6aa4 | 2009-06-18 00:07:14 +0000 | [diff] [blame] | 351 | with self.open(support.TESTFN, "r") as fp: |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 352 | self.assertRaises(exc, fp.write, "blah") |
| 353 | self.assertRaises(exc, fp.writelines, ["blah\n"]) |
| 354 | # Non-zero seeking from current or end pos |
| 355 | self.assertRaises(exc, fp.seek, 1, self.SEEK_CUR) |
| 356 | self.assertRaises(exc, fp.seek, -1, self.SEEK_END) |
Benjamin Peterson | 81971ea | 2009-05-14 22:01:31 +0000 | [diff] [blame] | 357 | |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 358 | def test_raw_file_io(self): |
Benjamin Peterson | 45cec32 | 2009-04-24 23:14:50 +0000 | [diff] [blame] | 359 | with self.open(support.TESTFN, "wb", buffering=0) as f: |
| 360 | self.assertEqual(f.readable(), False) |
| 361 | self.assertEqual(f.writable(), True) |
| 362 | self.assertEqual(f.seekable(), True) |
| 363 | self.write_ops(f) |
| 364 | with self.open(support.TESTFN, "rb", buffering=0) as f: |
| 365 | self.assertEqual(f.readable(), True) |
| 366 | self.assertEqual(f.writable(), False) |
| 367 | self.assertEqual(f.seekable(), True) |
| 368 | self.read_ops(f) |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 369 | |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 370 | def test_buffered_file_io(self): |
Benjamin Peterson | 45cec32 | 2009-04-24 23:14:50 +0000 | [diff] [blame] | 371 | with self.open(support.TESTFN, "wb") as f: |
| 372 | self.assertEqual(f.readable(), False) |
| 373 | self.assertEqual(f.writable(), True) |
| 374 | self.assertEqual(f.seekable(), True) |
| 375 | self.write_ops(f) |
| 376 | with self.open(support.TESTFN, "rb") as f: |
| 377 | self.assertEqual(f.readable(), True) |
| 378 | self.assertEqual(f.writable(), False) |
| 379 | self.assertEqual(f.seekable(), True) |
| 380 | self.read_ops(f, True) |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 381 | |
Guido van Rossum | 48fc58a | 2007-06-07 23:45:37 +0000 | [diff] [blame] | 382 | def test_readline(self): |
Benjamin Peterson | 45cec32 | 2009-04-24 23:14:50 +0000 | [diff] [blame] | 383 | with self.open(support.TESTFN, "wb") as f: |
| 384 | f.write(b"abc\ndef\nxyzzy\nfoo\x00bar\nanother line") |
| 385 | with self.open(support.TESTFN, "rb") as f: |
| 386 | self.assertEqual(f.readline(), b"abc\n") |
| 387 | self.assertEqual(f.readline(10), b"def\n") |
| 388 | self.assertEqual(f.readline(2), b"xy") |
| 389 | self.assertEqual(f.readline(4), b"zzy\n") |
| 390 | self.assertEqual(f.readline(), b"foo\x00bar\n") |
Benjamin Peterson | bf5ff76 | 2009-12-13 19:25:34 +0000 | [diff] [blame] | 391 | self.assertEqual(f.readline(None), b"another line") |
Benjamin Peterson | 45cec32 | 2009-04-24 23:14:50 +0000 | [diff] [blame] | 392 | self.assertRaises(TypeError, f.readline, 5.3) |
| 393 | with self.open(support.TESTFN, "r") as f: |
| 394 | self.assertRaises(TypeError, f.readline, 5.3) |
Guido van Rossum | 48fc58a | 2007-06-07 23:45:37 +0000 | [diff] [blame] | 395 | |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 396 | def test_raw_bytes_io(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 397 | f = self.BytesIO() |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 398 | self.write_ops(f) |
| 399 | data = f.getvalue() |
| 400 | self.assertEqual(data, b"hello world\n") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 401 | f = self.BytesIO(data) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 402 | self.read_ops(f, True) |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 403 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 404 | def test_large_file_ops(self): |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 405 | # On Windows and Mac OSX this test comsumes large resources; It takes |
| 406 | # a long time to build the >2GB file and takes >2GB of disk space |
| 407 | # therefore the resource must be enabled to run this test. |
| 408 | if sys.platform[:3] == 'win' or sys.platform == 'darwin': |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 409 | if not support.is_resource_enabled("largefile"): |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 410 | print("\nTesting large file ops skipped on %s." % sys.platform, |
| 411 | file=sys.stderr) |
| 412 | print("It requires %d bytes and a long time." % self.LARGE, |
| 413 | file=sys.stderr) |
| 414 | print("Use 'regrtest.py -u largefile test_io' to run it.", |
| 415 | file=sys.stderr) |
| 416 | return |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 417 | with self.open(support.TESTFN, "w+b", 0) as f: |
| 418 | self.large_file_ops(f) |
| 419 | with self.open(support.TESTFN, "w+b") as f: |
| 420 | self.large_file_ops(f) |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 421 | |
| 422 | def test_with_open(self): |
| 423 | for bufsize in (0, 1, 100): |
| 424 | f = None |
Hirokazu Yamamoto | c7d6aa4 | 2009-06-18 00:07:14 +0000 | [diff] [blame] | 425 | with self.open(support.TESTFN, "wb", bufsize) as f: |
Guido van Rossum | 1f2ca56 | 2007-08-27 20:44:15 +0000 | [diff] [blame] | 426 | f.write(b"xxx") |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 427 | self.assertEqual(f.closed, True) |
| 428 | f = None |
| 429 | try: |
Hirokazu Yamamoto | c7d6aa4 | 2009-06-18 00:07:14 +0000 | [diff] [blame] | 430 | with self.open(support.TESTFN, "wb", bufsize) as f: |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 431 | 1/0 |
| 432 | except ZeroDivisionError: |
| 433 | self.assertEqual(f.closed, True) |
| 434 | else: |
| 435 | self.fail("1/0 didn't raise an exception") |
| 436 | |
Antoine Pitrou | 08838b6 | 2009-01-21 00:55:13 +0000 | [diff] [blame] | 437 | # issue 5008 |
| 438 | def test_append_mode_tell(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 439 | with self.open(support.TESTFN, "wb") as f: |
Antoine Pitrou | 08838b6 | 2009-01-21 00:55:13 +0000 | [diff] [blame] | 440 | f.write(b"xxx") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 441 | with self.open(support.TESTFN, "ab", buffering=0) as f: |
Antoine Pitrou | 08838b6 | 2009-01-21 00:55:13 +0000 | [diff] [blame] | 442 | self.assertEqual(f.tell(), 3) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 443 | with self.open(support.TESTFN, "ab") as f: |
Antoine Pitrou | 08838b6 | 2009-01-21 00:55:13 +0000 | [diff] [blame] | 444 | self.assertEqual(f.tell(), 3) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 445 | with self.open(support.TESTFN, "a") as f: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 446 | self.assertTrue(f.tell() > 0) |
Antoine Pitrou | 08838b6 | 2009-01-21 00:55:13 +0000 | [diff] [blame] | 447 | |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 448 | def test_destructor(self): |
| 449 | record = [] |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 450 | class MyFileIO(self.FileIO): |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 451 | def __del__(self): |
| 452 | record.append(1) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 453 | try: |
| 454 | f = super().__del__ |
| 455 | except AttributeError: |
| 456 | pass |
| 457 | else: |
| 458 | f() |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 459 | def close(self): |
| 460 | record.append(2) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 461 | super().close() |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 462 | def flush(self): |
| 463 | record.append(3) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 464 | super().flush() |
Brett Cannon | 5a9e91b | 2010-10-29 23:53:03 +0000 | [diff] [blame] | 465 | with support.check_warnings(('', ResourceWarning)): |
| 466 | f = MyFileIO(support.TESTFN, "wb") |
| 467 | f.write(b"xxx") |
| 468 | del f |
| 469 | support.gc_collect() |
| 470 | self.assertEqual(record, [1, 2, 3]) |
| 471 | with self.open(support.TESTFN, "rb") as f: |
| 472 | self.assertEqual(f.read(), b"xxx") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 473 | |
| 474 | def _check_base_destructor(self, base): |
| 475 | record = [] |
| 476 | class MyIO(base): |
| 477 | def __init__(self): |
| 478 | # This exercises the availability of attributes on object |
| 479 | # destruction. |
| 480 | # (in the C version, close() is called by the tp_dealloc |
| 481 | # function, not by __del__) |
| 482 | self.on_del = 1 |
| 483 | self.on_close = 2 |
| 484 | self.on_flush = 3 |
| 485 | def __del__(self): |
| 486 | record.append(self.on_del) |
| 487 | try: |
| 488 | f = super().__del__ |
| 489 | except AttributeError: |
| 490 | pass |
| 491 | else: |
| 492 | f() |
| 493 | def close(self): |
| 494 | record.append(self.on_close) |
| 495 | super().close() |
| 496 | def flush(self): |
| 497 | record.append(self.on_flush) |
| 498 | super().flush() |
| 499 | f = MyIO() |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 500 | del f |
Benjamin Peterson | 24fb1d0 | 2009-04-24 23:26:21 +0000 | [diff] [blame] | 501 | support.gc_collect() |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 502 | self.assertEqual(record, [1, 2, 3]) |
| 503 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 504 | def test_IOBase_destructor(self): |
| 505 | self._check_base_destructor(self.IOBase) |
| 506 | |
| 507 | def test_RawIOBase_destructor(self): |
| 508 | self._check_base_destructor(self.RawIOBase) |
| 509 | |
| 510 | def test_BufferedIOBase_destructor(self): |
| 511 | self._check_base_destructor(self.BufferedIOBase) |
| 512 | |
| 513 | def test_TextIOBase_destructor(self): |
| 514 | self._check_base_destructor(self.TextIOBase) |
| 515 | |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 516 | def test_close_flushes(self): |
Benjamin Peterson | 45cec32 | 2009-04-24 23:14:50 +0000 | [diff] [blame] | 517 | with self.open(support.TESTFN, "wb") as f: |
| 518 | f.write(b"xxx") |
| 519 | with self.open(support.TESTFN, "rb") as f: |
| 520 | self.assertEqual(f.read(), b"xxx") |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 521 | |
Guido van Rossum | d410395 | 2007-04-12 05:44:49 +0000 | [diff] [blame] | 522 | def test_array_writes(self): |
| 523 | a = array.array('i', range(10)) |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 524 | n = len(a.tobytes()) |
Benjamin Peterson | 45cec32 | 2009-04-24 23:14:50 +0000 | [diff] [blame] | 525 | with self.open(support.TESTFN, "wb", 0) as f: |
| 526 | self.assertEqual(f.write(a), n) |
| 527 | with self.open(support.TESTFN, "wb") as f: |
| 528 | self.assertEqual(f.write(a), n) |
Guido van Rossum | d410395 | 2007-04-12 05:44:49 +0000 | [diff] [blame] | 529 | |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 530 | def test_closefd(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 531 | self.assertRaises(ValueError, self.open, support.TESTFN, 'w', |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 532 | closefd=False) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 533 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 534 | def test_read_closed(self): |
| 535 | with self.open(support.TESTFN, "w") as f: |
Christian Heimes | ecc42a2 | 2008-11-05 19:30:32 +0000 | [diff] [blame] | 536 | f.write("egg\n") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 537 | with self.open(support.TESTFN, "r") as f: |
| 538 | file = self.open(f.fileno(), "r", closefd=False) |
Christian Heimes | ecc42a2 | 2008-11-05 19:30:32 +0000 | [diff] [blame] | 539 | self.assertEqual(file.read(), "egg\n") |
| 540 | file.seek(0) |
| 541 | file.close() |
| 542 | self.assertRaises(ValueError, file.read) |
| 543 | |
| 544 | def test_no_closefd_with_filename(self): |
| 545 | # can't use closefd in combination with a file name |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 546 | self.assertRaises(ValueError, self.open, support.TESTFN, "r", closefd=False) |
Christian Heimes | ecc42a2 | 2008-11-05 19:30:32 +0000 | [diff] [blame] | 547 | |
| 548 | def test_closefd_attr(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 549 | with self.open(support.TESTFN, "wb") as f: |
Christian Heimes | ecc42a2 | 2008-11-05 19:30:32 +0000 | [diff] [blame] | 550 | f.write(b"egg\n") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 551 | with self.open(support.TESTFN, "r") as f: |
Christian Heimes | ecc42a2 | 2008-11-05 19:30:32 +0000 | [diff] [blame] | 552 | self.assertEqual(f.buffer.raw.closefd, True) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 553 | file = self.open(f.fileno(), "r", closefd=False) |
Christian Heimes | ecc42a2 | 2008-11-05 19:30:32 +0000 | [diff] [blame] | 554 | self.assertEqual(file.buffer.raw.closefd, False) |
| 555 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 556 | def test_garbage_collection(self): |
| 557 | # FileIO objects are collected, and collecting them flushes |
| 558 | # all data to disk. |
Benjamin Peterson | ebe5d8a | 2010-10-31 01:30:11 +0000 | [diff] [blame] | 559 | with support.check_warnings(('', ResourceWarning)): |
| 560 | f = self.FileIO(support.TESTFN, "wb") |
| 561 | f.write(b"abcxxx") |
| 562 | f.f = f |
| 563 | wr = weakref.ref(f) |
| 564 | del f |
| 565 | support.gc_collect() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 566 | self.assertTrue(wr() is None, wr) |
Hirokazu Yamamoto | c7d6aa4 | 2009-06-18 00:07:14 +0000 | [diff] [blame] | 567 | with self.open(support.TESTFN, "rb") as f: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 568 | self.assertEqual(f.read(), b"abcxxx") |
Christian Heimes | ecc42a2 | 2008-11-05 19:30:32 +0000 | [diff] [blame] | 569 | |
Antoine Pitrou | 7d037a7 | 2009-03-29 18:55:12 +0000 | [diff] [blame] | 570 | def test_unbounded_file(self): |
| 571 | # Issue #1174606: reading from an unbounded stream such as /dev/zero. |
| 572 | zero = "/dev/zero" |
| 573 | if not os.path.exists(zero): |
Antoine Pitrou | c50cb8e | 2009-04-19 00:10:36 +0000 | [diff] [blame] | 574 | self.skipTest("{0} does not exist".format(zero)) |
Antoine Pitrou | 7d037a7 | 2009-03-29 18:55:12 +0000 | [diff] [blame] | 575 | if sys.maxsize > 0x7FFFFFFF: |
Antoine Pitrou | c50cb8e | 2009-04-19 00:10:36 +0000 | [diff] [blame] | 576 | self.skipTest("test can only run in a 32-bit address space") |
Antoine Pitrou | 7d037a7 | 2009-03-29 18:55:12 +0000 | [diff] [blame] | 577 | if support.real_max_memuse < support._2G: |
Antoine Pitrou | c50cb8e | 2009-04-19 00:10:36 +0000 | [diff] [blame] | 578 | self.skipTest("test requires at least 2GB of memory") |
Hirokazu Yamamoto | c7d6aa4 | 2009-06-18 00:07:14 +0000 | [diff] [blame] | 579 | with self.open(zero, "rb", buffering=0) as f: |
Antoine Pitrou | 7d037a7 | 2009-03-29 18:55:12 +0000 | [diff] [blame] | 580 | self.assertRaises(OverflowError, f.read) |
Hirokazu Yamamoto | c7d6aa4 | 2009-06-18 00:07:14 +0000 | [diff] [blame] | 581 | with self.open(zero, "rb") as f: |
Antoine Pitrou | 7d037a7 | 2009-03-29 18:55:12 +0000 | [diff] [blame] | 582 | self.assertRaises(OverflowError, f.read) |
Hirokazu Yamamoto | c7d6aa4 | 2009-06-18 00:07:14 +0000 | [diff] [blame] | 583 | with self.open(zero, "r") as f: |
Antoine Pitrou | 7d037a7 | 2009-03-29 18:55:12 +0000 | [diff] [blame] | 584 | self.assertRaises(OverflowError, f.read) |
| 585 | |
Antoine Pitrou | 6be8876 | 2010-05-03 16:48:20 +0000 | [diff] [blame] | 586 | def test_flush_error_on_close(self): |
| 587 | f = self.open(support.TESTFN, "wb", buffering=0) |
| 588 | def bad_flush(): |
| 589 | raise IOError() |
| 590 | f.flush = bad_flush |
| 591 | self.assertRaises(IOError, f.close) # exception not swallowed |
| 592 | |
| 593 | def test_multi_close(self): |
| 594 | f = self.open(support.TESTFN, "wb", buffering=0) |
| 595 | f.close() |
| 596 | f.close() |
| 597 | f.close() |
| 598 | self.assertRaises(ValueError, f.flush) |
| 599 | |
Antoine Pitrou | 328ec74 | 2010-09-14 18:37:24 +0000 | [diff] [blame] | 600 | def test_RawIOBase_read(self): |
| 601 | # Exercise the default RawIOBase.read() implementation (which calls |
| 602 | # readinto() internally). |
| 603 | rawio = self.MockRawIOWithoutRead((b"abc", b"d", None, b"efg", None)) |
| 604 | self.assertEqual(rawio.read(2), b"ab") |
| 605 | self.assertEqual(rawio.read(2), b"c") |
| 606 | self.assertEqual(rawio.read(2), b"d") |
| 607 | self.assertEqual(rawio.read(2), None) |
| 608 | self.assertEqual(rawio.read(2), b"ef") |
| 609 | self.assertEqual(rawio.read(2), b"g") |
| 610 | self.assertEqual(rawio.read(2), None) |
| 611 | self.assertEqual(rawio.read(2), b"") |
| 612 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 613 | class CIOTest(IOTest): |
| 614 | pass |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 615 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 616 | class PyIOTest(IOTest): |
| 617 | pass |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 618 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 619 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 620 | class CommonBufferedTests: |
| 621 | # Tests common to BufferedReader, BufferedWriter and BufferedRandom |
| 622 | |
Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 623 | def test_detach(self): |
| 624 | raw = self.MockRawIO() |
| 625 | buf = self.tp(raw) |
| 626 | self.assertIs(buf.detach(), raw) |
| 627 | self.assertRaises(ValueError, buf.detach) |
| 628 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 629 | def test_fileno(self): |
| 630 | rawio = self.MockRawIO() |
| 631 | bufio = self.tp(rawio) |
| 632 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 633 | self.assertEqual(42, bufio.fileno()) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 634 | |
| 635 | def test_no_fileno(self): |
| 636 | # XXX will we always have fileno() function? If so, kill |
| 637 | # this test. Else, write it. |
| 638 | pass |
| 639 | |
| 640 | def test_invalid_args(self): |
| 641 | rawio = self.MockRawIO() |
| 642 | bufio = self.tp(rawio) |
| 643 | # Invalid whence |
| 644 | self.assertRaises(ValueError, bufio.seek, 0, -1) |
| 645 | self.assertRaises(ValueError, bufio.seek, 0, 3) |
| 646 | |
| 647 | def test_override_destructor(self): |
| 648 | tp = self.tp |
| 649 | record = [] |
| 650 | class MyBufferedIO(tp): |
| 651 | def __del__(self): |
| 652 | record.append(1) |
| 653 | try: |
| 654 | f = super().__del__ |
| 655 | except AttributeError: |
| 656 | pass |
| 657 | else: |
| 658 | f() |
| 659 | def close(self): |
| 660 | record.append(2) |
| 661 | super().close() |
| 662 | def flush(self): |
| 663 | record.append(3) |
| 664 | super().flush() |
| 665 | rawio = self.MockRawIO() |
| 666 | bufio = MyBufferedIO(rawio) |
| 667 | writable = bufio.writable() |
| 668 | del bufio |
Benjamin Peterson | 24fb1d0 | 2009-04-24 23:26:21 +0000 | [diff] [blame] | 669 | support.gc_collect() |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 670 | if writable: |
| 671 | self.assertEqual(record, [1, 2, 3]) |
| 672 | else: |
| 673 | self.assertEqual(record, [1, 2]) |
| 674 | |
| 675 | def test_context_manager(self): |
| 676 | # Test usability as a context manager |
| 677 | rawio = self.MockRawIO() |
| 678 | bufio = self.tp(rawio) |
| 679 | def _with(): |
| 680 | with bufio: |
| 681 | pass |
| 682 | _with() |
| 683 | # bufio should now be closed, and using it a second time should raise |
| 684 | # a ValueError. |
| 685 | self.assertRaises(ValueError, _with) |
| 686 | |
| 687 | def test_error_through_destructor(self): |
| 688 | # Test that the exception state is not modified by a destructor, |
| 689 | # even if close() fails. |
| 690 | rawio = self.CloseFailureIO() |
| 691 | def f(): |
| 692 | self.tp(rawio).xyzzy |
| 693 | with support.captured_output("stderr") as s: |
| 694 | self.assertRaises(AttributeError, f) |
| 695 | s = s.getvalue().strip() |
| 696 | if s: |
| 697 | # The destructor *may* have printed an unraisable error, check it |
| 698 | self.assertEqual(len(s.splitlines()), 1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 699 | self.assertTrue(s.startswith("Exception IOError: "), s) |
| 700 | self.assertTrue(s.endswith(" ignored"), s) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 701 | |
Antoine Pitrou | 716c444 | 2009-05-23 19:04:03 +0000 | [diff] [blame] | 702 | def test_repr(self): |
| 703 | raw = self.MockRawIO() |
| 704 | b = self.tp(raw) |
| 705 | clsname = "%s.%s" % (self.tp.__module__, self.tp.__name__) |
| 706 | self.assertEqual(repr(b), "<%s>" % clsname) |
| 707 | raw.name = "dummy" |
| 708 | self.assertEqual(repr(b), "<%s name='dummy'>" % clsname) |
| 709 | raw.name = b"dummy" |
| 710 | self.assertEqual(repr(b), "<%s name=b'dummy'>" % clsname) |
| 711 | |
Antoine Pitrou | 6be8876 | 2010-05-03 16:48:20 +0000 | [diff] [blame] | 712 | def test_flush_error_on_close(self): |
| 713 | raw = self.MockRawIO() |
| 714 | def bad_flush(): |
| 715 | raise IOError() |
| 716 | raw.flush = bad_flush |
| 717 | b = self.tp(raw) |
| 718 | self.assertRaises(IOError, b.close) # exception not swallowed |
| 719 | |
| 720 | def test_multi_close(self): |
| 721 | raw = self.MockRawIO() |
| 722 | b = self.tp(raw) |
| 723 | b.close() |
| 724 | b.close() |
| 725 | b.close() |
| 726 | self.assertRaises(ValueError, b.flush) |
| 727 | |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 728 | def test_unseekable(self): |
| 729 | bufio = self.tp(self.MockUnseekableIO(b"A" * 10)) |
| 730 | self.assertRaises(self.UnsupportedOperation, bufio.tell) |
| 731 | self.assertRaises(self.UnsupportedOperation, bufio.seek, 0) |
| 732 | |
Antoine Pitrou | 7f8f418 | 2010-12-21 21:20:59 +0000 | [diff] [blame] | 733 | def test_readonly_attributes(self): |
| 734 | raw = self.MockRawIO() |
| 735 | buf = self.tp(raw) |
| 736 | x = self.MockRawIO() |
| 737 | with self.assertRaises(AttributeError): |
| 738 | buf.raw = x |
| 739 | |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 740 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 741 | class BufferedReaderTest(unittest.TestCase, CommonBufferedTests): |
| 742 | read_mode = "rb" |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 743 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 744 | def test_constructor(self): |
| 745 | rawio = self.MockRawIO([b"abc"]) |
| 746 | bufio = self.tp(rawio) |
| 747 | bufio.__init__(rawio) |
| 748 | bufio.__init__(rawio, buffer_size=1024) |
| 749 | bufio.__init__(rawio, buffer_size=16) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 750 | self.assertEqual(b"abc", bufio.read()) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 751 | self.assertRaises(ValueError, bufio.__init__, rawio, buffer_size=0) |
| 752 | self.assertRaises(ValueError, bufio.__init__, rawio, buffer_size=-16) |
| 753 | self.assertRaises(ValueError, bufio.__init__, rawio, buffer_size=-1) |
| 754 | rawio = self.MockRawIO([b"abc"]) |
| 755 | bufio.__init__(rawio) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 756 | self.assertEqual(b"abc", bufio.read()) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 757 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 758 | def test_read(self): |
Benjamin Peterson | bf5ff76 | 2009-12-13 19:25:34 +0000 | [diff] [blame] | 759 | for arg in (None, 7): |
| 760 | rawio = self.MockRawIO((b"abc", b"d", b"efg")) |
| 761 | bufio = self.tp(rawio) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 762 | self.assertEqual(b"abcdefg", bufio.read(arg)) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 763 | # Invalid args |
| 764 | self.assertRaises(ValueError, bufio.read, -2) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 765 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 766 | def test_read1(self): |
| 767 | rawio = self.MockRawIO((b"abc", b"d", b"efg")) |
| 768 | bufio = self.tp(rawio) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 769 | self.assertEqual(b"a", bufio.read(1)) |
| 770 | self.assertEqual(b"b", bufio.read1(1)) |
| 771 | self.assertEqual(rawio._reads, 1) |
| 772 | self.assertEqual(b"c", bufio.read1(100)) |
| 773 | self.assertEqual(rawio._reads, 1) |
| 774 | self.assertEqual(b"d", bufio.read1(100)) |
| 775 | self.assertEqual(rawio._reads, 2) |
| 776 | self.assertEqual(b"efg", bufio.read1(100)) |
| 777 | self.assertEqual(rawio._reads, 3) |
| 778 | self.assertEqual(b"", bufio.read1(100)) |
| 779 | self.assertEqual(rawio._reads, 4) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 780 | # Invalid args |
| 781 | self.assertRaises(ValueError, bufio.read1, -1) |
| 782 | |
| 783 | def test_readinto(self): |
| 784 | rawio = self.MockRawIO((b"abc", b"d", b"efg")) |
| 785 | bufio = self.tp(rawio) |
| 786 | b = bytearray(2) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 787 | self.assertEqual(bufio.readinto(b), 2) |
| 788 | self.assertEqual(b, b"ab") |
| 789 | self.assertEqual(bufio.readinto(b), 2) |
| 790 | self.assertEqual(b, b"cd") |
| 791 | self.assertEqual(bufio.readinto(b), 2) |
| 792 | self.assertEqual(b, b"ef") |
| 793 | self.assertEqual(bufio.readinto(b), 1) |
| 794 | self.assertEqual(b, b"gf") |
| 795 | self.assertEqual(bufio.readinto(b), 0) |
| 796 | self.assertEqual(b, b"gf") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 797 | |
Benjamin Peterson | bf5ff76 | 2009-12-13 19:25:34 +0000 | [diff] [blame] | 798 | def test_readlines(self): |
| 799 | def bufio(): |
| 800 | rawio = self.MockRawIO((b"abc\n", b"d\n", b"ef")) |
| 801 | return self.tp(rawio) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 802 | self.assertEqual(bufio().readlines(), [b"abc\n", b"d\n", b"ef"]) |
| 803 | self.assertEqual(bufio().readlines(5), [b"abc\n", b"d\n"]) |
| 804 | self.assertEqual(bufio().readlines(None), [b"abc\n", b"d\n", b"ef"]) |
Benjamin Peterson | bf5ff76 | 2009-12-13 19:25:34 +0000 | [diff] [blame] | 805 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 806 | def test_buffering(self): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 807 | data = b"abcdefghi" |
| 808 | dlen = len(data) |
| 809 | |
| 810 | tests = [ |
| 811 | [ 100, [ 3, 1, 4, 8 ], [ dlen, 0 ] ], |
| 812 | [ 100, [ 3, 3, 3], [ dlen ] ], |
| 813 | [ 4, [ 1, 2, 4, 2 ], [ 4, 4, 1 ] ], |
| 814 | ] |
| 815 | |
| 816 | for bufsize, buf_read_sizes, raw_read_sizes in tests: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 817 | rawio = self.MockFileIO(data) |
| 818 | bufio = self.tp(rawio, buffer_size=bufsize) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 819 | pos = 0 |
| 820 | for nbytes in buf_read_sizes: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 821 | self.assertEqual(bufio.read(nbytes), data[pos:pos+nbytes]) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 822 | pos += nbytes |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 823 | # this is mildly implementation-dependent |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 824 | self.assertEqual(rawio.read_history, raw_read_sizes) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 825 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 826 | def test_read_non_blocking(self): |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 827 | # Inject some None's in there to simulate EWOULDBLOCK |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 828 | rawio = self.MockRawIO((b"abc", b"d", None, b"efg", None, None, None)) |
| 829 | bufio = self.tp(rawio) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 830 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 831 | self.assertEqual(b"abcd", bufio.read(6)) |
| 832 | self.assertEqual(b"e", bufio.read(1)) |
| 833 | self.assertEqual(b"fg", bufio.read()) |
| 834 | self.assertEqual(b"", bufio.peek(1)) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 835 | self.assertTrue(None is bufio.read()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 836 | self.assertEqual(b"", bufio.read()) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 837 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 838 | def test_read_past_eof(self): |
| 839 | rawio = self.MockRawIO((b"abc", b"d", b"efg")) |
| 840 | bufio = self.tp(rawio) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 841 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 842 | self.assertEqual(b"abcdefg", bufio.read(9000)) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 843 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 844 | def test_read_all(self): |
| 845 | rawio = self.MockRawIO((b"abc", b"d", b"efg")) |
| 846 | bufio = self.tp(rawio) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 847 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 848 | self.assertEqual(b"abcdefg", bufio.read()) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 849 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 850 | @unittest.skipUnless(threading, 'Threading required for this test.') |
Antoine Pitrou | 5bc4fa7 | 2010-10-14 15:34:31 +0000 | [diff] [blame] | 851 | @support.requires_resource('cpu') |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 852 | def test_threads(self): |
Antoine Pitrou | 8769576 | 2008-08-14 22:44:29 +0000 | [diff] [blame] | 853 | try: |
| 854 | # Write out many bytes with exactly the same number of 0's, |
| 855 | # 1's... 255's. This will help us check that concurrent reading |
| 856 | # doesn't duplicate or forget contents. |
| 857 | N = 1000 |
| 858 | l = list(range(256)) * N |
| 859 | random.shuffle(l) |
| 860 | s = bytes(bytearray(l)) |
Hirokazu Yamamoto | c7d6aa4 | 2009-06-18 00:07:14 +0000 | [diff] [blame] | 861 | with self.open(support.TESTFN, "wb") as f: |
Antoine Pitrou | 8769576 | 2008-08-14 22:44:29 +0000 | [diff] [blame] | 862 | f.write(s) |
Hirokazu Yamamoto | c7d6aa4 | 2009-06-18 00:07:14 +0000 | [diff] [blame] | 863 | with self.open(support.TESTFN, self.read_mode, buffering=0) as raw: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 864 | bufio = self.tp(raw, 8) |
Antoine Pitrou | 8769576 | 2008-08-14 22:44:29 +0000 | [diff] [blame] | 865 | errors = [] |
| 866 | results = [] |
| 867 | def f(): |
| 868 | try: |
| 869 | # Intra-buffer read then buffer-flushing read |
| 870 | for n in cycle([1, 19]): |
| 871 | s = bufio.read(n) |
| 872 | if not s: |
| 873 | break |
| 874 | # list.append() is atomic |
| 875 | results.append(s) |
| 876 | except Exception as e: |
| 877 | errors.append(e) |
| 878 | raise |
| 879 | threads = [threading.Thread(target=f) for x in range(20)] |
| 880 | for t in threads: |
| 881 | t.start() |
| 882 | time.sleep(0.02) # yield |
| 883 | for t in threads: |
| 884 | t.join() |
| 885 | self.assertFalse(errors, |
| 886 | "the following exceptions were caught: %r" % errors) |
| 887 | s = b''.join(results) |
| 888 | for i in range(256): |
| 889 | c = bytes(bytearray([i])) |
| 890 | self.assertEqual(s.count(c), N) |
| 891 | finally: |
| 892 | support.unlink(support.TESTFN) |
| 893 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 894 | def test_misbehaved_io(self): |
| 895 | rawio = self.MisbehavedRawIO((b"abc", b"d", b"efg")) |
| 896 | bufio = self.tp(rawio) |
| 897 | self.assertRaises(IOError, bufio.seek, 0) |
| 898 | self.assertRaises(IOError, bufio.tell) |
| 899 | |
Antoine Pitrou | 32cfede | 2010-08-11 13:31:33 +0000 | [diff] [blame] | 900 | def test_no_extraneous_read(self): |
| 901 | # Issue #9550; when the raw IO object has satisfied the read request, |
| 902 | # we should not issue any additional reads, otherwise it may block |
| 903 | # (e.g. socket). |
| 904 | bufsize = 16 |
| 905 | for n in (2, bufsize - 1, bufsize, bufsize + 1, bufsize * 2): |
| 906 | rawio = self.MockRawIO([b"x" * n]) |
| 907 | bufio = self.tp(rawio, bufsize) |
| 908 | self.assertEqual(bufio.read(n), b"x" * n) |
| 909 | # Simple case: one raw read is enough to satisfy the request. |
| 910 | self.assertEqual(rawio._extraneous_reads, 0, |
| 911 | "failed for {}: {} != 0".format(n, rawio._extraneous_reads)) |
| 912 | # A more complex case where two raw reads are needed to satisfy |
| 913 | # the request. |
| 914 | rawio = self.MockRawIO([b"x" * (n - 1), b"x"]) |
| 915 | bufio = self.tp(rawio, bufsize) |
| 916 | self.assertEqual(bufio.read(n), b"x" * n) |
| 917 | self.assertEqual(rawio._extraneous_reads, 0, |
| 918 | "failed for {}: {} != 0".format(n, rawio._extraneous_reads)) |
| 919 | |
| 920 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 921 | class CBufferedReaderTest(BufferedReaderTest): |
| 922 | tp = io.BufferedReader |
| 923 | |
| 924 | def test_constructor(self): |
| 925 | BufferedReaderTest.test_constructor(self) |
| 926 | # The allocation can succeed on 32-bit builds, e.g. with more |
| 927 | # than 2GB RAM and a 64-bit kernel. |
| 928 | if sys.maxsize > 0x7FFFFFFF: |
| 929 | rawio = self.MockRawIO() |
| 930 | bufio = self.tp(rawio) |
| 931 | self.assertRaises((OverflowError, MemoryError, ValueError), |
| 932 | bufio.__init__, rawio, sys.maxsize) |
| 933 | |
| 934 | def test_initialization(self): |
| 935 | rawio = self.MockRawIO([b"abc"]) |
| 936 | bufio = self.tp(rawio) |
| 937 | self.assertRaises(ValueError, bufio.__init__, rawio, buffer_size=0) |
| 938 | self.assertRaises(ValueError, bufio.read) |
| 939 | self.assertRaises(ValueError, bufio.__init__, rawio, buffer_size=-16) |
| 940 | self.assertRaises(ValueError, bufio.read) |
| 941 | self.assertRaises(ValueError, bufio.__init__, rawio, buffer_size=-1) |
| 942 | self.assertRaises(ValueError, bufio.read) |
| 943 | |
| 944 | def test_misbehaved_io_read(self): |
| 945 | rawio = self.MisbehavedRawIO((b"abc", b"d", b"efg")) |
| 946 | bufio = self.tp(rawio) |
| 947 | # _pyio.BufferedReader seems to implement reading different, so that |
| 948 | # checking this is not so easy. |
| 949 | self.assertRaises(IOError, bufio.read, 10) |
| 950 | |
| 951 | def test_garbage_collection(self): |
| 952 | # C BufferedReader objects are collected. |
| 953 | # The Python version has __del__, so it ends into gc.garbage instead |
| 954 | rawio = self.FileIO(support.TESTFN, "w+b") |
| 955 | f = self.tp(rawio) |
| 956 | f.f = f |
| 957 | wr = weakref.ref(f) |
| 958 | del f |
Benjamin Peterson | 45cec32 | 2009-04-24 23:14:50 +0000 | [diff] [blame] | 959 | support.gc_collect() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 960 | self.assertTrue(wr() is None, wr) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 961 | |
| 962 | class PyBufferedReaderTest(BufferedReaderTest): |
| 963 | tp = pyio.BufferedReader |
Antoine Pitrou | 8769576 | 2008-08-14 22:44:29 +0000 | [diff] [blame] | 964 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 965 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 966 | class BufferedWriterTest(unittest.TestCase, CommonBufferedTests): |
| 967 | write_mode = "wb" |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 968 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 969 | def test_constructor(self): |
| 970 | rawio = self.MockRawIO() |
| 971 | bufio = self.tp(rawio) |
| 972 | bufio.__init__(rawio) |
| 973 | bufio.__init__(rawio, buffer_size=1024) |
| 974 | bufio.__init__(rawio, buffer_size=16) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 975 | self.assertEqual(3, bufio.write(b"abc")) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 976 | bufio.flush() |
| 977 | self.assertRaises(ValueError, bufio.__init__, rawio, buffer_size=0) |
| 978 | self.assertRaises(ValueError, bufio.__init__, rawio, buffer_size=-16) |
| 979 | self.assertRaises(ValueError, bufio.__init__, rawio, buffer_size=-1) |
| 980 | bufio.__init__(rawio) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 981 | self.assertEqual(3, bufio.write(b"ghi")) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 982 | bufio.flush() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 983 | self.assertEqual(b"".join(rawio._write_stack), b"abcghi") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 984 | |
Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 985 | def test_detach_flush(self): |
| 986 | raw = self.MockRawIO() |
| 987 | buf = self.tp(raw) |
| 988 | buf.write(b"howdy!") |
| 989 | self.assertFalse(raw._write_stack) |
| 990 | buf.detach() |
| 991 | self.assertEqual(raw._write_stack, [b"howdy!"]) |
| 992 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 993 | def test_write(self): |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 994 | # Write to the buffered IO but don't overflow the buffer. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 995 | writer = self.MockRawIO() |
| 996 | bufio = self.tp(writer, 8) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 997 | bufio.write(b"abc") |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 998 | self.assertFalse(writer._write_stack) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 999 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1000 | def test_write_overflow(self): |
| 1001 | writer = self.MockRawIO() |
| 1002 | bufio = self.tp(writer, 8) |
| 1003 | contents = b"abcdefghijklmnop" |
| 1004 | for n in range(0, len(contents), 3): |
| 1005 | bufio.write(contents[n:n+3]) |
| 1006 | flushed = b"".join(writer._write_stack) |
| 1007 | # At least (total - 8) bytes were implicitly flushed, perhaps more |
| 1008 | # depending on the implementation. |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1009 | self.assertTrue(flushed.startswith(contents[:-8]), flushed) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 1010 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1011 | def check_writes(self, intermediate_func): |
| 1012 | # Lots of writes, test the flushed output is as expected. |
| 1013 | contents = bytes(range(256)) * 1000 |
| 1014 | n = 0 |
| 1015 | writer = self.MockRawIO() |
| 1016 | bufio = self.tp(writer, 13) |
| 1017 | # Generator of write sizes: repeat each N 15 times then proceed to N+1 |
| 1018 | def gen_sizes(): |
| 1019 | for size in count(1): |
| 1020 | for i in range(15): |
| 1021 | yield size |
| 1022 | sizes = gen_sizes() |
| 1023 | while n < len(contents): |
| 1024 | size = min(next(sizes), len(contents) - n) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1025 | self.assertEqual(bufio.write(contents[n:n+size]), size) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1026 | intermediate_func(bufio) |
| 1027 | n += size |
| 1028 | bufio.flush() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1029 | self.assertEqual(contents, b"".join(writer._write_stack)) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 1030 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1031 | def test_writes(self): |
| 1032 | self.check_writes(lambda bufio: None) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 1033 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1034 | def test_writes_and_flushes(self): |
| 1035 | self.check_writes(lambda bufio: bufio.flush()) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 1036 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1037 | def test_writes_and_seeks(self): |
| 1038 | def _seekabs(bufio): |
| 1039 | pos = bufio.tell() |
| 1040 | bufio.seek(pos + 1, 0) |
| 1041 | bufio.seek(pos - 1, 0) |
| 1042 | bufio.seek(pos, 0) |
| 1043 | self.check_writes(_seekabs) |
| 1044 | def _seekrel(bufio): |
| 1045 | pos = bufio.seek(0, 1) |
| 1046 | bufio.seek(+1, 1) |
| 1047 | bufio.seek(-1, 1) |
| 1048 | bufio.seek(pos, 0) |
| 1049 | self.check_writes(_seekrel) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 1050 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1051 | def test_writes_and_truncates(self): |
| 1052 | self.check_writes(lambda bufio: bufio.truncate(bufio.tell())) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 1053 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1054 | def test_write_non_blocking(self): |
| 1055 | raw = self.MockNonBlockWriterIO() |
Benjamin Peterson | 59406a9 | 2009-03-26 17:10:29 +0000 | [diff] [blame] | 1056 | bufio = self.tp(raw, 8) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 1057 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1058 | self.assertEqual(bufio.write(b"abcd"), 4) |
| 1059 | self.assertEqual(bufio.write(b"efghi"), 5) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1060 | # 1 byte will be written, the rest will be buffered |
| 1061 | raw.block_on(b"k") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1062 | self.assertEqual(bufio.write(b"jklmn"), 5) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 1063 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1064 | # 8 bytes will be written, 8 will be buffered and the rest will be lost |
| 1065 | raw.block_on(b"0") |
| 1066 | try: |
| 1067 | bufio.write(b"opqrwxyz0123456789") |
| 1068 | except self.BlockingIOError as e: |
| 1069 | written = e.characters_written |
| 1070 | else: |
| 1071 | self.fail("BlockingIOError should have been raised") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1072 | self.assertEqual(written, 16) |
| 1073 | self.assertEqual(raw.pop_written(), |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1074 | b"abcdefghijklmnopqrwxyz") |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 1075 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1076 | self.assertEqual(bufio.write(b"ABCDEFGHI"), 9) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1077 | s = raw.pop_written() |
| 1078 | # Previously buffered bytes were flushed |
| 1079 | self.assertTrue(s.startswith(b"01234567A"), s) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 1080 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1081 | def test_write_and_rewind(self): |
| 1082 | raw = io.BytesIO() |
| 1083 | bufio = self.tp(raw, 4) |
| 1084 | self.assertEqual(bufio.write(b"abcdef"), 6) |
| 1085 | self.assertEqual(bufio.tell(), 6) |
| 1086 | bufio.seek(0, 0) |
| 1087 | self.assertEqual(bufio.write(b"XY"), 2) |
| 1088 | bufio.seek(6, 0) |
| 1089 | self.assertEqual(raw.getvalue(), b"XYcdef") |
| 1090 | self.assertEqual(bufio.write(b"123456"), 6) |
| 1091 | bufio.flush() |
| 1092 | self.assertEqual(raw.getvalue(), b"XYcdef123456") |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 1093 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1094 | def test_flush(self): |
| 1095 | writer = self.MockRawIO() |
| 1096 | bufio = self.tp(writer, 8) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 1097 | bufio.write(b"abc") |
| 1098 | bufio.flush() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1099 | self.assertEqual(b"abc", writer._write_stack[0]) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 1100 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1101 | def test_destructor(self): |
| 1102 | writer = self.MockRawIO() |
| 1103 | bufio = self.tp(writer, 8) |
| 1104 | bufio.write(b"abc") |
| 1105 | del bufio |
Benjamin Peterson | 24fb1d0 | 2009-04-24 23:26:21 +0000 | [diff] [blame] | 1106 | support.gc_collect() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1107 | self.assertEqual(b"abc", writer._write_stack[0]) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1108 | |
| 1109 | def test_truncate(self): |
| 1110 | # Truncate implicitly flushes the buffer. |
Hirokazu Yamamoto | c7d6aa4 | 2009-06-18 00:07:14 +0000 | [diff] [blame] | 1111 | with self.open(support.TESTFN, self.write_mode, buffering=0) as raw: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1112 | bufio = self.tp(raw, 8) |
| 1113 | bufio.write(b"abcdef") |
| 1114 | self.assertEqual(bufio.truncate(3), 3) |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 1115 | self.assertEqual(bufio.tell(), 6) |
Hirokazu Yamamoto | c7d6aa4 | 2009-06-18 00:07:14 +0000 | [diff] [blame] | 1116 | with self.open(support.TESTFN, "rb", buffering=0) as f: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1117 | self.assertEqual(f.read(), b"abc") |
| 1118 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 1119 | @unittest.skipUnless(threading, 'Threading required for this test.') |
Antoine Pitrou | 5bc4fa7 | 2010-10-14 15:34:31 +0000 | [diff] [blame] | 1120 | @support.requires_resource('cpu') |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1121 | def test_threads(self): |
Antoine Pitrou | 8769576 | 2008-08-14 22:44:29 +0000 | [diff] [blame] | 1122 | try: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1123 | # Write out many bytes from many threads and test they were |
| 1124 | # all flushed. |
| 1125 | N = 1000 |
| 1126 | contents = bytes(range(256)) * N |
| 1127 | sizes = cycle([1, 19]) |
| 1128 | n = 0 |
| 1129 | queue = deque() |
| 1130 | while n < len(contents): |
| 1131 | size = next(sizes) |
| 1132 | queue.append(contents[n:n+size]) |
| 1133 | n += size |
| 1134 | del contents |
Antoine Pitrou | 8769576 | 2008-08-14 22:44:29 +0000 | [diff] [blame] | 1135 | # We use a real file object because it allows us to |
| 1136 | # exercise situations where the GIL is released before |
| 1137 | # writing the buffer to the raw streams. This is in addition |
| 1138 | # to concurrency issues due to switching threads in the middle |
| 1139 | # of Python code. |
Hirokazu Yamamoto | c7d6aa4 | 2009-06-18 00:07:14 +0000 | [diff] [blame] | 1140 | with self.open(support.TESTFN, self.write_mode, buffering=0) as raw: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1141 | bufio = self.tp(raw, 8) |
Antoine Pitrou | 8769576 | 2008-08-14 22:44:29 +0000 | [diff] [blame] | 1142 | errors = [] |
| 1143 | def f(): |
| 1144 | try: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1145 | while True: |
| 1146 | try: |
| 1147 | s = queue.popleft() |
| 1148 | except IndexError: |
| 1149 | return |
Antoine Pitrou | 8769576 | 2008-08-14 22:44:29 +0000 | [diff] [blame] | 1150 | bufio.write(s) |
| 1151 | except Exception as e: |
| 1152 | errors.append(e) |
| 1153 | raise |
| 1154 | threads = [threading.Thread(target=f) for x in range(20)] |
| 1155 | for t in threads: |
| 1156 | t.start() |
| 1157 | time.sleep(0.02) # yield |
| 1158 | for t in threads: |
| 1159 | t.join() |
| 1160 | self.assertFalse(errors, |
| 1161 | "the following exceptions were caught: %r" % errors) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1162 | bufio.close() |
Hirokazu Yamamoto | c7d6aa4 | 2009-06-18 00:07:14 +0000 | [diff] [blame] | 1163 | with self.open(support.TESTFN, "rb") as f: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1164 | s = f.read() |
| 1165 | for i in range(256): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1166 | self.assertEqual(s.count(bytes([i])), N) |
Antoine Pitrou | 8769576 | 2008-08-14 22:44:29 +0000 | [diff] [blame] | 1167 | finally: |
| 1168 | support.unlink(support.TESTFN) |
| 1169 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1170 | def test_misbehaved_io(self): |
| 1171 | rawio = self.MisbehavedRawIO() |
| 1172 | bufio = self.tp(rawio, 5) |
| 1173 | self.assertRaises(IOError, bufio.seek, 0) |
| 1174 | self.assertRaises(IOError, bufio.tell) |
| 1175 | self.assertRaises(IOError, bufio.write, b"abcdef") |
| 1176 | |
Benjamin Peterson | 59406a9 | 2009-03-26 17:10:29 +0000 | [diff] [blame] | 1177 | def test_max_buffer_size_deprecation(self): |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 1178 | with support.check_warnings(("max_buffer_size is deprecated", |
| 1179 | DeprecationWarning)): |
Benjamin Peterson | 59406a9 | 2009-03-26 17:10:29 +0000 | [diff] [blame] | 1180 | self.tp(self.MockRawIO(), 8, 12) |
Benjamin Peterson | 59406a9 | 2009-03-26 17:10:29 +0000 | [diff] [blame] | 1181 | |
| 1182 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1183 | class CBufferedWriterTest(BufferedWriterTest): |
| 1184 | tp = io.BufferedWriter |
| 1185 | |
| 1186 | def test_constructor(self): |
| 1187 | BufferedWriterTest.test_constructor(self) |
| 1188 | # The allocation can succeed on 32-bit builds, e.g. with more |
| 1189 | # than 2GB RAM and a 64-bit kernel. |
| 1190 | if sys.maxsize > 0x7FFFFFFF: |
| 1191 | rawio = self.MockRawIO() |
| 1192 | bufio = self.tp(rawio) |
| 1193 | self.assertRaises((OverflowError, MemoryError, ValueError), |
| 1194 | bufio.__init__, rawio, sys.maxsize) |
| 1195 | |
| 1196 | def test_initialization(self): |
| 1197 | rawio = self.MockRawIO() |
| 1198 | bufio = self.tp(rawio) |
| 1199 | self.assertRaises(ValueError, bufio.__init__, rawio, buffer_size=0) |
| 1200 | self.assertRaises(ValueError, bufio.write, b"def") |
| 1201 | self.assertRaises(ValueError, bufio.__init__, rawio, buffer_size=-16) |
| 1202 | self.assertRaises(ValueError, bufio.write, b"def") |
| 1203 | self.assertRaises(ValueError, bufio.__init__, rawio, buffer_size=-1) |
| 1204 | self.assertRaises(ValueError, bufio.write, b"def") |
| 1205 | |
| 1206 | def test_garbage_collection(self): |
| 1207 | # C BufferedWriter objects are collected, and collecting them flushes |
| 1208 | # all data to disk. |
| 1209 | # The Python version has __del__, so it ends into gc.garbage instead |
| 1210 | rawio = self.FileIO(support.TESTFN, "w+b") |
| 1211 | f = self.tp(rawio) |
| 1212 | f.write(b"123xxx") |
| 1213 | f.x = f |
| 1214 | wr = weakref.ref(f) |
| 1215 | del f |
Benjamin Peterson | 24fb1d0 | 2009-04-24 23:26:21 +0000 | [diff] [blame] | 1216 | support.gc_collect() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1217 | self.assertTrue(wr() is None, wr) |
Hirokazu Yamamoto | c7d6aa4 | 2009-06-18 00:07:14 +0000 | [diff] [blame] | 1218 | with self.open(support.TESTFN, "rb") as f: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1219 | self.assertEqual(f.read(), b"123xxx") |
| 1220 | |
| 1221 | |
| 1222 | class PyBufferedWriterTest(BufferedWriterTest): |
| 1223 | tp = pyio.BufferedWriter |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 1224 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 1225 | class BufferedRWPairTest(unittest.TestCase): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 1226 | |
Antoine Pitrou | cf4c749 | 2009-04-19 00:09:36 +0000 | [diff] [blame] | 1227 | def test_constructor(self): |
| 1228 | pair = self.tp(self.MockRawIO(), self.MockRawIO()) |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1229 | self.assertFalse(pair.closed) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 1230 | |
Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 1231 | def test_detach(self): |
| 1232 | pair = self.tp(self.MockRawIO(), self.MockRawIO()) |
| 1233 | self.assertRaises(self.UnsupportedOperation, pair.detach) |
| 1234 | |
Antoine Pitrou | cf4c749 | 2009-04-19 00:09:36 +0000 | [diff] [blame] | 1235 | def test_constructor_max_buffer_size_deprecation(self): |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 1236 | with support.check_warnings(("max_buffer_size is deprecated", |
| 1237 | DeprecationWarning)): |
Benjamin Peterson | 59406a9 | 2009-03-26 17:10:29 +0000 | [diff] [blame] | 1238 | self.tp(self.MockRawIO(), self.MockRawIO(), 8, 12) |
Benjamin Peterson | 59406a9 | 2009-03-26 17:10:29 +0000 | [diff] [blame] | 1239 | |
Antoine Pitrou | cf4c749 | 2009-04-19 00:09:36 +0000 | [diff] [blame] | 1240 | def test_constructor_with_not_readable(self): |
| 1241 | class NotReadable(MockRawIO): |
| 1242 | def readable(self): |
| 1243 | return False |
| 1244 | |
| 1245 | self.assertRaises(IOError, self.tp, NotReadable(), self.MockRawIO()) |
| 1246 | |
| 1247 | def test_constructor_with_not_writeable(self): |
| 1248 | class NotWriteable(MockRawIO): |
| 1249 | def writable(self): |
| 1250 | return False |
| 1251 | |
| 1252 | self.assertRaises(IOError, self.tp, self.MockRawIO(), NotWriteable()) |
| 1253 | |
| 1254 | def test_read(self): |
| 1255 | pair = self.tp(self.BytesIO(b"abcdef"), self.MockRawIO()) |
| 1256 | |
| 1257 | self.assertEqual(pair.read(3), b"abc") |
| 1258 | self.assertEqual(pair.read(1), b"d") |
| 1259 | self.assertEqual(pair.read(), b"ef") |
Benjamin Peterson | bf5ff76 | 2009-12-13 19:25:34 +0000 | [diff] [blame] | 1260 | pair = self.tp(self.BytesIO(b"abc"), self.MockRawIO()) |
| 1261 | self.assertEqual(pair.read(None), b"abc") |
| 1262 | |
| 1263 | def test_readlines(self): |
| 1264 | pair = lambda: self.tp(self.BytesIO(b"abc\ndef\nh"), self.MockRawIO()) |
| 1265 | self.assertEqual(pair().readlines(), [b"abc\n", b"def\n", b"h"]) |
| 1266 | self.assertEqual(pair().readlines(), [b"abc\n", b"def\n", b"h"]) |
| 1267 | self.assertEqual(pair().readlines(5), [b"abc\n", b"def\n"]) |
Antoine Pitrou | cf4c749 | 2009-04-19 00:09:36 +0000 | [diff] [blame] | 1268 | |
| 1269 | def test_read1(self): |
| 1270 | # .read1() is delegated to the underlying reader object, so this test |
| 1271 | # can be shallow. |
| 1272 | pair = self.tp(self.BytesIO(b"abcdef"), self.MockRawIO()) |
| 1273 | |
| 1274 | self.assertEqual(pair.read1(3), b"abc") |
| 1275 | |
| 1276 | def test_readinto(self): |
| 1277 | pair = self.tp(self.BytesIO(b"abcdef"), self.MockRawIO()) |
| 1278 | |
| 1279 | data = bytearray(5) |
| 1280 | self.assertEqual(pair.readinto(data), 5) |
| 1281 | self.assertEqual(data, b"abcde") |
| 1282 | |
| 1283 | def test_write(self): |
| 1284 | w = self.MockRawIO() |
| 1285 | pair = self.tp(self.MockRawIO(), w) |
| 1286 | |
| 1287 | pair.write(b"abc") |
| 1288 | pair.flush() |
| 1289 | pair.write(b"def") |
| 1290 | pair.flush() |
| 1291 | self.assertEqual(w._write_stack, [b"abc", b"def"]) |
| 1292 | |
| 1293 | def test_peek(self): |
| 1294 | pair = self.tp(self.BytesIO(b"abcdef"), self.MockRawIO()) |
| 1295 | |
| 1296 | self.assertTrue(pair.peek(3).startswith(b"abc")) |
| 1297 | self.assertEqual(pair.read(3), b"abc") |
| 1298 | |
| 1299 | def test_readable(self): |
| 1300 | pair = self.tp(self.MockRawIO(), self.MockRawIO()) |
| 1301 | self.assertTrue(pair.readable()) |
| 1302 | |
| 1303 | def test_writeable(self): |
| 1304 | pair = self.tp(self.MockRawIO(), self.MockRawIO()) |
| 1305 | self.assertTrue(pair.writable()) |
| 1306 | |
| 1307 | def test_seekable(self): |
| 1308 | # BufferedRWPairs are never seekable, even if their readers and writers |
| 1309 | # are. |
| 1310 | pair = self.tp(self.MockRawIO(), self.MockRawIO()) |
| 1311 | self.assertFalse(pair.seekable()) |
| 1312 | |
| 1313 | # .flush() is delegated to the underlying writer object and has been |
| 1314 | # tested in the test_write method. |
| 1315 | |
| 1316 | def test_close_and_closed(self): |
| 1317 | pair = self.tp(self.MockRawIO(), self.MockRawIO()) |
| 1318 | self.assertFalse(pair.closed) |
| 1319 | pair.close() |
| 1320 | self.assertTrue(pair.closed) |
| 1321 | |
| 1322 | def test_isatty(self): |
| 1323 | class SelectableIsAtty(MockRawIO): |
| 1324 | def __init__(self, isatty): |
| 1325 | MockRawIO.__init__(self) |
| 1326 | self._isatty = isatty |
| 1327 | |
| 1328 | def isatty(self): |
| 1329 | return self._isatty |
| 1330 | |
| 1331 | pair = self.tp(SelectableIsAtty(False), SelectableIsAtty(False)) |
| 1332 | self.assertFalse(pair.isatty()) |
| 1333 | |
| 1334 | pair = self.tp(SelectableIsAtty(True), SelectableIsAtty(False)) |
| 1335 | self.assertTrue(pair.isatty()) |
| 1336 | |
| 1337 | pair = self.tp(SelectableIsAtty(False), SelectableIsAtty(True)) |
| 1338 | self.assertTrue(pair.isatty()) |
| 1339 | |
| 1340 | pair = self.tp(SelectableIsAtty(True), SelectableIsAtty(True)) |
| 1341 | self.assertTrue(pair.isatty()) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 1342 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1343 | class CBufferedRWPairTest(BufferedRWPairTest): |
| 1344 | tp = io.BufferedRWPair |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 1345 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1346 | class PyBufferedRWPairTest(BufferedRWPairTest): |
| 1347 | tp = pyio.BufferedRWPair |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 1348 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1349 | |
| 1350 | class BufferedRandomTest(BufferedReaderTest, BufferedWriterTest): |
| 1351 | read_mode = "rb+" |
| 1352 | write_mode = "wb+" |
| 1353 | |
| 1354 | def test_constructor(self): |
| 1355 | BufferedReaderTest.test_constructor(self) |
| 1356 | BufferedWriterTest.test_constructor(self) |
| 1357 | |
| 1358 | def test_read_and_write(self): |
| 1359 | raw = self.MockRawIO((b"asdf", b"ghjk")) |
Benjamin Peterson | 59406a9 | 2009-03-26 17:10:29 +0000 | [diff] [blame] | 1360 | rw = self.tp(raw, 8) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 1361 | |
| 1362 | self.assertEqual(b"as", rw.read(2)) |
| 1363 | rw.write(b"ddd") |
| 1364 | rw.write(b"eee") |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 1365 | self.assertFalse(raw._write_stack) # Buffer writes |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1366 | self.assertEqual(b"ghjk", rw.read()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1367 | self.assertEqual(b"dddeee", raw._write_stack[0]) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 1368 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1369 | def test_seek_and_tell(self): |
| 1370 | raw = self.BytesIO(b"asdfghjkl") |
| 1371 | rw = self.tp(raw) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 1372 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1373 | self.assertEqual(b"as", rw.read(2)) |
| 1374 | self.assertEqual(2, rw.tell()) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 1375 | rw.seek(0, 0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1376 | self.assertEqual(b"asdf", rw.read(4)) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 1377 | |
| 1378 | rw.write(b"asdf") |
| 1379 | rw.seek(0, 0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1380 | self.assertEqual(b"asdfasdfl", rw.read()) |
| 1381 | self.assertEqual(9, rw.tell()) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 1382 | rw.seek(-4, 2) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1383 | self.assertEqual(5, rw.tell()) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 1384 | rw.seek(2, 1) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1385 | self.assertEqual(7, rw.tell()) |
| 1386 | self.assertEqual(b"fl", rw.read(11)) |
Christian Heimes | 8e42a0a | 2007-11-08 18:04:45 +0000 | [diff] [blame] | 1387 | self.assertRaises(TypeError, rw.seek, 0.0) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 1388 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1389 | def check_flush_and_read(self, read_func): |
| 1390 | raw = self.BytesIO(b"abcdefghi") |
| 1391 | bufio = self.tp(raw) |
| 1392 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1393 | self.assertEqual(b"ab", read_func(bufio, 2)) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1394 | bufio.write(b"12") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1395 | self.assertEqual(b"ef", read_func(bufio, 2)) |
| 1396 | self.assertEqual(6, bufio.tell()) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1397 | bufio.flush() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1398 | self.assertEqual(6, bufio.tell()) |
| 1399 | self.assertEqual(b"ghi", read_func(bufio)) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1400 | raw.seek(0, 0) |
| 1401 | raw.write(b"XYZ") |
| 1402 | # flush() resets the read buffer |
| 1403 | bufio.flush() |
| 1404 | bufio.seek(0, 0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1405 | self.assertEqual(b"XYZ", read_func(bufio, 3)) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1406 | |
| 1407 | def test_flush_and_read(self): |
| 1408 | self.check_flush_and_read(lambda bufio, *args: bufio.read(*args)) |
| 1409 | |
| 1410 | def test_flush_and_readinto(self): |
| 1411 | def _readinto(bufio, n=-1): |
| 1412 | b = bytearray(n if n >= 0 else 9999) |
| 1413 | n = bufio.readinto(b) |
| 1414 | return bytes(b[:n]) |
| 1415 | self.check_flush_and_read(_readinto) |
| 1416 | |
| 1417 | def test_flush_and_peek(self): |
| 1418 | def _peek(bufio, n=-1): |
| 1419 | # This relies on the fact that the buffer can contain the whole |
| 1420 | # raw stream, otherwise peek() can return less. |
| 1421 | b = bufio.peek(n) |
| 1422 | if n != -1: |
| 1423 | b = b[:n] |
| 1424 | bufio.seek(len(b), 1) |
| 1425 | return b |
| 1426 | self.check_flush_and_read(_peek) |
| 1427 | |
| 1428 | def test_flush_and_write(self): |
| 1429 | raw = self.BytesIO(b"abcdefghi") |
| 1430 | bufio = self.tp(raw) |
| 1431 | |
| 1432 | bufio.write(b"123") |
| 1433 | bufio.flush() |
| 1434 | bufio.write(b"45") |
| 1435 | bufio.flush() |
| 1436 | bufio.seek(0, 0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1437 | self.assertEqual(b"12345fghi", raw.getvalue()) |
| 1438 | self.assertEqual(b"12345fghi", bufio.read()) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1439 | |
| 1440 | def test_threads(self): |
| 1441 | BufferedReaderTest.test_threads(self) |
| 1442 | BufferedWriterTest.test_threads(self) |
| 1443 | |
| 1444 | def test_writes_and_peek(self): |
| 1445 | def _peek(bufio): |
| 1446 | bufio.peek(1) |
| 1447 | self.check_writes(_peek) |
| 1448 | def _peek(bufio): |
| 1449 | pos = bufio.tell() |
| 1450 | bufio.seek(-1, 1) |
| 1451 | bufio.peek(1) |
| 1452 | bufio.seek(pos, 0) |
| 1453 | self.check_writes(_peek) |
| 1454 | |
| 1455 | def test_writes_and_reads(self): |
| 1456 | def _read(bufio): |
| 1457 | bufio.seek(-1, 1) |
| 1458 | bufio.read(1) |
| 1459 | self.check_writes(_read) |
| 1460 | |
| 1461 | def test_writes_and_read1s(self): |
| 1462 | def _read1(bufio): |
| 1463 | bufio.seek(-1, 1) |
| 1464 | bufio.read1(1) |
| 1465 | self.check_writes(_read1) |
| 1466 | |
| 1467 | def test_writes_and_readintos(self): |
| 1468 | def _read(bufio): |
| 1469 | bufio.seek(-1, 1) |
| 1470 | bufio.readinto(bytearray(1)) |
| 1471 | self.check_writes(_read) |
| 1472 | |
Antoine Pitrou | a0ceb73 | 2009-08-06 20:29:56 +0000 | [diff] [blame] | 1473 | def test_write_after_readahead(self): |
| 1474 | # Issue #6629: writing after the buffer was filled by readahead should |
| 1475 | # first rewind the raw stream. |
| 1476 | for overwrite_size in [1, 5]: |
| 1477 | raw = self.BytesIO(b"A" * 10) |
| 1478 | bufio = self.tp(raw, 4) |
| 1479 | # Trigger readahead |
| 1480 | self.assertEqual(bufio.read(1), b"A") |
| 1481 | self.assertEqual(bufio.tell(), 1) |
| 1482 | # Overwriting should rewind the raw stream if it needs so |
| 1483 | bufio.write(b"B" * overwrite_size) |
| 1484 | self.assertEqual(bufio.tell(), overwrite_size + 1) |
| 1485 | # If the write size was smaller than the buffer size, flush() and |
| 1486 | # check that rewind happens. |
| 1487 | bufio.flush() |
| 1488 | self.assertEqual(bufio.tell(), overwrite_size + 1) |
| 1489 | s = raw.getvalue() |
| 1490 | self.assertEqual(s, |
| 1491 | b"A" + b"B" * overwrite_size + b"A" * (9 - overwrite_size)) |
| 1492 | |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 1493 | def test_truncate_after_read_or_write(self): |
| 1494 | raw = self.BytesIO(b"A" * 10) |
| 1495 | bufio = self.tp(raw, 100) |
| 1496 | self.assertEqual(bufio.read(2), b"AA") # the read buffer gets filled |
| 1497 | self.assertEqual(bufio.truncate(), 2) |
| 1498 | self.assertEqual(bufio.write(b"BB"), 2) # the write buffer increases |
| 1499 | self.assertEqual(bufio.truncate(), 4) |
| 1500 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1501 | def test_misbehaved_io(self): |
| 1502 | BufferedReaderTest.test_misbehaved_io(self) |
| 1503 | BufferedWriterTest.test_misbehaved_io(self) |
| 1504 | |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 1505 | # You can't construct a BufferedRandom over a non-seekable stream. |
| 1506 | test_unseekable = None |
| 1507 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1508 | class CBufferedRandomTest(BufferedRandomTest): |
| 1509 | tp = io.BufferedRandom |
| 1510 | |
| 1511 | def test_constructor(self): |
| 1512 | BufferedRandomTest.test_constructor(self) |
| 1513 | # The allocation can succeed on 32-bit builds, e.g. with more |
| 1514 | # than 2GB RAM and a 64-bit kernel. |
| 1515 | if sys.maxsize > 0x7FFFFFFF: |
| 1516 | rawio = self.MockRawIO() |
| 1517 | bufio = self.tp(rawio) |
| 1518 | self.assertRaises((OverflowError, MemoryError, ValueError), |
| 1519 | bufio.__init__, rawio, sys.maxsize) |
| 1520 | |
| 1521 | def test_garbage_collection(self): |
| 1522 | CBufferedReaderTest.test_garbage_collection(self) |
| 1523 | CBufferedWriterTest.test_garbage_collection(self) |
| 1524 | |
| 1525 | class PyBufferedRandomTest(BufferedRandomTest): |
| 1526 | tp = pyio.BufferedRandom |
| 1527 | |
| 1528 | |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 1529 | # To fully exercise seek/tell, the StatefulIncrementalDecoder has these |
| 1530 | # properties: |
| 1531 | # - A single output character can correspond to many bytes of input. |
| 1532 | # - The number of input bytes to complete the character can be |
| 1533 | # undetermined until the last input byte is received. |
| 1534 | # - The number of input bytes can vary depending on previous input. |
| 1535 | # - A single input byte can correspond to many characters of output. |
| 1536 | # - The number of output characters can be undetermined until the |
| 1537 | # last input byte is received. |
| 1538 | # - The number of output characters can vary depending on previous input. |
| 1539 | |
| 1540 | class StatefulIncrementalDecoder(codecs.IncrementalDecoder): |
| 1541 | """ |
| 1542 | For testing seek/tell behavior with a stateful, buffering decoder. |
| 1543 | |
| 1544 | Input is a sequence of words. Words may be fixed-length (length set |
| 1545 | by input) or variable-length (period-terminated). In variable-length |
| 1546 | mode, extra periods are ignored. Possible words are: |
| 1547 | - 'i' followed by a number sets the input length, I (maximum 99). |
| 1548 | When I is set to 0, words are space-terminated. |
| 1549 | - 'o' followed by a number sets the output length, O (maximum 99). |
| 1550 | - Any other word is converted into a word followed by a period on |
| 1551 | the output. The output word consists of the input word truncated |
| 1552 | or padded out with hyphens to make its length equal to O. If O |
| 1553 | is 0, the word is output verbatim without truncating or padding. |
| 1554 | I and O are initially set to 1. When I changes, any buffered input is |
| 1555 | re-scanned according to the new I. EOF also terminates the last word. |
| 1556 | """ |
| 1557 | |
| 1558 | def __init__(self, errors='strict'): |
Christian Heimes | ab56887 | 2008-03-23 02:11:13 +0000 | [diff] [blame] | 1559 | codecs.IncrementalDecoder.__init__(self, errors) |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 1560 | self.reset() |
| 1561 | |
| 1562 | def __repr__(self): |
| 1563 | return '<SID %x>' % id(self) |
| 1564 | |
| 1565 | def reset(self): |
| 1566 | self.i = 1 |
| 1567 | self.o = 1 |
| 1568 | self.buffer = bytearray() |
| 1569 | |
| 1570 | def getstate(self): |
| 1571 | i, o = self.i ^ 1, self.o ^ 1 # so that flags = 0 after reset() |
| 1572 | return bytes(self.buffer), i*100 + o |
| 1573 | |
| 1574 | def setstate(self, state): |
| 1575 | buffer, io = state |
| 1576 | self.buffer = bytearray(buffer) |
| 1577 | i, o = divmod(io, 100) |
| 1578 | self.i, self.o = i ^ 1, o ^ 1 |
| 1579 | |
| 1580 | def decode(self, input, final=False): |
| 1581 | output = '' |
| 1582 | for b in input: |
| 1583 | if self.i == 0: # variable-length, terminated with period |
| 1584 | if b == ord('.'): |
| 1585 | if self.buffer: |
| 1586 | output += self.process_word() |
| 1587 | else: |
| 1588 | self.buffer.append(b) |
| 1589 | else: # fixed-length, terminate after self.i bytes |
| 1590 | self.buffer.append(b) |
| 1591 | if len(self.buffer) == self.i: |
| 1592 | output += self.process_word() |
| 1593 | if final and self.buffer: # EOF terminates the last word |
| 1594 | output += self.process_word() |
| 1595 | return output |
| 1596 | |
| 1597 | def process_word(self): |
| 1598 | output = '' |
| 1599 | if self.buffer[0] == ord('i'): |
| 1600 | self.i = min(99, int(self.buffer[1:] or 0)) # set input length |
| 1601 | elif self.buffer[0] == ord('o'): |
| 1602 | self.o = min(99, int(self.buffer[1:] or 0)) # set output length |
| 1603 | else: |
| 1604 | output = self.buffer.decode('ascii') |
| 1605 | if len(output) < self.o: |
| 1606 | output += '-'*self.o # pad out with hyphens |
| 1607 | if self.o: |
| 1608 | output = output[:self.o] # truncate to output length |
| 1609 | output += '.' |
| 1610 | self.buffer = bytearray() |
| 1611 | return output |
| 1612 | |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 1613 | codecEnabled = False |
| 1614 | |
| 1615 | @classmethod |
| 1616 | def lookupTestDecoder(cls, name): |
| 1617 | if cls.codecEnabled and name == 'test_decoder': |
Antoine Pitrou | 180a336 | 2008-12-14 16:36:46 +0000 | [diff] [blame] | 1618 | latin1 = codecs.lookup('latin-1') |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 1619 | return codecs.CodecInfo( |
Antoine Pitrou | 180a336 | 2008-12-14 16:36:46 +0000 | [diff] [blame] | 1620 | name='test_decoder', encode=latin1.encode, decode=None, |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 1621 | incrementalencoder=None, |
| 1622 | streamreader=None, streamwriter=None, |
| 1623 | incrementaldecoder=cls) |
| 1624 | |
| 1625 | # Register the previous decoder for testing. |
| 1626 | # Disabled by default, tests will enable it. |
| 1627 | codecs.register(StatefulIncrementalDecoder.lookupTestDecoder) |
| 1628 | |
| 1629 | |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 1630 | class StatefulIncrementalDecoderTest(unittest.TestCase): |
| 1631 | """ |
| 1632 | Make sure the StatefulIncrementalDecoder actually works. |
| 1633 | """ |
| 1634 | |
| 1635 | test_cases = [ |
Ka-Ping Yee | d24a5b6 | 2008-03-20 10:51:27 +0000 | [diff] [blame] | 1636 | # I=1, O=1 (fixed-length input == fixed-length output) |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 1637 | (b'abcd', False, 'a.b.c.d.'), |
Ka-Ping Yee | d24a5b6 | 2008-03-20 10:51:27 +0000 | [diff] [blame] | 1638 | # I=0, O=0 (variable-length input, variable-length output) |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 1639 | (b'oiabcd', True, 'abcd.'), |
Ka-Ping Yee | d24a5b6 | 2008-03-20 10:51:27 +0000 | [diff] [blame] | 1640 | # I=0, O=0 (should ignore extra periods) |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 1641 | (b'oi...abcd...', True, 'abcd.'), |
Ka-Ping Yee | d24a5b6 | 2008-03-20 10:51:27 +0000 | [diff] [blame] | 1642 | # I=0, O=6 (variable-length input, fixed-length output) |
| 1643 | (b'i.o6.x.xyz.toolongtofit.', False, 'x-----.xyz---.toolon.'), |
| 1644 | # I=2, O=6 (fixed-length input < fixed-length output) |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 1645 | (b'i.i2.o6xyz', True, 'xy----.z-----.'), |
Ka-Ping Yee | d24a5b6 | 2008-03-20 10:51:27 +0000 | [diff] [blame] | 1646 | # I=6, O=3 (fixed-length input > fixed-length output) |
| 1647 | (b'i.o3.i6.abcdefghijklmnop', True, 'abc.ghi.mno.'), |
| 1648 | # I=0, then 3; O=29, then 15 (with longer output) |
| 1649 | (b'i.o29.a.b.cde.o15.abcdefghijabcdefghij.i3.a.b.c.d.ei00k.l.m', True, |
| 1650 | 'a----------------------------.' + |
| 1651 | 'b----------------------------.' + |
| 1652 | 'cde--------------------------.' + |
| 1653 | 'abcdefghijabcde.' + |
| 1654 | 'a.b------------.' + |
| 1655 | '.c.------------.' + |
| 1656 | 'd.e------------.' + |
| 1657 | 'k--------------.' + |
| 1658 | 'l--------------.' + |
| 1659 | 'm--------------.') |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 1660 | ] |
| 1661 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1662 | def test_decoder(self): |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 1663 | # Try a few one-shot test cases. |
| 1664 | for input, eof, output in self.test_cases: |
| 1665 | d = StatefulIncrementalDecoder() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1666 | self.assertEqual(d.decode(input, eof), output) |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 1667 | |
| 1668 | # Also test an unfinished decode, followed by forcing EOF. |
| 1669 | d = StatefulIncrementalDecoder() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1670 | self.assertEqual(d.decode(b'oiabcd'), '') |
| 1671 | self.assertEqual(d.decode(b'', 1), 'abcd.') |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 1672 | |
| 1673 | class TextIOWrapperTest(unittest.TestCase): |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 1674 | |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 1675 | def setUp(self): |
| 1676 | self.testdata = b"AAA\r\nBBB\rCCC\r\nDDD\nEEE\r\n" |
| 1677 | self.normalized = b"AAA\nBBB\nCCC\nDDD\nEEE\n".decode("ascii") |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1678 | support.unlink(support.TESTFN) |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 1679 | |
Guido van Rossum | d071281 | 2007-04-11 16:32:43 +0000 | [diff] [blame] | 1680 | def tearDown(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1681 | support.unlink(support.TESTFN) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 1682 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1683 | def test_constructor(self): |
| 1684 | r = self.BytesIO(b"\xc3\xa9\n\n") |
| 1685 | b = self.BufferedReader(r, 1000) |
| 1686 | t = self.TextIOWrapper(b) |
| 1687 | t.__init__(b, encoding="latin1", newline="\r\n") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1688 | self.assertEqual(t.encoding, "latin1") |
| 1689 | self.assertEqual(t.line_buffering, False) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1690 | t.__init__(b, encoding="utf8", line_buffering=True) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1691 | self.assertEqual(t.encoding, "utf8") |
| 1692 | self.assertEqual(t.line_buffering, True) |
| 1693 | self.assertEqual("\xe9\n", t.readline()) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1694 | self.assertRaises(TypeError, t.__init__, b, newline=42) |
| 1695 | self.assertRaises(ValueError, t.__init__, b, newline='xyzzy') |
| 1696 | |
Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 1697 | def test_detach(self): |
| 1698 | r = self.BytesIO() |
| 1699 | b = self.BufferedWriter(r) |
| 1700 | t = self.TextIOWrapper(b) |
| 1701 | self.assertIs(t.detach(), b) |
| 1702 | |
| 1703 | t = self.TextIOWrapper(b, encoding="ascii") |
| 1704 | t.write("howdy") |
| 1705 | self.assertFalse(r.getvalue()) |
| 1706 | t.detach() |
| 1707 | self.assertEqual(r.getvalue(), b"howdy") |
| 1708 | self.assertRaises(ValueError, t.detach) |
| 1709 | |
Benjamin Peterson | c4c0eae | 2009-03-09 00:07:03 +0000 | [diff] [blame] | 1710 | def test_repr(self): |
| 1711 | raw = self.BytesIO("hello".encode("utf-8")) |
| 1712 | b = self.BufferedReader(raw) |
| 1713 | t = self.TextIOWrapper(b, encoding="utf-8") |
Antoine Pitrou | 716c444 | 2009-05-23 19:04:03 +0000 | [diff] [blame] | 1714 | modname = self.TextIOWrapper.__module__ |
| 1715 | self.assertEqual(repr(t), |
| 1716 | "<%s.TextIOWrapper encoding='utf-8'>" % modname) |
| 1717 | raw.name = "dummy" |
| 1718 | self.assertEqual(repr(t), |
| 1719 | "<%s.TextIOWrapper name='dummy' encoding='utf-8'>" % modname) |
Antoine Pitrou | a4815ca | 2011-01-09 20:38:15 +0000 | [diff] [blame] | 1720 | t.mode = "r" |
| 1721 | self.assertEqual(repr(t), |
| 1722 | "<%s.TextIOWrapper name='dummy' mode='r' encoding='utf-8'>" % modname) |
Antoine Pitrou | 716c444 | 2009-05-23 19:04:03 +0000 | [diff] [blame] | 1723 | raw.name = b"dummy" |
| 1724 | self.assertEqual(repr(t), |
Antoine Pitrou | a4815ca | 2011-01-09 20:38:15 +0000 | [diff] [blame] | 1725 | "<%s.TextIOWrapper name=b'dummy' mode='r' encoding='utf-8'>" % modname) |
Benjamin Peterson | c4c0eae | 2009-03-09 00:07:03 +0000 | [diff] [blame] | 1726 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1727 | def test_line_buffering(self): |
| 1728 | r = self.BytesIO() |
| 1729 | b = self.BufferedWriter(r, 1000) |
| 1730 | t = self.TextIOWrapper(b, newline="\n", line_buffering=True) |
Guido van Rossum | f64db9f | 2007-12-06 01:04:26 +0000 | [diff] [blame] | 1731 | t.write("X") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1732 | self.assertEqual(r.getvalue(), b"") # No flush happened |
Guido van Rossum | f64db9f | 2007-12-06 01:04:26 +0000 | [diff] [blame] | 1733 | t.write("Y\nZ") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1734 | self.assertEqual(r.getvalue(), b"XY\nZ") # All got flushed |
Guido van Rossum | f64db9f | 2007-12-06 01:04:26 +0000 | [diff] [blame] | 1735 | t.write("A\rB") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1736 | self.assertEqual(r.getvalue(), b"XY\nZA\rB") |
Guido van Rossum | f64db9f | 2007-12-06 01:04:26 +0000 | [diff] [blame] | 1737 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1738 | def test_encoding(self): |
| 1739 | # Check the encoding attribute is always set, and valid |
| 1740 | b = self.BytesIO() |
| 1741 | t = self.TextIOWrapper(b, encoding="utf8") |
| 1742 | self.assertEqual(t.encoding, "utf8") |
| 1743 | t = self.TextIOWrapper(b) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1744 | self.assertTrue(t.encoding is not None) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1745 | codecs.lookup(t.encoding) |
| 1746 | |
| 1747 | def test_encoding_errors_reading(self): |
Guido van Rossum | e7fc50f | 2007-12-03 22:54:21 +0000 | [diff] [blame] | 1748 | # (1) default |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1749 | b = self.BytesIO(b"abc\n\xff\n") |
| 1750 | t = self.TextIOWrapper(b, encoding="ascii") |
Guido van Rossum | e7fc50f | 2007-12-03 22:54:21 +0000 | [diff] [blame] | 1751 | self.assertRaises(UnicodeError, t.read) |
| 1752 | # (2) explicit strict |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1753 | b = self.BytesIO(b"abc\n\xff\n") |
| 1754 | t = self.TextIOWrapper(b, encoding="ascii", errors="strict") |
Guido van Rossum | e7fc50f | 2007-12-03 22:54:21 +0000 | [diff] [blame] | 1755 | self.assertRaises(UnicodeError, t.read) |
| 1756 | # (3) ignore |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1757 | b = self.BytesIO(b"abc\n\xff\n") |
| 1758 | t = self.TextIOWrapper(b, encoding="ascii", errors="ignore") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1759 | self.assertEqual(t.read(), "abc\n\n") |
Guido van Rossum | e7fc50f | 2007-12-03 22:54:21 +0000 | [diff] [blame] | 1760 | # (4) replace |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1761 | b = self.BytesIO(b"abc\n\xff\n") |
| 1762 | t = self.TextIOWrapper(b, encoding="ascii", errors="replace") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1763 | self.assertEqual(t.read(), "abc\n\ufffd\n") |
Guido van Rossum | e7fc50f | 2007-12-03 22:54:21 +0000 | [diff] [blame] | 1764 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1765 | def test_encoding_errors_writing(self): |
Guido van Rossum | e7fc50f | 2007-12-03 22:54:21 +0000 | [diff] [blame] | 1766 | # (1) default |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1767 | b = self.BytesIO() |
| 1768 | t = self.TextIOWrapper(b, encoding="ascii") |
Guido van Rossum | e7fc50f | 2007-12-03 22:54:21 +0000 | [diff] [blame] | 1769 | self.assertRaises(UnicodeError, t.write, "\xff") |
| 1770 | # (2) explicit strict |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1771 | b = self.BytesIO() |
| 1772 | t = self.TextIOWrapper(b, encoding="ascii", errors="strict") |
Guido van Rossum | e7fc50f | 2007-12-03 22:54:21 +0000 | [diff] [blame] | 1773 | self.assertRaises(UnicodeError, t.write, "\xff") |
| 1774 | # (3) ignore |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1775 | b = self.BytesIO() |
| 1776 | t = self.TextIOWrapper(b, encoding="ascii", errors="ignore", |
Guido van Rossum | f64db9f | 2007-12-06 01:04:26 +0000 | [diff] [blame] | 1777 | newline="\n") |
Guido van Rossum | e7fc50f | 2007-12-03 22:54:21 +0000 | [diff] [blame] | 1778 | t.write("abc\xffdef\n") |
| 1779 | t.flush() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1780 | self.assertEqual(b.getvalue(), b"abcdef\n") |
Guido van Rossum | e7fc50f | 2007-12-03 22:54:21 +0000 | [diff] [blame] | 1781 | # (4) replace |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1782 | b = self.BytesIO() |
| 1783 | t = self.TextIOWrapper(b, encoding="ascii", errors="replace", |
Guido van Rossum | f64db9f | 2007-12-06 01:04:26 +0000 | [diff] [blame] | 1784 | newline="\n") |
Guido van Rossum | e7fc50f | 2007-12-03 22:54:21 +0000 | [diff] [blame] | 1785 | t.write("abc\xffdef\n") |
| 1786 | t.flush() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1787 | self.assertEqual(b.getvalue(), b"abc?def\n") |
Guido van Rossum | e7fc50f | 2007-12-03 22:54:21 +0000 | [diff] [blame] | 1788 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1789 | def test_newlines(self): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 1790 | input_lines = [ "unix\n", "windows\r\n", "os9\r", "last\n", "nonl" ] |
| 1791 | |
| 1792 | tests = [ |
| 1793 | [ None, [ 'unix\n', 'windows\n', 'os9\n', 'last\n', 'nonl' ] ], |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 1794 | [ '', input_lines ], |
| 1795 | [ '\n', [ "unix\n", "windows\r\n", "os9\rlast\n", "nonl" ] ], |
| 1796 | [ '\r\n', [ "unix\nwindows\r\n", "os9\rlast\nnonl" ] ], |
| 1797 | [ '\r', [ "unix\nwindows\r", "\nos9\r", "last\nnonl" ] ], |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 1798 | ] |
Antoine Pitrou | 180a336 | 2008-12-14 16:36:46 +0000 | [diff] [blame] | 1799 | encodings = ( |
| 1800 | 'utf-8', 'latin-1', |
| 1801 | 'utf-16', 'utf-16-le', 'utf-16-be', |
| 1802 | 'utf-32', 'utf-32-le', 'utf-32-be', |
| 1803 | ) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 1804 | |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 1805 | # 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] | 1806 | # character in TextIOWrapper._pending_line. |
| 1807 | for encoding in encodings: |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 1808 | # XXX: str.encode() should return bytes |
| 1809 | data = bytes(''.join(input_lines).encode(encoding)) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 1810 | for do_reads in (False, True): |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 1811 | for bufsize in range(1, 10): |
| 1812 | for newline, exp_lines in tests: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1813 | bufio = self.BufferedReader(self.BytesIO(data), bufsize) |
| 1814 | textio = self.TextIOWrapper(bufio, newline=newline, |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 1815 | encoding=encoding) |
| 1816 | if do_reads: |
| 1817 | got_lines = [] |
| 1818 | while True: |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 1819 | c2 = textio.read(2) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 1820 | if c2 == '': |
| 1821 | break |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1822 | self.assertEqual(len(c2), 2) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 1823 | got_lines.append(c2 + textio.readline()) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 1824 | else: |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 1825 | got_lines = list(textio) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 1826 | |
| 1827 | for got_line, exp_line in zip(got_lines, exp_lines): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1828 | self.assertEqual(got_line, exp_line) |
| 1829 | self.assertEqual(len(got_lines), len(exp_lines)) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 1830 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1831 | def test_newlines_input(self): |
| 1832 | testdata = b"AAA\nBB\x00B\nCCC\rDDD\rEEE\r\nFFF\r\nGGG" |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 1833 | normalized = testdata.replace(b"\r\n", b"\n").replace(b"\r", b"\n") |
| 1834 | for newline, expected in [ |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 1835 | (None, normalized.decode("ascii").splitlines(True)), |
| 1836 | ("", testdata.decode("ascii").splitlines(True)), |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1837 | ("\n", ["AAA\n", "BB\x00B\n", "CCC\rDDD\rEEE\r\n", "FFF\r\n", "GGG"]), |
| 1838 | ("\r\n", ["AAA\nBB\x00B\nCCC\rDDD\rEEE\r\n", "FFF\r\n", "GGG"]), |
| 1839 | ("\r", ["AAA\nBB\x00B\nCCC\r", "DDD\r", "EEE\r", "\nFFF\r", "\nGGG"]), |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 1840 | ]: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1841 | buf = self.BytesIO(testdata) |
| 1842 | txt = self.TextIOWrapper(buf, encoding="ascii", newline=newline) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1843 | self.assertEqual(txt.readlines(), expected) |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 1844 | txt.seek(0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1845 | self.assertEqual(txt.read(), "".join(expected)) |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 1846 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1847 | def test_newlines_output(self): |
| 1848 | testdict = { |
| 1849 | "": b"AAA\nBBB\nCCC\nX\rY\r\nZ", |
| 1850 | "\n": b"AAA\nBBB\nCCC\nX\rY\r\nZ", |
| 1851 | "\r": b"AAA\rBBB\rCCC\rX\rY\r\rZ", |
| 1852 | "\r\n": b"AAA\r\nBBB\r\nCCC\r\nX\rY\r\r\nZ", |
| 1853 | } |
| 1854 | tests = [(None, testdict[os.linesep])] + sorted(testdict.items()) |
| 1855 | for newline, expected in tests: |
| 1856 | buf = self.BytesIO() |
| 1857 | txt = self.TextIOWrapper(buf, encoding="ascii", newline=newline) |
| 1858 | txt.write("AAA\nB") |
| 1859 | txt.write("BB\nCCC\n") |
| 1860 | txt.write("X\rY\r\nZ") |
| 1861 | txt.flush() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1862 | self.assertEqual(buf.closed, False) |
| 1863 | self.assertEqual(buf.getvalue(), expected) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1864 | |
| 1865 | def test_destructor(self): |
| 1866 | l = [] |
| 1867 | base = self.BytesIO |
| 1868 | class MyBytesIO(base): |
| 1869 | def close(self): |
| 1870 | l.append(self.getvalue()) |
| 1871 | base.close(self) |
| 1872 | b = MyBytesIO() |
| 1873 | t = self.TextIOWrapper(b, encoding="ascii") |
| 1874 | t.write("abc") |
| 1875 | del t |
Benjamin Peterson | 24fb1d0 | 2009-04-24 23:26:21 +0000 | [diff] [blame] | 1876 | support.gc_collect() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1877 | self.assertEqual([b"abc"], l) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1878 | |
| 1879 | def test_override_destructor(self): |
| 1880 | record = [] |
| 1881 | class MyTextIO(self.TextIOWrapper): |
| 1882 | def __del__(self): |
| 1883 | record.append(1) |
| 1884 | try: |
| 1885 | f = super().__del__ |
| 1886 | except AttributeError: |
| 1887 | pass |
| 1888 | else: |
| 1889 | f() |
| 1890 | def close(self): |
| 1891 | record.append(2) |
| 1892 | super().close() |
| 1893 | def flush(self): |
| 1894 | record.append(3) |
| 1895 | super().flush() |
| 1896 | b = self.BytesIO() |
| 1897 | t = MyTextIO(b, encoding="ascii") |
| 1898 | del t |
Benjamin Peterson | 24fb1d0 | 2009-04-24 23:26:21 +0000 | [diff] [blame] | 1899 | support.gc_collect() |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1900 | self.assertEqual(record, [1, 2, 3]) |
| 1901 | |
| 1902 | def test_error_through_destructor(self): |
| 1903 | # Test that the exception state is not modified by a destructor, |
| 1904 | # even if close() fails. |
| 1905 | rawio = self.CloseFailureIO() |
| 1906 | def f(): |
| 1907 | self.TextIOWrapper(rawio).xyzzy |
| 1908 | with support.captured_output("stderr") as s: |
| 1909 | self.assertRaises(AttributeError, f) |
| 1910 | s = s.getvalue().strip() |
| 1911 | if s: |
| 1912 | # The destructor *may* have printed an unraisable error, check it |
| 1913 | self.assertEqual(len(s.splitlines()), 1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1914 | self.assertTrue(s.startswith("Exception IOError: "), s) |
| 1915 | self.assertTrue(s.endswith(" ignored"), s) |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 1916 | |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 1917 | # Systematic tests of the text I/O API |
| 1918 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1919 | def test_basic_io(self): |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 1920 | for chunksize in (1, 2, 3, 4, 5, 15, 16, 17, 31, 32, 33, 63, 64, 65): |
| 1921 | for enc in "ascii", "latin1", "utf8" :# , "utf-16-be", "utf-16-le": |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1922 | f = self.open(support.TESTFN, "w+", encoding=enc) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 1923 | f._CHUNK_SIZE = chunksize |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1924 | self.assertEqual(f.write("abc"), 3) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 1925 | f.close() |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1926 | f = self.open(support.TESTFN, "r+", encoding=enc) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 1927 | f._CHUNK_SIZE = chunksize |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1928 | self.assertEqual(f.tell(), 0) |
| 1929 | self.assertEqual(f.read(), "abc") |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 1930 | cookie = f.tell() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1931 | self.assertEqual(f.seek(0), 0) |
| 1932 | self.assertEqual(f.read(None), "abc") |
Benjamin Peterson | bf5ff76 | 2009-12-13 19:25:34 +0000 | [diff] [blame] | 1933 | f.seek(0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1934 | self.assertEqual(f.read(2), "ab") |
| 1935 | self.assertEqual(f.read(1), "c") |
| 1936 | self.assertEqual(f.read(1), "") |
| 1937 | self.assertEqual(f.read(), "") |
| 1938 | self.assertEqual(f.tell(), cookie) |
| 1939 | self.assertEqual(f.seek(0), 0) |
| 1940 | self.assertEqual(f.seek(0, 2), cookie) |
| 1941 | self.assertEqual(f.write("def"), 3) |
| 1942 | self.assertEqual(f.seek(cookie), cookie) |
| 1943 | self.assertEqual(f.read(), "def") |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 1944 | if enc.startswith("utf"): |
| 1945 | self.multi_line_test(f, enc) |
| 1946 | f.close() |
| 1947 | |
| 1948 | def multi_line_test(self, f, enc): |
| 1949 | f.seek(0) |
| 1950 | f.truncate() |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1951 | sample = "s\xff\u0fff\uffff" |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 1952 | wlines = [] |
Guido van Rossum | cba608c | 2007-04-11 14:19:59 +0000 | [diff] [blame] | 1953 | 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] | 1954 | chars = [] |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1955 | for i in range(size): |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 1956 | chars.append(sample[i % len(sample)]) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1957 | line = "".join(chars) + "\n" |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 1958 | wlines.append((f.tell(), line)) |
| 1959 | f.write(line) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 1960 | f.seek(0) |
| 1961 | rlines = [] |
| 1962 | while True: |
| 1963 | pos = f.tell() |
| 1964 | line = f.readline() |
| 1965 | if not line: |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 1966 | break |
| 1967 | rlines.append((pos, line)) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1968 | self.assertEqual(rlines, wlines) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 1969 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1970 | def test_telling(self): |
| 1971 | f = self.open(support.TESTFN, "w+", encoding="utf8") |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 1972 | p0 = f.tell() |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1973 | f.write("\xff\n") |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 1974 | p1 = f.tell() |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1975 | f.write("\xff\n") |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 1976 | p2 = f.tell() |
| 1977 | f.seek(0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1978 | self.assertEqual(f.tell(), p0) |
| 1979 | self.assertEqual(f.readline(), "\xff\n") |
| 1980 | self.assertEqual(f.tell(), p1) |
| 1981 | self.assertEqual(f.readline(), "\xff\n") |
| 1982 | self.assertEqual(f.tell(), p2) |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 1983 | f.seek(0) |
| 1984 | for line in f: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1985 | self.assertEqual(line, "\xff\n") |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 1986 | self.assertRaises(IOError, f.tell) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1987 | self.assertEqual(f.tell(), p2) |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 1988 | f.close() |
| 1989 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1990 | def test_seeking(self): |
| 1991 | chunk_size = _default_chunk_size() |
Guido van Rossum | d76e779 | 2007-04-17 02:38:04 +0000 | [diff] [blame] | 1992 | prefix_size = chunk_size - 2 |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1993 | u_prefix = "a" * prefix_size |
Guido van Rossum | d76e779 | 2007-04-17 02:38:04 +0000 | [diff] [blame] | 1994 | prefix = bytes(u_prefix.encode("utf-8")) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1995 | self.assertEqual(len(u_prefix), len(prefix)) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1996 | u_suffix = "\u8888\n" |
Guido van Rossum | d76e779 | 2007-04-17 02:38:04 +0000 | [diff] [blame] | 1997 | suffix = bytes(u_suffix.encode("utf-8")) |
| 1998 | line = prefix + suffix |
Benjamin Peterson | ebe5d8a | 2010-10-31 01:30:11 +0000 | [diff] [blame] | 1999 | with self.open(support.TESTFN, "wb") as f: |
| 2000 | f.write(line*2) |
| 2001 | with self.open(support.TESTFN, "r", encoding="utf-8") as f: |
| 2002 | s = f.read(prefix_size) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2003 | self.assertEqual(s, str(prefix, "ascii")) |
| 2004 | self.assertEqual(f.tell(), prefix_size) |
| 2005 | self.assertEqual(f.readline(), u_suffix) |
Guido van Rossum | d76e779 | 2007-04-17 02:38:04 +0000 | [diff] [blame] | 2006 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2007 | def test_seeking_too(self): |
Guido van Rossum | d76e779 | 2007-04-17 02:38:04 +0000 | [diff] [blame] | 2008 | # Regression test for a specific bug |
| 2009 | data = b'\xe0\xbf\xbf\n' |
Benjamin Peterson | ebe5d8a | 2010-10-31 01:30:11 +0000 | [diff] [blame] | 2010 | with self.open(support.TESTFN, "wb") as f: |
| 2011 | f.write(data) |
| 2012 | with self.open(support.TESTFN, "r", encoding="utf-8") as f: |
| 2013 | f._CHUNK_SIZE # Just test that it exists |
| 2014 | f._CHUNK_SIZE = 2 |
| 2015 | f.readline() |
| 2016 | f.tell() |
Guido van Rossum | d76e779 | 2007-04-17 02:38:04 +0000 | [diff] [blame] | 2017 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2018 | def test_seek_and_tell(self): |
| 2019 | #Test seek/tell using the StatefulIncrementalDecoder. |
| 2020 | # Make test faster by doing smaller seeks |
| 2021 | CHUNK_SIZE = 128 |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 2022 | |
Benjamin Peterson | 5fd871d | 2009-03-05 00:49:53 +0000 | [diff] [blame] | 2023 | def test_seek_and_tell_with_data(data, min_pos=0): |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 2024 | """Tell/seek to various points within a data stream and ensure |
| 2025 | that the decoded data returned by read() is consistent.""" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2026 | f = self.open(support.TESTFN, 'wb') |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 2027 | f.write(data) |
| 2028 | f.close() |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2029 | f = self.open(support.TESTFN, encoding='test_decoder') |
| 2030 | f._CHUNK_SIZE = CHUNK_SIZE |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 2031 | decoded = f.read() |
| 2032 | f.close() |
| 2033 | |
Neal Norwitz | e2b0705 | 2008-03-18 19:52:05 +0000 | [diff] [blame] | 2034 | for i in range(min_pos, len(decoded) + 1): # seek positions |
| 2035 | for j in [1, 5, len(decoded) - i]: # read lengths |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2036 | f = self.open(support.TESTFN, encoding='test_decoder') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2037 | self.assertEqual(f.read(i), decoded[:i]) |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 2038 | cookie = f.tell() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2039 | self.assertEqual(f.read(j), decoded[i:i + j]) |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 2040 | f.seek(cookie) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2041 | self.assertEqual(f.read(), decoded[i:]) |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 2042 | f.close() |
| 2043 | |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 2044 | # Enable the test decoder. |
| 2045 | StatefulIncrementalDecoder.codecEnabled = 1 |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 2046 | |
| 2047 | # Run the tests. |
| 2048 | try: |
| 2049 | # Try each test case. |
| 2050 | for input, _, _ in StatefulIncrementalDecoderTest.test_cases: |
Benjamin Peterson | 5fd871d | 2009-03-05 00:49:53 +0000 | [diff] [blame] | 2051 | test_seek_and_tell_with_data(input) |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 2052 | |
| 2053 | # Position each test case so that it crosses a chunk boundary. |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 2054 | for input, _, _ in StatefulIncrementalDecoderTest.test_cases: |
| 2055 | offset = CHUNK_SIZE - len(input)//2 |
| 2056 | prefix = b'.'*offset |
| 2057 | # Don't bother seeking into the prefix (takes too long). |
| 2058 | min_pos = offset*2 |
Benjamin Peterson | 5fd871d | 2009-03-05 00:49:53 +0000 | [diff] [blame] | 2059 | test_seek_and_tell_with_data(prefix + input, min_pos) |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 2060 | |
| 2061 | # Ensure our test decoder won't interfere with subsequent tests. |
| 2062 | finally: |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 2063 | StatefulIncrementalDecoder.codecEnabled = 0 |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 2064 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2065 | def test_encoded_writes(self): |
Alexandre Vassalotti | a38f73b | 2008-01-07 18:30:48 +0000 | [diff] [blame] | 2066 | data = "1234567890" |
| 2067 | tests = ("utf-16", |
| 2068 | "utf-16-le", |
| 2069 | "utf-16-be", |
| 2070 | "utf-32", |
| 2071 | "utf-32-le", |
| 2072 | "utf-32-be") |
| 2073 | for encoding in tests: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2074 | buf = self.BytesIO() |
| 2075 | f = self.TextIOWrapper(buf, encoding=encoding) |
Alexandre Vassalotti | a38f73b | 2008-01-07 18:30:48 +0000 | [diff] [blame] | 2076 | # Check if the BOM is written only once (see issue1753). |
| 2077 | f.write(data) |
| 2078 | f.write(data) |
| 2079 | f.seek(0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2080 | self.assertEqual(f.read(), data * 2) |
Benjamin Peterson | 9363a65 | 2009-03-05 00:42:09 +0000 | [diff] [blame] | 2081 | f.seek(0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2082 | self.assertEqual(f.read(), data * 2) |
| 2083 | self.assertEqual(buf.getvalue(), (data * 2).encode(encoding)) |
Alexandre Vassalotti | a38f73b | 2008-01-07 18:30:48 +0000 | [diff] [blame] | 2084 | |
Benjamin Peterson | a1b4901 | 2009-03-31 23:11:32 +0000 | [diff] [blame] | 2085 | def test_unreadable(self): |
| 2086 | class UnReadable(self.BytesIO): |
| 2087 | def readable(self): |
| 2088 | return False |
| 2089 | txt = self.TextIOWrapper(UnReadable()) |
| 2090 | self.assertRaises(IOError, txt.read) |
| 2091 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2092 | def test_read_one_by_one(self): |
| 2093 | txt = self.TextIOWrapper(self.BytesIO(b"AA\r\nBB")) |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 2094 | reads = "" |
| 2095 | while True: |
| 2096 | c = txt.read(1) |
| 2097 | if not c: |
| 2098 | break |
| 2099 | reads += c |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2100 | self.assertEqual(reads, "AA\nBB") |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 2101 | |
Benjamin Peterson | bf5ff76 | 2009-12-13 19:25:34 +0000 | [diff] [blame] | 2102 | def test_readlines(self): |
| 2103 | txt = self.TextIOWrapper(self.BytesIO(b"AA\nBB\nCC")) |
| 2104 | self.assertEqual(txt.readlines(), ["AA\n", "BB\n", "CC"]) |
| 2105 | txt.seek(0) |
| 2106 | self.assertEqual(txt.readlines(None), ["AA\n", "BB\n", "CC"]) |
| 2107 | txt.seek(0) |
| 2108 | self.assertEqual(txt.readlines(5), ["AA\n", "BB\n"]) |
| 2109 | |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 2110 | # read in amounts equal to TextIOWrapper._CHUNK_SIZE which is 128. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2111 | def test_read_by_chunk(self): |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 2112 | # make sure "\r\n" straddles 128 char boundary. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2113 | txt = self.TextIOWrapper(self.BytesIO(b"A" * 127 + b"\r\nB")) |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 2114 | reads = "" |
| 2115 | while True: |
| 2116 | c = txt.read(128) |
| 2117 | if not c: |
| 2118 | break |
| 2119 | reads += c |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2120 | self.assertEqual(reads, "A"*127+"\nB") |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 2121 | |
| 2122 | def test_issue1395_1(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2123 | txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii") |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 2124 | |
| 2125 | # read one char at a time |
| 2126 | reads = "" |
| 2127 | while True: |
| 2128 | c = txt.read(1) |
| 2129 | if not c: |
| 2130 | break |
| 2131 | reads += c |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2132 | self.assertEqual(reads, self.normalized) |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 2133 | |
| 2134 | def test_issue1395_2(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2135 | txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii") |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 2136 | txt._CHUNK_SIZE = 4 |
| 2137 | |
| 2138 | reads = "" |
| 2139 | while True: |
| 2140 | c = txt.read(4) |
| 2141 | if not c: |
| 2142 | break |
| 2143 | reads += c |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2144 | self.assertEqual(reads, self.normalized) |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 2145 | |
| 2146 | def test_issue1395_3(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2147 | txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii") |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 2148 | txt._CHUNK_SIZE = 4 |
| 2149 | |
| 2150 | reads = txt.read(4) |
| 2151 | reads += txt.read(4) |
| 2152 | reads += txt.readline() |
| 2153 | reads += txt.readline() |
| 2154 | reads += txt.readline() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2155 | self.assertEqual(reads, self.normalized) |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 2156 | |
| 2157 | def test_issue1395_4(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2158 | txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii") |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 2159 | txt._CHUNK_SIZE = 4 |
| 2160 | |
| 2161 | reads = txt.read(4) |
| 2162 | reads += txt.read() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2163 | self.assertEqual(reads, self.normalized) |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 2164 | |
| 2165 | def test_issue1395_5(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2166 | txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii") |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 2167 | txt._CHUNK_SIZE = 4 |
| 2168 | |
| 2169 | reads = txt.read(4) |
| 2170 | pos = txt.tell() |
| 2171 | txt.seek(0) |
| 2172 | txt.seek(pos) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2173 | self.assertEqual(txt.read(4), "BBB\n") |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 2174 | |
Ka-Ping Yee | ddaa706 | 2008-03-17 20:35:15 +0000 | [diff] [blame] | 2175 | def test_issue2282(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2176 | buffer = self.BytesIO(self.testdata) |
| 2177 | txt = self.TextIOWrapper(buffer, encoding="ascii") |
Ka-Ping Yee | ddaa706 | 2008-03-17 20:35:15 +0000 | [diff] [blame] | 2178 | |
| 2179 | self.assertEqual(buffer.seekable(), txt.seekable()) |
| 2180 | |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 2181 | def test_append_bom(self): |
| 2182 | # The BOM is not written again when appending to a non-empty file |
| 2183 | filename = support.TESTFN |
| 2184 | for charset in ('utf-8-sig', 'utf-16', 'utf-32'): |
| 2185 | with self.open(filename, 'w', encoding=charset) as f: |
| 2186 | f.write('aaa') |
| 2187 | pos = f.tell() |
| 2188 | with self.open(filename, 'rb') as f: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2189 | self.assertEqual(f.read(), 'aaa'.encode(charset)) |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 2190 | |
| 2191 | with self.open(filename, 'a', encoding=charset) as f: |
| 2192 | f.write('xxx') |
| 2193 | with self.open(filename, 'rb') as f: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2194 | self.assertEqual(f.read(), 'aaaxxx'.encode(charset)) |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 2195 | |
| 2196 | def test_seek_bom(self): |
| 2197 | # Same test, but when seeking manually |
| 2198 | filename = support.TESTFN |
| 2199 | for charset in ('utf-8-sig', 'utf-16', 'utf-32'): |
| 2200 | with self.open(filename, 'w', encoding=charset) as f: |
| 2201 | f.write('aaa') |
| 2202 | pos = f.tell() |
| 2203 | with self.open(filename, 'r+', encoding=charset) as f: |
| 2204 | f.seek(pos) |
| 2205 | f.write('zzz') |
| 2206 | f.seek(0) |
| 2207 | f.write('bbb') |
| 2208 | with self.open(filename, 'rb') as f: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2209 | self.assertEqual(f.read(), 'bbbzzz'.encode(charset)) |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 2210 | |
Benjamin Peterson | 0926ad1 | 2009-06-06 18:02:12 +0000 | [diff] [blame] | 2211 | def test_errors_property(self): |
| 2212 | with self.open(support.TESTFN, "w") as f: |
| 2213 | self.assertEqual(f.errors, "strict") |
| 2214 | with self.open(support.TESTFN, "w", errors="replace") as f: |
| 2215 | self.assertEqual(f.errors, "replace") |
| 2216 | |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame^] | 2217 | @support.no_tracing |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 2218 | @unittest.skipUnless(threading, 'Threading required for this test.') |
Amaury Forgeot d'Arc | ccd686a | 2009-08-29 23:00:38 +0000 | [diff] [blame] | 2219 | def test_threads_write(self): |
| 2220 | # Issue6750: concurrent writes could duplicate data |
| 2221 | event = threading.Event() |
| 2222 | with self.open(support.TESTFN, "w", buffering=1) as f: |
| 2223 | def run(n): |
| 2224 | text = "Thread%03d\n" % n |
| 2225 | event.wait() |
| 2226 | f.write(text) |
| 2227 | threads = [threading.Thread(target=lambda n=x: run(n)) |
| 2228 | for x in range(20)] |
| 2229 | for t in threads: |
| 2230 | t.start() |
| 2231 | time.sleep(0.02) |
| 2232 | event.set() |
| 2233 | for t in threads: |
| 2234 | t.join() |
| 2235 | with self.open(support.TESTFN) as f: |
| 2236 | content = f.read() |
| 2237 | for n in range(20): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2238 | self.assertEqual(content.count("Thread%03d\n" % n), 1) |
Amaury Forgeot d'Arc | ccd686a | 2009-08-29 23:00:38 +0000 | [diff] [blame] | 2239 | |
Antoine Pitrou | 6be8876 | 2010-05-03 16:48:20 +0000 | [diff] [blame] | 2240 | def test_flush_error_on_close(self): |
| 2241 | txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii") |
| 2242 | def bad_flush(): |
| 2243 | raise IOError() |
| 2244 | txt.flush = bad_flush |
| 2245 | self.assertRaises(IOError, txt.close) # exception not swallowed |
| 2246 | |
| 2247 | def test_multi_close(self): |
| 2248 | txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii") |
| 2249 | txt.close() |
| 2250 | txt.close() |
| 2251 | txt.close() |
| 2252 | self.assertRaises(ValueError, txt.flush) |
| 2253 | |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 2254 | def test_unseekable(self): |
| 2255 | txt = self.TextIOWrapper(self.MockUnseekableIO(self.testdata)) |
| 2256 | self.assertRaises(self.UnsupportedOperation, txt.tell) |
| 2257 | self.assertRaises(self.UnsupportedOperation, txt.seek, 0) |
| 2258 | |
Antoine Pitrou | 7f8f418 | 2010-12-21 21:20:59 +0000 | [diff] [blame] | 2259 | def test_readonly_attributes(self): |
| 2260 | txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii") |
| 2261 | buf = self.BytesIO(self.testdata) |
| 2262 | with self.assertRaises(AttributeError): |
| 2263 | txt.buffer = buf |
| 2264 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2265 | class CTextIOWrapperTest(TextIOWrapperTest): |
| 2266 | |
| 2267 | def test_initialization(self): |
| 2268 | r = self.BytesIO(b"\xc3\xa9\n\n") |
| 2269 | b = self.BufferedReader(r, 1000) |
| 2270 | t = self.TextIOWrapper(b) |
| 2271 | self.assertRaises(TypeError, t.__init__, b, newline=42) |
| 2272 | self.assertRaises(ValueError, t.read) |
| 2273 | self.assertRaises(ValueError, t.__init__, b, newline='xyzzy') |
| 2274 | self.assertRaises(ValueError, t.read) |
| 2275 | |
| 2276 | def test_garbage_collection(self): |
| 2277 | # C TextIOWrapper objects are collected, and collecting them flushes |
| 2278 | # all data to disk. |
| 2279 | # The Python version has __del__, so it ends in gc.garbage instead. |
| 2280 | rawio = io.FileIO(support.TESTFN, "wb") |
| 2281 | b = self.BufferedWriter(rawio) |
| 2282 | t = self.TextIOWrapper(b, encoding="ascii") |
| 2283 | t.write("456def") |
| 2284 | t.x = t |
| 2285 | wr = weakref.ref(t) |
| 2286 | del t |
Benjamin Peterson | 24fb1d0 | 2009-04-24 23:26:21 +0000 | [diff] [blame] | 2287 | support.gc_collect() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 2288 | self.assertTrue(wr() is None, wr) |
Hirokazu Yamamoto | c7d6aa4 | 2009-06-18 00:07:14 +0000 | [diff] [blame] | 2289 | with self.open(support.TESTFN, "rb") as f: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2290 | self.assertEqual(f.read(), b"456def") |
| 2291 | |
| 2292 | class PyTextIOWrapperTest(TextIOWrapperTest): |
| 2293 | pass |
| 2294 | |
| 2295 | |
| 2296 | class IncrementalNewlineDecoderTest(unittest.TestCase): |
| 2297 | |
| 2298 | def check_newline_decoding_utf8(self, decoder): |
Antoine Pitrou | 180a336 | 2008-12-14 16:36:46 +0000 | [diff] [blame] | 2299 | # UTF-8 specific tests for a newline decoder |
| 2300 | def _check_decode(b, s, **kwargs): |
| 2301 | # We exercise getstate() / setstate() as well as decode() |
| 2302 | state = decoder.getstate() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2303 | self.assertEqual(decoder.decode(b, **kwargs), s) |
Antoine Pitrou | 180a336 | 2008-12-14 16:36:46 +0000 | [diff] [blame] | 2304 | decoder.setstate(state) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2305 | self.assertEqual(decoder.decode(b, **kwargs), s) |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 2306 | |
Antoine Pitrou | 180a336 | 2008-12-14 16:36:46 +0000 | [diff] [blame] | 2307 | _check_decode(b'\xe8\xa2\x88', "\u8888") |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 2308 | |
Antoine Pitrou | 180a336 | 2008-12-14 16:36:46 +0000 | [diff] [blame] | 2309 | _check_decode(b'\xe8', "") |
| 2310 | _check_decode(b'\xa2', "") |
| 2311 | _check_decode(b'\x88', "\u8888") |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 2312 | |
Antoine Pitrou | 180a336 | 2008-12-14 16:36:46 +0000 | [diff] [blame] | 2313 | _check_decode(b'\xe8', "") |
| 2314 | _check_decode(b'\xa2', "") |
| 2315 | _check_decode(b'\x88', "\u8888") |
| 2316 | |
| 2317 | _check_decode(b'\xe8', "") |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 2318 | self.assertRaises(UnicodeDecodeError, decoder.decode, b'', final=True) |
| 2319 | |
Antoine Pitrou | 180a336 | 2008-12-14 16:36:46 +0000 | [diff] [blame] | 2320 | decoder.reset() |
| 2321 | _check_decode(b'\n', "\n") |
| 2322 | _check_decode(b'\r', "") |
| 2323 | _check_decode(b'', "\n", final=True) |
| 2324 | _check_decode(b'\r', "\n", final=True) |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 2325 | |
Antoine Pitrou | 180a336 | 2008-12-14 16:36:46 +0000 | [diff] [blame] | 2326 | _check_decode(b'\r', "") |
| 2327 | _check_decode(b'a', "\na") |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 2328 | |
Antoine Pitrou | 180a336 | 2008-12-14 16:36:46 +0000 | [diff] [blame] | 2329 | _check_decode(b'\r\r\n', "\n\n") |
| 2330 | _check_decode(b'\r', "") |
| 2331 | _check_decode(b'\r', "\n") |
| 2332 | _check_decode(b'\na', "\na") |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 2333 | |
Antoine Pitrou | 180a336 | 2008-12-14 16:36:46 +0000 | [diff] [blame] | 2334 | _check_decode(b'\xe8\xa2\x88\r\n', "\u8888\n") |
| 2335 | _check_decode(b'\xe8\xa2\x88', "\u8888") |
| 2336 | _check_decode(b'\n', "\n") |
| 2337 | _check_decode(b'\xe8\xa2\x88\r', "\u8888") |
| 2338 | _check_decode(b'\n', "\n") |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 2339 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2340 | def check_newline_decoding(self, decoder, encoding): |
Antoine Pitrou | 180a336 | 2008-12-14 16:36:46 +0000 | [diff] [blame] | 2341 | result = [] |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2342 | if encoding is not None: |
| 2343 | encoder = codecs.getincrementalencoder(encoding)() |
| 2344 | def _decode_bytewise(s): |
| 2345 | # Decode one byte at a time |
| 2346 | for b in encoder.encode(s): |
| 2347 | result.append(decoder.decode(bytes([b]))) |
| 2348 | else: |
| 2349 | encoder = None |
| 2350 | def _decode_bytewise(s): |
| 2351 | # Decode one char at a time |
| 2352 | for c in s: |
| 2353 | result.append(decoder.decode(c)) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2354 | self.assertEqual(decoder.newlines, None) |
Antoine Pitrou | 180a336 | 2008-12-14 16:36:46 +0000 | [diff] [blame] | 2355 | _decode_bytewise("abc\n\r") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2356 | self.assertEqual(decoder.newlines, '\n') |
Antoine Pitrou | 180a336 | 2008-12-14 16:36:46 +0000 | [diff] [blame] | 2357 | _decode_bytewise("\nabc") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2358 | self.assertEqual(decoder.newlines, ('\n', '\r\n')) |
Antoine Pitrou | 180a336 | 2008-12-14 16:36:46 +0000 | [diff] [blame] | 2359 | _decode_bytewise("abc\r") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2360 | self.assertEqual(decoder.newlines, ('\n', '\r\n')) |
Antoine Pitrou | 180a336 | 2008-12-14 16:36:46 +0000 | [diff] [blame] | 2361 | _decode_bytewise("abc") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2362 | self.assertEqual(decoder.newlines, ('\r', '\n', '\r\n')) |
Antoine Pitrou | 180a336 | 2008-12-14 16:36:46 +0000 | [diff] [blame] | 2363 | _decode_bytewise("abc\r") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2364 | self.assertEqual("".join(result), "abc\n\nabcabc\nabcabc") |
Antoine Pitrou | 180a336 | 2008-12-14 16:36:46 +0000 | [diff] [blame] | 2365 | decoder.reset() |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2366 | input = "abc" |
| 2367 | if encoder is not None: |
| 2368 | encoder.reset() |
| 2369 | input = encoder.encode(input) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2370 | self.assertEqual(decoder.decode(input), "abc") |
| 2371 | self.assertEqual(decoder.newlines, None) |
Antoine Pitrou | 180a336 | 2008-12-14 16:36:46 +0000 | [diff] [blame] | 2372 | |
| 2373 | def test_newline_decoder(self): |
| 2374 | encodings = ( |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2375 | # None meaning the IncrementalNewlineDecoder takes unicode input |
| 2376 | # rather than bytes input |
| 2377 | None, 'utf-8', 'latin-1', |
Antoine Pitrou | 180a336 | 2008-12-14 16:36:46 +0000 | [diff] [blame] | 2378 | 'utf-16', 'utf-16-le', 'utf-16-be', |
| 2379 | 'utf-32', 'utf-32-le', 'utf-32-be', |
| 2380 | ) |
| 2381 | for enc in encodings: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2382 | decoder = enc and codecs.getincrementaldecoder(enc)() |
| 2383 | decoder = self.IncrementalNewlineDecoder(decoder, translate=True) |
| 2384 | self.check_newline_decoding(decoder, enc) |
Alexandre Vassalotti | 472f07d | 2008-01-06 00:34:32 +0000 | [diff] [blame] | 2385 | decoder = codecs.getincrementaldecoder("utf-8")() |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2386 | decoder = self.IncrementalNewlineDecoder(decoder, translate=True) |
| 2387 | self.check_newline_decoding_utf8(decoder) |
| 2388 | |
Antoine Pitrou | 66913e2 | 2009-03-06 23:40:56 +0000 | [diff] [blame] | 2389 | def test_newline_bytes(self): |
| 2390 | # Issue 5433: Excessive optimization in IncrementalNewlineDecoder |
| 2391 | def _check(dec): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2392 | self.assertEqual(dec.newlines, None) |
| 2393 | self.assertEqual(dec.decode("\u0D00"), "\u0D00") |
| 2394 | self.assertEqual(dec.newlines, None) |
| 2395 | self.assertEqual(dec.decode("\u0A00"), "\u0A00") |
| 2396 | self.assertEqual(dec.newlines, None) |
Antoine Pitrou | 66913e2 | 2009-03-06 23:40:56 +0000 | [diff] [blame] | 2397 | dec = self.IncrementalNewlineDecoder(None, translate=False) |
| 2398 | _check(dec) |
| 2399 | dec = self.IncrementalNewlineDecoder(None, translate=True) |
| 2400 | _check(dec) |
| 2401 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2402 | class CIncrementalNewlineDecoderTest(IncrementalNewlineDecoderTest): |
| 2403 | pass |
| 2404 | |
| 2405 | class PyIncrementalNewlineDecoderTest(IncrementalNewlineDecoderTest): |
| 2406 | pass |
Antoine Pitrou | 180a336 | 2008-12-14 16:36:46 +0000 | [diff] [blame] | 2407 | |
Alexandre Vassalotti | 472f07d | 2008-01-06 00:34:32 +0000 | [diff] [blame] | 2408 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 2409 | # XXX Tests for open() |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 2410 | |
Guido van Rossum | 5abbf75 | 2007-08-27 17:39:33 +0000 | [diff] [blame] | 2411 | class MiscIOTest(unittest.TestCase): |
| 2412 | |
Barry Warsaw | 40e8246 | 2008-11-20 20:14:50 +0000 | [diff] [blame] | 2413 | def tearDown(self): |
| 2414 | support.unlink(support.TESTFN) |
| 2415 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2416 | def test___all__(self): |
| 2417 | for name in self.io.__all__: |
| 2418 | obj = getattr(self.io, name, None) |
Benjamin Peterson | bfb9594 | 2009-04-02 01:13:40 +0000 | [diff] [blame] | 2419 | self.assertTrue(obj is not None, name) |
Guido van Rossum | 5abbf75 | 2007-08-27 17:39:33 +0000 | [diff] [blame] | 2420 | if name == "open": |
| 2421 | continue |
Benjamin Peterson | 6a52a9c | 2009-04-29 22:00:44 +0000 | [diff] [blame] | 2422 | elif "error" in name.lower() or name == "UnsupportedOperation": |
Benjamin Peterson | bfb9594 | 2009-04-02 01:13:40 +0000 | [diff] [blame] | 2423 | self.assertTrue(issubclass(obj, Exception), name) |
| 2424 | elif not name.startswith("SEEK_"): |
| 2425 | self.assertTrue(issubclass(obj, self.IOBase)) |
Benjamin Peterson | 65676e4 | 2008-11-05 21:42:45 +0000 | [diff] [blame] | 2426 | |
Barry Warsaw | 40e8246 | 2008-11-20 20:14:50 +0000 | [diff] [blame] | 2427 | def test_attributes(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2428 | f = self.open(support.TESTFN, "wb", buffering=0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2429 | self.assertEqual(f.mode, "wb") |
Barry Warsaw | 40e8246 | 2008-11-20 20:14:50 +0000 | [diff] [blame] | 2430 | f.close() |
| 2431 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2432 | f = self.open(support.TESTFN, "U") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2433 | self.assertEqual(f.name, support.TESTFN) |
| 2434 | self.assertEqual(f.buffer.name, support.TESTFN) |
| 2435 | self.assertEqual(f.buffer.raw.name, support.TESTFN) |
| 2436 | self.assertEqual(f.mode, "U") |
| 2437 | self.assertEqual(f.buffer.mode, "rb") |
| 2438 | self.assertEqual(f.buffer.raw.mode, "rb") |
Barry Warsaw | 40e8246 | 2008-11-20 20:14:50 +0000 | [diff] [blame] | 2439 | f.close() |
| 2440 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2441 | f = self.open(support.TESTFN, "w+") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2442 | self.assertEqual(f.mode, "w+") |
| 2443 | self.assertEqual(f.buffer.mode, "rb+") # Does it really matter? |
| 2444 | self.assertEqual(f.buffer.raw.mode, "rb+") |
Barry Warsaw | 40e8246 | 2008-11-20 20:14:50 +0000 | [diff] [blame] | 2445 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2446 | g = self.open(f.fileno(), "wb", closefd=False) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 2447 | self.assertEqual(g.mode, "wb") |
| 2448 | self.assertEqual(g.raw.mode, "wb") |
| 2449 | self.assertEqual(g.name, f.fileno()) |
| 2450 | self.assertEqual(g.raw.name, f.fileno()) |
Barry Warsaw | 40e8246 | 2008-11-20 20:14:50 +0000 | [diff] [blame] | 2451 | f.close() |
| 2452 | g.close() |
| 2453 | |
Antoine Pitrou | 8043cf8 | 2009-01-09 19:54:29 +0000 | [diff] [blame] | 2454 | def test_io_after_close(self): |
| 2455 | for kwargs in [ |
| 2456 | {"mode": "w"}, |
| 2457 | {"mode": "wb"}, |
| 2458 | {"mode": "w", "buffering": 1}, |
| 2459 | {"mode": "w", "buffering": 2}, |
| 2460 | {"mode": "wb", "buffering": 0}, |
| 2461 | {"mode": "r"}, |
| 2462 | {"mode": "rb"}, |
| 2463 | {"mode": "r", "buffering": 1}, |
| 2464 | {"mode": "r", "buffering": 2}, |
| 2465 | {"mode": "rb", "buffering": 0}, |
| 2466 | {"mode": "w+"}, |
| 2467 | {"mode": "w+b"}, |
| 2468 | {"mode": "w+", "buffering": 1}, |
| 2469 | {"mode": "w+", "buffering": 2}, |
| 2470 | {"mode": "w+b", "buffering": 0}, |
| 2471 | ]: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2472 | f = self.open(support.TESTFN, **kwargs) |
Antoine Pitrou | 8043cf8 | 2009-01-09 19:54:29 +0000 | [diff] [blame] | 2473 | f.close() |
| 2474 | self.assertRaises(ValueError, f.flush) |
| 2475 | self.assertRaises(ValueError, f.fileno) |
| 2476 | self.assertRaises(ValueError, f.isatty) |
| 2477 | self.assertRaises(ValueError, f.__iter__) |
| 2478 | if hasattr(f, "peek"): |
| 2479 | self.assertRaises(ValueError, f.peek, 1) |
| 2480 | self.assertRaises(ValueError, f.read) |
| 2481 | if hasattr(f, "read1"): |
| 2482 | self.assertRaises(ValueError, f.read1, 1024) |
| 2483 | if hasattr(f, "readinto"): |
| 2484 | self.assertRaises(ValueError, f.readinto, bytearray(1024)) |
| 2485 | self.assertRaises(ValueError, f.readline) |
| 2486 | self.assertRaises(ValueError, f.readlines) |
| 2487 | self.assertRaises(ValueError, f.seek, 0) |
| 2488 | self.assertRaises(ValueError, f.tell) |
| 2489 | self.assertRaises(ValueError, f.truncate) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2490 | self.assertRaises(ValueError, f.write, |
| 2491 | b"" if "b" in kwargs['mode'] else "") |
Antoine Pitrou | 8043cf8 | 2009-01-09 19:54:29 +0000 | [diff] [blame] | 2492 | self.assertRaises(ValueError, f.writelines, []) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2493 | self.assertRaises(ValueError, next, f) |
Antoine Pitrou | 8043cf8 | 2009-01-09 19:54:29 +0000 | [diff] [blame] | 2494 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2495 | def test_blockingioerror(self): |
| 2496 | # Various BlockingIOError issues |
| 2497 | self.assertRaises(TypeError, self.BlockingIOError) |
| 2498 | self.assertRaises(TypeError, self.BlockingIOError, 1) |
| 2499 | self.assertRaises(TypeError, self.BlockingIOError, 1, 2, 3, 4) |
| 2500 | self.assertRaises(TypeError, self.BlockingIOError, 1, "", None) |
| 2501 | b = self.BlockingIOError(1, "") |
| 2502 | self.assertEqual(b.characters_written, 0) |
| 2503 | class C(str): |
| 2504 | pass |
| 2505 | c = C("") |
| 2506 | b = self.BlockingIOError(1, c) |
| 2507 | c.b = b |
| 2508 | b.c = c |
| 2509 | wr = weakref.ref(c) |
| 2510 | del c, b |
Benjamin Peterson | 24fb1d0 | 2009-04-24 23:26:21 +0000 | [diff] [blame] | 2511 | support.gc_collect() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 2512 | self.assertTrue(wr() is None, wr) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2513 | |
| 2514 | def test_abcs(self): |
| 2515 | # Test the visible base classes are ABCs. |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 2516 | self.assertIsInstance(self.IOBase, abc.ABCMeta) |
| 2517 | self.assertIsInstance(self.RawIOBase, abc.ABCMeta) |
| 2518 | self.assertIsInstance(self.BufferedIOBase, abc.ABCMeta) |
| 2519 | self.assertIsInstance(self.TextIOBase, abc.ABCMeta) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2520 | |
| 2521 | def _check_abc_inheritance(self, abcmodule): |
| 2522 | with self.open(support.TESTFN, "wb", buffering=0) as f: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 2523 | self.assertIsInstance(f, abcmodule.IOBase) |
| 2524 | self.assertIsInstance(f, abcmodule.RawIOBase) |
| 2525 | self.assertNotIsInstance(f, abcmodule.BufferedIOBase) |
| 2526 | self.assertNotIsInstance(f, abcmodule.TextIOBase) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2527 | with self.open(support.TESTFN, "wb") as f: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 2528 | self.assertIsInstance(f, abcmodule.IOBase) |
| 2529 | self.assertNotIsInstance(f, abcmodule.RawIOBase) |
| 2530 | self.assertIsInstance(f, abcmodule.BufferedIOBase) |
| 2531 | self.assertNotIsInstance(f, abcmodule.TextIOBase) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2532 | with self.open(support.TESTFN, "w") as f: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 2533 | self.assertIsInstance(f, abcmodule.IOBase) |
| 2534 | self.assertNotIsInstance(f, abcmodule.RawIOBase) |
| 2535 | self.assertNotIsInstance(f, abcmodule.BufferedIOBase) |
| 2536 | self.assertIsInstance(f, abcmodule.TextIOBase) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2537 | |
| 2538 | def test_abc_inheritance(self): |
| 2539 | # Test implementations inherit from their respective ABCs |
| 2540 | self._check_abc_inheritance(self) |
| 2541 | |
| 2542 | def test_abc_inheritance_official(self): |
| 2543 | # Test implementations inherit from the official ABCs of the |
| 2544 | # baseline "io" module. |
| 2545 | self._check_abc_inheritance(io) |
| 2546 | |
Antoine Pitrou | e033e06 | 2010-10-29 10:38:18 +0000 | [diff] [blame] | 2547 | def _check_warn_on_dealloc(self, *args, **kwargs): |
| 2548 | f = open(*args, **kwargs) |
| 2549 | r = repr(f) |
| 2550 | with self.assertWarns(ResourceWarning) as cm: |
| 2551 | f = None |
| 2552 | support.gc_collect() |
| 2553 | self.assertIn(r, str(cm.warning.args[0])) |
| 2554 | |
| 2555 | def test_warn_on_dealloc(self): |
| 2556 | self._check_warn_on_dealloc(support.TESTFN, "wb", buffering=0) |
| 2557 | self._check_warn_on_dealloc(support.TESTFN, "wb") |
| 2558 | self._check_warn_on_dealloc(support.TESTFN, "w") |
| 2559 | |
| 2560 | def _check_warn_on_dealloc_fd(self, *args, **kwargs): |
| 2561 | fds = [] |
Benjamin Peterson | 556c735 | 2010-10-31 01:35:43 +0000 | [diff] [blame] | 2562 | def cleanup_fds(): |
Antoine Pitrou | e033e06 | 2010-10-29 10:38:18 +0000 | [diff] [blame] | 2563 | for fd in fds: |
| 2564 | try: |
| 2565 | os.close(fd) |
| 2566 | except EnvironmentError as e: |
| 2567 | if e.errno != errno.EBADF: |
| 2568 | raise |
Benjamin Peterson | 556c735 | 2010-10-31 01:35:43 +0000 | [diff] [blame] | 2569 | self.addCleanup(cleanup_fds) |
| 2570 | r, w = os.pipe() |
| 2571 | fds += r, w |
| 2572 | self._check_warn_on_dealloc(r, *args, **kwargs) |
| 2573 | # When using closefd=False, there's no warning |
| 2574 | r, w = os.pipe() |
| 2575 | fds += r, w |
| 2576 | with warnings.catch_warnings(record=True) as recorded: |
| 2577 | open(r, *args, closefd=False, **kwargs) |
| 2578 | support.gc_collect() |
| 2579 | self.assertEqual(recorded, []) |
Antoine Pitrou | e033e06 | 2010-10-29 10:38:18 +0000 | [diff] [blame] | 2580 | |
| 2581 | def test_warn_on_dealloc_fd(self): |
| 2582 | self._check_warn_on_dealloc_fd("rb", buffering=0) |
| 2583 | self._check_warn_on_dealloc_fd("rb") |
| 2584 | self._check_warn_on_dealloc_fd("r") |
| 2585 | |
| 2586 | |
Antoine Pitrou | 243757e | 2010-11-05 21:15:39 +0000 | [diff] [blame] | 2587 | def test_pickling(self): |
| 2588 | # Pickling file objects is forbidden |
| 2589 | for kwargs in [ |
| 2590 | {"mode": "w"}, |
| 2591 | {"mode": "wb"}, |
| 2592 | {"mode": "wb", "buffering": 0}, |
| 2593 | {"mode": "r"}, |
| 2594 | {"mode": "rb"}, |
| 2595 | {"mode": "rb", "buffering": 0}, |
| 2596 | {"mode": "w+"}, |
| 2597 | {"mode": "w+b"}, |
| 2598 | {"mode": "w+b", "buffering": 0}, |
| 2599 | ]: |
| 2600 | for protocol in range(pickle.HIGHEST_PROTOCOL + 1): |
| 2601 | with self.open(support.TESTFN, **kwargs) as f: |
| 2602 | self.assertRaises(TypeError, pickle.dumps, f, protocol) |
| 2603 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2604 | class CMiscIOTest(MiscIOTest): |
| 2605 | io = io |
| 2606 | |
| 2607 | class PyMiscIOTest(MiscIOTest): |
| 2608 | io = pyio |
Barry Warsaw | 40e8246 | 2008-11-20 20:14:50 +0000 | [diff] [blame] | 2609 | |
Antoine Pitrou | b46b9d5 | 2010-08-21 19:09:32 +0000 | [diff] [blame] | 2610 | |
| 2611 | @unittest.skipIf(os.name == 'nt', 'POSIX signals required for this test.') |
| 2612 | class SignalsTest(unittest.TestCase): |
| 2613 | |
| 2614 | def setUp(self): |
| 2615 | self.oldalrm = signal.signal(signal.SIGALRM, self.alarm_interrupt) |
| 2616 | |
| 2617 | def tearDown(self): |
| 2618 | signal.signal(signal.SIGALRM, self.oldalrm) |
| 2619 | |
| 2620 | def alarm_interrupt(self, sig, frame): |
| 2621 | 1/0 |
| 2622 | |
| 2623 | @unittest.skipUnless(threading, 'Threading required for this test.') |
| 2624 | def check_interrupted_write(self, item, bytes, **fdopen_kwargs): |
| 2625 | """Check that a partial write, when it gets interrupted, properly |
| 2626 | invokes the signal handler.""" |
| 2627 | read_results = [] |
| 2628 | def _read(): |
| 2629 | s = os.read(r, 1) |
| 2630 | read_results.append(s) |
| 2631 | t = threading.Thread(target=_read) |
| 2632 | t.daemon = True |
| 2633 | r, w = os.pipe() |
Benjamin Peterson | d8fc2e1 | 2010-10-31 01:19:53 +0000 | [diff] [blame] | 2634 | fdopen_kwargs["closefd"] = False |
Antoine Pitrou | b46b9d5 | 2010-08-21 19:09:32 +0000 | [diff] [blame] | 2635 | try: |
| 2636 | wio = self.io.open(w, **fdopen_kwargs) |
| 2637 | t.start() |
| 2638 | signal.alarm(1) |
| 2639 | # Fill the pipe enough that the write will be blocking. |
| 2640 | # It will be interrupted by the timer armed above. Since the |
| 2641 | # other thread has read one byte, the low-level write will |
| 2642 | # return with a successful (partial) result rather than an EINTR. |
| 2643 | # The buffered IO layer must check for pending signal |
| 2644 | # handlers, which in this case will invoke alarm_interrupt(). |
| 2645 | self.assertRaises(ZeroDivisionError, |
| 2646 | wio.write, item * (1024 * 1024)) |
| 2647 | t.join() |
| 2648 | # We got one byte, get another one and check that it isn't a |
| 2649 | # repeat of the first one. |
| 2650 | read_results.append(os.read(r, 1)) |
| 2651 | self.assertEqual(read_results, [bytes[0:1], bytes[1:2]]) |
| 2652 | finally: |
| 2653 | os.close(w) |
| 2654 | os.close(r) |
| 2655 | # This is deliberate. If we didn't close the file descriptor |
| 2656 | # before closing wio, wio would try to flush its internal |
| 2657 | # buffer, and block again. |
| 2658 | try: |
| 2659 | wio.close() |
| 2660 | except IOError as e: |
| 2661 | if e.errno != errno.EBADF: |
| 2662 | raise |
| 2663 | |
| 2664 | def test_interrupted_write_unbuffered(self): |
| 2665 | self.check_interrupted_write(b"xy", b"xy", mode="wb", buffering=0) |
| 2666 | |
| 2667 | def test_interrupted_write_buffered(self): |
| 2668 | self.check_interrupted_write(b"xy", b"xy", mode="wb") |
| 2669 | |
| 2670 | def test_interrupted_write_text(self): |
| 2671 | self.check_interrupted_write("xy", b"xy", mode="w", encoding="ascii") |
| 2672 | |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame^] | 2673 | @support.no_tracing |
Antoine Pitrou | f3b68b3 | 2010-12-03 18:41:39 +0000 | [diff] [blame] | 2674 | def check_reentrant_write(self, data, **fdopen_kwargs): |
| 2675 | def on_alarm(*args): |
| 2676 | # Will be called reentrantly from the same thread |
| 2677 | wio.write(data) |
| 2678 | 1/0 |
| 2679 | signal.signal(signal.SIGALRM, on_alarm) |
| 2680 | r, w = os.pipe() |
| 2681 | wio = self.io.open(w, **fdopen_kwargs) |
| 2682 | try: |
| 2683 | signal.alarm(1) |
| 2684 | # Either the reentrant call to wio.write() fails with RuntimeError, |
| 2685 | # or the signal handler raises ZeroDivisionError. |
| 2686 | with self.assertRaises((ZeroDivisionError, RuntimeError)) as cm: |
| 2687 | while 1: |
| 2688 | for i in range(100): |
| 2689 | wio.write(data) |
| 2690 | wio.flush() |
| 2691 | # Make sure the buffer doesn't fill up and block further writes |
| 2692 | os.read(r, len(data) * 100) |
| 2693 | exc = cm.exception |
| 2694 | if isinstance(exc, RuntimeError): |
| 2695 | self.assertTrue(str(exc).startswith("reentrant call"), str(exc)) |
| 2696 | finally: |
| 2697 | wio.close() |
| 2698 | os.close(r) |
| 2699 | |
| 2700 | def test_reentrant_write_buffered(self): |
| 2701 | self.check_reentrant_write(b"xy", mode="wb") |
| 2702 | |
| 2703 | def test_reentrant_write_text(self): |
| 2704 | self.check_reentrant_write("xy", mode="w", encoding="ascii") |
| 2705 | |
| 2706 | |
Antoine Pitrou | b46b9d5 | 2010-08-21 19:09:32 +0000 | [diff] [blame] | 2707 | class CSignalsTest(SignalsTest): |
| 2708 | io = io |
| 2709 | |
| 2710 | class PySignalsTest(SignalsTest): |
| 2711 | io = pyio |
| 2712 | |
Antoine Pitrou | f3b68b3 | 2010-12-03 18:41:39 +0000 | [diff] [blame] | 2713 | # Handling reentrancy issues would slow down _pyio even more, so the |
| 2714 | # tests are disabled. |
| 2715 | test_reentrant_write_buffered = None |
| 2716 | test_reentrant_write_text = None |
| 2717 | |
Antoine Pitrou | b46b9d5 | 2010-08-21 19:09:32 +0000 | [diff] [blame] | 2718 | |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 2719 | def test_main(): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2720 | tests = (CIOTest, PyIOTest, |
| 2721 | CBufferedReaderTest, PyBufferedReaderTest, |
| 2722 | CBufferedWriterTest, PyBufferedWriterTest, |
| 2723 | CBufferedRWPairTest, PyBufferedRWPairTest, |
| 2724 | CBufferedRandomTest, PyBufferedRandomTest, |
| 2725 | StatefulIncrementalDecoderTest, |
| 2726 | CIncrementalNewlineDecoderTest, PyIncrementalNewlineDecoderTest, |
| 2727 | CTextIOWrapperTest, PyTextIOWrapperTest, |
Antoine Pitrou | b46b9d5 | 2010-08-21 19:09:32 +0000 | [diff] [blame] | 2728 | CMiscIOTest, PyMiscIOTest, |
| 2729 | CSignalsTest, PySignalsTest, |
| 2730 | ) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2731 | |
| 2732 | # Put the namespaces of the IO module we are testing and some useful mock |
| 2733 | # classes in the __dict__ of each test. |
| 2734 | mocks = (MockRawIO, MisbehavedRawIO, MockFileIO, CloseFailureIO, |
Antoine Pitrou | 328ec74 | 2010-09-14 18:37:24 +0000 | [diff] [blame] | 2735 | MockNonBlockWriterIO, MockUnseekableIO, MockRawIOWithoutRead) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2736 | all_members = io.__all__ + ["IncrementalNewlineDecoder"] |
| 2737 | c_io_ns = {name : getattr(io, name) for name in all_members} |
| 2738 | py_io_ns = {name : getattr(pyio, name) for name in all_members} |
| 2739 | globs = globals() |
| 2740 | c_io_ns.update((x.__name__, globs["C" + x.__name__]) for x in mocks) |
| 2741 | py_io_ns.update((x.__name__, globs["Py" + x.__name__]) for x in mocks) |
| 2742 | # Avoid turning open into a bound method. |
| 2743 | py_io_ns["open"] = pyio.OpenWrapper |
| 2744 | for test in tests: |
| 2745 | if test.__name__.startswith("C"): |
| 2746 | for name, obj in c_io_ns.items(): |
| 2747 | setattr(test, name, obj) |
| 2748 | elif test.__name__.startswith("Py"): |
| 2749 | for name, obj in py_io_ns.items(): |
| 2750 | setattr(test, name, obj) |
| 2751 | |
| 2752 | support.run_unittest(*tests) |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 2753 | |
| 2754 | if __name__ == "__main__": |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2755 | test_main() |