Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 1 | """Unit tests for io.py.""" |
| 2 | |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 3 | import os |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 4 | import sys |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 5 | import time |
Guido van Rossum | d410395 | 2007-04-12 05:44:49 +0000 | [diff] [blame] | 6 | import array |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 7 | import unittest |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 8 | from itertools import chain |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 9 | from test import support |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 10 | |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 11 | import codecs |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 12 | import io # The module under test |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 15 | class MockRawIO(io.RawIOBase): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 17 | def __init__(self, read_stack=()): |
| 18 | self._read_stack = list(read_stack) |
| 19 | self._write_stack = [] |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 20 | |
| 21 | def read(self, n=None): |
| 22 | try: |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 23 | return self._read_stack.pop(0) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 24 | except: |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 25 | return b"" |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 27 | def write(self, b): |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 28 | self._write_stack.append(b[:]) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 29 | return len(b) |
| 30 | |
| 31 | def writable(self): |
| 32 | return True |
| 33 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 34 | def fileno(self): |
| 35 | return 42 |
| 36 | |
| 37 | def readable(self): |
| 38 | return True |
| 39 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 40 | def seekable(self): |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 41 | return True |
| 42 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 43 | def seek(self, pos, whence): |
| 44 | pass |
| 45 | |
| 46 | def tell(self): |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 47 | return 42 |
| 48 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 49 | |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 50 | class MockFileIO(io.BytesIO): |
| 51 | |
| 52 | def __init__(self, data): |
| 53 | self.read_history = [] |
| 54 | io.BytesIO.__init__(self, data) |
| 55 | |
| 56 | def read(self, n=None): |
| 57 | res = io.BytesIO.read(self, n) |
| 58 | self.read_history.append(None if res is None else len(res)) |
| 59 | return res |
| 60 | |
| 61 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 62 | class MockNonBlockWriterIO(io.RawIOBase): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 64 | def __init__(self, blocking_script): |
| 65 | self._blocking_script = list(blocking_script) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 66 | self._write_stack = [] |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 67 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 68 | def write(self, b): |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 69 | self._write_stack.append(b[:]) |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 70 | n = self._blocking_script.pop(0) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 71 | if (n < 0): |
Guido van Rossum | 141f767 | 2007-04-10 00:22:16 +0000 | [diff] [blame] | 72 | raise io.BlockingIOError(0, "test blocking", -n) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 73 | else: |
| 74 | return n |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 76 | def writable(self): |
| 77 | return True |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 78 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 79 | |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 80 | class IOTest(unittest.TestCase): |
| 81 | |
Neal Norwitz | e7789b1 | 2008-03-24 06:18:09 +0000 | [diff] [blame] | 82 | def setUp(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 83 | support.unlink(support.TESTFN) |
Neal Norwitz | e7789b1 | 2008-03-24 06:18:09 +0000 | [diff] [blame] | 84 | |
Guido van Rossum | 4d0f5a4 | 2007-03-07 22:59:39 +0000 | [diff] [blame] | 85 | def tearDown(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 86 | support.unlink(support.TESTFN) |
Guido van Rossum | 4d0f5a4 | 2007-03-07 22:59:39 +0000 | [diff] [blame] | 87 | |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 88 | def write_ops(self, f): |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 89 | self.assertEqual(f.write(b"blah."), 5) |
| 90 | self.assertEqual(f.seek(0), 0) |
| 91 | self.assertEqual(f.write(b"Hello."), 6) |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 92 | self.assertEqual(f.tell(), 6) |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 93 | self.assertEqual(f.seek(-1, 1), 5) |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 94 | self.assertEqual(f.tell(), 5) |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 95 | self.assertEqual(f.write(bytearray(b" world\n\n\n")), 9) |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 96 | self.assertEqual(f.seek(0), 0) |
Guido van Rossum | 2b08b38 | 2007-05-08 20:18:39 +0000 | [diff] [blame] | 97 | self.assertEqual(f.write(b"h"), 1) |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 98 | self.assertEqual(f.seek(-1, 2), 13) |
| 99 | self.assertEqual(f.tell(), 13) |
| 100 | self.assertEqual(f.truncate(12), 12) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 101 | self.assertEqual(f.tell(), 12) |
Christian Heimes | 8e42a0a | 2007-11-08 18:04:45 +0000 | [diff] [blame] | 102 | self.assertRaises(TypeError, f.seek, 0.0) |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 103 | |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 104 | def read_ops(self, f, buffered=False): |
| 105 | data = f.read(5) |
| 106 | self.assertEqual(data, b"hello") |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 107 | data = bytearray(data) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 108 | self.assertEqual(f.readinto(data), 5) |
| 109 | self.assertEqual(data, b" worl") |
| 110 | self.assertEqual(f.readinto(data), 2) |
| 111 | self.assertEqual(len(data), 5) |
| 112 | self.assertEqual(data[:2], b"d\n") |
| 113 | self.assertEqual(f.seek(0), 0) |
| 114 | self.assertEqual(f.read(20), b"hello world\n") |
| 115 | self.assertEqual(f.read(1), b"") |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 116 | self.assertEqual(f.readinto(bytearray(b"x")), 0) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 117 | self.assertEqual(f.seek(-6, 2), 6) |
| 118 | self.assertEqual(f.read(5), b"world") |
| 119 | self.assertEqual(f.read(0), b"") |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 120 | self.assertEqual(f.readinto(bytearray()), 0) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 121 | self.assertEqual(f.seek(-6, 1), 5) |
| 122 | self.assertEqual(f.read(5), b" worl") |
| 123 | self.assertEqual(f.tell(), 10) |
Christian Heimes | 8e42a0a | 2007-11-08 18:04:45 +0000 | [diff] [blame] | 124 | self.assertRaises(TypeError, f.seek, 0.0) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 125 | if buffered: |
| 126 | f.seek(0) |
| 127 | self.assertEqual(f.read(), b"hello world\n") |
| 128 | f.seek(6) |
| 129 | self.assertEqual(f.read(), b"world\n") |
| 130 | self.assertEqual(f.read(), b"") |
| 131 | |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 132 | LARGE = 2**31 |
| 133 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 134 | def large_file_ops(self, f): |
| 135 | assert f.readable() |
| 136 | assert f.writable() |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 137 | self.assertEqual(f.seek(self.LARGE), self.LARGE) |
| 138 | self.assertEqual(f.tell(), self.LARGE) |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 139 | self.assertEqual(f.write(b"xxx"), 3) |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 140 | self.assertEqual(f.tell(), self.LARGE + 3) |
| 141 | self.assertEqual(f.seek(-1, 1), self.LARGE + 2) |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 142 | self.assertEqual(f.truncate(), self.LARGE + 2) |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 143 | self.assertEqual(f.tell(), self.LARGE + 2) |
| 144 | self.assertEqual(f.seek(0, 2), self.LARGE + 2) |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 145 | self.assertEqual(f.truncate(self.LARGE + 1), self.LARGE + 1) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 146 | self.assertEqual(f.tell(), self.LARGE + 1) |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 147 | self.assertEqual(f.seek(0, 2), self.LARGE + 1) |
| 148 | self.assertEqual(f.seek(-1, 2), self.LARGE) |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 149 | self.assertEqual(f.read(2), b"x") |
| 150 | |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 151 | def test_raw_file_io(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 152 | f = io.open(support.TESTFN, "wb", buffering=0) |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 153 | self.assertEqual(f.readable(), False) |
| 154 | self.assertEqual(f.writable(), True) |
| 155 | self.assertEqual(f.seekable(), True) |
| 156 | self.write_ops(f) |
| 157 | f.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 158 | f = io.open(support.TESTFN, "rb", buffering=0) |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 159 | self.assertEqual(f.readable(), True) |
| 160 | self.assertEqual(f.writable(), False) |
| 161 | self.assertEqual(f.seekable(), True) |
| 162 | self.read_ops(f) |
| 163 | f.close() |
| 164 | |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 165 | def test_buffered_file_io(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 166 | f = io.open(support.TESTFN, "wb") |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 167 | self.assertEqual(f.readable(), False) |
| 168 | self.assertEqual(f.writable(), True) |
| 169 | self.assertEqual(f.seekable(), True) |
| 170 | self.write_ops(f) |
| 171 | f.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 172 | f = io.open(support.TESTFN, "rb") |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 173 | self.assertEqual(f.readable(), True) |
| 174 | self.assertEqual(f.writable(), False) |
| 175 | self.assertEqual(f.seekable(), True) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 176 | self.read_ops(f, True) |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 177 | f.close() |
| 178 | |
Guido van Rossum | 48fc58a | 2007-06-07 23:45:37 +0000 | [diff] [blame] | 179 | def test_readline(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 180 | f = io.open(support.TESTFN, "wb") |
Guido van Rossum | 48fc58a | 2007-06-07 23:45:37 +0000 | [diff] [blame] | 181 | f.write(b"abc\ndef\nxyzzy\nfoo") |
| 182 | f.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 183 | f = io.open(support.TESTFN, "rb") |
Guido van Rossum | 48fc58a | 2007-06-07 23:45:37 +0000 | [diff] [blame] | 184 | self.assertEqual(f.readline(), b"abc\n") |
| 185 | self.assertEqual(f.readline(10), b"def\n") |
| 186 | self.assertEqual(f.readline(2), b"xy") |
| 187 | self.assertEqual(f.readline(4), b"zzy\n") |
| 188 | self.assertEqual(f.readline(), b"foo") |
| 189 | f.close() |
| 190 | |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 191 | def test_raw_bytes_io(self): |
| 192 | f = io.BytesIO() |
| 193 | self.write_ops(f) |
| 194 | data = f.getvalue() |
| 195 | self.assertEqual(data, b"hello world\n") |
| 196 | f = io.BytesIO(data) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 197 | self.read_ops(f, True) |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 198 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 199 | def test_large_file_ops(self): |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 200 | # On Windows and Mac OSX this test comsumes large resources; It takes |
| 201 | # a long time to build the >2GB file and takes >2GB of disk space |
| 202 | # therefore the resource must be enabled to run this test. |
| 203 | if sys.platform[:3] == 'win' or sys.platform == 'darwin': |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 204 | if not support.is_resource_enabled("largefile"): |
Guido van Rossum | 34d69e5 | 2007-04-10 20:08:41 +0000 | [diff] [blame] | 205 | print("\nTesting large file ops skipped on %s." % sys.platform, |
| 206 | file=sys.stderr) |
| 207 | print("It requires %d bytes and a long time." % self.LARGE, |
| 208 | file=sys.stderr) |
| 209 | print("Use 'regrtest.py -u largefile test_io' to run it.", |
| 210 | file=sys.stderr) |
| 211 | return |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 212 | f = io.open(support.TESTFN, "w+b", 0) |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 213 | self.large_file_ops(f) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 214 | f.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 215 | f = io.open(support.TESTFN, "w+b") |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 216 | self.large_file_ops(f) |
| 217 | f.close() |
| 218 | |
| 219 | def test_with_open(self): |
| 220 | for bufsize in (0, 1, 100): |
| 221 | f = None |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 222 | with open(support.TESTFN, "wb", bufsize) as f: |
Guido van Rossum | 1f2ca56 | 2007-08-27 20:44:15 +0000 | [diff] [blame] | 223 | f.write(b"xxx") |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 224 | self.assertEqual(f.closed, True) |
| 225 | f = None |
| 226 | try: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 227 | with open(support.TESTFN, "wb", bufsize) as f: |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 228 | 1/0 |
| 229 | except ZeroDivisionError: |
| 230 | self.assertEqual(f.closed, True) |
| 231 | else: |
| 232 | self.fail("1/0 didn't raise an exception") |
| 233 | |
| 234 | def test_destructor(self): |
| 235 | record = [] |
| 236 | class MyFileIO(io.FileIO): |
| 237 | def __del__(self): |
| 238 | record.append(1) |
| 239 | io.FileIO.__del__(self) |
| 240 | def close(self): |
| 241 | record.append(2) |
| 242 | io.FileIO.close(self) |
| 243 | def flush(self): |
| 244 | record.append(3) |
| 245 | io.FileIO.flush(self) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 246 | f = MyFileIO(support.TESTFN, "w") |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 247 | f.write("xxx") |
| 248 | del f |
| 249 | self.assertEqual(record, [1, 2, 3]) |
| 250 | |
| 251 | def test_close_flushes(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 252 | f = io.open(support.TESTFN, "wb") |
Guido van Rossum | 2b08b38 | 2007-05-08 20:18:39 +0000 | [diff] [blame] | 253 | f.write(b"xxx") |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 254 | f.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 255 | f = io.open(support.TESTFN, "rb") |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 256 | self.assertEqual(f.read(), b"xxx") |
| 257 | f.close() |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 258 | |
Guido van Rossum | d410395 | 2007-04-12 05:44:49 +0000 | [diff] [blame] | 259 | def test_array_writes(self): |
| 260 | a = array.array('i', range(10)) |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 261 | n = len(memoryview(a)) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 262 | f = io.open(support.TESTFN, "wb", 0) |
Guido van Rossum | d410395 | 2007-04-12 05:44:49 +0000 | [diff] [blame] | 263 | self.assertEqual(f.write(a), n) |
| 264 | f.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 265 | f = io.open(support.TESTFN, "wb") |
Guido van Rossum | d410395 | 2007-04-12 05:44:49 +0000 | [diff] [blame] | 266 | self.assertEqual(f.write(a), n) |
| 267 | f.close() |
| 268 | |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 269 | def test_closefd(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 270 | self.assertRaises(ValueError, io.open, support.TESTFN, 'w', |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 271 | closefd=False) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 272 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 273 | class MemorySeekTestMixin: |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 274 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 275 | def testInit(self): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 276 | buf = self.buftype("1234567890") |
| 277 | bytesIo = self.ioclass(buf) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 278 | |
| 279 | def testRead(self): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 280 | buf = self.buftype("1234567890") |
| 281 | bytesIo = self.ioclass(buf) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 282 | |
| 283 | self.assertEquals(buf[:1], bytesIo.read(1)) |
| 284 | self.assertEquals(buf[1:5], bytesIo.read(4)) |
| 285 | self.assertEquals(buf[5:], bytesIo.read(900)) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 286 | self.assertEquals(self.EOF, bytesIo.read()) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 287 | |
| 288 | def testReadNoArgs(self): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 289 | buf = self.buftype("1234567890") |
| 290 | bytesIo = self.ioclass(buf) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 291 | |
| 292 | self.assertEquals(buf, bytesIo.read()) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 293 | self.assertEquals(self.EOF, bytesIo.read()) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 294 | |
| 295 | def testSeek(self): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 296 | buf = self.buftype("1234567890") |
| 297 | bytesIo = self.ioclass(buf) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 298 | |
| 299 | bytesIo.read(5) |
| 300 | bytesIo.seek(0) |
| 301 | self.assertEquals(buf, bytesIo.read()) |
| 302 | |
| 303 | bytesIo.seek(3) |
| 304 | self.assertEquals(buf[3:], bytesIo.read()) |
Christian Heimes | 8e42a0a | 2007-11-08 18:04:45 +0000 | [diff] [blame] | 305 | self.assertRaises(TypeError, bytesIo.seek, 0.0) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 306 | |
| 307 | def testTell(self): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 308 | buf = self.buftype("1234567890") |
| 309 | bytesIo = self.ioclass(buf) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 310 | |
| 311 | self.assertEquals(0, bytesIo.tell()) |
| 312 | bytesIo.seek(5) |
| 313 | self.assertEquals(5, bytesIo.tell()) |
| 314 | bytesIo.seek(10000) |
| 315 | self.assertEquals(10000, bytesIo.tell()) |
| 316 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 317 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 318 | class BytesIOTest(MemorySeekTestMixin, unittest.TestCase): |
Guido van Rossum | 39478e8 | 2007-08-27 17:23:59 +0000 | [diff] [blame] | 319 | @staticmethod |
| 320 | def buftype(s): |
| 321 | return s.encode("utf-8") |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 322 | ioclass = io.BytesIO |
| 323 | EOF = b"" |
| 324 | |
| 325 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 326 | class StringIOTest(MemorySeekTestMixin, unittest.TestCase): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 327 | buftype = str |
| 328 | ioclass = io.StringIO |
| 329 | EOF = "" |
| 330 | |
| 331 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 332 | class BufferedReaderTest(unittest.TestCase): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 333 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 334 | def testRead(self): |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 335 | rawio = MockRawIO((b"abc", b"d", b"efg")) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 336 | bufio = io.BufferedReader(rawio) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 337 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 338 | self.assertEquals(b"abcdef", bufio.read(6)) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 339 | |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 340 | def testBuffering(self): |
| 341 | data = b"abcdefghi" |
| 342 | dlen = len(data) |
| 343 | |
| 344 | tests = [ |
| 345 | [ 100, [ 3, 1, 4, 8 ], [ dlen, 0 ] ], |
| 346 | [ 100, [ 3, 3, 3], [ dlen ] ], |
| 347 | [ 4, [ 1, 2, 4, 2 ], [ 4, 4, 1 ] ], |
| 348 | ] |
| 349 | |
| 350 | for bufsize, buf_read_sizes, raw_read_sizes in tests: |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 351 | rawio = MockFileIO(data) |
| 352 | bufio = io.BufferedReader(rawio, buffer_size=bufsize) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 353 | pos = 0 |
| 354 | for nbytes in buf_read_sizes: |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 355 | self.assertEquals(bufio.read(nbytes), data[pos:pos+nbytes]) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 356 | pos += nbytes |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 357 | self.assertEquals(rawio.read_history, raw_read_sizes) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 358 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 359 | def testReadNonBlocking(self): |
| 360 | # Inject some None's in there to simulate EWOULDBLOCK |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 361 | rawio = MockRawIO((b"abc", b"d", None, b"efg", None, None)) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 362 | bufio = io.BufferedReader(rawio) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 363 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 364 | self.assertEquals(b"abcd", bufio.read(6)) |
| 365 | self.assertEquals(b"e", bufio.read(1)) |
| 366 | self.assertEquals(b"fg", bufio.read()) |
| 367 | self.assert_(None is bufio.read()) |
| 368 | self.assertEquals(b"", bufio.read()) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 369 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 370 | def testReadToEof(self): |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 371 | rawio = MockRawIO((b"abc", b"d", b"efg")) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 372 | bufio = io.BufferedReader(rawio) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 373 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 374 | self.assertEquals(b"abcdefg", bufio.read(9000)) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 375 | |
| 376 | def testReadNoArgs(self): |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 377 | rawio = MockRawIO((b"abc", b"d", b"efg")) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 378 | bufio = io.BufferedReader(rawio) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 379 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 380 | self.assertEquals(b"abcdefg", bufio.read()) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 381 | |
| 382 | def testFileno(self): |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 383 | rawio = MockRawIO((b"abc", b"d", b"efg")) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 384 | bufio = io.BufferedReader(rawio) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 385 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 386 | self.assertEquals(42, bufio.fileno()) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 387 | |
| 388 | def testFilenoNoFileno(self): |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 389 | # XXX will we always have fileno() function? If so, kill |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 390 | # this test. Else, write it. |
| 391 | pass |
| 392 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 393 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 394 | class BufferedWriterTest(unittest.TestCase): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 395 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 396 | def testWrite(self): |
| 397 | # Write to the buffered IO but don't overflow the buffer. |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 398 | writer = MockRawIO() |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 399 | bufio = io.BufferedWriter(writer, 8) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 400 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 401 | bufio.write(b"abc") |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 402 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 403 | self.assertFalse(writer._write_stack) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 404 | |
| 405 | def testWriteOverflow(self): |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 406 | writer = MockRawIO() |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 407 | bufio = io.BufferedWriter(writer, 8) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 408 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 409 | bufio.write(b"abc") |
| 410 | bufio.write(b"defghijkl") |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 411 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 412 | self.assertEquals(b"abcdefghijkl", writer._write_stack[0]) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 413 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 414 | def testWriteNonBlocking(self): |
| 415 | raw = MockNonBlockWriterIO((9, 2, 22, -6, 10, 12, 12)) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 416 | bufio = io.BufferedWriter(raw, 8, 16) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 417 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 418 | bufio.write(b"asdf") |
| 419 | bufio.write(b"asdfa") |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 420 | self.assertEquals(b"asdfasdfa", raw._write_stack[0]) |
| 421 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 422 | bufio.write(b"asdfasdfasdf") |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 423 | self.assertEquals(b"asdfasdfasdf", raw._write_stack[1]) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 424 | bufio.write(b"asdfasdfasdf") |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 425 | self.assertEquals(b"dfasdfasdf", raw._write_stack[2]) |
| 426 | self.assertEquals(b"asdfasdfasdf", raw._write_stack[3]) |
| 427 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 428 | bufio.write(b"asdfasdfasdf") |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 429 | |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 430 | # XXX I don't like this test. It relies too heavily on how the |
| 431 | # algorithm actually works, which we might change. Refactor |
| 432 | # later. |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 433 | |
| 434 | def testFileno(self): |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 435 | rawio = MockRawIO((b"abc", b"d", b"efg")) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 436 | bufio = io.BufferedWriter(rawio) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 437 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 438 | self.assertEquals(42, bufio.fileno()) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 439 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 440 | def testFlush(self): |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 441 | writer = MockRawIO() |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 442 | bufio = io.BufferedWriter(writer, 8) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 443 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 444 | bufio.write(b"abc") |
| 445 | bufio.flush() |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 446 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 447 | self.assertEquals(b"abc", writer._write_stack[0]) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 448 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 449 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 450 | class BufferedRWPairTest(unittest.TestCase): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 451 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 452 | def testRWPair(self): |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 453 | r = MockRawIO(()) |
| 454 | w = MockRawIO() |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 455 | pair = io.BufferedRWPair(r, w) |
| 456 | |
| 457 | # XXX need implementation |
| 458 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 459 | |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 460 | class BufferedRandomTest(unittest.TestCase): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 461 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 462 | def testReadAndWrite(self): |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 463 | raw = MockRawIO((b"asdf", b"ghjk")) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 464 | rw = io.BufferedRandom(raw, 8, 12) |
| 465 | |
| 466 | self.assertEqual(b"as", rw.read(2)) |
| 467 | rw.write(b"ddd") |
| 468 | rw.write(b"eee") |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 469 | self.assertFalse(raw._write_stack) # Buffer writes |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 470 | self.assertEqual(b"ghjk", rw.read()) # This read forces write flush |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 471 | self.assertEquals(b"dddeee", raw._write_stack[0]) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 472 | |
| 473 | def testSeekAndTell(self): |
| 474 | raw = io.BytesIO(b"asdfghjkl") |
| 475 | rw = io.BufferedRandom(raw) |
| 476 | |
| 477 | self.assertEquals(b"as", rw.read(2)) |
| 478 | self.assertEquals(2, rw.tell()) |
| 479 | rw.seek(0, 0) |
| 480 | self.assertEquals(b"asdf", rw.read(4)) |
| 481 | |
| 482 | rw.write(b"asdf") |
| 483 | rw.seek(0, 0) |
| 484 | self.assertEquals(b"asdfasdfl", rw.read()) |
| 485 | self.assertEquals(9, rw.tell()) |
| 486 | rw.seek(-4, 2) |
| 487 | self.assertEquals(5, rw.tell()) |
| 488 | rw.seek(2, 1) |
| 489 | self.assertEquals(7, rw.tell()) |
| 490 | self.assertEquals(b"fl", rw.read(11)) |
Christian Heimes | 8e42a0a | 2007-11-08 18:04:45 +0000 | [diff] [blame] | 491 | self.assertRaises(TypeError, rw.seek, 0.0) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 492 | |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 493 | # To fully exercise seek/tell, the StatefulIncrementalDecoder has these |
| 494 | # properties: |
| 495 | # - A single output character can correspond to many bytes of input. |
| 496 | # - The number of input bytes to complete the character can be |
| 497 | # undetermined until the last input byte is received. |
| 498 | # - The number of input bytes can vary depending on previous input. |
| 499 | # - A single input byte can correspond to many characters of output. |
| 500 | # - The number of output characters can be undetermined until the |
| 501 | # last input byte is received. |
| 502 | # - The number of output characters can vary depending on previous input. |
| 503 | |
| 504 | class StatefulIncrementalDecoder(codecs.IncrementalDecoder): |
| 505 | """ |
| 506 | For testing seek/tell behavior with a stateful, buffering decoder. |
| 507 | |
| 508 | Input is a sequence of words. Words may be fixed-length (length set |
| 509 | by input) or variable-length (period-terminated). In variable-length |
| 510 | mode, extra periods are ignored. Possible words are: |
| 511 | - 'i' followed by a number sets the input length, I (maximum 99). |
| 512 | When I is set to 0, words are space-terminated. |
| 513 | - 'o' followed by a number sets the output length, O (maximum 99). |
| 514 | - Any other word is converted into a word followed by a period on |
| 515 | the output. The output word consists of the input word truncated |
| 516 | or padded out with hyphens to make its length equal to O. If O |
| 517 | is 0, the word is output verbatim without truncating or padding. |
| 518 | I and O are initially set to 1. When I changes, any buffered input is |
| 519 | re-scanned according to the new I. EOF also terminates the last word. |
| 520 | """ |
| 521 | |
| 522 | def __init__(self, errors='strict'): |
Christian Heimes | ab56887 | 2008-03-23 02:11:13 +0000 | [diff] [blame] | 523 | codecs.IncrementalDecoder.__init__(self, errors) |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 524 | self.reset() |
| 525 | |
| 526 | def __repr__(self): |
| 527 | return '<SID %x>' % id(self) |
| 528 | |
| 529 | def reset(self): |
| 530 | self.i = 1 |
| 531 | self.o = 1 |
| 532 | self.buffer = bytearray() |
| 533 | |
| 534 | def getstate(self): |
| 535 | i, o = self.i ^ 1, self.o ^ 1 # so that flags = 0 after reset() |
| 536 | return bytes(self.buffer), i*100 + o |
| 537 | |
| 538 | def setstate(self, state): |
| 539 | buffer, io = state |
| 540 | self.buffer = bytearray(buffer) |
| 541 | i, o = divmod(io, 100) |
| 542 | self.i, self.o = i ^ 1, o ^ 1 |
| 543 | |
| 544 | def decode(self, input, final=False): |
| 545 | output = '' |
| 546 | for b in input: |
| 547 | if self.i == 0: # variable-length, terminated with period |
| 548 | if b == ord('.'): |
| 549 | if self.buffer: |
| 550 | output += self.process_word() |
| 551 | else: |
| 552 | self.buffer.append(b) |
| 553 | else: # fixed-length, terminate after self.i bytes |
| 554 | self.buffer.append(b) |
| 555 | if len(self.buffer) == self.i: |
| 556 | output += self.process_word() |
| 557 | if final and self.buffer: # EOF terminates the last word |
| 558 | output += self.process_word() |
| 559 | return output |
| 560 | |
| 561 | def process_word(self): |
| 562 | output = '' |
| 563 | if self.buffer[0] == ord('i'): |
| 564 | self.i = min(99, int(self.buffer[1:] or 0)) # set input length |
| 565 | elif self.buffer[0] == ord('o'): |
| 566 | self.o = min(99, int(self.buffer[1:] or 0)) # set output length |
| 567 | else: |
| 568 | output = self.buffer.decode('ascii') |
| 569 | if len(output) < self.o: |
| 570 | output += '-'*self.o # pad out with hyphens |
| 571 | if self.o: |
| 572 | output = output[:self.o] # truncate to output length |
| 573 | output += '.' |
| 574 | self.buffer = bytearray() |
| 575 | return output |
| 576 | |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 577 | codecEnabled = False |
| 578 | |
| 579 | @classmethod |
| 580 | def lookupTestDecoder(cls, name): |
| 581 | if cls.codecEnabled and name == 'test_decoder': |
| 582 | return codecs.CodecInfo( |
| 583 | name='test_decoder', encode=None, decode=None, |
| 584 | incrementalencoder=None, |
| 585 | streamreader=None, streamwriter=None, |
| 586 | incrementaldecoder=cls) |
| 587 | |
| 588 | # Register the previous decoder for testing. |
| 589 | # Disabled by default, tests will enable it. |
| 590 | codecs.register(StatefulIncrementalDecoder.lookupTestDecoder) |
| 591 | |
| 592 | |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 593 | class StatefulIncrementalDecoderTest(unittest.TestCase): |
| 594 | """ |
| 595 | Make sure the StatefulIncrementalDecoder actually works. |
| 596 | """ |
| 597 | |
| 598 | test_cases = [ |
Ka-Ping Yee | d24a5b6 | 2008-03-20 10:51:27 +0000 | [diff] [blame] | 599 | # I=1, O=1 (fixed-length input == fixed-length output) |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 600 | (b'abcd', False, 'a.b.c.d.'), |
Ka-Ping Yee | d24a5b6 | 2008-03-20 10:51:27 +0000 | [diff] [blame] | 601 | # I=0, O=0 (variable-length input, variable-length output) |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 602 | (b'oiabcd', True, 'abcd.'), |
Ka-Ping Yee | d24a5b6 | 2008-03-20 10:51:27 +0000 | [diff] [blame] | 603 | # I=0, O=0 (should ignore extra periods) |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 604 | (b'oi...abcd...', True, 'abcd.'), |
Ka-Ping Yee | d24a5b6 | 2008-03-20 10:51:27 +0000 | [diff] [blame] | 605 | # I=0, O=6 (variable-length input, fixed-length output) |
| 606 | (b'i.o6.x.xyz.toolongtofit.', False, 'x-----.xyz---.toolon.'), |
| 607 | # I=2, O=6 (fixed-length input < fixed-length output) |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 608 | (b'i.i2.o6xyz', True, 'xy----.z-----.'), |
Ka-Ping Yee | d24a5b6 | 2008-03-20 10:51:27 +0000 | [diff] [blame] | 609 | # I=6, O=3 (fixed-length input > fixed-length output) |
| 610 | (b'i.o3.i6.abcdefghijklmnop', True, 'abc.ghi.mno.'), |
| 611 | # I=0, then 3; O=29, then 15 (with longer output) |
| 612 | (b'i.o29.a.b.cde.o15.abcdefghijabcdefghij.i3.a.b.c.d.ei00k.l.m', True, |
| 613 | 'a----------------------------.' + |
| 614 | 'b----------------------------.' + |
| 615 | 'cde--------------------------.' + |
| 616 | 'abcdefghijabcde.' + |
| 617 | 'a.b------------.' + |
| 618 | '.c.------------.' + |
| 619 | 'd.e------------.' + |
| 620 | 'k--------------.' + |
| 621 | 'l--------------.' + |
| 622 | 'm--------------.') |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 623 | ] |
| 624 | |
| 625 | def testDecoder(self): |
| 626 | # Try a few one-shot test cases. |
| 627 | for input, eof, output in self.test_cases: |
| 628 | d = StatefulIncrementalDecoder() |
| 629 | self.assertEquals(d.decode(input, eof), output) |
| 630 | |
| 631 | # Also test an unfinished decode, followed by forcing EOF. |
| 632 | d = StatefulIncrementalDecoder() |
| 633 | self.assertEquals(d.decode(b'oiabcd'), '') |
| 634 | self.assertEquals(d.decode(b'', 1), 'abcd.') |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 635 | |
| 636 | class TextIOWrapperTest(unittest.TestCase): |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 637 | |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 638 | def setUp(self): |
| 639 | self.testdata = b"AAA\r\nBBB\rCCC\r\nDDD\nEEE\r\n" |
| 640 | self.normalized = b"AAA\nBBB\nCCC\nDDD\nEEE\n".decode("ascii") |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 641 | support.unlink(support.TESTFN) |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 642 | |
Guido van Rossum | d071281 | 2007-04-11 16:32:43 +0000 | [diff] [blame] | 643 | def tearDown(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 644 | support.unlink(support.TESTFN) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 645 | |
Guido van Rossum | f64db9f | 2007-12-06 01:04:26 +0000 | [diff] [blame] | 646 | def testLineBuffering(self): |
| 647 | r = io.BytesIO() |
| 648 | b = io.BufferedWriter(r, 1000) |
| 649 | t = io.TextIOWrapper(b, newline="\n", line_buffering=True) |
| 650 | t.write("X") |
| 651 | self.assertEquals(r.getvalue(), b"") # No flush happened |
| 652 | t.write("Y\nZ") |
| 653 | self.assertEquals(r.getvalue(), b"XY\nZ") # All got flushed |
| 654 | t.write("A\rB") |
| 655 | self.assertEquals(r.getvalue(), b"XY\nZA\rB") |
| 656 | |
Guido van Rossum | e7fc50f | 2007-12-03 22:54:21 +0000 | [diff] [blame] | 657 | def testEncodingErrorsReading(self): |
| 658 | # (1) default |
| 659 | b = io.BytesIO(b"abc\n\xff\n") |
| 660 | t = io.TextIOWrapper(b, encoding="ascii") |
| 661 | self.assertRaises(UnicodeError, t.read) |
| 662 | # (2) explicit strict |
| 663 | b = io.BytesIO(b"abc\n\xff\n") |
| 664 | t = io.TextIOWrapper(b, encoding="ascii", errors="strict") |
| 665 | self.assertRaises(UnicodeError, t.read) |
| 666 | # (3) ignore |
| 667 | b = io.BytesIO(b"abc\n\xff\n") |
| 668 | t = io.TextIOWrapper(b, encoding="ascii", errors="ignore") |
| 669 | self.assertEquals(t.read(), "abc\n\n") |
| 670 | # (4) replace |
| 671 | b = io.BytesIO(b"abc\n\xff\n") |
| 672 | t = io.TextIOWrapper(b, encoding="ascii", errors="replace") |
| 673 | self.assertEquals(t.read(), "abc\n\ufffd\n") |
| 674 | |
| 675 | def testEncodingErrorsWriting(self): |
| 676 | # (1) default |
| 677 | b = io.BytesIO() |
| 678 | t = io.TextIOWrapper(b, encoding="ascii") |
| 679 | self.assertRaises(UnicodeError, t.write, "\xff") |
| 680 | # (2) explicit strict |
| 681 | b = io.BytesIO() |
| 682 | t = io.TextIOWrapper(b, encoding="ascii", errors="strict") |
| 683 | self.assertRaises(UnicodeError, t.write, "\xff") |
| 684 | # (3) ignore |
| 685 | b = io.BytesIO() |
Guido van Rossum | f64db9f | 2007-12-06 01:04:26 +0000 | [diff] [blame] | 686 | t = io.TextIOWrapper(b, encoding="ascii", errors="ignore", |
| 687 | newline="\n") |
Guido van Rossum | e7fc50f | 2007-12-03 22:54:21 +0000 | [diff] [blame] | 688 | t.write("abc\xffdef\n") |
| 689 | t.flush() |
Christian Heimes | ecda261 | 2007-12-05 17:59:44 +0000 | [diff] [blame] | 690 | self.assertEquals(b.getvalue(), b"abcdef\n") |
Guido van Rossum | e7fc50f | 2007-12-03 22:54:21 +0000 | [diff] [blame] | 691 | # (4) replace |
| 692 | b = io.BytesIO() |
Guido van Rossum | f64db9f | 2007-12-06 01:04:26 +0000 | [diff] [blame] | 693 | t = io.TextIOWrapper(b, encoding="ascii", errors="replace", |
| 694 | newline="\n") |
Guido van Rossum | e7fc50f | 2007-12-03 22:54:21 +0000 | [diff] [blame] | 695 | t.write("abc\xffdef\n") |
| 696 | t.flush() |
Christian Heimes | ecda261 | 2007-12-05 17:59:44 +0000 | [diff] [blame] | 697 | self.assertEquals(b.getvalue(), b"abc?def\n") |
Guido van Rossum | e7fc50f | 2007-12-03 22:54:21 +0000 | [diff] [blame] | 698 | |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 699 | def testNewlinesInput(self): |
| 700 | testdata = b"AAA\nBBB\nCCC\rDDD\rEEE\r\nFFF\r\nGGG" |
| 701 | normalized = testdata.replace(b"\r\n", b"\n").replace(b"\r", b"\n") |
| 702 | for newline, expected in [ |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 703 | (None, normalized.decode("ascii").splitlines(True)), |
| 704 | ("", testdata.decode("ascii").splitlines(True)), |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 705 | ("\n", ["AAA\n", "BBB\n", "CCC\rDDD\rEEE\r\n", "FFF\r\n", "GGG"]), |
| 706 | ("\r\n", ["AAA\nBBB\nCCC\rDDD\rEEE\r\n", "FFF\r\n", "GGG"]), |
| 707 | ("\r", ["AAA\nBBB\nCCC\r", "DDD\r", "EEE\r", "\nFFF\r", "\nGGG"]), |
| 708 | ]: |
| 709 | buf = io.BytesIO(testdata) |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 710 | txt = io.TextIOWrapper(buf, encoding="ascii", newline=newline) |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 711 | self.assertEquals(txt.readlines(), expected) |
| 712 | txt.seek(0) |
| 713 | self.assertEquals(txt.read(), "".join(expected)) |
| 714 | |
| 715 | def testNewlinesOutput(self): |
| 716 | testdict = { |
| 717 | "": b"AAA\nBBB\nCCC\nX\rY\r\nZ", |
| 718 | "\n": b"AAA\nBBB\nCCC\nX\rY\r\nZ", |
| 719 | "\r": b"AAA\rBBB\rCCC\rX\rY\r\rZ", |
| 720 | "\r\n": b"AAA\r\nBBB\r\nCCC\r\nX\rY\r\r\nZ", |
| 721 | } |
| 722 | tests = [(None, testdict[os.linesep])] + sorted(testdict.items()) |
| 723 | for newline, expected in tests: |
| 724 | buf = io.BytesIO() |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 725 | txt = io.TextIOWrapper(buf, encoding="ascii", newline=newline) |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 726 | txt.write("AAA\nB") |
| 727 | txt.write("BB\nCCC\n") |
| 728 | txt.write("X\rY\r\nZ") |
| 729 | txt.flush() |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 730 | self.assertEquals(buf.closed, False) |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 731 | self.assertEquals(buf.getvalue(), expected) |
| 732 | |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 733 | def testNewlines(self): |
| 734 | input_lines = [ "unix\n", "windows\r\n", "os9\r", "last\n", "nonl" ] |
| 735 | |
| 736 | tests = [ |
| 737 | [ None, [ 'unix\n', 'windows\n', 'os9\n', 'last\n', 'nonl' ] ], |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 738 | [ '', input_lines ], |
| 739 | [ '\n', [ "unix\n", "windows\r\n", "os9\rlast\n", "nonl" ] ], |
| 740 | [ '\r\n', [ "unix\nwindows\r\n", "os9\rlast\nnonl" ] ], |
| 741 | [ '\r', [ "unix\nwindows\r", "\nos9\r", "last\nnonl" ] ], |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 742 | ] |
| 743 | |
Guido van Rossum | c2f93dc | 2007-05-24 00:50:02 +0000 | [diff] [blame] | 744 | encodings = ('utf-8', 'latin-1') |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 745 | |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 746 | # 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] | 747 | # character in TextIOWrapper._pending_line. |
| 748 | for encoding in encodings: |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 749 | # XXX: str.encode() should return bytes |
| 750 | data = bytes(''.join(input_lines).encode(encoding)) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 751 | for do_reads in (False, True): |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 752 | for bufsize in range(1, 10): |
| 753 | for newline, exp_lines in tests: |
| 754 | bufio = io.BufferedReader(io.BytesIO(data), bufsize) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 755 | textio = io.TextIOWrapper(bufio, newline=newline, |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 756 | encoding=encoding) |
| 757 | if do_reads: |
| 758 | got_lines = [] |
| 759 | while True: |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 760 | c2 = textio.read(2) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 761 | if c2 == '': |
| 762 | break |
| 763 | self.assertEquals(len(c2), 2) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 764 | got_lines.append(c2 + textio.readline()) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 765 | else: |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 766 | got_lines = list(textio) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 767 | |
| 768 | for got_line, exp_line in zip(got_lines, exp_lines): |
| 769 | self.assertEquals(got_line, exp_line) |
| 770 | self.assertEquals(len(got_lines), len(exp_lines)) |
| 771 | |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 772 | def testNewlinesInput(self): |
| 773 | testdata = b"AAA\nBBB\nCCC\rDDD\rEEE\r\nFFF\r\nGGG" |
| 774 | normalized = testdata.replace(b"\r\n", b"\n").replace(b"\r", b"\n") |
| 775 | for newline, expected in [ |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 776 | (None, normalized.decode("ascii").splitlines(True)), |
| 777 | ("", testdata.decode("ascii").splitlines(True)), |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 778 | ("\n", ["AAA\n", "BBB\n", "CCC\rDDD\rEEE\r\n", "FFF\r\n", "GGG"]), |
| 779 | ("\r\n", ["AAA\nBBB\nCCC\rDDD\rEEE\r\n", "FFF\r\n", "GGG"]), |
| 780 | ("\r", ["AAA\nBBB\nCCC\r", "DDD\r", "EEE\r", "\nFFF\r", "\nGGG"]), |
| 781 | ]: |
| 782 | buf = io.BytesIO(testdata) |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 783 | txt = io.TextIOWrapper(buf, encoding="ascii", newline=newline) |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 784 | self.assertEquals(txt.readlines(), expected) |
| 785 | txt.seek(0) |
| 786 | self.assertEquals(txt.read(), "".join(expected)) |
| 787 | |
| 788 | def testNewlinesOutput(self): |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 789 | data = "AAA\nBBB\rCCC\n" |
| 790 | data_lf = b"AAA\nBBB\rCCC\n" |
| 791 | data_cr = b"AAA\rBBB\rCCC\r" |
| 792 | data_crlf = b"AAA\r\nBBB\rCCC\r\n" |
Guido van Rossum | 1836358 | 2007-08-18 22:05:58 +0000 | [diff] [blame] | 793 | save_linesep = os.linesep |
| 794 | try: |
| 795 | for os.linesep, newline, expected in [ |
| 796 | ("\n", None, data_lf), |
| 797 | ("\r\n", None, data_crlf), |
| 798 | ("\n", "", data_lf), |
| 799 | ("\r\n", "", data_lf), |
| 800 | ("\n", "\n", data_lf), |
| 801 | ("\r\n", "\n", data_lf), |
| 802 | ("\n", "\r", data_cr), |
| 803 | ("\r\n", "\r", data_cr), |
| 804 | ("\n", "\r\n", data_crlf), |
| 805 | ("\r\n", "\r\n", data_crlf), |
| 806 | ]: |
| 807 | buf = io.BytesIO() |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 808 | txt = io.TextIOWrapper(buf, encoding="ascii", newline=newline) |
Guido van Rossum | 1836358 | 2007-08-18 22:05:58 +0000 | [diff] [blame] | 809 | txt.write(data) |
| 810 | txt.close() |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 811 | self.assertEquals(buf.closed, True) |
| 812 | self.assertRaises(ValueError, buf.getvalue) |
Guido van Rossum | 1836358 | 2007-08-18 22:05:58 +0000 | [diff] [blame] | 813 | finally: |
| 814 | os.linesep = save_linesep |
Guido van Rossum | 8358db2 | 2007-08-18 21:39:55 +0000 | [diff] [blame] | 815 | |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 816 | # Systematic tests of the text I/O API |
| 817 | |
| 818 | def testBasicIO(self): |
| 819 | for chunksize in (1, 2, 3, 4, 5, 15, 16, 17, 31, 32, 33, 63, 64, 65): |
| 820 | for enc in "ascii", "latin1", "utf8" :# , "utf-16-be", "utf-16-le": |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 821 | f = io.open(support.TESTFN, "w+", encoding=enc) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 822 | f._CHUNK_SIZE = chunksize |
| 823 | self.assertEquals(f.write("abc"), 3) |
| 824 | f.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 825 | f = io.open(support.TESTFN, "r+", encoding=enc) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 826 | f._CHUNK_SIZE = chunksize |
| 827 | self.assertEquals(f.tell(), 0) |
| 828 | self.assertEquals(f.read(), "abc") |
| 829 | cookie = f.tell() |
| 830 | self.assertEquals(f.seek(0), 0) |
| 831 | self.assertEquals(f.read(2), "ab") |
| 832 | self.assertEquals(f.read(1), "c") |
| 833 | self.assertEquals(f.read(1), "") |
| 834 | self.assertEquals(f.read(), "") |
| 835 | self.assertEquals(f.tell(), cookie) |
| 836 | self.assertEquals(f.seek(0), 0) |
| 837 | self.assertEquals(f.seek(0, 2), cookie) |
| 838 | self.assertEquals(f.write("def"), 3) |
| 839 | self.assertEquals(f.seek(cookie), cookie) |
| 840 | self.assertEquals(f.read(), "def") |
| 841 | if enc.startswith("utf"): |
| 842 | self.multi_line_test(f, enc) |
| 843 | f.close() |
| 844 | |
| 845 | def multi_line_test(self, f, enc): |
| 846 | f.seek(0) |
| 847 | f.truncate() |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 848 | sample = "s\xff\u0fff\uffff" |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 849 | wlines = [] |
Guido van Rossum | cba608c | 2007-04-11 14:19:59 +0000 | [diff] [blame] | 850 | 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] | 851 | chars = [] |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 852 | for i in range(size): |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 853 | chars.append(sample[i % len(sample)]) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 854 | line = "".join(chars) + "\n" |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 855 | wlines.append((f.tell(), line)) |
| 856 | f.write(line) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 857 | f.seek(0) |
| 858 | rlines = [] |
| 859 | while True: |
| 860 | pos = f.tell() |
| 861 | line = f.readline() |
| 862 | if not line: |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 863 | break |
| 864 | rlines.append((pos, line)) |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 865 | self.assertEquals(rlines, wlines) |
| 866 | |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 867 | def testTelling(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 868 | f = io.open(support.TESTFN, "w+", encoding="utf8") |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 869 | p0 = f.tell() |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 870 | f.write("\xff\n") |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 871 | p1 = f.tell() |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 872 | f.write("\xff\n") |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 873 | p2 = f.tell() |
| 874 | f.seek(0) |
| 875 | self.assertEquals(f.tell(), p0) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 876 | self.assertEquals(f.readline(), "\xff\n") |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 877 | self.assertEquals(f.tell(), p1) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 878 | self.assertEquals(f.readline(), "\xff\n") |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 879 | self.assertEquals(f.tell(), p2) |
| 880 | f.seek(0) |
| 881 | for line in f: |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 882 | self.assertEquals(line, "\xff\n") |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 883 | self.assertRaises(IOError, f.tell) |
| 884 | self.assertEquals(f.tell(), p2) |
| 885 | f.close() |
| 886 | |
Guido van Rossum | d76e779 | 2007-04-17 02:38:04 +0000 | [diff] [blame] | 887 | def testSeeking(self): |
| 888 | chunk_size = io.TextIOWrapper._CHUNK_SIZE |
| 889 | prefix_size = chunk_size - 2 |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 890 | u_prefix = "a" * prefix_size |
Guido van Rossum | d76e779 | 2007-04-17 02:38:04 +0000 | [diff] [blame] | 891 | prefix = bytes(u_prefix.encode("utf-8")) |
| 892 | self.assertEquals(len(u_prefix), len(prefix)) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 893 | u_suffix = "\u8888\n" |
Guido van Rossum | d76e779 | 2007-04-17 02:38:04 +0000 | [diff] [blame] | 894 | suffix = bytes(u_suffix.encode("utf-8")) |
| 895 | line = prefix + suffix |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 896 | f = io.open(support.TESTFN, "wb") |
Guido van Rossum | d76e779 | 2007-04-17 02:38:04 +0000 | [diff] [blame] | 897 | f.write(line*2) |
| 898 | f.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 899 | f = io.open(support.TESTFN, "r", encoding="utf-8") |
Guido van Rossum | d76e779 | 2007-04-17 02:38:04 +0000 | [diff] [blame] | 900 | s = f.read(prefix_size) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 901 | self.assertEquals(s, str(prefix, "ascii")) |
Guido van Rossum | d76e779 | 2007-04-17 02:38:04 +0000 | [diff] [blame] | 902 | self.assertEquals(f.tell(), prefix_size) |
| 903 | self.assertEquals(f.readline(), u_suffix) |
| 904 | |
| 905 | def testSeekingToo(self): |
| 906 | # Regression test for a specific bug |
| 907 | data = b'\xe0\xbf\xbf\n' |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 908 | f = io.open(support.TESTFN, "wb") |
Guido van Rossum | d76e779 | 2007-04-17 02:38:04 +0000 | [diff] [blame] | 909 | f.write(data) |
| 910 | f.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 911 | f = io.open(support.TESTFN, "r", encoding="utf-8") |
Guido van Rossum | d76e779 | 2007-04-17 02:38:04 +0000 | [diff] [blame] | 912 | f._CHUNK_SIZE # Just test that it exists |
| 913 | f._CHUNK_SIZE = 2 |
| 914 | f.readline() |
| 915 | f.tell() |
| 916 | |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 917 | def testSeekAndTell(self): |
| 918 | """Test seek/tell using the StatefulIncrementalDecoder.""" |
| 919 | |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 920 | def testSeekAndTellWithData(data, min_pos=0): |
| 921 | """Tell/seek to various points within a data stream and ensure |
| 922 | that the decoded data returned by read() is consistent.""" |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 923 | f = io.open(support.TESTFN, 'wb') |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 924 | f.write(data) |
| 925 | f.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 926 | f = io.open(support.TESTFN, encoding='test_decoder') |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 927 | decoded = f.read() |
| 928 | f.close() |
| 929 | |
Neal Norwitz | e2b0705 | 2008-03-18 19:52:05 +0000 | [diff] [blame] | 930 | for i in range(min_pos, len(decoded) + 1): # seek positions |
| 931 | for j in [1, 5, len(decoded) - i]: # read lengths |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 932 | f = io.open(support.TESTFN, encoding='test_decoder') |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 933 | self.assertEquals(f.read(i), decoded[:i]) |
| 934 | cookie = f.tell() |
| 935 | self.assertEquals(f.read(j), decoded[i:i + j]) |
| 936 | f.seek(cookie) |
| 937 | self.assertEquals(f.read(), decoded[i:]) |
| 938 | f.close() |
| 939 | |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 940 | # Enable the test decoder. |
| 941 | StatefulIncrementalDecoder.codecEnabled = 1 |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 942 | |
| 943 | # Run the tests. |
| 944 | try: |
| 945 | # Try each test case. |
| 946 | for input, _, _ in StatefulIncrementalDecoderTest.test_cases: |
| 947 | testSeekAndTellWithData(input) |
| 948 | |
| 949 | # Position each test case so that it crosses a chunk boundary. |
| 950 | CHUNK_SIZE = io.TextIOWrapper._CHUNK_SIZE |
| 951 | for input, _, _ in StatefulIncrementalDecoderTest.test_cases: |
| 952 | offset = CHUNK_SIZE - len(input)//2 |
| 953 | prefix = b'.'*offset |
| 954 | # Don't bother seeking into the prefix (takes too long). |
| 955 | min_pos = offset*2 |
| 956 | testSeekAndTellWithData(prefix + input, min_pos) |
| 957 | |
| 958 | # Ensure our test decoder won't interfere with subsequent tests. |
| 959 | finally: |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 960 | StatefulIncrementalDecoder.codecEnabled = 0 |
Ka-Ping Yee | f44c7e8 | 2008-03-18 04:51:32 +0000 | [diff] [blame] | 961 | |
Alexandre Vassalotti | a38f73b | 2008-01-07 18:30:48 +0000 | [diff] [blame] | 962 | def testEncodedWrites(self): |
| 963 | data = "1234567890" |
| 964 | tests = ("utf-16", |
| 965 | "utf-16-le", |
| 966 | "utf-16-be", |
| 967 | "utf-32", |
| 968 | "utf-32-le", |
| 969 | "utf-32-be") |
| 970 | for encoding in tests: |
| 971 | buf = io.BytesIO() |
| 972 | f = io.TextIOWrapper(buf, encoding=encoding) |
| 973 | # Check if the BOM is written only once (see issue1753). |
| 974 | f.write(data) |
| 975 | f.write(data) |
| 976 | f.seek(0) |
| 977 | self.assertEquals(f.read(), data * 2) |
| 978 | self.assertEquals(buf.getvalue(), (data * 2).encode(encoding)) |
| 979 | |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 980 | def timingTest(self): |
| 981 | timer = time.time |
| 982 | enc = "utf8" |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 983 | line = "\0\x0f\xff\u0fff\uffff\U000fffff\U0010ffff"*3 + "\n" |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 984 | nlines = 10000 |
| 985 | nchars = len(line) |
| 986 | nbytes = len(line.encode(enc)) |
| 987 | for chunk_size in (32, 64, 128, 256): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 988 | f = io.open(support.TESTFN, "w+", encoding=enc) |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 989 | f._CHUNK_SIZE = chunk_size |
| 990 | t0 = timer() |
| 991 | for i in range(nlines): |
| 992 | f.write(line) |
| 993 | f.flush() |
| 994 | t1 = timer() |
| 995 | f.seek(0) |
| 996 | for line in f: |
| 997 | pass |
| 998 | t2 = timer() |
| 999 | f.seek(0) |
| 1000 | while f.readline(): |
| 1001 | pass |
| 1002 | t3 = timer() |
| 1003 | f.seek(0) |
| 1004 | while f.readline(): |
| 1005 | f.tell() |
| 1006 | t4 = timer() |
| 1007 | f.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1008 | if support.verbose: |
Guido van Rossum | b9c4c3e | 2007-04-11 16:07:50 +0000 | [diff] [blame] | 1009 | print("\nTiming test: %d lines of %d characters (%d bytes)" % |
| 1010 | (nlines, nchars, nbytes)) |
| 1011 | print("File chunk size: %6s" % f._CHUNK_SIZE) |
| 1012 | print("Writing: %6.3f seconds" % (t1-t0)) |
| 1013 | print("Reading using iteration: %6.3f seconds" % (t2-t1)) |
| 1014 | print("Reading using readline(): %6.3f seconds" % (t3-t2)) |
| 1015 | print("Using readline()+tell(): %6.3f seconds" % (t4-t3)) |
| 1016 | |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 1017 | def testReadOneByOne(self): |
| 1018 | txt = io.TextIOWrapper(io.BytesIO(b"AA\r\nBB")) |
| 1019 | reads = "" |
| 1020 | while True: |
| 1021 | c = txt.read(1) |
| 1022 | if not c: |
| 1023 | break |
| 1024 | reads += c |
| 1025 | self.assertEquals(reads, "AA\nBB") |
| 1026 | |
| 1027 | # read in amounts equal to TextIOWrapper._CHUNK_SIZE which is 128. |
| 1028 | def testReadByChunk(self): |
| 1029 | # make sure "\r\n" straddles 128 char boundary. |
| 1030 | txt = io.TextIOWrapper(io.BytesIO(b"A" * 127 + b"\r\nB")) |
| 1031 | reads = "" |
| 1032 | while True: |
| 1033 | c = txt.read(128) |
| 1034 | if not c: |
| 1035 | break |
| 1036 | reads += c |
| 1037 | self.assertEquals(reads, "A"*127+"\nB") |
| 1038 | |
| 1039 | def test_issue1395_1(self): |
| 1040 | txt = io.TextIOWrapper(io.BytesIO(self.testdata), encoding="ascii") |
| 1041 | |
| 1042 | # read one char at a time |
| 1043 | reads = "" |
| 1044 | while True: |
| 1045 | c = txt.read(1) |
| 1046 | if not c: |
| 1047 | break |
| 1048 | reads += c |
| 1049 | self.assertEquals(reads, self.normalized) |
| 1050 | |
| 1051 | def test_issue1395_2(self): |
| 1052 | txt = io.TextIOWrapper(io.BytesIO(self.testdata), encoding="ascii") |
| 1053 | txt._CHUNK_SIZE = 4 |
| 1054 | |
| 1055 | reads = "" |
| 1056 | while True: |
| 1057 | c = txt.read(4) |
| 1058 | if not c: |
| 1059 | break |
| 1060 | reads += c |
| 1061 | self.assertEquals(reads, self.normalized) |
| 1062 | |
| 1063 | def test_issue1395_3(self): |
| 1064 | txt = io.TextIOWrapper(io.BytesIO(self.testdata), encoding="ascii") |
| 1065 | txt._CHUNK_SIZE = 4 |
| 1066 | |
| 1067 | reads = txt.read(4) |
| 1068 | reads += txt.read(4) |
| 1069 | reads += txt.readline() |
| 1070 | reads += txt.readline() |
| 1071 | reads += txt.readline() |
| 1072 | self.assertEquals(reads, self.normalized) |
| 1073 | |
| 1074 | def test_issue1395_4(self): |
| 1075 | txt = io.TextIOWrapper(io.BytesIO(self.testdata), encoding="ascii") |
| 1076 | txt._CHUNK_SIZE = 4 |
| 1077 | |
| 1078 | reads = txt.read(4) |
| 1079 | reads += txt.read() |
| 1080 | self.assertEquals(reads, self.normalized) |
| 1081 | |
| 1082 | def test_issue1395_5(self): |
| 1083 | txt = io.TextIOWrapper(io.BytesIO(self.testdata), encoding="ascii") |
| 1084 | txt._CHUNK_SIZE = 4 |
| 1085 | |
| 1086 | reads = txt.read(4) |
| 1087 | pos = txt.tell() |
| 1088 | txt.seek(0) |
| 1089 | txt.seek(pos) |
| 1090 | self.assertEquals(txt.read(4), "BBB\n") |
| 1091 | |
Ka-Ping Yee | ddaa706 | 2008-03-17 20:35:15 +0000 | [diff] [blame] | 1092 | def test_issue2282(self): |
| 1093 | buffer = io.BytesIO(self.testdata) |
| 1094 | txt = io.TextIOWrapper(buffer, encoding="ascii") |
| 1095 | |
| 1096 | self.assertEqual(buffer.seekable(), txt.seekable()) |
| 1097 | |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 1098 | def test_newline_decoder(self): |
| 1099 | import codecs |
| 1100 | decoder = codecs.getincrementaldecoder("utf-8")() |
| 1101 | decoder = io.IncrementalNewlineDecoder(decoder, translate=True) |
| 1102 | |
| 1103 | self.assertEquals(decoder.decode(b'\xe8\xa2\x88'), "\u8888") |
| 1104 | |
| 1105 | self.assertEquals(decoder.decode(b'\xe8'), "") |
| 1106 | self.assertEquals(decoder.decode(b'\xa2'), "") |
| 1107 | self.assertEquals(decoder.decode(b'\x88'), "\u8888") |
| 1108 | |
| 1109 | self.assertEquals(decoder.decode(b'\xe8'), "") |
| 1110 | self.assertRaises(UnicodeDecodeError, decoder.decode, b'', final=True) |
| 1111 | |
| 1112 | decoder.setstate((b'', 0)) |
| 1113 | self.assertEquals(decoder.decode(b'\n'), "\n") |
| 1114 | self.assertEquals(decoder.decode(b'\r'), "") |
| 1115 | self.assertEquals(decoder.decode(b'', final=True), "\n") |
| 1116 | self.assertEquals(decoder.decode(b'\r', final=True), "\n") |
| 1117 | |
| 1118 | self.assertEquals(decoder.decode(b'\r'), "") |
| 1119 | self.assertEquals(decoder.decode(b'a'), "\na") |
| 1120 | |
| 1121 | self.assertEquals(decoder.decode(b'\r\r\n'), "\n\n") |
| 1122 | self.assertEquals(decoder.decode(b'\r'), "") |
| 1123 | self.assertEquals(decoder.decode(b'\r'), "\n") |
| 1124 | self.assertEquals(decoder.decode(b'\na'), "\na") |
| 1125 | |
| 1126 | self.assertEquals(decoder.decode(b'\xe8\xa2\x88\r\n'), "\u8888\n") |
| 1127 | self.assertEquals(decoder.decode(b'\xe8\xa2\x88'), "\u8888") |
| 1128 | self.assertEquals(decoder.decode(b'\n'), "\n") |
| 1129 | self.assertEquals(decoder.decode(b'\xe8\xa2\x88\r'), "\u8888") |
| 1130 | self.assertEquals(decoder.decode(b'\n'), "\n") |
Guido van Rossum | 9b76da6 | 2007-04-11 01:09:03 +0000 | [diff] [blame] | 1131 | |
Alexandre Vassalotti | 472f07d | 2008-01-06 00:34:32 +0000 | [diff] [blame] | 1132 | decoder = codecs.getincrementaldecoder("utf-8")() |
| 1133 | decoder = io.IncrementalNewlineDecoder(decoder, translate=True) |
| 1134 | self.assertEquals(decoder.newlines, None) |
| 1135 | decoder.decode(b"abc\n\r") |
| 1136 | self.assertEquals(decoder.newlines, '\n') |
| 1137 | decoder.decode(b"\nabc") |
| 1138 | self.assertEquals(decoder.newlines, ('\n', '\r\n')) |
| 1139 | decoder.decode(b"abc\r") |
| 1140 | self.assertEquals(decoder.newlines, ('\n', '\r\n')) |
| 1141 | decoder.decode(b"abc") |
| 1142 | self.assertEquals(decoder.newlines, ('\r', '\n', '\r\n')) |
| 1143 | decoder.decode(b"abc\r") |
| 1144 | decoder.reset() |
| 1145 | self.assertEquals(decoder.decode(b"abc"), "abc") |
| 1146 | self.assertEquals(decoder.newlines, None) |
| 1147 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 1148 | # XXX Tests for open() |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 1149 | |
Guido van Rossum | 5abbf75 | 2007-08-27 17:39:33 +0000 | [diff] [blame] | 1150 | class MiscIOTest(unittest.TestCase): |
| 1151 | |
| 1152 | def testImport__all__(self): |
| 1153 | for name in io.__all__: |
| 1154 | obj = getattr(io, name, None) |
| 1155 | self.assert_(obj is not None, name) |
| 1156 | if name == "open": |
| 1157 | continue |
| 1158 | elif "error" in name.lower(): |
| 1159 | self.assert_(issubclass(obj, Exception), name) |
| 1160 | else: |
| 1161 | self.assert_(issubclass(obj, io.IOBase)) |
| 1162 | |
| 1163 | |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 1164 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1165 | support.run_unittest(IOTest, BytesIOTest, StringIOTest, |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1166 | BufferedReaderTest, BufferedWriterTest, |
| 1167 | BufferedRWPairTest, BufferedRandomTest, |
| 1168 | StatefulIncrementalDecoderTest, |
| 1169 | TextIOWrapperTest, MiscIOTest) |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 1170 | |
| 1171 | if __name__ == "__main__": |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 1172 | unittest.main() |