blob: a0a467092cc199d72f387ab1958d888e1e2b8d69 [file] [log] [blame]
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001"""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 Rossum68bbcd22007-02-27 17:19:33 +000021
Victor Stinnerf86a5e82012-06-05 13:43:22 +020022import abc
23import array
24import errno
25import locale
Guido van Rossum8358db22007-08-18 21:39:55 +000026import os
Victor Stinnerf86a5e82012-06-05 13:43:22 +020027import pickle
28import random
29import signal
Guido van Rossum34d69e52007-04-10 20:08:41 +000030import sys
Guido van Rossumb9c4c3e2007-04-11 16:07:50 +000031import time
Guido van Rossum28524c72007-02-27 05:47:44 +000032import unittest
Antoine Pitroue033e062010-10-29 10:38:18 +000033import warnings
Victor Stinnerf86a5e82012-06-05 13:43:22 +020034import weakref
Antoine Pitrou131a4892012-10-16 22:57:11 +020035from collections import deque, UserList
Victor Stinnerf86a5e82012-06-05 13:43:22 +020036from itertools import cycle, count
Benjamin Petersonee8712c2008-05-20 21:35:26 +000037from test import support
Antoine Pitrou712cb732013-12-21 15:51:54 +010038from test.script_helper import assert_python_ok
Guido van Rossum76c5d4d2007-04-06 19:10:29 +000039
Ka-Ping Yeef44c7e82008-03-18 04:51:32 +000040import codecs
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000041import io # C implementation of io
42import _pyio as pyio # Python implementation of io
Victor Stinner45df8202010-04-28 22:31:17 +000043try:
44 import threading
45except ImportError:
46 threading = None
Guido van Rossuma9e20242007-03-08 00:43:48 +000047
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000048def _default_chunk_size():
49 """Get the default TextIOWrapper chunk size"""
Marc-André Lemburg8f36af72011-02-25 15:42:01 +000050 with open(__file__, "r", encoding="latin-1") as f:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000051 return f._CHUNK_SIZE
52
53
Antoine Pitrou328ec742010-09-14 18:37:24 +000054class MockRawIOWithoutRead:
55 """A RawIO implementation without read(), so as to exercise the default
56 RawIO.read() which calls readinto()."""
Guido van Rossuma9e20242007-03-08 00:43:48 +000057
Guido van Rossum76c5d4d2007-04-06 19:10:29 +000058 def __init__(self, read_stack=()):
59 self._read_stack = list(read_stack)
60 self._write_stack = []
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000061 self._reads = 0
Antoine Pitrou32cfede2010-08-11 13:31:33 +000062 self._extraneous_reads = 0
Guido van Rossum68bbcd22007-02-27 17:19:33 +000063
Guido van Rossum01a27522007-03-07 01:00:12 +000064 def write(self, b):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000065 self._write_stack.append(bytes(b))
Guido van Rossum01a27522007-03-07 01:00:12 +000066 return len(b)
67
68 def writable(self):
69 return True
70
Guido van Rossum68bbcd22007-02-27 17:19:33 +000071 def fileno(self):
72 return 42
73
74 def readable(self):
75 return True
76
Guido van Rossum01a27522007-03-07 01:00:12 +000077 def seekable(self):
Guido van Rossum68bbcd22007-02-27 17:19:33 +000078 return True
79
Guido van Rossum01a27522007-03-07 01:00:12 +000080 def seek(self, pos, whence):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000081 return 0 # wrong but we gotta return something
Guido van Rossum01a27522007-03-07 01:00:12 +000082
83 def tell(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000084 return 0 # same comment as above
85
86 def readinto(self, buf):
87 self._reads += 1
88 max_len = len(buf)
89 try:
90 data = self._read_stack[0]
91 except IndexError:
Antoine Pitrou32cfede2010-08-11 13:31:33 +000092 self._extraneous_reads += 1
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000093 return 0
94 if data is None:
95 del self._read_stack[0]
96 return None
97 n = len(data)
98 if len(data) <= max_len:
99 del self._read_stack[0]
100 buf[:n] = data
101 return n
102 else:
103 buf[:] = data[:max_len]
104 self._read_stack[0] = data[max_len:]
105 return max_len
106
107 def truncate(self, pos=None):
108 return pos
109
Antoine Pitrou328ec742010-09-14 18:37:24 +0000110class CMockRawIOWithoutRead(MockRawIOWithoutRead, io.RawIOBase):
111 pass
112
113class PyMockRawIOWithoutRead(MockRawIOWithoutRead, pyio.RawIOBase):
114 pass
115
116
117class MockRawIO(MockRawIOWithoutRead):
118
119 def read(self, n=None):
120 self._reads += 1
121 try:
122 return self._read_stack.pop(0)
123 except:
124 self._extraneous_reads += 1
125 return b""
126
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000127class CMockRawIO(MockRawIO, io.RawIOBase):
128 pass
129
130class PyMockRawIO(MockRawIO, pyio.RawIOBase):
131 pass
Guido van Rossum68bbcd22007-02-27 17:19:33 +0000132
Guido van Rossuma9e20242007-03-08 00:43:48 +0000133
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000134class MisbehavedRawIO(MockRawIO):
135 def write(self, b):
136 return super().write(b) * 2
137
138 def read(self, n=None):
139 return super().read(n) * 2
140
141 def seek(self, pos, whence):
142 return -123
143
144 def tell(self):
145 return -456
146
147 def readinto(self, buf):
148 super().readinto(buf)
149 return len(buf) * 5
150
151class CMisbehavedRawIO(MisbehavedRawIO, io.RawIOBase):
152 pass
153
154class PyMisbehavedRawIO(MisbehavedRawIO, pyio.RawIOBase):
155 pass
156
157
158class CloseFailureIO(MockRawIO):
159 closed = 0
160
161 def close(self):
162 if not self.closed:
163 self.closed = 1
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200164 raise OSError
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000165
166class CCloseFailureIO(CloseFailureIO, io.RawIOBase):
167 pass
168
169class PyCloseFailureIO(CloseFailureIO, pyio.RawIOBase):
170 pass
171
172
173class MockFileIO:
Guido van Rossum78892e42007-04-06 17:31:18 +0000174
175 def __init__(self, data):
176 self.read_history = []
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000177 super().__init__(data)
Guido van Rossum78892e42007-04-06 17:31:18 +0000178
179 def read(self, n=None):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000180 res = super().read(n)
Guido van Rossum78892e42007-04-06 17:31:18 +0000181 self.read_history.append(None if res is None else len(res))
182 return res
183
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000184 def readinto(self, b):
185 res = super().readinto(b)
186 self.read_history.append(res)
187 return res
Guido van Rossum78892e42007-04-06 17:31:18 +0000188
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000189class CMockFileIO(MockFileIO, io.BytesIO):
190 pass
Guido van Rossuma9e20242007-03-08 00:43:48 +0000191
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000192class PyMockFileIO(MockFileIO, pyio.BytesIO):
193 pass
194
195
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000196class MockUnseekableIO:
197 def seekable(self):
198 return False
199
200 def seek(self, *args):
201 raise self.UnsupportedOperation("not seekable")
202
203 def tell(self, *args):
204 raise self.UnsupportedOperation("not seekable")
205
206class CMockUnseekableIO(MockUnseekableIO, io.BytesIO):
207 UnsupportedOperation = io.UnsupportedOperation
208
209class PyMockUnseekableIO(MockUnseekableIO, pyio.BytesIO):
210 UnsupportedOperation = pyio.UnsupportedOperation
211
212
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000213class MockNonBlockWriterIO:
214
215 def __init__(self):
Guido van Rossum01a27522007-03-07 01:00:12 +0000216 self._write_stack = []
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000217 self._blocker_char = None
Guido van Rossuma9e20242007-03-08 00:43:48 +0000218
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000219 def pop_written(self):
220 s = b"".join(self._write_stack)
221 self._write_stack[:] = []
222 return s
223
224 def block_on(self, char):
225 """Block when a given char is encountered."""
226 self._blocker_char = char
227
228 def readable(self):
229 return True
230
231 def seekable(self):
232 return True
Guido van Rossuma9e20242007-03-08 00:43:48 +0000233
Guido van Rossum01a27522007-03-07 01:00:12 +0000234 def writable(self):
235 return True
Guido van Rossum68bbcd22007-02-27 17:19:33 +0000236
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000237 def write(self, b):
238 b = bytes(b)
239 n = -1
240 if self._blocker_char:
241 try:
242 n = b.index(self._blocker_char)
243 except ValueError:
244 pass
245 else:
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +0100246 if n > 0:
247 # write data up to the first blocker
248 self._write_stack.append(b[:n])
249 return n
250 else:
251 # cancel blocker and indicate would block
252 self._blocker_char = None
253 return None
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000254 self._write_stack.append(b)
255 return len(b)
256
257class CMockNonBlockWriterIO(MockNonBlockWriterIO, io.RawIOBase):
258 BlockingIOError = io.BlockingIOError
259
260class PyMockNonBlockWriterIO(MockNonBlockWriterIO, pyio.RawIOBase):
261 BlockingIOError = pyio.BlockingIOError
262
Guido van Rossuma9e20242007-03-08 00:43:48 +0000263
Guido van Rossum28524c72007-02-27 05:47:44 +0000264class IOTest(unittest.TestCase):
265
Neal Norwitze7789b12008-03-24 06:18:09 +0000266 def setUp(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000267 support.unlink(support.TESTFN)
Neal Norwitze7789b12008-03-24 06:18:09 +0000268
Guido van Rossum4d0f5a42007-03-07 22:59:39 +0000269 def tearDown(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000270 support.unlink(support.TESTFN)
Guido van Rossum4d0f5a42007-03-07 22:59:39 +0000271
Guido van Rossum28524c72007-02-27 05:47:44 +0000272 def write_ops(self, f):
Guido van Rossum87429772007-04-10 21:06:59 +0000273 self.assertEqual(f.write(b"blah."), 5)
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000274 f.truncate(0)
275 self.assertEqual(f.tell(), 5)
276 f.seek(0)
277
278 self.assertEqual(f.write(b"blah."), 5)
Guido van Rossum87429772007-04-10 21:06:59 +0000279 self.assertEqual(f.seek(0), 0)
280 self.assertEqual(f.write(b"Hello."), 6)
Guido van Rossum28524c72007-02-27 05:47:44 +0000281 self.assertEqual(f.tell(), 6)
Guido van Rossum87429772007-04-10 21:06:59 +0000282 self.assertEqual(f.seek(-1, 1), 5)
Guido van Rossum28524c72007-02-27 05:47:44 +0000283 self.assertEqual(f.tell(), 5)
Guido van Rossum254348e2007-11-21 19:29:53 +0000284 self.assertEqual(f.write(bytearray(b" world\n\n\n")), 9)
Guido van Rossum87429772007-04-10 21:06:59 +0000285 self.assertEqual(f.seek(0), 0)
Guido van Rossum2b08b382007-05-08 20:18:39 +0000286 self.assertEqual(f.write(b"h"), 1)
Guido van Rossum87429772007-04-10 21:06:59 +0000287 self.assertEqual(f.seek(-1, 2), 13)
288 self.assertEqual(f.tell(), 13)
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000289
Guido van Rossum87429772007-04-10 21:06:59 +0000290 self.assertEqual(f.truncate(12), 12)
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000291 self.assertEqual(f.tell(), 13)
Christian Heimes8e42a0a2007-11-08 18:04:45 +0000292 self.assertRaises(TypeError, f.seek, 0.0)
Guido van Rossum28524c72007-02-27 05:47:44 +0000293
Guido van Rossum9b76da62007-04-11 01:09:03 +0000294 def read_ops(self, f, buffered=False):
295 data = f.read(5)
296 self.assertEqual(data, b"hello")
Guido van Rossum254348e2007-11-21 19:29:53 +0000297 data = bytearray(data)
Guido van Rossum9b76da62007-04-11 01:09:03 +0000298 self.assertEqual(f.readinto(data), 5)
299 self.assertEqual(data, b" worl")
300 self.assertEqual(f.readinto(data), 2)
301 self.assertEqual(len(data), 5)
302 self.assertEqual(data[:2], b"d\n")
303 self.assertEqual(f.seek(0), 0)
304 self.assertEqual(f.read(20), b"hello world\n")
305 self.assertEqual(f.read(1), b"")
Guido van Rossum254348e2007-11-21 19:29:53 +0000306 self.assertEqual(f.readinto(bytearray(b"x")), 0)
Guido van Rossum9b76da62007-04-11 01:09:03 +0000307 self.assertEqual(f.seek(-6, 2), 6)
308 self.assertEqual(f.read(5), b"world")
309 self.assertEqual(f.read(0), b"")
Guido van Rossum254348e2007-11-21 19:29:53 +0000310 self.assertEqual(f.readinto(bytearray()), 0)
Guido van Rossum9b76da62007-04-11 01:09:03 +0000311 self.assertEqual(f.seek(-6, 1), 5)
312 self.assertEqual(f.read(5), b" worl")
313 self.assertEqual(f.tell(), 10)
Christian Heimes8e42a0a2007-11-08 18:04:45 +0000314 self.assertRaises(TypeError, f.seek, 0.0)
Guido van Rossum9b76da62007-04-11 01:09:03 +0000315 if buffered:
316 f.seek(0)
317 self.assertEqual(f.read(), b"hello world\n")
318 f.seek(6)
319 self.assertEqual(f.read(), b"world\n")
320 self.assertEqual(f.read(), b"")
321
Guido van Rossum34d69e52007-04-10 20:08:41 +0000322 LARGE = 2**31
323
Guido van Rossum53807da2007-04-10 19:01:47 +0000324 def large_file_ops(self, f):
325 assert f.readable()
326 assert f.writable()
Guido van Rossum34d69e52007-04-10 20:08:41 +0000327 self.assertEqual(f.seek(self.LARGE), self.LARGE)
328 self.assertEqual(f.tell(), self.LARGE)
Guido van Rossum53807da2007-04-10 19:01:47 +0000329 self.assertEqual(f.write(b"xxx"), 3)
Guido van Rossum34d69e52007-04-10 20:08:41 +0000330 self.assertEqual(f.tell(), self.LARGE + 3)
331 self.assertEqual(f.seek(-1, 1), self.LARGE + 2)
Guido van Rossum87429772007-04-10 21:06:59 +0000332 self.assertEqual(f.truncate(), self.LARGE + 2)
Guido van Rossum34d69e52007-04-10 20:08:41 +0000333 self.assertEqual(f.tell(), self.LARGE + 2)
334 self.assertEqual(f.seek(0, 2), self.LARGE + 2)
Guido van Rossum87429772007-04-10 21:06:59 +0000335 self.assertEqual(f.truncate(self.LARGE + 1), self.LARGE + 1)
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000336 self.assertEqual(f.tell(), self.LARGE + 2)
Guido van Rossum34d69e52007-04-10 20:08:41 +0000337 self.assertEqual(f.seek(0, 2), self.LARGE + 1)
338 self.assertEqual(f.seek(-1, 2), self.LARGE)
Guido van Rossum53807da2007-04-10 19:01:47 +0000339 self.assertEqual(f.read(2), b"x")
340
Benjamin Peterson81971ea2009-05-14 22:01:31 +0000341 def test_invalid_operations(self):
342 # Try writing on a file opened in read mode and vice-versa.
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000343 exc = self.UnsupportedOperation
Benjamin Peterson81971ea2009-05-14 22:01:31 +0000344 for mode in ("w", "wb"):
Hirokazu Yamamotoc7d6aa42009-06-18 00:07:14 +0000345 with self.open(support.TESTFN, mode) as fp:
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000346 self.assertRaises(exc, fp.read)
347 self.assertRaises(exc, fp.readline)
348 with self.open(support.TESTFN, "wb", buffering=0) as fp:
349 self.assertRaises(exc, fp.read)
350 self.assertRaises(exc, fp.readline)
351 with self.open(support.TESTFN, "rb", buffering=0) as fp:
352 self.assertRaises(exc, fp.write, b"blah")
353 self.assertRaises(exc, fp.writelines, [b"blah\n"])
Hirokazu Yamamotoc7d6aa42009-06-18 00:07:14 +0000354 with self.open(support.TESTFN, "rb") as fp:
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000355 self.assertRaises(exc, fp.write, b"blah")
356 self.assertRaises(exc, fp.writelines, [b"blah\n"])
Hirokazu Yamamotoc7d6aa42009-06-18 00:07:14 +0000357 with self.open(support.TESTFN, "r") as fp:
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000358 self.assertRaises(exc, fp.write, "blah")
359 self.assertRaises(exc, fp.writelines, ["blah\n"])
360 # Non-zero seeking from current or end pos
361 self.assertRaises(exc, fp.seek, 1, self.SEEK_CUR)
362 self.assertRaises(exc, fp.seek, -1, self.SEEK_END)
Benjamin Peterson81971ea2009-05-14 22:01:31 +0000363
Antoine Pitrou13348842012-01-29 18:36:34 +0100364 def test_open_handles_NUL_chars(self):
365 fn_with_NUL = 'foo\0bar'
Serhiy Storchakad8a14472014-09-06 20:07:17 +0300366 self.assertRaises(ValueError, self.open, fn_with_NUL, 'w')
367 self.assertRaises(ValueError, self.open, bytes(fn_with_NUL, 'ascii'), 'w')
Antoine Pitrou13348842012-01-29 18:36:34 +0100368
Guido van Rossum28524c72007-02-27 05:47:44 +0000369 def test_raw_file_io(self):
Benjamin Peterson45cec322009-04-24 23:14:50 +0000370 with self.open(support.TESTFN, "wb", buffering=0) as f:
371 self.assertEqual(f.readable(), False)
372 self.assertEqual(f.writable(), True)
373 self.assertEqual(f.seekable(), True)
374 self.write_ops(f)
375 with self.open(support.TESTFN, "rb", buffering=0) as f:
376 self.assertEqual(f.readable(), True)
377 self.assertEqual(f.writable(), False)
378 self.assertEqual(f.seekable(), True)
379 self.read_ops(f)
Guido van Rossum28524c72007-02-27 05:47:44 +0000380
Guido van Rossum87429772007-04-10 21:06:59 +0000381 def test_buffered_file_io(self):
Benjamin Peterson45cec322009-04-24 23:14:50 +0000382 with self.open(support.TESTFN, "wb") as f:
383 self.assertEqual(f.readable(), False)
384 self.assertEqual(f.writable(), True)
385 self.assertEqual(f.seekable(), True)
386 self.write_ops(f)
387 with self.open(support.TESTFN, "rb") as f:
388 self.assertEqual(f.readable(), True)
389 self.assertEqual(f.writable(), False)
390 self.assertEqual(f.seekable(), True)
391 self.read_ops(f, True)
Guido van Rossum87429772007-04-10 21:06:59 +0000392
Guido van Rossum48fc58a2007-06-07 23:45:37 +0000393 def test_readline(self):
Benjamin Peterson45cec322009-04-24 23:14:50 +0000394 with self.open(support.TESTFN, "wb") as f:
395 f.write(b"abc\ndef\nxyzzy\nfoo\x00bar\nanother line")
396 with self.open(support.TESTFN, "rb") as f:
397 self.assertEqual(f.readline(), b"abc\n")
398 self.assertEqual(f.readline(10), b"def\n")
399 self.assertEqual(f.readline(2), b"xy")
400 self.assertEqual(f.readline(4), b"zzy\n")
401 self.assertEqual(f.readline(), b"foo\x00bar\n")
Benjamin Petersonbf5ff762009-12-13 19:25:34 +0000402 self.assertEqual(f.readline(None), b"another line")
Benjamin Peterson45cec322009-04-24 23:14:50 +0000403 self.assertRaises(TypeError, f.readline, 5.3)
404 with self.open(support.TESTFN, "r") as f:
405 self.assertRaises(TypeError, f.readline, 5.3)
Guido van Rossum48fc58a2007-06-07 23:45:37 +0000406
Guido van Rossum28524c72007-02-27 05:47:44 +0000407 def test_raw_bytes_io(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000408 f = self.BytesIO()
Guido van Rossum28524c72007-02-27 05:47:44 +0000409 self.write_ops(f)
410 data = f.getvalue()
411 self.assertEqual(data, b"hello world\n")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000412 f = self.BytesIO(data)
Guido van Rossum9b76da62007-04-11 01:09:03 +0000413 self.read_ops(f, True)
Guido van Rossum28524c72007-02-27 05:47:44 +0000414
Guido van Rossum53807da2007-04-10 19:01:47 +0000415 def test_large_file_ops(self):
Guido van Rossum34d69e52007-04-10 20:08:41 +0000416 # On Windows and Mac OSX this test comsumes large resources; It takes
417 # a long time to build the >2GB file and takes >2GB of disk space
418 # therefore the resource must be enabled to run this test.
419 if sys.platform[:3] == 'win' or sys.platform == 'darwin':
Zachary Ware9fe6d862013-12-08 00:20:35 -0600420 support.requires(
421 'largefile',
422 'test requires %s bytes and a long time to run' % self.LARGE)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000423 with self.open(support.TESTFN, "w+b", 0) as f:
424 self.large_file_ops(f)
425 with self.open(support.TESTFN, "w+b") as f:
426 self.large_file_ops(f)
Guido van Rossum87429772007-04-10 21:06:59 +0000427
428 def test_with_open(self):
429 for bufsize in (0, 1, 100):
430 f = None
Hirokazu Yamamotoc7d6aa42009-06-18 00:07:14 +0000431 with self.open(support.TESTFN, "wb", bufsize) as f:
Guido van Rossum1f2ca562007-08-27 20:44:15 +0000432 f.write(b"xxx")
Guido van Rossum87429772007-04-10 21:06:59 +0000433 self.assertEqual(f.closed, True)
434 f = None
435 try:
Hirokazu Yamamotoc7d6aa42009-06-18 00:07:14 +0000436 with self.open(support.TESTFN, "wb", bufsize) as f:
Guido van Rossum87429772007-04-10 21:06:59 +0000437 1/0
438 except ZeroDivisionError:
439 self.assertEqual(f.closed, True)
440 else:
441 self.fail("1/0 didn't raise an exception")
442
Antoine Pitrou08838b62009-01-21 00:55:13 +0000443 # issue 5008
444 def test_append_mode_tell(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000445 with self.open(support.TESTFN, "wb") as f:
Antoine Pitrou08838b62009-01-21 00:55:13 +0000446 f.write(b"xxx")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000447 with self.open(support.TESTFN, "ab", buffering=0) as f:
Antoine Pitrou08838b62009-01-21 00:55:13 +0000448 self.assertEqual(f.tell(), 3)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000449 with self.open(support.TESTFN, "ab") as f:
Antoine Pitrou08838b62009-01-21 00:55:13 +0000450 self.assertEqual(f.tell(), 3)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000451 with self.open(support.TESTFN, "a") as f:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000452 self.assertTrue(f.tell() > 0)
Antoine Pitrou08838b62009-01-21 00:55:13 +0000453
Guido van Rossum87429772007-04-10 21:06:59 +0000454 def test_destructor(self):
455 record = []
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000456 class MyFileIO(self.FileIO):
Guido van Rossum87429772007-04-10 21:06:59 +0000457 def __del__(self):
458 record.append(1)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000459 try:
460 f = super().__del__
461 except AttributeError:
462 pass
463 else:
464 f()
Guido van Rossum87429772007-04-10 21:06:59 +0000465 def close(self):
466 record.append(2)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000467 super().close()
Guido van Rossum87429772007-04-10 21:06:59 +0000468 def flush(self):
469 record.append(3)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000470 super().flush()
Brett Cannon5a9e91b2010-10-29 23:53:03 +0000471 with support.check_warnings(('', ResourceWarning)):
472 f = MyFileIO(support.TESTFN, "wb")
473 f.write(b"xxx")
474 del f
475 support.gc_collect()
476 self.assertEqual(record, [1, 2, 3])
477 with self.open(support.TESTFN, "rb") as f:
478 self.assertEqual(f.read(), b"xxx")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000479
480 def _check_base_destructor(self, base):
481 record = []
482 class MyIO(base):
483 def __init__(self):
484 # This exercises the availability of attributes on object
485 # destruction.
486 # (in the C version, close() is called by the tp_dealloc
487 # function, not by __del__)
488 self.on_del = 1
489 self.on_close = 2
490 self.on_flush = 3
491 def __del__(self):
492 record.append(self.on_del)
493 try:
494 f = super().__del__
495 except AttributeError:
496 pass
497 else:
498 f()
499 def close(self):
500 record.append(self.on_close)
501 super().close()
502 def flush(self):
503 record.append(self.on_flush)
504 super().flush()
505 f = MyIO()
Guido van Rossum87429772007-04-10 21:06:59 +0000506 del f
Benjamin Peterson24fb1d02009-04-24 23:26:21 +0000507 support.gc_collect()
Guido van Rossum87429772007-04-10 21:06:59 +0000508 self.assertEqual(record, [1, 2, 3])
509
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000510 def test_IOBase_destructor(self):
511 self._check_base_destructor(self.IOBase)
512
513 def test_RawIOBase_destructor(self):
514 self._check_base_destructor(self.RawIOBase)
515
516 def test_BufferedIOBase_destructor(self):
517 self._check_base_destructor(self.BufferedIOBase)
518
519 def test_TextIOBase_destructor(self):
520 self._check_base_destructor(self.TextIOBase)
521
Guido van Rossum87429772007-04-10 21:06:59 +0000522 def test_close_flushes(self):
Benjamin Peterson45cec322009-04-24 23:14:50 +0000523 with self.open(support.TESTFN, "wb") as f:
524 f.write(b"xxx")
525 with self.open(support.TESTFN, "rb") as f:
526 self.assertEqual(f.read(), b"xxx")
Guido van Rossuma9e20242007-03-08 00:43:48 +0000527
Guido van Rossumd4103952007-04-12 05:44:49 +0000528 def test_array_writes(self):
529 a = array.array('i', range(10))
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +0000530 n = len(a.tobytes())
Benjamin Peterson45cec322009-04-24 23:14:50 +0000531 with self.open(support.TESTFN, "wb", 0) as f:
532 self.assertEqual(f.write(a), n)
533 with self.open(support.TESTFN, "wb") as f:
534 self.assertEqual(f.write(a), n)
Guido van Rossumd4103952007-04-12 05:44:49 +0000535
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000536 def test_closefd(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000537 self.assertRaises(ValueError, self.open, support.TESTFN, 'w',
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000538 closefd=False)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000539
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000540 def test_read_closed(self):
541 with self.open(support.TESTFN, "w") as f:
Christian Heimesecc42a22008-11-05 19:30:32 +0000542 f.write("egg\n")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000543 with self.open(support.TESTFN, "r") as f:
544 file = self.open(f.fileno(), "r", closefd=False)
Christian Heimesecc42a22008-11-05 19:30:32 +0000545 self.assertEqual(file.read(), "egg\n")
546 file.seek(0)
547 file.close()
548 self.assertRaises(ValueError, file.read)
549
550 def test_no_closefd_with_filename(self):
551 # can't use closefd in combination with a file name
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000552 self.assertRaises(ValueError, self.open, support.TESTFN, "r", closefd=False)
Christian Heimesecc42a22008-11-05 19:30:32 +0000553
554 def test_closefd_attr(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000555 with self.open(support.TESTFN, "wb") as f:
Christian Heimesecc42a22008-11-05 19:30:32 +0000556 f.write(b"egg\n")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000557 with self.open(support.TESTFN, "r") as f:
Christian Heimesecc42a22008-11-05 19:30:32 +0000558 self.assertEqual(f.buffer.raw.closefd, True)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000559 file = self.open(f.fileno(), "r", closefd=False)
Christian Heimesecc42a22008-11-05 19:30:32 +0000560 self.assertEqual(file.buffer.raw.closefd, False)
561
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000562 def test_garbage_collection(self):
563 # FileIO objects are collected, and collecting them flushes
564 # all data to disk.
Benjamin Petersonebe5d8a2010-10-31 01:30:11 +0000565 with support.check_warnings(('', ResourceWarning)):
566 f = self.FileIO(support.TESTFN, "wb")
567 f.write(b"abcxxx")
568 f.f = f
569 wr = weakref.ref(f)
570 del f
571 support.gc_collect()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000572 self.assertTrue(wr() is None, wr)
Hirokazu Yamamotoc7d6aa42009-06-18 00:07:14 +0000573 with self.open(support.TESTFN, "rb") as f:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000574 self.assertEqual(f.read(), b"abcxxx")
Christian Heimesecc42a22008-11-05 19:30:32 +0000575
Antoine Pitrou7d037a72009-03-29 18:55:12 +0000576 def test_unbounded_file(self):
577 # Issue #1174606: reading from an unbounded stream such as /dev/zero.
578 zero = "/dev/zero"
579 if not os.path.exists(zero):
Antoine Pitrouc50cb8e2009-04-19 00:10:36 +0000580 self.skipTest("{0} does not exist".format(zero))
Antoine Pitrou7d037a72009-03-29 18:55:12 +0000581 if sys.maxsize > 0x7FFFFFFF:
Antoine Pitrouc50cb8e2009-04-19 00:10:36 +0000582 self.skipTest("test can only run in a 32-bit address space")
Antoine Pitrou7d037a72009-03-29 18:55:12 +0000583 if support.real_max_memuse < support._2G:
Antoine Pitrouc50cb8e2009-04-19 00:10:36 +0000584 self.skipTest("test requires at least 2GB of memory")
Hirokazu Yamamotoc7d6aa42009-06-18 00:07:14 +0000585 with self.open(zero, "rb", buffering=0) as f:
Antoine Pitrou7d037a72009-03-29 18:55:12 +0000586 self.assertRaises(OverflowError, f.read)
Hirokazu Yamamotoc7d6aa42009-06-18 00:07:14 +0000587 with self.open(zero, "rb") as f:
Antoine Pitrou7d037a72009-03-29 18:55:12 +0000588 self.assertRaises(OverflowError, f.read)
Hirokazu Yamamotoc7d6aa42009-06-18 00:07:14 +0000589 with self.open(zero, "r") as f:
Antoine Pitrou7d037a72009-03-29 18:55:12 +0000590 self.assertRaises(OverflowError, f.read)
591
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200592 def check_flush_error_on_close(self, *args, **kwargs):
593 # Test that the file is closed despite failed flush
594 # and that flush() is called before file closed.
595 f = self.open(*args, **kwargs)
596 closed = []
Antoine Pitrou6be88762010-05-03 16:48:20 +0000597 def bad_flush():
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200598 closed[:] = [f.closed]
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200599 raise OSError()
Antoine Pitrou6be88762010-05-03 16:48:20 +0000600 f.flush = bad_flush
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200601 self.assertRaises(OSError, f.close) # exception not swallowed
Benjamin Peterson68623612012-12-20 11:53:11 -0600602 self.assertTrue(f.closed)
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200603 self.assertTrue(closed) # flush() called
604 self.assertFalse(closed[0]) # flush() called before file closed
Serhiy Storchakac26a1a42015-02-23 00:28:38 +0200605 f.flush = lambda: None # break reference loop
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200606
607 def test_flush_error_on_close(self):
608 # raw file
609 # Issue #5700: io.FileIO calls flush() after file closed
610 self.check_flush_error_on_close(support.TESTFN, 'wb', buffering=0)
611 fd = os.open(support.TESTFN, os.O_WRONLY|os.O_CREAT)
612 self.check_flush_error_on_close(fd, 'wb', buffering=0)
613 fd = os.open(support.TESTFN, os.O_WRONLY|os.O_CREAT)
614 self.check_flush_error_on_close(fd, 'wb', buffering=0, closefd=False)
615 os.close(fd)
616 # buffered io
617 self.check_flush_error_on_close(support.TESTFN, 'wb')
618 fd = os.open(support.TESTFN, os.O_WRONLY|os.O_CREAT)
619 self.check_flush_error_on_close(fd, 'wb')
620 fd = os.open(support.TESTFN, os.O_WRONLY|os.O_CREAT)
621 self.check_flush_error_on_close(fd, 'wb', closefd=False)
622 os.close(fd)
623 # text io
624 self.check_flush_error_on_close(support.TESTFN, 'w')
625 fd = os.open(support.TESTFN, os.O_WRONLY|os.O_CREAT)
626 self.check_flush_error_on_close(fd, 'w')
627 fd = os.open(support.TESTFN, os.O_WRONLY|os.O_CREAT)
628 self.check_flush_error_on_close(fd, 'w', closefd=False)
629 os.close(fd)
Antoine Pitrou6be88762010-05-03 16:48:20 +0000630
631 def test_multi_close(self):
632 f = self.open(support.TESTFN, "wb", buffering=0)
633 f.close()
634 f.close()
635 f.close()
636 self.assertRaises(ValueError, f.flush)
637
Antoine Pitrou328ec742010-09-14 18:37:24 +0000638 def test_RawIOBase_read(self):
639 # Exercise the default RawIOBase.read() implementation (which calls
640 # readinto() internally).
641 rawio = self.MockRawIOWithoutRead((b"abc", b"d", None, b"efg", None))
642 self.assertEqual(rawio.read(2), b"ab")
643 self.assertEqual(rawio.read(2), b"c")
644 self.assertEqual(rawio.read(2), b"d")
645 self.assertEqual(rawio.read(2), None)
646 self.assertEqual(rawio.read(2), b"ef")
647 self.assertEqual(rawio.read(2), b"g")
648 self.assertEqual(rawio.read(2), None)
649 self.assertEqual(rawio.read(2), b"")
650
Benjamin Petersonf6f3a352011-09-03 09:26:20 -0400651 def test_types_have_dict(self):
652 test = (
653 self.IOBase(),
654 self.RawIOBase(),
655 self.TextIOBase(),
656 self.StringIO(),
657 self.BytesIO()
658 )
659 for obj in test:
660 self.assertTrue(hasattr(obj, "__dict__"))
661
Ross Lagerwall59142db2011-10-31 20:34:46 +0200662 def test_opener(self):
663 with self.open(support.TESTFN, "w") as f:
664 f.write("egg\n")
665 fd = os.open(support.TESTFN, os.O_RDONLY)
666 def opener(path, flags):
667 return fd
668 with self.open("non-existent", "r", opener=opener) as f:
669 self.assertEqual(f.read(), "egg\n")
670
Hynek Schlawack2cc71562012-05-25 10:05:53 +0200671 def test_fileio_closefd(self):
672 # Issue #4841
673 with self.open(__file__, 'rb') as f1, \
674 self.open(__file__, 'rb') as f2:
675 fileio = self.FileIO(f1.fileno(), closefd=False)
676 # .__init__() must not close f1
677 fileio.__init__(f2.fileno(), closefd=False)
678 f1.readline()
679 # .close() must not close f2
680 fileio.close()
681 f2.readline()
682
Serhiy Storchakaf10063e2014-06-09 13:32:34 +0300683 def test_nonbuffered_textio(self):
684 with warnings.catch_warnings(record=True) as recorded:
685 with self.assertRaises(ValueError):
686 self.open(support.TESTFN, 'w', buffering=0)
687 support.gc_collect()
688 self.assertEqual(recorded, [])
689
690 def test_invalid_newline(self):
691 with warnings.catch_warnings(record=True) as recorded:
692 with self.assertRaises(ValueError):
693 self.open(support.TESTFN, 'w', newline='invalid')
694 support.gc_collect()
695 self.assertEqual(recorded, [])
696
Hynek Schlawack2cc71562012-05-25 10:05:53 +0200697
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000698class CIOTest(IOTest):
Antoine Pitrou84f1b172011-07-12 21:57:15 +0200699
700 def test_IOBase_finalize(self):
701 # Issue #12149: segmentation fault on _PyIOBase_finalize when both a
702 # class which inherits IOBase and an object of this class are caught
703 # in a reference cycle and close() is already in the method cache.
704 class MyIO(self.IOBase):
705 def close(self):
706 pass
707
708 # create an instance to populate the method cache
709 MyIO()
710 obj = MyIO()
711 obj.obj = obj
712 wr = weakref.ref(obj)
713 del MyIO
714 del obj
715 support.gc_collect()
716 self.assertTrue(wr() is None, wr)
Guido van Rossuma9e20242007-03-08 00:43:48 +0000717
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000718class PyIOTest(IOTest):
719 pass
Guido van Rossum68bbcd22007-02-27 17:19:33 +0000720
Guido van Rossuma9e20242007-03-08 00:43:48 +0000721
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000722class CommonBufferedTests:
723 # Tests common to BufferedReader, BufferedWriter and BufferedRandom
724
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000725 def test_detach(self):
726 raw = self.MockRawIO()
727 buf = self.tp(raw)
728 self.assertIs(buf.detach(), raw)
729 self.assertRaises(ValueError, buf.detach)
730
Benjamin Peterson10e76b62014-12-21 20:51:50 -0600731 repr(buf) # Should still work
732
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000733 def test_fileno(self):
734 rawio = self.MockRawIO()
735 bufio = self.tp(rawio)
736
Ezio Melottib3aedd42010-11-20 19:04:17 +0000737 self.assertEqual(42, bufio.fileno())
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000738
Zachary Ware9fe6d862013-12-08 00:20:35 -0600739 @unittest.skip('test having existential crisis')
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000740 def test_no_fileno(self):
741 # XXX will we always have fileno() function? If so, kill
742 # this test. Else, write it.
743 pass
744
745 def test_invalid_args(self):
746 rawio = self.MockRawIO()
747 bufio = self.tp(rawio)
748 # Invalid whence
749 self.assertRaises(ValueError, bufio.seek, 0, -1)
Jesus Cea94363612012-06-22 18:32:07 +0200750 self.assertRaises(ValueError, bufio.seek, 0, 9)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000751
752 def test_override_destructor(self):
753 tp = self.tp
754 record = []
755 class MyBufferedIO(tp):
756 def __del__(self):
757 record.append(1)
758 try:
759 f = super().__del__
760 except AttributeError:
761 pass
762 else:
763 f()
764 def close(self):
765 record.append(2)
766 super().close()
767 def flush(self):
768 record.append(3)
769 super().flush()
770 rawio = self.MockRawIO()
771 bufio = MyBufferedIO(rawio)
772 writable = bufio.writable()
773 del bufio
Benjamin Peterson24fb1d02009-04-24 23:26:21 +0000774 support.gc_collect()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000775 if writable:
776 self.assertEqual(record, [1, 2, 3])
777 else:
778 self.assertEqual(record, [1, 2])
779
780 def test_context_manager(self):
781 # Test usability as a context manager
782 rawio = self.MockRawIO()
783 bufio = self.tp(rawio)
784 def _with():
785 with bufio:
786 pass
787 _with()
788 # bufio should now be closed, and using it a second time should raise
789 # a ValueError.
790 self.assertRaises(ValueError, _with)
791
792 def test_error_through_destructor(self):
793 # Test that the exception state is not modified by a destructor,
794 # even if close() fails.
795 rawio = self.CloseFailureIO()
796 def f():
797 self.tp(rawio).xyzzy
798 with support.captured_output("stderr") as s:
799 self.assertRaises(AttributeError, f)
800 s = s.getvalue().strip()
801 if s:
802 # The destructor *may* have printed an unraisable error, check it
803 self.assertEqual(len(s.splitlines()), 1)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200804 self.assertTrue(s.startswith("Exception OSError: "), s)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000805 self.assertTrue(s.endswith(" ignored"), s)
Guido van Rossum78892e42007-04-06 17:31:18 +0000806
Antoine Pitrou716c4442009-05-23 19:04:03 +0000807 def test_repr(self):
808 raw = self.MockRawIO()
809 b = self.tp(raw)
Serhiy Storchaka521e5862014-07-22 15:00:37 +0300810 clsname = "%s.%s" % (self.tp.__module__, self.tp.__qualname__)
Antoine Pitrou716c4442009-05-23 19:04:03 +0000811 self.assertEqual(repr(b), "<%s>" % clsname)
812 raw.name = "dummy"
813 self.assertEqual(repr(b), "<%s name='dummy'>" % clsname)
814 raw.name = b"dummy"
815 self.assertEqual(repr(b), "<%s name=b'dummy'>" % clsname)
816
Antoine Pitrou6be88762010-05-03 16:48:20 +0000817 def test_flush_error_on_close(self):
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200818 # Test that buffered file is closed despite failed flush
819 # and that flush() is called before file closed.
Antoine Pitrou6be88762010-05-03 16:48:20 +0000820 raw = self.MockRawIO()
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200821 closed = []
Antoine Pitrou6be88762010-05-03 16:48:20 +0000822 def bad_flush():
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200823 closed[:] = [b.closed, raw.closed]
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200824 raise OSError()
Antoine Pitrou6be88762010-05-03 16:48:20 +0000825 raw.flush = bad_flush
826 b = self.tp(raw)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200827 self.assertRaises(OSError, b.close) # exception not swallowed
Benjamin Peterson68623612012-12-20 11:53:11 -0600828 self.assertTrue(b.closed)
Serhiy Storchakaa3712a92015-02-21 00:35:09 +0200829 self.assertTrue(raw.closed)
830 self.assertTrue(closed) # flush() called
831 self.assertFalse(closed[0]) # flush() called before file closed
832 self.assertFalse(closed[1])
Serhiy Storchakac26a1a42015-02-23 00:28:38 +0200833 raw.flush = lambda: None # break reference loop
Benjamin Peterson68623612012-12-20 11:53:11 -0600834
835 def test_close_error_on_close(self):
836 raw = self.MockRawIO()
837 def bad_flush():
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200838 raise OSError('flush')
Benjamin Peterson68623612012-12-20 11:53:11 -0600839 def bad_close():
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200840 raise OSError('close')
Benjamin Peterson68623612012-12-20 11:53:11 -0600841 raw.close = bad_close
842 b = self.tp(raw)
843 b.flush = bad_flush
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200844 with self.assertRaises(OSError) as err: # exception not swallowed
Benjamin Peterson68623612012-12-20 11:53:11 -0600845 b.close()
846 self.assertEqual(err.exception.args, ('close',))
Serhiy Storchaka8a8f7f92014-06-09 09:13:04 +0300847 self.assertIsInstance(err.exception.__context__, OSError)
Benjamin Peterson68623612012-12-20 11:53:11 -0600848 self.assertEqual(err.exception.__context__.args, ('flush',))
849 self.assertFalse(b.closed)
Antoine Pitrou6be88762010-05-03 16:48:20 +0000850
Serhiy Storchaka8a8f7f92014-06-09 09:13:04 +0300851 def test_nonnormalized_close_error_on_close(self):
852 # Issue #21677
853 raw = self.MockRawIO()
854 def bad_flush():
855 raise non_existing_flush
856 def bad_close():
857 raise non_existing_close
858 raw.close = bad_close
859 b = self.tp(raw)
860 b.flush = bad_flush
861 with self.assertRaises(NameError) as err: # exception not swallowed
862 b.close()
863 self.assertIn('non_existing_close', str(err.exception))
864 self.assertIsInstance(err.exception.__context__, NameError)
865 self.assertIn('non_existing_flush', str(err.exception.__context__))
866 self.assertFalse(b.closed)
867
Antoine Pitrou6be88762010-05-03 16:48:20 +0000868 def test_multi_close(self):
869 raw = self.MockRawIO()
870 b = self.tp(raw)
871 b.close()
872 b.close()
873 b.close()
874 self.assertRaises(ValueError, b.flush)
875
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000876 def test_unseekable(self):
877 bufio = self.tp(self.MockUnseekableIO(b"A" * 10))
878 self.assertRaises(self.UnsupportedOperation, bufio.tell)
879 self.assertRaises(self.UnsupportedOperation, bufio.seek, 0)
880
Antoine Pitrou7f8f4182010-12-21 21:20:59 +0000881 def test_readonly_attributes(self):
882 raw = self.MockRawIO()
883 buf = self.tp(raw)
884 x = self.MockRawIO()
885 with self.assertRaises(AttributeError):
886 buf.raw = x
887
Guido van Rossum78892e42007-04-06 17:31:18 +0000888
Antoine Pitrou10f0c502012-07-29 19:02:46 +0200889class SizeofTest:
890
891 @support.cpython_only
892 def test_sizeof(self):
893 bufsize1 = 4096
894 bufsize2 = 8192
895 rawio = self.MockRawIO()
896 bufio = self.tp(rawio, buffer_size=bufsize1)
897 size = sys.getsizeof(bufio) - bufsize1
898 rawio = self.MockRawIO()
899 bufio = self.tp(rawio, buffer_size=bufsize2)
900 self.assertEqual(sys.getsizeof(bufio), size + bufsize2)
901
Jesus Ceadc469452012-10-04 12:37:56 +0200902 @support.cpython_only
903 def test_buffer_freeing(self) :
904 bufsize = 4096
905 rawio = self.MockRawIO()
906 bufio = self.tp(rawio, buffer_size=bufsize)
907 size = sys.getsizeof(bufio) - bufsize
908 bufio.close()
909 self.assertEqual(sys.getsizeof(bufio), size)
Antoine Pitrou10f0c502012-07-29 19:02:46 +0200910
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000911class BufferedReaderTest(unittest.TestCase, CommonBufferedTests):
912 read_mode = "rb"
Guido van Rossum78892e42007-04-06 17:31:18 +0000913
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000914 def test_constructor(self):
915 rawio = self.MockRawIO([b"abc"])
916 bufio = self.tp(rawio)
917 bufio.__init__(rawio)
918 bufio.__init__(rawio, buffer_size=1024)
919 bufio.__init__(rawio, buffer_size=16)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000920 self.assertEqual(b"abc", bufio.read())
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000921 self.assertRaises(ValueError, bufio.__init__, rawio, buffer_size=0)
922 self.assertRaises(ValueError, bufio.__init__, rawio, buffer_size=-16)
923 self.assertRaises(ValueError, bufio.__init__, rawio, buffer_size=-1)
924 rawio = self.MockRawIO([b"abc"])
925 bufio.__init__(rawio)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000926 self.assertEqual(b"abc", bufio.read())
Guido van Rossum78892e42007-04-06 17:31:18 +0000927
Serhiy Storchaka61e24932014-02-12 10:52:35 +0200928 def test_uninitialized(self):
929 bufio = self.tp.__new__(self.tp)
930 del bufio
931 bufio = self.tp.__new__(self.tp)
932 self.assertRaisesRegex((ValueError, AttributeError),
933 'uninitialized|has no attribute',
934 bufio.read, 0)
935 bufio.__init__(self.MockRawIO())
936 self.assertEqual(bufio.read(0), b'')
937
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000938 def test_read(self):
Benjamin Petersonbf5ff762009-12-13 19:25:34 +0000939 for arg in (None, 7):
940 rawio = self.MockRawIO((b"abc", b"d", b"efg"))
941 bufio = self.tp(rawio)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000942 self.assertEqual(b"abcdefg", bufio.read(arg))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000943 # Invalid args
944 self.assertRaises(ValueError, bufio.read, -2)
Guido van Rossum68bbcd22007-02-27 17:19:33 +0000945
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000946 def test_read1(self):
947 rawio = self.MockRawIO((b"abc", b"d", b"efg"))
948 bufio = self.tp(rawio)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000949 self.assertEqual(b"a", bufio.read(1))
950 self.assertEqual(b"b", bufio.read1(1))
951 self.assertEqual(rawio._reads, 1)
952 self.assertEqual(b"c", bufio.read1(100))
953 self.assertEqual(rawio._reads, 1)
954 self.assertEqual(b"d", bufio.read1(100))
955 self.assertEqual(rawio._reads, 2)
956 self.assertEqual(b"efg", bufio.read1(100))
957 self.assertEqual(rawio._reads, 3)
958 self.assertEqual(b"", bufio.read1(100))
959 self.assertEqual(rawio._reads, 4)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000960 # Invalid args
961 self.assertRaises(ValueError, bufio.read1, -1)
962
963 def test_readinto(self):
964 rawio = self.MockRawIO((b"abc", b"d", b"efg"))
965 bufio = self.tp(rawio)
966 b = bytearray(2)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000967 self.assertEqual(bufio.readinto(b), 2)
968 self.assertEqual(b, b"ab")
969 self.assertEqual(bufio.readinto(b), 2)
970 self.assertEqual(b, b"cd")
971 self.assertEqual(bufio.readinto(b), 2)
972 self.assertEqual(b, b"ef")
973 self.assertEqual(bufio.readinto(b), 1)
974 self.assertEqual(b, b"gf")
975 self.assertEqual(bufio.readinto(b), 0)
976 self.assertEqual(b, b"gf")
Antoine Pitrou3486a982011-05-12 01:57:53 +0200977 rawio = self.MockRawIO((b"abc", None))
978 bufio = self.tp(rawio)
979 self.assertEqual(bufio.readinto(b), 2)
980 self.assertEqual(b, b"ab")
981 self.assertEqual(bufio.readinto(b), 1)
982 self.assertEqual(b, b"cb")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000983
Benjamin Petersona96fea02014-06-22 14:17:44 -0700984 def test_readinto1(self):
985 buffer_size = 10
986 rawio = self.MockRawIO((b"abc", b"de", b"fgh", b"jkl"))
987 bufio = self.tp(rawio, buffer_size=buffer_size)
988 b = bytearray(2)
989 self.assertEqual(bufio.peek(3), b'abc')
990 self.assertEqual(rawio._reads, 1)
991 self.assertEqual(bufio.readinto1(b), 2)
992 self.assertEqual(b, b"ab")
993 self.assertEqual(rawio._reads, 1)
994 self.assertEqual(bufio.readinto1(b), 1)
995 self.assertEqual(b[:1], b"c")
996 self.assertEqual(rawio._reads, 1)
997 self.assertEqual(bufio.readinto1(b), 2)
998 self.assertEqual(b, b"de")
999 self.assertEqual(rawio._reads, 2)
1000 b = bytearray(2*buffer_size)
1001 self.assertEqual(bufio.peek(3), b'fgh')
1002 self.assertEqual(rawio._reads, 3)
1003 self.assertEqual(bufio.readinto1(b), 6)
1004 self.assertEqual(b[:6], b"fghjkl")
1005 self.assertEqual(rawio._reads, 4)
1006
1007 def test_readinto_array(self):
1008 buffer_size = 60
1009 data = b"a" * 26
1010 rawio = self.MockRawIO((data,))
1011 bufio = self.tp(rawio, buffer_size=buffer_size)
1012
1013 # Create an array with element size > 1 byte
1014 b = array.array('i', b'x' * 32)
1015 assert len(b) != 16
1016
1017 # Read into it. We should get as many *bytes* as we can fit into b
1018 # (which is more than the number of elements)
1019 n = bufio.readinto(b)
1020 self.assertGreater(n, len(b))
1021
1022 # Check that old contents of b are preserved
1023 bm = memoryview(b).cast('B')
1024 self.assertLess(n, len(bm))
1025 self.assertEqual(bm[:n], data[:n])
1026 self.assertEqual(bm[n:], b'x' * (len(bm[n:])))
1027
1028 def test_readinto1_array(self):
1029 buffer_size = 60
1030 data = b"a" * 26
1031 rawio = self.MockRawIO((data,))
1032 bufio = self.tp(rawio, buffer_size=buffer_size)
1033
1034 # Create an array with element size > 1 byte
1035 b = array.array('i', b'x' * 32)
1036 assert len(b) != 16
1037
1038 # Read into it. We should get as many *bytes* as we can fit into b
1039 # (which is more than the number of elements)
1040 n = bufio.readinto1(b)
1041 self.assertGreater(n, len(b))
1042
1043 # Check that old contents of b are preserved
1044 bm = memoryview(b).cast('B')
1045 self.assertLess(n, len(bm))
1046 self.assertEqual(bm[:n], data[:n])
1047 self.assertEqual(bm[n:], b'x' * (len(bm[n:])))
1048
Benjamin Petersonbf5ff762009-12-13 19:25:34 +00001049 def test_readlines(self):
1050 def bufio():
1051 rawio = self.MockRawIO((b"abc\n", b"d\n", b"ef"))
1052 return self.tp(rawio)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001053 self.assertEqual(bufio().readlines(), [b"abc\n", b"d\n", b"ef"])
1054 self.assertEqual(bufio().readlines(5), [b"abc\n", b"d\n"])
1055 self.assertEqual(bufio().readlines(None), [b"abc\n", b"d\n", b"ef"])
Benjamin Petersonbf5ff762009-12-13 19:25:34 +00001056
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001057 def test_buffering(self):
Guido van Rossum78892e42007-04-06 17:31:18 +00001058 data = b"abcdefghi"
1059 dlen = len(data)
1060
1061 tests = [
1062 [ 100, [ 3, 1, 4, 8 ], [ dlen, 0 ] ],
1063 [ 100, [ 3, 3, 3], [ dlen ] ],
1064 [ 4, [ 1, 2, 4, 2 ], [ 4, 4, 1 ] ],
1065 ]
1066
1067 for bufsize, buf_read_sizes, raw_read_sizes in tests:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001068 rawio = self.MockFileIO(data)
1069 bufio = self.tp(rawio, buffer_size=bufsize)
Guido van Rossum78892e42007-04-06 17:31:18 +00001070 pos = 0
1071 for nbytes in buf_read_sizes:
Ezio Melottib3aedd42010-11-20 19:04:17 +00001072 self.assertEqual(bufio.read(nbytes), data[pos:pos+nbytes])
Guido van Rossum78892e42007-04-06 17:31:18 +00001073 pos += nbytes
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001074 # this is mildly implementation-dependent
Ezio Melottib3aedd42010-11-20 19:04:17 +00001075 self.assertEqual(rawio.read_history, raw_read_sizes)
Guido van Rossum78892e42007-04-06 17:31:18 +00001076
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001077 def test_read_non_blocking(self):
Guido van Rossum01a27522007-03-07 01:00:12 +00001078 # Inject some None's in there to simulate EWOULDBLOCK
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001079 rawio = self.MockRawIO((b"abc", b"d", None, b"efg", None, None, None))
1080 bufio = self.tp(rawio)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001081 self.assertEqual(b"abcd", bufio.read(6))
1082 self.assertEqual(b"e", bufio.read(1))
1083 self.assertEqual(b"fg", bufio.read())
1084 self.assertEqual(b"", bufio.peek(1))
Victor Stinnera80987f2011-05-25 22:47:16 +02001085 self.assertIsNone(bufio.read())
Ezio Melottib3aedd42010-11-20 19:04:17 +00001086 self.assertEqual(b"", bufio.read())
Guido van Rossum01a27522007-03-07 01:00:12 +00001087
Victor Stinnera80987f2011-05-25 22:47:16 +02001088 rawio = self.MockRawIO((b"a", None, None))
1089 self.assertEqual(b"a", rawio.readall())
1090 self.assertIsNone(rawio.readall())
1091
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001092 def test_read_past_eof(self):
1093 rawio = self.MockRawIO((b"abc", b"d", b"efg"))
1094 bufio = self.tp(rawio)
Guido van Rossum68bbcd22007-02-27 17:19:33 +00001095
Ezio Melottib3aedd42010-11-20 19:04:17 +00001096 self.assertEqual(b"abcdefg", bufio.read(9000))
Guido van Rossum68bbcd22007-02-27 17:19:33 +00001097
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001098 def test_read_all(self):
1099 rawio = self.MockRawIO((b"abc", b"d", b"efg"))
1100 bufio = self.tp(rawio)
Guido van Rossum68bbcd22007-02-27 17:19:33 +00001101
Ezio Melottib3aedd42010-11-20 19:04:17 +00001102 self.assertEqual(b"abcdefg", bufio.read())
Guido van Rossum68bbcd22007-02-27 17:19:33 +00001103
Victor Stinner45df8202010-04-28 22:31:17 +00001104 @unittest.skipUnless(threading, 'Threading required for this test.')
Antoine Pitrou5bc4fa72010-10-14 15:34:31 +00001105 @support.requires_resource('cpu')
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001106 def test_threads(self):
Antoine Pitrou87695762008-08-14 22:44:29 +00001107 try:
1108 # Write out many bytes with exactly the same number of 0's,
1109 # 1's... 255's. This will help us check that concurrent reading
1110 # doesn't duplicate or forget contents.
1111 N = 1000
1112 l = list(range(256)) * N
1113 random.shuffle(l)
1114 s = bytes(bytearray(l))
Hirokazu Yamamotoc7d6aa42009-06-18 00:07:14 +00001115 with self.open(support.TESTFN, "wb") as f:
Antoine Pitrou87695762008-08-14 22:44:29 +00001116 f.write(s)
Hirokazu Yamamotoc7d6aa42009-06-18 00:07:14 +00001117 with self.open(support.TESTFN, self.read_mode, buffering=0) as raw:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001118 bufio = self.tp(raw, 8)
Antoine Pitrou87695762008-08-14 22:44:29 +00001119 errors = []
1120 results = []
1121 def f():
1122 try:
1123 # Intra-buffer read then buffer-flushing read
1124 for n in cycle([1, 19]):
1125 s = bufio.read(n)
1126 if not s:
1127 break
1128 # list.append() is atomic
1129 results.append(s)
1130 except Exception as e:
1131 errors.append(e)
1132 raise
1133 threads = [threading.Thread(target=f) for x in range(20)]
1134 for t in threads:
1135 t.start()
1136 time.sleep(0.02) # yield
1137 for t in threads:
1138 t.join()
1139 self.assertFalse(errors,
1140 "the following exceptions were caught: %r" % errors)
1141 s = b''.join(results)
1142 for i in range(256):
1143 c = bytes(bytearray([i]))
1144 self.assertEqual(s.count(c), N)
1145 finally:
1146 support.unlink(support.TESTFN)
1147
Antoine Pitrou1e44fec2011-10-04 12:26:20 +02001148 def test_unseekable(self):
1149 bufio = self.tp(self.MockUnseekableIO(b"A" * 10))
1150 self.assertRaises(self.UnsupportedOperation, bufio.tell)
1151 self.assertRaises(self.UnsupportedOperation, bufio.seek, 0)
1152 bufio.read(1)
1153 self.assertRaises(self.UnsupportedOperation, bufio.seek, 0)
1154 self.assertRaises(self.UnsupportedOperation, bufio.tell)
1155
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001156 def test_misbehaved_io(self):
1157 rawio = self.MisbehavedRawIO((b"abc", b"d", b"efg"))
1158 bufio = self.tp(rawio)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001159 self.assertRaises(OSError, bufio.seek, 0)
1160 self.assertRaises(OSError, bufio.tell)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001161
Antoine Pitrou32cfede2010-08-11 13:31:33 +00001162 def test_no_extraneous_read(self):
1163 # Issue #9550; when the raw IO object has satisfied the read request,
1164 # we should not issue any additional reads, otherwise it may block
1165 # (e.g. socket).
1166 bufsize = 16
1167 for n in (2, bufsize - 1, bufsize, bufsize + 1, bufsize * 2):
1168 rawio = self.MockRawIO([b"x" * n])
1169 bufio = self.tp(rawio, bufsize)
1170 self.assertEqual(bufio.read(n), b"x" * n)
1171 # Simple case: one raw read is enough to satisfy the request.
1172 self.assertEqual(rawio._extraneous_reads, 0,
1173 "failed for {}: {} != 0".format(n, rawio._extraneous_reads))
1174 # A more complex case where two raw reads are needed to satisfy
1175 # the request.
1176 rawio = self.MockRawIO([b"x" * (n - 1), b"x"])
1177 bufio = self.tp(rawio, bufsize)
1178 self.assertEqual(bufio.read(n), b"x" * n)
1179 self.assertEqual(rawio._extraneous_reads, 0,
1180 "failed for {}: {} != 0".format(n, rawio._extraneous_reads))
1181
1182
Antoine Pitrou10f0c502012-07-29 19:02:46 +02001183class CBufferedReaderTest(BufferedReaderTest, SizeofTest):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001184 tp = io.BufferedReader
1185
1186 def test_constructor(self):
1187 BufferedReaderTest.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([b"abc"])
1198 bufio = self.tp(rawio)
1199 self.assertRaises(ValueError, bufio.__init__, rawio, buffer_size=0)
1200 self.assertRaises(ValueError, bufio.read)
1201 self.assertRaises(ValueError, bufio.__init__, rawio, buffer_size=-16)
1202 self.assertRaises(ValueError, bufio.read)
1203 self.assertRaises(ValueError, bufio.__init__, rawio, buffer_size=-1)
1204 self.assertRaises(ValueError, bufio.read)
1205
1206 def test_misbehaved_io_read(self):
1207 rawio = self.MisbehavedRawIO((b"abc", b"d", b"efg"))
1208 bufio = self.tp(rawio)
1209 # _pyio.BufferedReader seems to implement reading different, so that
1210 # checking this is not so easy.
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001211 self.assertRaises(OSError, bufio.read, 10)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001212
1213 def test_garbage_collection(self):
1214 # C BufferedReader objects are collected.
1215 # The Python version has __del__, so it ends into gc.garbage instead
Antoine Pitrou796564c2013-07-30 19:59:21 +02001216 with support.check_warnings(('', ResourceWarning)):
1217 rawio = self.FileIO(support.TESTFN, "w+b")
1218 f = self.tp(rawio)
1219 f.f = f
1220 wr = weakref.ref(f)
1221 del f
1222 support.gc_collect()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001223 self.assertTrue(wr() is None, wr)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001224
R David Murray67bfe802013-02-23 21:51:05 -05001225 def test_args_error(self):
1226 # Issue #17275
1227 with self.assertRaisesRegex(TypeError, "BufferedReader"):
1228 self.tp(io.BytesIO(), 1024, 1024, 1024)
1229
1230
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001231class PyBufferedReaderTest(BufferedReaderTest):
1232 tp = pyio.BufferedReader
Antoine Pitrou87695762008-08-14 22:44:29 +00001233
Guido van Rossuma9e20242007-03-08 00:43:48 +00001234
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001235class BufferedWriterTest(unittest.TestCase, CommonBufferedTests):
1236 write_mode = "wb"
Guido van Rossuma9e20242007-03-08 00:43:48 +00001237
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001238 def test_constructor(self):
1239 rawio = self.MockRawIO()
1240 bufio = self.tp(rawio)
1241 bufio.__init__(rawio)
1242 bufio.__init__(rawio, buffer_size=1024)
1243 bufio.__init__(rawio, buffer_size=16)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001244 self.assertEqual(3, bufio.write(b"abc"))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001245 bufio.flush()
1246 self.assertRaises(ValueError, bufio.__init__, rawio, buffer_size=0)
1247 self.assertRaises(ValueError, bufio.__init__, rawio, buffer_size=-16)
1248 self.assertRaises(ValueError, bufio.__init__, rawio, buffer_size=-1)
1249 bufio.__init__(rawio)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001250 self.assertEqual(3, bufio.write(b"ghi"))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001251 bufio.flush()
Ezio Melottib3aedd42010-11-20 19:04:17 +00001252 self.assertEqual(b"".join(rawio._write_stack), b"abcghi")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001253
Serhiy Storchaka61e24932014-02-12 10:52:35 +02001254 def test_uninitialized(self):
1255 bufio = self.tp.__new__(self.tp)
1256 del bufio
1257 bufio = self.tp.__new__(self.tp)
1258 self.assertRaisesRegex((ValueError, AttributeError),
1259 'uninitialized|has no attribute',
1260 bufio.write, b'')
1261 bufio.__init__(self.MockRawIO())
1262 self.assertEqual(bufio.write(b''), 0)
1263
Benjamin Petersond2e0c792009-05-01 20:40:59 +00001264 def test_detach_flush(self):
1265 raw = self.MockRawIO()
1266 buf = self.tp(raw)
1267 buf.write(b"howdy!")
1268 self.assertFalse(raw._write_stack)
1269 buf.detach()
1270 self.assertEqual(raw._write_stack, [b"howdy!"])
1271
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001272 def test_write(self):
Guido van Rossum68bbcd22007-02-27 17:19:33 +00001273 # Write to the buffered IO but don't overflow the buffer.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001274 writer = self.MockRawIO()
1275 bufio = self.tp(writer, 8)
Guido van Rossum76c5d4d2007-04-06 19:10:29 +00001276 bufio.write(b"abc")
Guido van Rossum76c5d4d2007-04-06 19:10:29 +00001277 self.assertFalse(writer._write_stack)
Guido van Rossum68bbcd22007-02-27 17:19:33 +00001278
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001279 def test_write_overflow(self):
1280 writer = self.MockRawIO()
1281 bufio = self.tp(writer, 8)
1282 contents = b"abcdefghijklmnop"
1283 for n in range(0, len(contents), 3):
1284 bufio.write(contents[n:n+3])
1285 flushed = b"".join(writer._write_stack)
1286 # At least (total - 8) bytes were implicitly flushed, perhaps more
1287 # depending on the implementation.
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001288 self.assertTrue(flushed.startswith(contents[:-8]), flushed)
Guido van Rossum68bbcd22007-02-27 17:19:33 +00001289
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001290 def check_writes(self, intermediate_func):
1291 # Lots of writes, test the flushed output is as expected.
1292 contents = bytes(range(256)) * 1000
1293 n = 0
1294 writer = self.MockRawIO()
1295 bufio = self.tp(writer, 13)
1296 # Generator of write sizes: repeat each N 15 times then proceed to N+1
1297 def gen_sizes():
1298 for size in count(1):
1299 for i in range(15):
1300 yield size
1301 sizes = gen_sizes()
1302 while n < len(contents):
1303 size = min(next(sizes), len(contents) - n)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001304 self.assertEqual(bufio.write(contents[n:n+size]), size)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001305 intermediate_func(bufio)
1306 n += size
1307 bufio.flush()
Ezio Melottib3aedd42010-11-20 19:04:17 +00001308 self.assertEqual(contents, b"".join(writer._write_stack))
Guido van Rossum68bbcd22007-02-27 17:19:33 +00001309
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001310 def test_writes(self):
1311 self.check_writes(lambda bufio: None)
Guido van Rossum68bbcd22007-02-27 17:19:33 +00001312
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001313 def test_writes_and_flushes(self):
1314 self.check_writes(lambda bufio: bufio.flush())
Guido van Rossum01a27522007-03-07 01:00:12 +00001315
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001316 def test_writes_and_seeks(self):
1317 def _seekabs(bufio):
1318 pos = bufio.tell()
1319 bufio.seek(pos + 1, 0)
1320 bufio.seek(pos - 1, 0)
1321 bufio.seek(pos, 0)
1322 self.check_writes(_seekabs)
1323 def _seekrel(bufio):
1324 pos = bufio.seek(0, 1)
1325 bufio.seek(+1, 1)
1326 bufio.seek(-1, 1)
1327 bufio.seek(pos, 0)
1328 self.check_writes(_seekrel)
Guido van Rossum01a27522007-03-07 01:00:12 +00001329
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001330 def test_writes_and_truncates(self):
1331 self.check_writes(lambda bufio: bufio.truncate(bufio.tell()))
Guido van Rossum01a27522007-03-07 01:00:12 +00001332
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001333 def test_write_non_blocking(self):
1334 raw = self.MockNonBlockWriterIO()
Benjamin Peterson59406a92009-03-26 17:10:29 +00001335 bufio = self.tp(raw, 8)
Guido van Rossum01a27522007-03-07 01:00:12 +00001336
Ezio Melottib3aedd42010-11-20 19:04:17 +00001337 self.assertEqual(bufio.write(b"abcd"), 4)
1338 self.assertEqual(bufio.write(b"efghi"), 5)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001339 # 1 byte will be written, the rest will be buffered
1340 raw.block_on(b"k")
Ezio Melottib3aedd42010-11-20 19:04:17 +00001341 self.assertEqual(bufio.write(b"jklmn"), 5)
Guido van Rossum01a27522007-03-07 01:00:12 +00001342
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001343 # 8 bytes will be written, 8 will be buffered and the rest will be lost
1344 raw.block_on(b"0")
1345 try:
1346 bufio.write(b"opqrwxyz0123456789")
1347 except self.BlockingIOError as e:
1348 written = e.characters_written
1349 else:
1350 self.fail("BlockingIOError should have been raised")
Ezio Melottib3aedd42010-11-20 19:04:17 +00001351 self.assertEqual(written, 16)
1352 self.assertEqual(raw.pop_written(),
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001353 b"abcdefghijklmnopqrwxyz")
Guido van Rossum01a27522007-03-07 01:00:12 +00001354
Ezio Melottib3aedd42010-11-20 19:04:17 +00001355 self.assertEqual(bufio.write(b"ABCDEFGHI"), 9)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001356 s = raw.pop_written()
1357 # Previously buffered bytes were flushed
1358 self.assertTrue(s.startswith(b"01234567A"), s)
Guido van Rossum01a27522007-03-07 01:00:12 +00001359
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001360 def test_write_and_rewind(self):
1361 raw = io.BytesIO()
1362 bufio = self.tp(raw, 4)
1363 self.assertEqual(bufio.write(b"abcdef"), 6)
1364 self.assertEqual(bufio.tell(), 6)
1365 bufio.seek(0, 0)
1366 self.assertEqual(bufio.write(b"XY"), 2)
1367 bufio.seek(6, 0)
1368 self.assertEqual(raw.getvalue(), b"XYcdef")
1369 self.assertEqual(bufio.write(b"123456"), 6)
1370 bufio.flush()
1371 self.assertEqual(raw.getvalue(), b"XYcdef123456")
Guido van Rossum68bbcd22007-02-27 17:19:33 +00001372
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001373 def test_flush(self):
1374 writer = self.MockRawIO()
1375 bufio = self.tp(writer, 8)
Guido van Rossum76c5d4d2007-04-06 19:10:29 +00001376 bufio.write(b"abc")
1377 bufio.flush()
Ezio Melottib3aedd42010-11-20 19:04:17 +00001378 self.assertEqual(b"abc", writer._write_stack[0])
Guido van Rossum68bbcd22007-02-27 17:19:33 +00001379
Antoine Pitrou131a4892012-10-16 22:57:11 +02001380 def test_writelines(self):
1381 l = [b'ab', b'cd', b'ef']
1382 writer = self.MockRawIO()
1383 bufio = self.tp(writer, 8)
1384 bufio.writelines(l)
1385 bufio.flush()
1386 self.assertEqual(b''.join(writer._write_stack), b'abcdef')
1387
1388 def test_writelines_userlist(self):
1389 l = UserList([b'ab', b'cd', b'ef'])
1390 writer = self.MockRawIO()
1391 bufio = self.tp(writer, 8)
1392 bufio.writelines(l)
1393 bufio.flush()
1394 self.assertEqual(b''.join(writer._write_stack), b'abcdef')
1395
1396 def test_writelines_error(self):
1397 writer = self.MockRawIO()
1398 bufio = self.tp(writer, 8)
1399 self.assertRaises(TypeError, bufio.writelines, [1, 2, 3])
1400 self.assertRaises(TypeError, bufio.writelines, None)
1401 self.assertRaises(TypeError, bufio.writelines, 'abc')
1402
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001403 def test_destructor(self):
1404 writer = self.MockRawIO()
1405 bufio = self.tp(writer, 8)
1406 bufio.write(b"abc")
1407 del bufio
Benjamin Peterson24fb1d02009-04-24 23:26:21 +00001408 support.gc_collect()
Ezio Melottib3aedd42010-11-20 19:04:17 +00001409 self.assertEqual(b"abc", writer._write_stack[0])
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001410
1411 def test_truncate(self):
1412 # Truncate implicitly flushes the buffer.
Hirokazu Yamamotoc7d6aa42009-06-18 00:07:14 +00001413 with self.open(support.TESTFN, self.write_mode, buffering=0) as raw:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001414 bufio = self.tp(raw, 8)
1415 bufio.write(b"abcdef")
1416 self.assertEqual(bufio.truncate(3), 3)
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001417 self.assertEqual(bufio.tell(), 6)
Hirokazu Yamamotoc7d6aa42009-06-18 00:07:14 +00001418 with self.open(support.TESTFN, "rb", buffering=0) as f:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001419 self.assertEqual(f.read(), b"abc")
1420
Victor Stinner45df8202010-04-28 22:31:17 +00001421 @unittest.skipUnless(threading, 'Threading required for this test.')
Antoine Pitrou5bc4fa72010-10-14 15:34:31 +00001422 @support.requires_resource('cpu')
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001423 def test_threads(self):
Antoine Pitrou87695762008-08-14 22:44:29 +00001424 try:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001425 # Write out many bytes from many threads and test they were
1426 # all flushed.
1427 N = 1000
1428 contents = bytes(range(256)) * N
1429 sizes = cycle([1, 19])
1430 n = 0
1431 queue = deque()
1432 while n < len(contents):
1433 size = next(sizes)
1434 queue.append(contents[n:n+size])
1435 n += size
1436 del contents
Antoine Pitrou87695762008-08-14 22:44:29 +00001437 # We use a real file object because it allows us to
1438 # exercise situations where the GIL is released before
1439 # writing the buffer to the raw streams. This is in addition
1440 # to concurrency issues due to switching threads in the middle
1441 # of Python code.
Hirokazu Yamamotoc7d6aa42009-06-18 00:07:14 +00001442 with self.open(support.TESTFN, self.write_mode, buffering=0) as raw:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001443 bufio = self.tp(raw, 8)
Antoine Pitrou87695762008-08-14 22:44:29 +00001444 errors = []
1445 def f():
1446 try:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001447 while True:
1448 try:
1449 s = queue.popleft()
1450 except IndexError:
1451 return
Antoine Pitrou87695762008-08-14 22:44:29 +00001452 bufio.write(s)
1453 except Exception as e:
1454 errors.append(e)
1455 raise
1456 threads = [threading.Thread(target=f) for x in range(20)]
1457 for t in threads:
1458 t.start()
1459 time.sleep(0.02) # yield
1460 for t in threads:
1461 t.join()
1462 self.assertFalse(errors,
1463 "the following exceptions were caught: %r" % errors)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001464 bufio.close()
Hirokazu Yamamotoc7d6aa42009-06-18 00:07:14 +00001465 with self.open(support.TESTFN, "rb") as f:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001466 s = f.read()
1467 for i in range(256):
Ezio Melottib3aedd42010-11-20 19:04:17 +00001468 self.assertEqual(s.count(bytes([i])), N)
Antoine Pitrou87695762008-08-14 22:44:29 +00001469 finally:
1470 support.unlink(support.TESTFN)
1471
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001472 def test_misbehaved_io(self):
1473 rawio = self.MisbehavedRawIO()
1474 bufio = self.tp(rawio, 5)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001475 self.assertRaises(OSError, bufio.seek, 0)
1476 self.assertRaises(OSError, bufio.tell)
1477 self.assertRaises(OSError, bufio.write, b"abcdef")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001478
Florent Xicluna109d5732012-07-07 17:03:22 +02001479 def test_max_buffer_size_removal(self):
1480 with self.assertRaises(TypeError):
Benjamin Peterson59406a92009-03-26 17:10:29 +00001481 self.tp(self.MockRawIO(), 8, 12)
Benjamin Peterson59406a92009-03-26 17:10:29 +00001482
Benjamin Peterson68623612012-12-20 11:53:11 -06001483 def test_write_error_on_close(self):
1484 raw = self.MockRawIO()
1485 def bad_write(b):
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001486 raise OSError()
Benjamin Peterson68623612012-12-20 11:53:11 -06001487 raw.write = bad_write
1488 b = self.tp(raw)
1489 b.write(b'spam')
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001490 self.assertRaises(OSError, b.close) # exception not swallowed
Benjamin Peterson68623612012-12-20 11:53:11 -06001491 self.assertTrue(b.closed)
1492
Benjamin Peterson59406a92009-03-26 17:10:29 +00001493
Antoine Pitrou10f0c502012-07-29 19:02:46 +02001494class CBufferedWriterTest(BufferedWriterTest, SizeofTest):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001495 tp = io.BufferedWriter
1496
1497 def test_constructor(self):
1498 BufferedWriterTest.test_constructor(self)
1499 # The allocation can succeed on 32-bit builds, e.g. with more
1500 # than 2GB RAM and a 64-bit kernel.
1501 if sys.maxsize > 0x7FFFFFFF:
1502 rawio = self.MockRawIO()
1503 bufio = self.tp(rawio)
1504 self.assertRaises((OverflowError, MemoryError, ValueError),
1505 bufio.__init__, rawio, sys.maxsize)
1506
1507 def test_initialization(self):
1508 rawio = self.MockRawIO()
1509 bufio = self.tp(rawio)
1510 self.assertRaises(ValueError, bufio.__init__, rawio, buffer_size=0)
1511 self.assertRaises(ValueError, bufio.write, b"def")
1512 self.assertRaises(ValueError, bufio.__init__, rawio, buffer_size=-16)
1513 self.assertRaises(ValueError, bufio.write, b"def")
1514 self.assertRaises(ValueError, bufio.__init__, rawio, buffer_size=-1)
1515 self.assertRaises(ValueError, bufio.write, b"def")
1516
1517 def test_garbage_collection(self):
1518 # C BufferedWriter objects are collected, and collecting them flushes
1519 # all data to disk.
1520 # The Python version has __del__, so it ends into gc.garbage instead
Antoine Pitrou796564c2013-07-30 19:59:21 +02001521 with support.check_warnings(('', ResourceWarning)):
1522 rawio = self.FileIO(support.TESTFN, "w+b")
1523 f = self.tp(rawio)
1524 f.write(b"123xxx")
1525 f.x = f
1526 wr = weakref.ref(f)
1527 del f
1528 support.gc_collect()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001529 self.assertTrue(wr() is None, wr)
Hirokazu Yamamotoc7d6aa42009-06-18 00:07:14 +00001530 with self.open(support.TESTFN, "rb") as f:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001531 self.assertEqual(f.read(), b"123xxx")
1532
R David Murray67bfe802013-02-23 21:51:05 -05001533 def test_args_error(self):
1534 # Issue #17275
1535 with self.assertRaisesRegex(TypeError, "BufferedWriter"):
1536 self.tp(io.BytesIO(), 1024, 1024, 1024)
1537
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001538
1539class PyBufferedWriterTest(BufferedWriterTest):
1540 tp = pyio.BufferedWriter
Guido van Rossuma9e20242007-03-08 00:43:48 +00001541
Guido van Rossum01a27522007-03-07 01:00:12 +00001542class BufferedRWPairTest(unittest.TestCase):
Guido van Rossuma9e20242007-03-08 00:43:48 +00001543
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001544 def test_constructor(self):
1545 pair = self.tp(self.MockRawIO(), self.MockRawIO())
Benjamin Peterson92035012008-12-27 16:00:54 +00001546 self.assertFalse(pair.closed)
Guido van Rossum01a27522007-03-07 01:00:12 +00001547
Serhiy Storchaka61e24932014-02-12 10:52:35 +02001548 def test_uninitialized(self):
1549 pair = self.tp.__new__(self.tp)
1550 del pair
1551 pair = self.tp.__new__(self.tp)
1552 self.assertRaisesRegex((ValueError, AttributeError),
1553 'uninitialized|has no attribute',
1554 pair.read, 0)
1555 self.assertRaisesRegex((ValueError, AttributeError),
1556 'uninitialized|has no attribute',
1557 pair.write, b'')
1558 pair.__init__(self.MockRawIO(), self.MockRawIO())
1559 self.assertEqual(pair.read(0), b'')
1560 self.assertEqual(pair.write(b''), 0)
1561
Benjamin Petersond2e0c792009-05-01 20:40:59 +00001562 def test_detach(self):
1563 pair = self.tp(self.MockRawIO(), self.MockRawIO())
1564 self.assertRaises(self.UnsupportedOperation, pair.detach)
1565
Florent Xicluna109d5732012-07-07 17:03:22 +02001566 def test_constructor_max_buffer_size_removal(self):
1567 with self.assertRaises(TypeError):
Benjamin Peterson59406a92009-03-26 17:10:29 +00001568 self.tp(self.MockRawIO(), self.MockRawIO(), 8, 12)
Benjamin Peterson59406a92009-03-26 17:10:29 +00001569
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001570 def test_constructor_with_not_readable(self):
1571 class NotReadable(MockRawIO):
1572 def readable(self):
1573 return False
1574
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001575 self.assertRaises(OSError, self.tp, NotReadable(), self.MockRawIO())
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001576
1577 def test_constructor_with_not_writeable(self):
1578 class NotWriteable(MockRawIO):
1579 def writable(self):
1580 return False
1581
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001582 self.assertRaises(OSError, self.tp, self.MockRawIO(), NotWriteable())
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001583
1584 def test_read(self):
1585 pair = self.tp(self.BytesIO(b"abcdef"), self.MockRawIO())
1586
1587 self.assertEqual(pair.read(3), b"abc")
1588 self.assertEqual(pair.read(1), b"d")
1589 self.assertEqual(pair.read(), b"ef")
Benjamin Petersonbf5ff762009-12-13 19:25:34 +00001590 pair = self.tp(self.BytesIO(b"abc"), self.MockRawIO())
1591 self.assertEqual(pair.read(None), b"abc")
1592
1593 def test_readlines(self):
1594 pair = lambda: self.tp(self.BytesIO(b"abc\ndef\nh"), self.MockRawIO())
1595 self.assertEqual(pair().readlines(), [b"abc\n", b"def\n", b"h"])
1596 self.assertEqual(pair().readlines(), [b"abc\n", b"def\n", b"h"])
1597 self.assertEqual(pair().readlines(5), [b"abc\n", b"def\n"])
Antoine Pitroucf4c7492009-04-19 00:09:36 +00001598
1599 def test_read1(self):
1600 # .read1() is delegated to the underlying reader object, so this test
1601 # can be shallow.
1602 pair = self.tp(self.BytesIO(b"abcdef"), self.MockRawIO())
1603
1604 self.assertEqual(pair.read1(3), b"abc")
1605
1606 def test_readinto(self):
1607 pair = self.tp(self.BytesIO(b"abcdef"), self.MockRawIO())
1608
1609 data = bytearray(5)
1610 self.assertEqual(pair.readinto(data), 5)
1611 self.assertEqual(data, b"abcde")
1612
1613 def test_write(self):
1614 w = self.MockRawIO()
1615 pair = self.tp(self.MockRawIO(), w)
1616
1617 pair.write(b"abc")
1618 pair.flush()
1619 pair.write(b"def")
1620 pair.flush()
1621 self.assertEqual(w._write_stack, [b"abc", b"def"])
1622
1623 def test_peek(self):
1624 pair = self.tp(self.BytesIO(b"abcdef"), self.MockRawIO())
1625
1626 self.assertTrue(pair.peek(3).startswith(b"abc"))
1627 self.assertEqual(pair.read(3), b"abc")
1628
1629 def test_readable(self):
1630 pair = self.tp(self.MockRawIO(), self.MockRawIO())
1631 self.assertTrue(pair.readable())
1632
1633 def test_writeable(self):
1634 pair = self.tp(self.MockRawIO(), self.MockRawIO())
1635 self.assertTrue(pair.writable())
1636
1637 def test_seekable(self):
1638 # BufferedRWPairs are never seekable, even if their readers and writers
1639 # are.
1640 pair = self.tp(self.MockRawIO(), self.MockRawIO())
1641 self.assertFalse(pair.seekable())
1642
1643 # .flush() is delegated to the underlying writer object and has been
1644 # tested in the test_write method.
1645
1646 def test_close_and_closed(self):
1647 pair = self.tp(self.MockRawIO(), self.MockRawIO())
1648 self.assertFalse(pair.closed)
1649 pair.close()
1650 self.assertTrue(pair.closed)
1651
1652 def test_isatty(self):
1653 class SelectableIsAtty(MockRawIO):
1654 def __init__(self, isatty):
1655 MockRawIO.__init__(self)
1656 self._isatty = isatty
1657
1658 def isatty(self):
1659 return self._isatty
1660
1661 pair = self.tp(SelectableIsAtty(False), SelectableIsAtty(False))
1662 self.assertFalse(pair.isatty())
1663
1664 pair = self.tp(SelectableIsAtty(True), SelectableIsAtty(False))
1665 self.assertTrue(pair.isatty())
1666
1667 pair = self.tp(SelectableIsAtty(False), SelectableIsAtty(True))
1668 self.assertTrue(pair.isatty())
1669
1670 pair = self.tp(SelectableIsAtty(True), SelectableIsAtty(True))
1671 self.assertTrue(pair.isatty())
Guido van Rossum01a27522007-03-07 01:00:12 +00001672
Benjamin Petersonbbd0a322014-09-29 22:46:57 -04001673 def test_weakref_clearing(self):
1674 brw = self.tp(self.MockRawIO(), self.MockRawIO())
1675 ref = weakref.ref(brw)
1676 brw = None
1677 ref = None # Shouldn't segfault.
1678
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001679class CBufferedRWPairTest(BufferedRWPairTest):
1680 tp = io.BufferedRWPair
Guido van Rossuma9e20242007-03-08 00:43:48 +00001681
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001682class PyBufferedRWPairTest(BufferedRWPairTest):
1683 tp = pyio.BufferedRWPair
Guido van Rossuma9e20242007-03-08 00:43:48 +00001684
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001685
1686class BufferedRandomTest(BufferedReaderTest, BufferedWriterTest):
1687 read_mode = "rb+"
1688 write_mode = "wb+"
1689
1690 def test_constructor(self):
1691 BufferedReaderTest.test_constructor(self)
1692 BufferedWriterTest.test_constructor(self)
1693
Serhiy Storchaka61e24932014-02-12 10:52:35 +02001694 def test_uninitialized(self):
1695 BufferedReaderTest.test_uninitialized(self)
1696 BufferedWriterTest.test_uninitialized(self)
1697
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001698 def test_read_and_write(self):
1699 raw = self.MockRawIO((b"asdf", b"ghjk"))
Benjamin Peterson59406a92009-03-26 17:10:29 +00001700 rw = self.tp(raw, 8)
Guido van Rossum01a27522007-03-07 01:00:12 +00001701
1702 self.assertEqual(b"as", rw.read(2))
1703 rw.write(b"ddd")
1704 rw.write(b"eee")
Guido van Rossum76c5d4d2007-04-06 19:10:29 +00001705 self.assertFalse(raw._write_stack) # Buffer writes
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001706 self.assertEqual(b"ghjk", rw.read())
Ezio Melottib3aedd42010-11-20 19:04:17 +00001707 self.assertEqual(b"dddeee", raw._write_stack[0])
Guido van Rossum01a27522007-03-07 01:00:12 +00001708
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001709 def test_seek_and_tell(self):
1710 raw = self.BytesIO(b"asdfghjkl")
1711 rw = self.tp(raw)
Guido van Rossum01a27522007-03-07 01:00:12 +00001712
Ezio Melottib3aedd42010-11-20 19:04:17 +00001713 self.assertEqual(b"as", rw.read(2))
1714 self.assertEqual(2, rw.tell())
Guido van Rossum01a27522007-03-07 01:00:12 +00001715 rw.seek(0, 0)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001716 self.assertEqual(b"asdf", rw.read(4))
Guido van Rossum01a27522007-03-07 01:00:12 +00001717
Antoine Pitroue05565e2011-08-20 14:39:23 +02001718 rw.write(b"123f")
Guido van Rossum01a27522007-03-07 01:00:12 +00001719 rw.seek(0, 0)
Antoine Pitroue05565e2011-08-20 14:39:23 +02001720 self.assertEqual(b"asdf123fl", rw.read())
Ezio Melottib3aedd42010-11-20 19:04:17 +00001721 self.assertEqual(9, rw.tell())
Guido van Rossum01a27522007-03-07 01:00:12 +00001722 rw.seek(-4, 2)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001723 self.assertEqual(5, rw.tell())
Guido van Rossum01a27522007-03-07 01:00:12 +00001724 rw.seek(2, 1)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001725 self.assertEqual(7, rw.tell())
1726 self.assertEqual(b"fl", rw.read(11))
Antoine Pitroue05565e2011-08-20 14:39:23 +02001727 rw.flush()
1728 self.assertEqual(b"asdf123fl", raw.getvalue())
1729
Christian Heimes8e42a0a2007-11-08 18:04:45 +00001730 self.assertRaises(TypeError, rw.seek, 0.0)
Guido van Rossum01a27522007-03-07 01:00:12 +00001731
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001732 def check_flush_and_read(self, read_func):
1733 raw = self.BytesIO(b"abcdefghi")
1734 bufio = self.tp(raw)
1735
Ezio Melottib3aedd42010-11-20 19:04:17 +00001736 self.assertEqual(b"ab", read_func(bufio, 2))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001737 bufio.write(b"12")
Ezio Melottib3aedd42010-11-20 19:04:17 +00001738 self.assertEqual(b"ef", read_func(bufio, 2))
1739 self.assertEqual(6, bufio.tell())
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001740 bufio.flush()
Ezio Melottib3aedd42010-11-20 19:04:17 +00001741 self.assertEqual(6, bufio.tell())
1742 self.assertEqual(b"ghi", read_func(bufio))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001743 raw.seek(0, 0)
1744 raw.write(b"XYZ")
1745 # flush() resets the read buffer
1746 bufio.flush()
1747 bufio.seek(0, 0)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001748 self.assertEqual(b"XYZ", read_func(bufio, 3))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001749
1750 def test_flush_and_read(self):
1751 self.check_flush_and_read(lambda bufio, *args: bufio.read(*args))
1752
1753 def test_flush_and_readinto(self):
1754 def _readinto(bufio, n=-1):
1755 b = bytearray(n if n >= 0 else 9999)
1756 n = bufio.readinto(b)
1757 return bytes(b[:n])
1758 self.check_flush_and_read(_readinto)
1759
1760 def test_flush_and_peek(self):
1761 def _peek(bufio, n=-1):
1762 # This relies on the fact that the buffer can contain the whole
1763 # raw stream, otherwise peek() can return less.
1764 b = bufio.peek(n)
1765 if n != -1:
1766 b = b[:n]
1767 bufio.seek(len(b), 1)
1768 return b
1769 self.check_flush_and_read(_peek)
1770
1771 def test_flush_and_write(self):
1772 raw = self.BytesIO(b"abcdefghi")
1773 bufio = self.tp(raw)
1774
1775 bufio.write(b"123")
1776 bufio.flush()
1777 bufio.write(b"45")
1778 bufio.flush()
1779 bufio.seek(0, 0)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001780 self.assertEqual(b"12345fghi", raw.getvalue())
1781 self.assertEqual(b"12345fghi", bufio.read())
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001782
1783 def test_threads(self):
1784 BufferedReaderTest.test_threads(self)
1785 BufferedWriterTest.test_threads(self)
1786
1787 def test_writes_and_peek(self):
1788 def _peek(bufio):
1789 bufio.peek(1)
1790 self.check_writes(_peek)
1791 def _peek(bufio):
1792 pos = bufio.tell()
1793 bufio.seek(-1, 1)
1794 bufio.peek(1)
1795 bufio.seek(pos, 0)
1796 self.check_writes(_peek)
1797
1798 def test_writes_and_reads(self):
1799 def _read(bufio):
1800 bufio.seek(-1, 1)
1801 bufio.read(1)
1802 self.check_writes(_read)
1803
1804 def test_writes_and_read1s(self):
1805 def _read1(bufio):
1806 bufio.seek(-1, 1)
1807 bufio.read1(1)
1808 self.check_writes(_read1)
1809
1810 def test_writes_and_readintos(self):
1811 def _read(bufio):
1812 bufio.seek(-1, 1)
1813 bufio.readinto(bytearray(1))
1814 self.check_writes(_read)
1815
Antoine Pitroua0ceb732009-08-06 20:29:56 +00001816 def test_write_after_readahead(self):
1817 # Issue #6629: writing after the buffer was filled by readahead should
1818 # first rewind the raw stream.
1819 for overwrite_size in [1, 5]:
1820 raw = self.BytesIO(b"A" * 10)
1821 bufio = self.tp(raw, 4)
1822 # Trigger readahead
1823 self.assertEqual(bufio.read(1), b"A")
1824 self.assertEqual(bufio.tell(), 1)
1825 # Overwriting should rewind the raw stream if it needs so
1826 bufio.write(b"B" * overwrite_size)
1827 self.assertEqual(bufio.tell(), overwrite_size + 1)
1828 # If the write size was smaller than the buffer size, flush() and
1829 # check that rewind happens.
1830 bufio.flush()
1831 self.assertEqual(bufio.tell(), overwrite_size + 1)
1832 s = raw.getvalue()
1833 self.assertEqual(s,
1834 b"A" + b"B" * overwrite_size + b"A" * (9 - overwrite_size))
1835
Antoine Pitrou7c404892011-05-13 00:13:33 +02001836 def test_write_rewind_write(self):
1837 # Various combinations of reading / writing / seeking backwards / writing again
1838 def mutate(bufio, pos1, pos2):
1839 assert pos2 >= pos1
1840 # Fill the buffer
1841 bufio.seek(pos1)
1842 bufio.read(pos2 - pos1)
1843 bufio.write(b'\x02')
1844 # This writes earlier than the previous write, but still inside
1845 # the buffer.
1846 bufio.seek(pos1)
1847 bufio.write(b'\x01')
1848
1849 b = b"\x80\x81\x82\x83\x84"
1850 for i in range(0, len(b)):
1851 for j in range(i, len(b)):
1852 raw = self.BytesIO(b)
1853 bufio = self.tp(raw, 100)
1854 mutate(bufio, i, j)
1855 bufio.flush()
1856 expected = bytearray(b)
1857 expected[j] = 2
1858 expected[i] = 1
1859 self.assertEqual(raw.getvalue(), expected,
1860 "failed result for i=%d, j=%d" % (i, j))
1861
Antoine Pitrou905a2ff2010-01-31 22:47:27 +00001862 def test_truncate_after_read_or_write(self):
1863 raw = self.BytesIO(b"A" * 10)
1864 bufio = self.tp(raw, 100)
1865 self.assertEqual(bufio.read(2), b"AA") # the read buffer gets filled
1866 self.assertEqual(bufio.truncate(), 2)
1867 self.assertEqual(bufio.write(b"BB"), 2) # the write buffer increases
1868 self.assertEqual(bufio.truncate(), 4)
1869
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001870 def test_misbehaved_io(self):
1871 BufferedReaderTest.test_misbehaved_io(self)
1872 BufferedWriterTest.test_misbehaved_io(self)
1873
Antoine Pitroue05565e2011-08-20 14:39:23 +02001874 def test_interleaved_read_write(self):
1875 # Test for issue #12213
1876 with self.BytesIO(b'abcdefgh') as raw:
1877 with self.tp(raw, 100) as f:
1878 f.write(b"1")
1879 self.assertEqual(f.read(1), b'b')
1880 f.write(b'2')
1881 self.assertEqual(f.read1(1), b'd')
1882 f.write(b'3')
1883 buf = bytearray(1)
1884 f.readinto(buf)
1885 self.assertEqual(buf, b'f')
1886 f.write(b'4')
1887 self.assertEqual(f.peek(1), b'h')
1888 f.flush()
1889 self.assertEqual(raw.getvalue(), b'1b2d3f4h')
1890
1891 with self.BytesIO(b'abc') as raw:
1892 with self.tp(raw, 100) as f:
1893 self.assertEqual(f.read(1), b'a')
1894 f.write(b"2")
1895 self.assertEqual(f.read(1), b'c')
1896 f.flush()
1897 self.assertEqual(raw.getvalue(), b'a2c')
1898
1899 def test_interleaved_readline_write(self):
1900 with self.BytesIO(b'ab\ncdef\ng\n') as raw:
1901 with self.tp(raw) as f:
1902 f.write(b'1')
1903 self.assertEqual(f.readline(), b'b\n')
1904 f.write(b'2')
1905 self.assertEqual(f.readline(), b'def\n')
1906 f.write(b'3')
1907 self.assertEqual(f.readline(), b'\n')
1908 f.flush()
1909 self.assertEqual(raw.getvalue(), b'1b\n2def\n3\n')
1910
Antoine Pitrou0d739d72010-09-05 23:01:12 +00001911 # You can't construct a BufferedRandom over a non-seekable stream.
1912 test_unseekable = None
1913
R David Murray67bfe802013-02-23 21:51:05 -05001914
Antoine Pitrou10f0c502012-07-29 19:02:46 +02001915class CBufferedRandomTest(BufferedRandomTest, SizeofTest):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001916 tp = io.BufferedRandom
1917
1918 def test_constructor(self):
1919 BufferedRandomTest.test_constructor(self)
1920 # The allocation can succeed on 32-bit builds, e.g. with more
1921 # than 2GB RAM and a 64-bit kernel.
1922 if sys.maxsize > 0x7FFFFFFF:
1923 rawio = self.MockRawIO()
1924 bufio = self.tp(rawio)
1925 self.assertRaises((OverflowError, MemoryError, ValueError),
1926 bufio.__init__, rawio, sys.maxsize)
1927
1928 def test_garbage_collection(self):
1929 CBufferedReaderTest.test_garbage_collection(self)
1930 CBufferedWriterTest.test_garbage_collection(self)
1931
R David Murray67bfe802013-02-23 21:51:05 -05001932 def test_args_error(self):
1933 # Issue #17275
1934 with self.assertRaisesRegex(TypeError, "BufferedRandom"):
1935 self.tp(io.BytesIO(), 1024, 1024, 1024)
1936
1937
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001938class PyBufferedRandomTest(BufferedRandomTest):
1939 tp = pyio.BufferedRandom
1940
1941
Ka-Ping Yeef44c7e82008-03-18 04:51:32 +00001942# To fully exercise seek/tell, the StatefulIncrementalDecoder has these
1943# properties:
1944# - A single output character can correspond to many bytes of input.
1945# - The number of input bytes to complete the character can be
1946# undetermined until the last input byte is received.
1947# - The number of input bytes can vary depending on previous input.
1948# - A single input byte can correspond to many characters of output.
1949# - The number of output characters can be undetermined until the
1950# last input byte is received.
1951# - The number of output characters can vary depending on previous input.
1952
1953class StatefulIncrementalDecoder(codecs.IncrementalDecoder):
1954 """
1955 For testing seek/tell behavior with a stateful, buffering decoder.
1956
1957 Input is a sequence of words. Words may be fixed-length (length set
1958 by input) or variable-length (period-terminated). In variable-length
1959 mode, extra periods are ignored. Possible words are:
1960 - 'i' followed by a number sets the input length, I (maximum 99).
1961 When I is set to 0, words are space-terminated.
1962 - 'o' followed by a number sets the output length, O (maximum 99).
1963 - Any other word is converted into a word followed by a period on
1964 the output. The output word consists of the input word truncated
1965 or padded out with hyphens to make its length equal to O. If O
1966 is 0, the word is output verbatim without truncating or padding.
1967 I and O are initially set to 1. When I changes, any buffered input is
1968 re-scanned according to the new I. EOF also terminates the last word.
1969 """
1970
1971 def __init__(self, errors='strict'):
Christian Heimesab568872008-03-23 02:11:13 +00001972 codecs.IncrementalDecoder.__init__(self, errors)
Ka-Ping Yeef44c7e82008-03-18 04:51:32 +00001973 self.reset()
1974
1975 def __repr__(self):
1976 return '<SID %x>' % id(self)
1977
1978 def reset(self):
1979 self.i = 1
1980 self.o = 1
1981 self.buffer = bytearray()
1982
1983 def getstate(self):
1984 i, o = self.i ^ 1, self.o ^ 1 # so that flags = 0 after reset()
1985 return bytes(self.buffer), i*100 + o
1986
1987 def setstate(self, state):
1988 buffer, io = state
1989 self.buffer = bytearray(buffer)
1990 i, o = divmod(io, 100)
1991 self.i, self.o = i ^ 1, o ^ 1
1992
1993 def decode(self, input, final=False):
1994 output = ''
1995 for b in input:
1996 if self.i == 0: # variable-length, terminated with period
1997 if b == ord('.'):
1998 if self.buffer:
1999 output += self.process_word()
2000 else:
2001 self.buffer.append(b)
2002 else: # fixed-length, terminate after self.i bytes
2003 self.buffer.append(b)
2004 if len(self.buffer) == self.i:
2005 output += self.process_word()
2006 if final and self.buffer: # EOF terminates the last word
2007 output += self.process_word()
2008 return output
2009
2010 def process_word(self):
2011 output = ''
2012 if self.buffer[0] == ord('i'):
2013 self.i = min(99, int(self.buffer[1:] or 0)) # set input length
2014 elif self.buffer[0] == ord('o'):
2015 self.o = min(99, int(self.buffer[1:] or 0)) # set output length
2016 else:
2017 output = self.buffer.decode('ascii')
2018 if len(output) < self.o:
2019 output += '-'*self.o # pad out with hyphens
2020 if self.o:
2021 output = output[:self.o] # truncate to output length
2022 output += '.'
2023 self.buffer = bytearray()
2024 return output
2025
Benjamin Petersonad9d48d2008-04-02 21:49:44 +00002026 codecEnabled = False
2027
2028 @classmethod
2029 def lookupTestDecoder(cls, name):
2030 if cls.codecEnabled and name == 'test_decoder':
Antoine Pitrou180a3362008-12-14 16:36:46 +00002031 latin1 = codecs.lookup('latin-1')
Benjamin Petersonad9d48d2008-04-02 21:49:44 +00002032 return codecs.CodecInfo(
Antoine Pitrou180a3362008-12-14 16:36:46 +00002033 name='test_decoder', encode=latin1.encode, decode=None,
Benjamin Petersonad9d48d2008-04-02 21:49:44 +00002034 incrementalencoder=None,
2035 streamreader=None, streamwriter=None,
2036 incrementaldecoder=cls)
2037
2038# Register the previous decoder for testing.
2039# Disabled by default, tests will enable it.
2040codecs.register(StatefulIncrementalDecoder.lookupTestDecoder)
2041
2042
Ka-Ping Yeef44c7e82008-03-18 04:51:32 +00002043class StatefulIncrementalDecoderTest(unittest.TestCase):
2044 """
2045 Make sure the StatefulIncrementalDecoder actually works.
2046 """
2047
2048 test_cases = [
Ka-Ping Yeed24a5b62008-03-20 10:51:27 +00002049 # I=1, O=1 (fixed-length input == fixed-length output)
Ka-Ping Yeef44c7e82008-03-18 04:51:32 +00002050 (b'abcd', False, 'a.b.c.d.'),
Ka-Ping Yeed24a5b62008-03-20 10:51:27 +00002051 # I=0, O=0 (variable-length input, variable-length output)
Ka-Ping Yeef44c7e82008-03-18 04:51:32 +00002052 (b'oiabcd', True, 'abcd.'),
Ka-Ping Yeed24a5b62008-03-20 10:51:27 +00002053 # I=0, O=0 (should ignore extra periods)
Ka-Ping Yeef44c7e82008-03-18 04:51:32 +00002054 (b'oi...abcd...', True, 'abcd.'),
Ka-Ping Yeed24a5b62008-03-20 10:51:27 +00002055 # I=0, O=6 (variable-length input, fixed-length output)
2056 (b'i.o6.x.xyz.toolongtofit.', False, 'x-----.xyz---.toolon.'),
2057 # I=2, O=6 (fixed-length input < fixed-length output)
Ka-Ping Yeef44c7e82008-03-18 04:51:32 +00002058 (b'i.i2.o6xyz', True, 'xy----.z-----.'),
Ka-Ping Yeed24a5b62008-03-20 10:51:27 +00002059 # I=6, O=3 (fixed-length input > fixed-length output)
2060 (b'i.o3.i6.abcdefghijklmnop', True, 'abc.ghi.mno.'),
2061 # I=0, then 3; O=29, then 15 (with longer output)
2062 (b'i.o29.a.b.cde.o15.abcdefghijabcdefghij.i3.a.b.c.d.ei00k.l.m', True,
2063 'a----------------------------.' +
2064 'b----------------------------.' +
2065 'cde--------------------------.' +
2066 'abcdefghijabcde.' +
2067 'a.b------------.' +
2068 '.c.------------.' +
2069 'd.e------------.' +
2070 'k--------------.' +
2071 'l--------------.' +
2072 'm--------------.')
Ka-Ping Yeef44c7e82008-03-18 04:51:32 +00002073 ]
2074
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002075 def test_decoder(self):
Ka-Ping Yeef44c7e82008-03-18 04:51:32 +00002076 # Try a few one-shot test cases.
2077 for input, eof, output in self.test_cases:
2078 d = StatefulIncrementalDecoder()
Ezio Melottib3aedd42010-11-20 19:04:17 +00002079 self.assertEqual(d.decode(input, eof), output)
Ka-Ping Yeef44c7e82008-03-18 04:51:32 +00002080
2081 # Also test an unfinished decode, followed by forcing EOF.
2082 d = StatefulIncrementalDecoder()
Ezio Melottib3aedd42010-11-20 19:04:17 +00002083 self.assertEqual(d.decode(b'oiabcd'), '')
2084 self.assertEqual(d.decode(b'', 1), 'abcd.')
Guido van Rossum78892e42007-04-06 17:31:18 +00002085
2086class TextIOWrapperTest(unittest.TestCase):
Guido van Rossum9b76da62007-04-11 01:09:03 +00002087
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +00002088 def setUp(self):
2089 self.testdata = b"AAA\r\nBBB\rCCC\r\nDDD\nEEE\r\n"
2090 self.normalized = b"AAA\nBBB\nCCC\nDDD\nEEE\n".decode("ascii")
Benjamin Petersonee8712c2008-05-20 21:35:26 +00002091 support.unlink(support.TESTFN)
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +00002092
Guido van Rossumd0712812007-04-11 16:32:43 +00002093 def tearDown(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +00002094 support.unlink(support.TESTFN)
Guido van Rossum9b76da62007-04-11 01:09:03 +00002095
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002096 def test_constructor(self):
2097 r = self.BytesIO(b"\xc3\xa9\n\n")
2098 b = self.BufferedReader(r, 1000)
2099 t = self.TextIOWrapper(b)
Marc-André Lemburg8f36af72011-02-25 15:42:01 +00002100 t.__init__(b, encoding="latin-1", newline="\r\n")
2101 self.assertEqual(t.encoding, "latin-1")
Ezio Melottib3aedd42010-11-20 19:04:17 +00002102 self.assertEqual(t.line_buffering, False)
Marc-André Lemburg8f36af72011-02-25 15:42:01 +00002103 t.__init__(b, encoding="utf-8", line_buffering=True)
2104 self.assertEqual(t.encoding, "utf-8")
Ezio Melottib3aedd42010-11-20 19:04:17 +00002105 self.assertEqual(t.line_buffering, True)
2106 self.assertEqual("\xe9\n", t.readline())
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002107 self.assertRaises(TypeError, t.__init__, b, newline=42)
2108 self.assertRaises(ValueError, t.__init__, b, newline='xyzzy')
2109
Nick Coghlana9b15242014-02-04 22:11:18 +10002110 def test_non_text_encoding_codecs_are_rejected(self):
2111 # Ensure the constructor complains if passed a codec that isn't
2112 # marked as a text encoding
2113 # http://bugs.python.org/issue20404
2114 r = self.BytesIO()
2115 b = self.BufferedWriter(r)
2116 with self.assertRaisesRegex(LookupError, "is not a text encoding"):
2117 self.TextIOWrapper(b, encoding="hex")
2118
Benjamin Petersond2e0c792009-05-01 20:40:59 +00002119 def test_detach(self):
2120 r = self.BytesIO()
2121 b = self.BufferedWriter(r)
2122 t = self.TextIOWrapper(b)
2123 self.assertIs(t.detach(), b)
2124
2125 t = self.TextIOWrapper(b, encoding="ascii")
2126 t.write("howdy")
2127 self.assertFalse(r.getvalue())
2128 t.detach()
2129 self.assertEqual(r.getvalue(), b"howdy")
2130 self.assertRaises(ValueError, t.detach)
2131
Benjamin Peterson10e76b62014-12-21 20:51:50 -06002132 # Operations independent of the detached stream should still work
2133 repr(t)
2134 self.assertEqual(t.encoding, "ascii")
2135 self.assertEqual(t.errors, "strict")
2136 self.assertFalse(t.line_buffering)
2137
Benjamin Petersonc4c0eae2009-03-09 00:07:03 +00002138 def test_repr(self):
2139 raw = self.BytesIO("hello".encode("utf-8"))
2140 b = self.BufferedReader(raw)
2141 t = self.TextIOWrapper(b, encoding="utf-8")
Antoine Pitrou716c4442009-05-23 19:04:03 +00002142 modname = self.TextIOWrapper.__module__
2143 self.assertEqual(repr(t),
2144 "<%s.TextIOWrapper encoding='utf-8'>" % modname)
2145 raw.name = "dummy"
2146 self.assertEqual(repr(t),
2147 "<%s.TextIOWrapper name='dummy' encoding='utf-8'>" % modname)
Antoine Pitroua4815ca2011-01-09 20:38:15 +00002148 t.mode = "r"
2149 self.assertEqual(repr(t),
2150 "<%s.TextIOWrapper name='dummy' mode='r' encoding='utf-8'>" % modname)
Antoine Pitrou716c4442009-05-23 19:04:03 +00002151 raw.name = b"dummy"
2152 self.assertEqual(repr(t),
Antoine Pitroua4815ca2011-01-09 20:38:15 +00002153 "<%s.TextIOWrapper name=b'dummy' mode='r' encoding='utf-8'>" % modname)
Benjamin Petersonc4c0eae2009-03-09 00:07:03 +00002154
Benjamin Peterson10e76b62014-12-21 20:51:50 -06002155 t.buffer.detach()
2156 repr(t) # Should not raise an exception
2157
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002158 def test_line_buffering(self):
2159 r = self.BytesIO()
2160 b = self.BufferedWriter(r, 1000)
2161 t = self.TextIOWrapper(b, newline="\n", line_buffering=True)
Guido van Rossumf64db9f2007-12-06 01:04:26 +00002162 t.write("X")
Ezio Melottib3aedd42010-11-20 19:04:17 +00002163 self.assertEqual(r.getvalue(), b"") # No flush happened
Guido van Rossumf64db9f2007-12-06 01:04:26 +00002164 t.write("Y\nZ")
Ezio Melottib3aedd42010-11-20 19:04:17 +00002165 self.assertEqual(r.getvalue(), b"XY\nZ") # All got flushed
Guido van Rossumf64db9f2007-12-06 01:04:26 +00002166 t.write("A\rB")
Ezio Melottib3aedd42010-11-20 19:04:17 +00002167 self.assertEqual(r.getvalue(), b"XY\nZA\rB")
Guido van Rossumf64db9f2007-12-06 01:04:26 +00002168
Victor Stinnerf86a5e82012-06-05 13:43:22 +02002169 def test_default_encoding(self):
2170 old_environ = dict(os.environ)
2171 try:
2172 # try to get a user preferred encoding different than the current
2173 # locale encoding to check that TextIOWrapper() uses the current
2174 # locale encoding and not the user preferred encoding
2175 for key in ('LC_ALL', 'LANG', 'LC_CTYPE'):
2176 if key in os.environ:
2177 del os.environ[key]
2178
2179 current_locale_encoding = locale.getpreferredencoding(False)
2180 b = self.BytesIO()
2181 t = self.TextIOWrapper(b)
2182 self.assertEqual(t.encoding, current_locale_encoding)
2183 finally:
2184 os.environ.clear()
2185 os.environ.update(old_environ)
2186
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +02002187 @support.cpython_only
Serhiy Storchaka78980432013-01-15 01:12:17 +02002188 def test_device_encoding(self):
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +02002189 # Issue 15989
2190 import _testcapi
Serhiy Storchaka78980432013-01-15 01:12:17 +02002191 b = self.BytesIO()
2192 b.fileno = lambda: _testcapi.INT_MAX + 1
2193 self.assertRaises(OverflowError, self.TextIOWrapper, b)
2194 b.fileno = lambda: _testcapi.UINT_MAX + 1
2195 self.assertRaises(OverflowError, self.TextIOWrapper, b)
2196
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002197 def test_encoding(self):
2198 # Check the encoding attribute is always set, and valid
2199 b = self.BytesIO()
Marc-André Lemburg8f36af72011-02-25 15:42:01 +00002200 t = self.TextIOWrapper(b, encoding="utf-8")
2201 self.assertEqual(t.encoding, "utf-8")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002202 t = self.TextIOWrapper(b)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00002203 self.assertTrue(t.encoding is not None)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002204 codecs.lookup(t.encoding)
2205
2206 def test_encoding_errors_reading(self):
Guido van Rossume7fc50f2007-12-03 22:54:21 +00002207 # (1) default
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002208 b = self.BytesIO(b"abc\n\xff\n")
2209 t = self.TextIOWrapper(b, encoding="ascii")
Guido van Rossume7fc50f2007-12-03 22:54:21 +00002210 self.assertRaises(UnicodeError, t.read)
2211 # (2) explicit strict
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002212 b = self.BytesIO(b"abc\n\xff\n")
2213 t = self.TextIOWrapper(b, encoding="ascii", errors="strict")
Guido van Rossume7fc50f2007-12-03 22:54:21 +00002214 self.assertRaises(UnicodeError, t.read)
2215 # (3) ignore
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002216 b = self.BytesIO(b"abc\n\xff\n")
2217 t = self.TextIOWrapper(b, encoding="ascii", errors="ignore")
Ezio Melottib3aedd42010-11-20 19:04:17 +00002218 self.assertEqual(t.read(), "abc\n\n")
Guido van Rossume7fc50f2007-12-03 22:54:21 +00002219 # (4) replace
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002220 b = self.BytesIO(b"abc\n\xff\n")
2221 t = self.TextIOWrapper(b, encoding="ascii", errors="replace")
Ezio Melottib3aedd42010-11-20 19:04:17 +00002222 self.assertEqual(t.read(), "abc\n\ufffd\n")
Guido van Rossume7fc50f2007-12-03 22:54:21 +00002223
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002224 def test_encoding_errors_writing(self):
Guido van Rossume7fc50f2007-12-03 22:54:21 +00002225 # (1) default
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002226 b = self.BytesIO()
2227 t = self.TextIOWrapper(b, encoding="ascii")
Guido van Rossume7fc50f2007-12-03 22:54:21 +00002228 self.assertRaises(UnicodeError, t.write, "\xff")
2229 # (2) explicit strict
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002230 b = self.BytesIO()
2231 t = self.TextIOWrapper(b, encoding="ascii", errors="strict")
Guido van Rossume7fc50f2007-12-03 22:54:21 +00002232 self.assertRaises(UnicodeError, t.write, "\xff")
2233 # (3) ignore
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002234 b = self.BytesIO()
2235 t = self.TextIOWrapper(b, encoding="ascii", errors="ignore",
Guido van Rossumf64db9f2007-12-06 01:04:26 +00002236 newline="\n")
Guido van Rossume7fc50f2007-12-03 22:54:21 +00002237 t.write("abc\xffdef\n")
2238 t.flush()
Ezio Melottib3aedd42010-11-20 19:04:17 +00002239 self.assertEqual(b.getvalue(), b"abcdef\n")
Guido van Rossume7fc50f2007-12-03 22:54:21 +00002240 # (4) replace
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002241 b = self.BytesIO()
2242 t = self.TextIOWrapper(b, encoding="ascii", errors="replace",
Guido van Rossumf64db9f2007-12-06 01:04:26 +00002243 newline="\n")
Guido van Rossume7fc50f2007-12-03 22:54:21 +00002244 t.write("abc\xffdef\n")
2245 t.flush()
Ezio Melottib3aedd42010-11-20 19:04:17 +00002246 self.assertEqual(b.getvalue(), b"abc?def\n")
Guido van Rossume7fc50f2007-12-03 22:54:21 +00002247
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002248 def test_newlines(self):
Guido van Rossum78892e42007-04-06 17:31:18 +00002249 input_lines = [ "unix\n", "windows\r\n", "os9\r", "last\n", "nonl" ]
2250
2251 tests = [
2252 [ None, [ 'unix\n', 'windows\n', 'os9\n', 'last\n', 'nonl' ] ],
Guido van Rossum8358db22007-08-18 21:39:55 +00002253 [ '', input_lines ],
2254 [ '\n', [ "unix\n", "windows\r\n", "os9\rlast\n", "nonl" ] ],
2255 [ '\r\n', [ "unix\nwindows\r\n", "os9\rlast\nnonl" ] ],
2256 [ '\r', [ "unix\nwindows\r", "\nos9\r", "last\nnonl" ] ],
Guido van Rossum78892e42007-04-06 17:31:18 +00002257 ]
Antoine Pitrou180a3362008-12-14 16:36:46 +00002258 encodings = (
2259 'utf-8', 'latin-1',
2260 'utf-16', 'utf-16-le', 'utf-16-be',
2261 'utf-32', 'utf-32-le', 'utf-32-be',
2262 )
Guido van Rossum78892e42007-04-06 17:31:18 +00002263
Guido van Rossum8358db22007-08-18 21:39:55 +00002264 # Try a range of buffer sizes to test the case where \r is the last
Guido van Rossum78892e42007-04-06 17:31:18 +00002265 # character in TextIOWrapper._pending_line.
2266 for encoding in encodings:
Guido van Rossum8358db22007-08-18 21:39:55 +00002267 # XXX: str.encode() should return bytes
2268 data = bytes(''.join(input_lines).encode(encoding))
Guido van Rossum78892e42007-04-06 17:31:18 +00002269 for do_reads in (False, True):
Guido van Rossum8358db22007-08-18 21:39:55 +00002270 for bufsize in range(1, 10):
2271 for newline, exp_lines in tests:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002272 bufio = self.BufferedReader(self.BytesIO(data), bufsize)
2273 textio = self.TextIOWrapper(bufio, newline=newline,
Guido van Rossum78892e42007-04-06 17:31:18 +00002274 encoding=encoding)
2275 if do_reads:
2276 got_lines = []
2277 while True:
Guido van Rossum76c5d4d2007-04-06 19:10:29 +00002278 c2 = textio.read(2)
Guido van Rossum78892e42007-04-06 17:31:18 +00002279 if c2 == '':
2280 break
Ezio Melottib3aedd42010-11-20 19:04:17 +00002281 self.assertEqual(len(c2), 2)
Guido van Rossum76c5d4d2007-04-06 19:10:29 +00002282 got_lines.append(c2 + textio.readline())
Guido van Rossum78892e42007-04-06 17:31:18 +00002283 else:
Guido van Rossum76c5d4d2007-04-06 19:10:29 +00002284 got_lines = list(textio)
Guido van Rossum78892e42007-04-06 17:31:18 +00002285
2286 for got_line, exp_line in zip(got_lines, exp_lines):
Ezio Melottib3aedd42010-11-20 19:04:17 +00002287 self.assertEqual(got_line, exp_line)
2288 self.assertEqual(len(got_lines), len(exp_lines))
Guido van Rossum78892e42007-04-06 17:31:18 +00002289
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002290 def test_newlines_input(self):
2291 testdata = b"AAA\nBB\x00B\nCCC\rDDD\rEEE\r\nFFF\r\nGGG"
Guido van Rossum8358db22007-08-18 21:39:55 +00002292 normalized = testdata.replace(b"\r\n", b"\n").replace(b"\r", b"\n")
2293 for newline, expected in [
Ezio Melottid8b509b2011-09-28 17:37:55 +03002294 (None, normalized.decode("ascii").splitlines(keepends=True)),
2295 ("", testdata.decode("ascii").splitlines(keepends=True)),
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002296 ("\n", ["AAA\n", "BB\x00B\n", "CCC\rDDD\rEEE\r\n", "FFF\r\n", "GGG"]),
2297 ("\r\n", ["AAA\nBB\x00B\nCCC\rDDD\rEEE\r\n", "FFF\r\n", "GGG"]),
2298 ("\r", ["AAA\nBB\x00B\nCCC\r", "DDD\r", "EEE\r", "\nFFF\r", "\nGGG"]),
Guido van Rossum8358db22007-08-18 21:39:55 +00002299 ]:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002300 buf = self.BytesIO(testdata)
2301 txt = self.TextIOWrapper(buf, encoding="ascii", newline=newline)
Ezio Melottib3aedd42010-11-20 19:04:17 +00002302 self.assertEqual(txt.readlines(), expected)
Guido van Rossum8358db22007-08-18 21:39:55 +00002303 txt.seek(0)
Ezio Melottib3aedd42010-11-20 19:04:17 +00002304 self.assertEqual(txt.read(), "".join(expected))
Guido van Rossum8358db22007-08-18 21:39:55 +00002305
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002306 def test_newlines_output(self):
2307 testdict = {
2308 "": b"AAA\nBBB\nCCC\nX\rY\r\nZ",
2309 "\n": b"AAA\nBBB\nCCC\nX\rY\r\nZ",
2310 "\r": b"AAA\rBBB\rCCC\rX\rY\r\rZ",
2311 "\r\n": b"AAA\r\nBBB\r\nCCC\r\nX\rY\r\r\nZ",
2312 }
2313 tests = [(None, testdict[os.linesep])] + sorted(testdict.items())
2314 for newline, expected in tests:
2315 buf = self.BytesIO()
2316 txt = self.TextIOWrapper(buf, encoding="ascii", newline=newline)
2317 txt.write("AAA\nB")
2318 txt.write("BB\nCCC\n")
2319 txt.write("X\rY\r\nZ")
2320 txt.flush()
Ezio Melottib3aedd42010-11-20 19:04:17 +00002321 self.assertEqual(buf.closed, False)
2322 self.assertEqual(buf.getvalue(), expected)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002323
2324 def test_destructor(self):
2325 l = []
2326 base = self.BytesIO
2327 class MyBytesIO(base):
2328 def close(self):
2329 l.append(self.getvalue())
2330 base.close(self)
2331 b = MyBytesIO()
2332 t = self.TextIOWrapper(b, encoding="ascii")
2333 t.write("abc")
2334 del t
Benjamin Peterson24fb1d02009-04-24 23:26:21 +00002335 support.gc_collect()
Ezio Melottib3aedd42010-11-20 19:04:17 +00002336 self.assertEqual([b"abc"], l)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002337
2338 def test_override_destructor(self):
2339 record = []
2340 class MyTextIO(self.TextIOWrapper):
2341 def __del__(self):
2342 record.append(1)
2343 try:
2344 f = super().__del__
2345 except AttributeError:
2346 pass
2347 else:
2348 f()
2349 def close(self):
2350 record.append(2)
2351 super().close()
2352 def flush(self):
2353 record.append(3)
2354 super().flush()
2355 b = self.BytesIO()
2356 t = MyTextIO(b, encoding="ascii")
2357 del t
Benjamin Peterson24fb1d02009-04-24 23:26:21 +00002358 support.gc_collect()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002359 self.assertEqual(record, [1, 2, 3])
2360
2361 def test_error_through_destructor(self):
2362 # Test that the exception state is not modified by a destructor,
2363 # even if close() fails.
2364 rawio = self.CloseFailureIO()
2365 def f():
2366 self.TextIOWrapper(rawio).xyzzy
2367 with support.captured_output("stderr") as s:
2368 self.assertRaises(AttributeError, f)
2369 s = s.getvalue().strip()
2370 if s:
2371 # The destructor *may* have printed an unraisable error, check it
2372 self.assertEqual(len(s.splitlines()), 1)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02002373 self.assertTrue(s.startswith("Exception OSError: "), s)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00002374 self.assertTrue(s.endswith(" ignored"), s)
Guido van Rossum8358db22007-08-18 21:39:55 +00002375
Guido van Rossum9b76da62007-04-11 01:09:03 +00002376 # Systematic tests of the text I/O API
2377
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002378 def test_basic_io(self):
Guido van Rossum9b76da62007-04-11 01:09:03 +00002379 for chunksize in (1, 2, 3, 4, 5, 15, 16, 17, 31, 32, 33, 63, 64, 65):
Marc-André Lemburg8f36af72011-02-25 15:42:01 +00002380 for enc in "ascii", "latin-1", "utf-8" :# , "utf-16-be", "utf-16-le":
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002381 f = self.open(support.TESTFN, "w+", encoding=enc)
Guido van Rossum9b76da62007-04-11 01:09:03 +00002382 f._CHUNK_SIZE = chunksize
Ezio Melottib3aedd42010-11-20 19:04:17 +00002383 self.assertEqual(f.write("abc"), 3)
Guido van Rossum9b76da62007-04-11 01:09:03 +00002384 f.close()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002385 f = self.open(support.TESTFN, "r+", encoding=enc)
Guido van Rossum9b76da62007-04-11 01:09:03 +00002386 f._CHUNK_SIZE = chunksize
Ezio Melottib3aedd42010-11-20 19:04:17 +00002387 self.assertEqual(f.tell(), 0)
2388 self.assertEqual(f.read(), "abc")
Guido van Rossum9b76da62007-04-11 01:09:03 +00002389 cookie = f.tell()
Ezio Melottib3aedd42010-11-20 19:04:17 +00002390 self.assertEqual(f.seek(0), 0)
2391 self.assertEqual(f.read(None), "abc")
Benjamin Petersonbf5ff762009-12-13 19:25:34 +00002392 f.seek(0)
Ezio Melottib3aedd42010-11-20 19:04:17 +00002393 self.assertEqual(f.read(2), "ab")
2394 self.assertEqual(f.read(1), "c")
2395 self.assertEqual(f.read(1), "")
2396 self.assertEqual(f.read(), "")
2397 self.assertEqual(f.tell(), cookie)
2398 self.assertEqual(f.seek(0), 0)
2399 self.assertEqual(f.seek(0, 2), cookie)
2400 self.assertEqual(f.write("def"), 3)
2401 self.assertEqual(f.seek(cookie), cookie)
2402 self.assertEqual(f.read(), "def")
Guido van Rossum9b76da62007-04-11 01:09:03 +00002403 if enc.startswith("utf"):
2404 self.multi_line_test(f, enc)
2405 f.close()
2406
2407 def multi_line_test(self, f, enc):
2408 f.seek(0)
2409 f.truncate()
Guido van Rossumef87d6e2007-05-02 19:09:54 +00002410 sample = "s\xff\u0fff\uffff"
Guido van Rossum9b76da62007-04-11 01:09:03 +00002411 wlines = []
Guido van Rossumcba608c2007-04-11 14:19:59 +00002412 for size in (0, 1, 2, 3, 4, 5, 30, 31, 32, 33, 62, 63, 64, 65, 1000):
Guido van Rossum9b76da62007-04-11 01:09:03 +00002413 chars = []
Guido van Rossum805365e2007-05-07 22:24:25 +00002414 for i in range(size):
Guido van Rossum9b76da62007-04-11 01:09:03 +00002415 chars.append(sample[i % len(sample)])
Guido van Rossumef87d6e2007-05-02 19:09:54 +00002416 line = "".join(chars) + "\n"
Guido van Rossum9b76da62007-04-11 01:09:03 +00002417 wlines.append((f.tell(), line))
2418 f.write(line)
Guido van Rossum9b76da62007-04-11 01:09:03 +00002419 f.seek(0)
2420 rlines = []
2421 while True:
2422 pos = f.tell()
2423 line = f.readline()
2424 if not line:
Guido van Rossum9b76da62007-04-11 01:09:03 +00002425 break
2426 rlines.append((pos, line))
Ezio Melottib3aedd42010-11-20 19:04:17 +00002427 self.assertEqual(rlines, wlines)
Guido van Rossum9b76da62007-04-11 01:09:03 +00002428
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002429 def test_telling(self):
Marc-André Lemburg8f36af72011-02-25 15:42:01 +00002430 f = self.open(support.TESTFN, "w+", encoding="utf-8")
Guido van Rossumb9c4c3e2007-04-11 16:07:50 +00002431 p0 = f.tell()
Guido van Rossumef87d6e2007-05-02 19:09:54 +00002432 f.write("\xff\n")
Guido van Rossumb9c4c3e2007-04-11 16:07:50 +00002433 p1 = f.tell()
Guido van Rossumef87d6e2007-05-02 19:09:54 +00002434 f.write("\xff\n")
Guido van Rossumb9c4c3e2007-04-11 16:07:50 +00002435 p2 = f.tell()
2436 f.seek(0)
Ezio Melottib3aedd42010-11-20 19:04:17 +00002437 self.assertEqual(f.tell(), p0)
2438 self.assertEqual(f.readline(), "\xff\n")
2439 self.assertEqual(f.tell(), p1)
2440 self.assertEqual(f.readline(), "\xff\n")
2441 self.assertEqual(f.tell(), p2)
Guido van Rossumb9c4c3e2007-04-11 16:07:50 +00002442 f.seek(0)
2443 for line in f:
Ezio Melottib3aedd42010-11-20 19:04:17 +00002444 self.assertEqual(line, "\xff\n")
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02002445 self.assertRaises(OSError, f.tell)
Ezio Melottib3aedd42010-11-20 19:04:17 +00002446 self.assertEqual(f.tell(), p2)
Guido van Rossumb9c4c3e2007-04-11 16:07:50 +00002447 f.close()
2448
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002449 def test_seeking(self):
2450 chunk_size = _default_chunk_size()
Guido van Rossumd76e7792007-04-17 02:38:04 +00002451 prefix_size = chunk_size - 2
Guido van Rossumef87d6e2007-05-02 19:09:54 +00002452 u_prefix = "a" * prefix_size
Guido van Rossumd76e7792007-04-17 02:38:04 +00002453 prefix = bytes(u_prefix.encode("utf-8"))
Ezio Melottib3aedd42010-11-20 19:04:17 +00002454 self.assertEqual(len(u_prefix), len(prefix))
Guido van Rossumef87d6e2007-05-02 19:09:54 +00002455 u_suffix = "\u8888\n"
Guido van Rossumd76e7792007-04-17 02:38:04 +00002456 suffix = bytes(u_suffix.encode("utf-8"))
2457 line = prefix + suffix
Benjamin Petersonebe5d8a2010-10-31 01:30:11 +00002458 with self.open(support.TESTFN, "wb") as f:
2459 f.write(line*2)
2460 with self.open(support.TESTFN, "r", encoding="utf-8") as f:
2461 s = f.read(prefix_size)
Ezio Melottib3aedd42010-11-20 19:04:17 +00002462 self.assertEqual(s, str(prefix, "ascii"))
2463 self.assertEqual(f.tell(), prefix_size)
2464 self.assertEqual(f.readline(), u_suffix)
Guido van Rossumd76e7792007-04-17 02:38:04 +00002465
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002466 def test_seeking_too(self):
Guido van Rossumd76e7792007-04-17 02:38:04 +00002467 # Regression test for a specific bug
2468 data = b'\xe0\xbf\xbf\n'
Benjamin Petersonebe5d8a2010-10-31 01:30:11 +00002469 with self.open(support.TESTFN, "wb") as f:
2470 f.write(data)
2471 with self.open(support.TESTFN, "r", encoding="utf-8") as f:
2472 f._CHUNK_SIZE # Just test that it exists
2473 f._CHUNK_SIZE = 2
2474 f.readline()
2475 f.tell()
Guido van Rossumd76e7792007-04-17 02:38:04 +00002476
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002477 def test_seek_and_tell(self):
2478 #Test seek/tell using the StatefulIncrementalDecoder.
2479 # Make test faster by doing smaller seeks
2480 CHUNK_SIZE = 128
Ka-Ping Yeef44c7e82008-03-18 04:51:32 +00002481
Benjamin Peterson5fd871d2009-03-05 00:49:53 +00002482 def test_seek_and_tell_with_data(data, min_pos=0):
Ka-Ping Yeef44c7e82008-03-18 04:51:32 +00002483 """Tell/seek to various points within a data stream and ensure
2484 that the decoded data returned by read() is consistent."""
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002485 f = self.open(support.TESTFN, 'wb')
Ka-Ping Yeef44c7e82008-03-18 04:51:32 +00002486 f.write(data)
2487 f.close()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002488 f = self.open(support.TESTFN, encoding='test_decoder')
2489 f._CHUNK_SIZE = CHUNK_SIZE
Ka-Ping Yeef44c7e82008-03-18 04:51:32 +00002490 decoded = f.read()
2491 f.close()
2492
Neal Norwitze2b07052008-03-18 19:52:05 +00002493 for i in range(min_pos, len(decoded) + 1): # seek positions
2494 for j in [1, 5, len(decoded) - i]: # read lengths
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002495 f = self.open(support.TESTFN, encoding='test_decoder')
Ezio Melottib3aedd42010-11-20 19:04:17 +00002496 self.assertEqual(f.read(i), decoded[:i])
Ka-Ping Yeef44c7e82008-03-18 04:51:32 +00002497 cookie = f.tell()
Ezio Melottib3aedd42010-11-20 19:04:17 +00002498 self.assertEqual(f.read(j), decoded[i:i + j])
Ka-Ping Yeef44c7e82008-03-18 04:51:32 +00002499 f.seek(cookie)
Ezio Melottib3aedd42010-11-20 19:04:17 +00002500 self.assertEqual(f.read(), decoded[i:])
Ka-Ping Yeef44c7e82008-03-18 04:51:32 +00002501 f.close()
2502
Benjamin Petersonad9d48d2008-04-02 21:49:44 +00002503 # Enable the test decoder.
2504 StatefulIncrementalDecoder.codecEnabled = 1
Ka-Ping Yeef44c7e82008-03-18 04:51:32 +00002505
2506 # Run the tests.
2507 try:
2508 # Try each test case.
2509 for input, _, _ in StatefulIncrementalDecoderTest.test_cases:
Benjamin Peterson5fd871d2009-03-05 00:49:53 +00002510 test_seek_and_tell_with_data(input)
Ka-Ping Yeef44c7e82008-03-18 04:51:32 +00002511
2512 # Position each test case so that it crosses a chunk boundary.
Ka-Ping Yeef44c7e82008-03-18 04:51:32 +00002513 for input, _, _ in StatefulIncrementalDecoderTest.test_cases:
2514 offset = CHUNK_SIZE - len(input)//2
2515 prefix = b'.'*offset
2516 # Don't bother seeking into the prefix (takes too long).
2517 min_pos = offset*2
Benjamin Peterson5fd871d2009-03-05 00:49:53 +00002518 test_seek_and_tell_with_data(prefix + input, min_pos)
Ka-Ping Yeef44c7e82008-03-18 04:51:32 +00002519
2520 # Ensure our test decoder won't interfere with subsequent tests.
2521 finally:
Benjamin Petersonad9d48d2008-04-02 21:49:44 +00002522 StatefulIncrementalDecoder.codecEnabled = 0
Ka-Ping Yeef44c7e82008-03-18 04:51:32 +00002523
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002524 def test_encoded_writes(self):
Alexandre Vassalottia38f73b2008-01-07 18:30:48 +00002525 data = "1234567890"
2526 tests = ("utf-16",
2527 "utf-16-le",
2528 "utf-16-be",
2529 "utf-32",
2530 "utf-32-le",
2531 "utf-32-be")
2532 for encoding in tests:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002533 buf = self.BytesIO()
2534 f = self.TextIOWrapper(buf, encoding=encoding)
Alexandre Vassalottia38f73b2008-01-07 18:30:48 +00002535 # Check if the BOM is written only once (see issue1753).
2536 f.write(data)
2537 f.write(data)
2538 f.seek(0)
Ezio Melottib3aedd42010-11-20 19:04:17 +00002539 self.assertEqual(f.read(), data * 2)
Benjamin Peterson9363a652009-03-05 00:42:09 +00002540 f.seek(0)
Ezio Melottib3aedd42010-11-20 19:04:17 +00002541 self.assertEqual(f.read(), data * 2)
2542 self.assertEqual(buf.getvalue(), (data * 2).encode(encoding))
Alexandre Vassalottia38f73b2008-01-07 18:30:48 +00002543
Benjamin Petersona1b49012009-03-31 23:11:32 +00002544 def test_unreadable(self):
2545 class UnReadable(self.BytesIO):
2546 def readable(self):
2547 return False
2548 txt = self.TextIOWrapper(UnReadable())
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02002549 self.assertRaises(OSError, txt.read)
Benjamin Petersona1b49012009-03-31 23:11:32 +00002550
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002551 def test_read_one_by_one(self):
2552 txt = self.TextIOWrapper(self.BytesIO(b"AA\r\nBB"))
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +00002553 reads = ""
2554 while True:
2555 c = txt.read(1)
2556 if not c:
2557 break
2558 reads += c
Ezio Melottib3aedd42010-11-20 19:04:17 +00002559 self.assertEqual(reads, "AA\nBB")
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +00002560
Benjamin Petersonbf5ff762009-12-13 19:25:34 +00002561 def test_readlines(self):
2562 txt = self.TextIOWrapper(self.BytesIO(b"AA\nBB\nCC"))
2563 self.assertEqual(txt.readlines(), ["AA\n", "BB\n", "CC"])
2564 txt.seek(0)
2565 self.assertEqual(txt.readlines(None), ["AA\n", "BB\n", "CC"])
2566 txt.seek(0)
2567 self.assertEqual(txt.readlines(5), ["AA\n", "BB\n"])
2568
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +00002569 # read in amounts equal to TextIOWrapper._CHUNK_SIZE which is 128.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002570 def test_read_by_chunk(self):
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +00002571 # make sure "\r\n" straddles 128 char boundary.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002572 txt = self.TextIOWrapper(self.BytesIO(b"A" * 127 + b"\r\nB"))
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +00002573 reads = ""
2574 while True:
2575 c = txt.read(128)
2576 if not c:
2577 break
2578 reads += c
Ezio Melottib3aedd42010-11-20 19:04:17 +00002579 self.assertEqual(reads, "A"*127+"\nB")
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +00002580
Antoine Pitrou3ed2cb52012-10-16 23:02:27 +02002581 def test_writelines(self):
2582 l = ['ab', 'cd', 'ef']
2583 buf = self.BytesIO()
2584 txt = self.TextIOWrapper(buf)
2585 txt.writelines(l)
2586 txt.flush()
2587 self.assertEqual(buf.getvalue(), b'abcdef')
2588
2589 def test_writelines_userlist(self):
2590 l = UserList(['ab', 'cd', 'ef'])
2591 buf = self.BytesIO()
2592 txt = self.TextIOWrapper(buf)
2593 txt.writelines(l)
2594 txt.flush()
2595 self.assertEqual(buf.getvalue(), b'abcdef')
2596
2597 def test_writelines_error(self):
2598 txt = self.TextIOWrapper(self.BytesIO())
2599 self.assertRaises(TypeError, txt.writelines, [1, 2, 3])
2600 self.assertRaises(TypeError, txt.writelines, None)
2601 self.assertRaises(TypeError, txt.writelines, b'abc')
2602
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +00002603 def test_issue1395_1(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002604 txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii")
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +00002605
2606 # read one char at a time
2607 reads = ""
2608 while True:
2609 c = txt.read(1)
2610 if not c:
2611 break
2612 reads += c
Ezio Melottib3aedd42010-11-20 19:04:17 +00002613 self.assertEqual(reads, self.normalized)
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +00002614
2615 def test_issue1395_2(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002616 txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii")
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +00002617 txt._CHUNK_SIZE = 4
2618
2619 reads = ""
2620 while True:
2621 c = txt.read(4)
2622 if not c:
2623 break
2624 reads += c
Ezio Melottib3aedd42010-11-20 19:04:17 +00002625 self.assertEqual(reads, self.normalized)
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +00002626
2627 def test_issue1395_3(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002628 txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii")
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +00002629 txt._CHUNK_SIZE = 4
2630
2631 reads = txt.read(4)
2632 reads += txt.read(4)
2633 reads += txt.readline()
2634 reads += txt.readline()
2635 reads += txt.readline()
Ezio Melottib3aedd42010-11-20 19:04:17 +00002636 self.assertEqual(reads, self.normalized)
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +00002637
2638 def test_issue1395_4(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002639 txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii")
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +00002640 txt._CHUNK_SIZE = 4
2641
2642 reads = txt.read(4)
2643 reads += txt.read()
Ezio Melottib3aedd42010-11-20 19:04:17 +00002644 self.assertEqual(reads, self.normalized)
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +00002645
2646 def test_issue1395_5(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002647 txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii")
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +00002648 txt._CHUNK_SIZE = 4
2649
2650 reads = txt.read(4)
2651 pos = txt.tell()
2652 txt.seek(0)
2653 txt.seek(pos)
Ezio Melottib3aedd42010-11-20 19:04:17 +00002654 self.assertEqual(txt.read(4), "BBB\n")
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +00002655
Ka-Ping Yeeddaa7062008-03-17 20:35:15 +00002656 def test_issue2282(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002657 buffer = self.BytesIO(self.testdata)
2658 txt = self.TextIOWrapper(buffer, encoding="ascii")
Ka-Ping Yeeddaa7062008-03-17 20:35:15 +00002659
2660 self.assertEqual(buffer.seekable(), txt.seekable())
2661
Antoine Pitroue4501852009-05-14 18:55:55 +00002662 def test_append_bom(self):
2663 # The BOM is not written again when appending to a non-empty file
2664 filename = support.TESTFN
2665 for charset in ('utf-8-sig', 'utf-16', 'utf-32'):
2666 with self.open(filename, 'w', encoding=charset) as f:
2667 f.write('aaa')
2668 pos = f.tell()
2669 with self.open(filename, 'rb') as f:
Ezio Melottib3aedd42010-11-20 19:04:17 +00002670 self.assertEqual(f.read(), 'aaa'.encode(charset))
Antoine Pitroue4501852009-05-14 18:55:55 +00002671
2672 with self.open(filename, 'a', encoding=charset) as f:
2673 f.write('xxx')
2674 with self.open(filename, 'rb') as f:
Ezio Melottib3aedd42010-11-20 19:04:17 +00002675 self.assertEqual(f.read(), 'aaaxxx'.encode(charset))
Antoine Pitroue4501852009-05-14 18:55:55 +00002676
2677 def test_seek_bom(self):
2678 # Same test, but when seeking manually
2679 filename = support.TESTFN
2680 for charset in ('utf-8-sig', 'utf-16', 'utf-32'):
2681 with self.open(filename, 'w', encoding=charset) as f:
2682 f.write('aaa')
2683 pos = f.tell()
2684 with self.open(filename, 'r+', encoding=charset) as f:
2685 f.seek(pos)
2686 f.write('zzz')
2687 f.seek(0)
2688 f.write('bbb')
2689 with self.open(filename, 'rb') as f:
Ezio Melottib3aedd42010-11-20 19:04:17 +00002690 self.assertEqual(f.read(), 'bbbzzz'.encode(charset))
Antoine Pitroue4501852009-05-14 18:55:55 +00002691
Benjamin Peterson0926ad12009-06-06 18:02:12 +00002692 def test_errors_property(self):
2693 with self.open(support.TESTFN, "w") as f:
2694 self.assertEqual(f.errors, "strict")
2695 with self.open(support.TESTFN, "w", errors="replace") as f:
2696 self.assertEqual(f.errors, "replace")
2697
Brett Cannon31f59292011-02-21 19:29:56 +00002698 @support.no_tracing
Victor Stinner45df8202010-04-28 22:31:17 +00002699 @unittest.skipUnless(threading, 'Threading required for this test.')
Amaury Forgeot d'Arcccd686a2009-08-29 23:00:38 +00002700 def test_threads_write(self):
2701 # Issue6750: concurrent writes could duplicate data
2702 event = threading.Event()
2703 with self.open(support.TESTFN, "w", buffering=1) as f:
2704 def run(n):
2705 text = "Thread%03d\n" % n
2706 event.wait()
2707 f.write(text)
2708 threads = [threading.Thread(target=lambda n=x: run(n))
2709 for x in range(20)]
2710 for t in threads:
2711 t.start()
2712 time.sleep(0.02)
2713 event.set()
2714 for t in threads:
2715 t.join()
2716 with self.open(support.TESTFN) as f:
2717 content = f.read()
2718 for n in range(20):
Ezio Melottib3aedd42010-11-20 19:04:17 +00002719 self.assertEqual(content.count("Thread%03d\n" % n), 1)
Amaury Forgeot d'Arcccd686a2009-08-29 23:00:38 +00002720
Antoine Pitrou6be88762010-05-03 16:48:20 +00002721 def test_flush_error_on_close(self):
Serhiy Storchakaa3712a92015-02-21 00:35:09 +02002722 # Test that text file is closed despite failed flush
2723 # and that flush() is called before file closed.
Antoine Pitrou6be88762010-05-03 16:48:20 +00002724 txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii")
Serhiy Storchakaa3712a92015-02-21 00:35:09 +02002725 closed = []
Antoine Pitrou6be88762010-05-03 16:48:20 +00002726 def bad_flush():
Serhiy Storchakaa3712a92015-02-21 00:35:09 +02002727 closed[:] = [txt.closed, txt.buffer.closed]
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02002728 raise OSError()
Antoine Pitrou6be88762010-05-03 16:48:20 +00002729 txt.flush = bad_flush
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02002730 self.assertRaises(OSError, txt.close) # exception not swallowed
Benjamin Peterson68623612012-12-20 11:53:11 -06002731 self.assertTrue(txt.closed)
Serhiy Storchakaa3712a92015-02-21 00:35:09 +02002732 self.assertTrue(txt.buffer.closed)
2733 self.assertTrue(closed) # flush() called
2734 self.assertFalse(closed[0]) # flush() called before file closed
2735 self.assertFalse(closed[1])
Serhiy Storchakac26a1a42015-02-23 00:28:38 +02002736 txt.flush = lambda: None # break reference loop
Antoine Pitrou6be88762010-05-03 16:48:20 +00002737
Serhiy Storchaka8a8f7f92014-06-09 09:13:04 +03002738 def test_close_error_on_close(self):
2739 buffer = self.BytesIO(self.testdata)
2740 def bad_flush():
2741 raise OSError('flush')
2742 def bad_close():
2743 raise OSError('close')
2744 buffer.close = bad_close
2745 txt = self.TextIOWrapper(buffer, encoding="ascii")
2746 txt.flush = bad_flush
2747 with self.assertRaises(OSError) as err: # exception not swallowed
2748 txt.close()
2749 self.assertEqual(err.exception.args, ('close',))
2750 self.assertIsInstance(err.exception.__context__, OSError)
2751 self.assertEqual(err.exception.__context__.args, ('flush',))
2752 self.assertFalse(txt.closed)
2753
2754 def test_nonnormalized_close_error_on_close(self):
2755 # Issue #21677
2756 buffer = self.BytesIO(self.testdata)
2757 def bad_flush():
2758 raise non_existing_flush
2759 def bad_close():
2760 raise non_existing_close
2761 buffer.close = bad_close
2762 txt = self.TextIOWrapper(buffer, encoding="ascii")
2763 txt.flush = bad_flush
2764 with self.assertRaises(NameError) as err: # exception not swallowed
2765 txt.close()
2766 self.assertIn('non_existing_close', str(err.exception))
2767 self.assertIsInstance(err.exception.__context__, NameError)
2768 self.assertIn('non_existing_flush', str(err.exception.__context__))
2769 self.assertFalse(txt.closed)
2770
Antoine Pitrou6be88762010-05-03 16:48:20 +00002771 def test_multi_close(self):
2772 txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii")
2773 txt.close()
2774 txt.close()
2775 txt.close()
2776 self.assertRaises(ValueError, txt.flush)
2777
Antoine Pitrou0d739d72010-09-05 23:01:12 +00002778 def test_unseekable(self):
2779 txt = self.TextIOWrapper(self.MockUnseekableIO(self.testdata))
2780 self.assertRaises(self.UnsupportedOperation, txt.tell)
2781 self.assertRaises(self.UnsupportedOperation, txt.seek, 0)
2782
Antoine Pitrou7f8f4182010-12-21 21:20:59 +00002783 def test_readonly_attributes(self):
2784 txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii")
2785 buf = self.BytesIO(self.testdata)
2786 with self.assertRaises(AttributeError):
2787 txt.buffer = buf
2788
Antoine Pitroue96ec682011-07-23 21:46:35 +02002789 def test_rawio(self):
2790 # Issue #12591: TextIOWrapper must work with raw I/O objects, so
2791 # that subprocess.Popen() can have the required unbuffered
2792 # semantics with universal_newlines=True.
2793 raw = self.MockRawIO([b'abc', b'def', b'ghi\njkl\nopq\n'])
2794 txt = self.TextIOWrapper(raw, encoding='ascii', newline='\n')
2795 # Reads
2796 self.assertEqual(txt.read(4), 'abcd')
2797 self.assertEqual(txt.readline(), 'efghi\n')
2798 self.assertEqual(list(txt), ['jkl\n', 'opq\n'])
2799
2800 def test_rawio_write_through(self):
2801 # Issue #12591: with write_through=True, writes don't need a flush
2802 raw = self.MockRawIO([b'abc', b'def', b'ghi\njkl\nopq\n'])
2803 txt = self.TextIOWrapper(raw, encoding='ascii', newline='\n',
2804 write_through=True)
2805 txt.write('1')
2806 txt.write('23\n4')
2807 txt.write('5')
2808 self.assertEqual(b''.join(raw._write_stack), b'123\n45')
2809
Antoine Pitrouc644e7c2014-05-09 00:24:50 +02002810 def test_bufio_write_through(self):
2811 # Issue #21396: write_through=True doesn't force a flush()
2812 # on the underlying binary buffered object.
2813 flush_called, write_called = [], []
2814 class BufferedWriter(self.BufferedWriter):
2815 def flush(self, *args, **kwargs):
2816 flush_called.append(True)
2817 return super().flush(*args, **kwargs)
2818 def write(self, *args, **kwargs):
2819 write_called.append(True)
2820 return super().write(*args, **kwargs)
2821
2822 rawio = self.BytesIO()
2823 data = b"a"
2824 bufio = BufferedWriter(rawio, len(data)*2)
2825 textio = self.TextIOWrapper(bufio, encoding='ascii',
2826 write_through=True)
2827 # write to the buffered io but don't overflow the buffer
2828 text = data.decode('ascii')
2829 textio.write(text)
2830
2831 # buffer.flush is not called with write_through=True
2832 self.assertFalse(flush_called)
2833 # buffer.write *is* called with write_through=True
2834 self.assertTrue(write_called)
2835 self.assertEqual(rawio.getvalue(), b"") # no flush
2836
2837 write_called = [] # reset
2838 textio.write(text * 10) # total content is larger than bufio buffer
2839 self.assertTrue(write_called)
2840 self.assertEqual(rawio.getvalue(), data * 11) # all flushed
2841
Serhiy Storchaka94dc6732013-02-03 17:03:31 +02002842 def test_read_nonbytes(self):
2843 # Issue #17106
2844 # Crash when underlying read() returns non-bytes
2845 t = self.TextIOWrapper(self.StringIO('a'))
2846 self.assertRaises(TypeError, t.read, 1)
2847 t = self.TextIOWrapper(self.StringIO('a'))
2848 self.assertRaises(TypeError, t.readline)
2849 t = self.TextIOWrapper(self.StringIO('a'))
2850 self.assertRaises(TypeError, t.read)
2851
2852 def test_illegal_decoder(self):
2853 # Issue #17106
Nick Coghlana9b15242014-02-04 22:11:18 +10002854 # Bypass the early encoding check added in issue 20404
2855 def _make_illegal_wrapper():
2856 quopri = codecs.lookup("quopri")
2857 quopri._is_text_encoding = True
2858 try:
2859 t = self.TextIOWrapper(self.BytesIO(b'aaaaaa'),
2860 newline='\n', encoding="quopri")
2861 finally:
2862 quopri._is_text_encoding = False
2863 return t
Serhiy Storchaka94dc6732013-02-03 17:03:31 +02002864 # Crash when decoder returns non-string
Nick Coghlana9b15242014-02-04 22:11:18 +10002865 t = _make_illegal_wrapper()
Serhiy Storchaka94dc6732013-02-03 17:03:31 +02002866 self.assertRaises(TypeError, t.read, 1)
Nick Coghlana9b15242014-02-04 22:11:18 +10002867 t = _make_illegal_wrapper()
Serhiy Storchaka94dc6732013-02-03 17:03:31 +02002868 self.assertRaises(TypeError, t.readline)
Nick Coghlana9b15242014-02-04 22:11:18 +10002869 t = _make_illegal_wrapper()
Serhiy Storchaka94dc6732013-02-03 17:03:31 +02002870 self.assertRaises(TypeError, t.read)
2871
Antoine Pitrou712cb732013-12-21 15:51:54 +01002872 def _check_create_at_shutdown(self, **kwargs):
2873 # Issue #20037: creating a TextIOWrapper at shutdown
2874 # shouldn't crash the interpreter.
2875 iomod = self.io.__name__
2876 code = """if 1:
2877 import codecs
2878 import {iomod} as io
2879
2880 # Avoid looking up codecs at shutdown
2881 codecs.lookup('utf-8')
2882
2883 class C:
2884 def __init__(self):
2885 self.buf = io.BytesIO()
2886 def __del__(self):
2887 io.TextIOWrapper(self.buf, **{kwargs})
2888 print("ok")
2889 c = C()
2890 """.format(iomod=iomod, kwargs=kwargs)
2891 return assert_python_ok("-c", code)
2892
2893 def test_create_at_shutdown_without_encoding(self):
2894 rc, out, err = self._check_create_at_shutdown()
2895 if err:
2896 # Can error out with a RuntimeError if the module state
2897 # isn't found.
Nick Coghlana9b15242014-02-04 22:11:18 +10002898 self.assertIn(self.shutdown_error, err.decode())
Antoine Pitrou712cb732013-12-21 15:51:54 +01002899 else:
2900 self.assertEqual("ok", out.decode().strip())
2901
2902 def test_create_at_shutdown_with_encoding(self):
2903 rc, out, err = self._check_create_at_shutdown(encoding='utf-8',
2904 errors='strict')
2905 self.assertFalse(err)
2906 self.assertEqual("ok", out.decode().strip())
2907
Antoine Pitroub8503892014-04-29 10:14:02 +02002908 def test_read_byteslike(self):
2909 r = MemviewBytesIO(b'Just some random string\n')
2910 t = self.TextIOWrapper(r, 'utf-8')
2911
2912 # TextIOwrapper will not read the full string, because
2913 # we truncate it to a multiple of the native int size
2914 # so that we can construct a more complex memoryview.
2915 bytes_val = _to_memoryview(r.getvalue()).tobytes()
2916
2917 self.assertEqual(t.read(200), bytes_val.decode('utf-8'))
2918
Benjamin Peterson6c14f232014-11-12 10:19:46 -05002919 def test_issue22849(self):
2920 class F(object):
2921 def readable(self): return True
2922 def writable(self): return True
2923 def seekable(self): return True
2924
2925 for i in range(10):
2926 try:
2927 self.TextIOWrapper(F(), encoding='utf-8')
2928 except Exception:
2929 pass
2930
2931 F.tell = lambda x: 0
2932 t = self.TextIOWrapper(F(), encoding='utf-8')
2933
Serhiy Storchaka94dc6732013-02-03 17:03:31 +02002934
Antoine Pitroub8503892014-04-29 10:14:02 +02002935class MemviewBytesIO(io.BytesIO):
2936 '''A BytesIO object whose read method returns memoryviews
2937 rather than bytes'''
2938
2939 def read1(self, len_):
2940 return _to_memoryview(super().read1(len_))
2941
2942 def read(self, len_):
2943 return _to_memoryview(super().read(len_))
2944
2945def _to_memoryview(buf):
2946 '''Convert bytes-object *buf* to a non-trivial memoryview'''
2947
2948 arr = array.array('i')
2949 idx = len(buf) - len(buf) % arr.itemsize
2950 arr.frombytes(buf[:idx])
2951 return memoryview(arr)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00002952
Benjamin Peterson6fd113c2014-11-12 10:23:44 -05002953
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002954class CTextIOWrapperTest(TextIOWrapperTest):
Antoine Pitrou712cb732013-12-21 15:51:54 +01002955 io = io
Nick Coghlana9b15242014-02-04 22:11:18 +10002956 shutdown_error = "RuntimeError: could not find io module state"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002957
2958 def test_initialization(self):
2959 r = self.BytesIO(b"\xc3\xa9\n\n")
2960 b = self.BufferedReader(r, 1000)
2961 t = self.TextIOWrapper(b)
2962 self.assertRaises(TypeError, t.__init__, b, newline=42)
2963 self.assertRaises(ValueError, t.read)
2964 self.assertRaises(ValueError, t.__init__, b, newline='xyzzy')
2965 self.assertRaises(ValueError, t.read)
2966
Benjamin Peterson10e76b62014-12-21 20:51:50 -06002967 t = self.TextIOWrapper.__new__(self.TextIOWrapper)
2968 self.assertRaises(Exception, repr, t)
2969
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002970 def test_garbage_collection(self):
2971 # C TextIOWrapper objects are collected, and collecting them flushes
2972 # all data to disk.
2973 # The Python version has __del__, so it ends in gc.garbage instead.
Antoine Pitrou796564c2013-07-30 19:59:21 +02002974 with support.check_warnings(('', ResourceWarning)):
2975 rawio = io.FileIO(support.TESTFN, "wb")
2976 b = self.BufferedWriter(rawio)
2977 t = self.TextIOWrapper(b, encoding="ascii")
2978 t.write("456def")
2979 t.x = t
2980 wr = weakref.ref(t)
2981 del t
2982 support.gc_collect()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002983 self.assertTrue(wr() is None, wr)
Hirokazu Yamamotoc7d6aa42009-06-18 00:07:14 +00002984 with self.open(support.TESTFN, "rb") as f:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002985 self.assertEqual(f.read(), b"456def")
2986
Charles-François Natali42c28cd2011-10-05 19:53:43 +02002987 def test_rwpair_cleared_before_textio(self):
2988 # Issue 13070: TextIOWrapper's finalization would crash when called
2989 # after the reference to the underlying BufferedRWPair's writer got
2990 # cleared by the GC.
2991 for i in range(1000):
2992 b1 = self.BufferedRWPair(self.MockRawIO(), self.MockRawIO())
2993 t1 = self.TextIOWrapper(b1, encoding="ascii")
2994 b2 = self.BufferedRWPair(self.MockRawIO(), self.MockRawIO())
2995 t2 = self.TextIOWrapper(b2, encoding="ascii")
2996 # circular references
2997 t1.buddy = t2
2998 t2.buddy = t1
2999 support.gc_collect()
3000
3001
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003002class PyTextIOWrapperTest(TextIOWrapperTest):
Antoine Pitrou712cb732013-12-21 15:51:54 +01003003 io = pyio
Serhiy Storchakad667d722014-02-10 19:09:19 +02003004 #shutdown_error = "LookupError: unknown encoding: ascii"
3005 shutdown_error = "TypeError: 'NoneType' object is not iterable"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003006
3007
3008class IncrementalNewlineDecoderTest(unittest.TestCase):
3009
3010 def check_newline_decoding_utf8(self, decoder):
Antoine Pitrou180a3362008-12-14 16:36:46 +00003011 # UTF-8 specific tests for a newline decoder
3012 def _check_decode(b, s, **kwargs):
3013 # We exercise getstate() / setstate() as well as decode()
3014 state = decoder.getstate()
Ezio Melottib3aedd42010-11-20 19:04:17 +00003015 self.assertEqual(decoder.decode(b, **kwargs), s)
Antoine Pitrou180a3362008-12-14 16:36:46 +00003016 decoder.setstate(state)
Ezio Melottib3aedd42010-11-20 19:04:17 +00003017 self.assertEqual(decoder.decode(b, **kwargs), s)
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +00003018
Antoine Pitrou180a3362008-12-14 16:36:46 +00003019 _check_decode(b'\xe8\xa2\x88', "\u8888")
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +00003020
Antoine Pitrou180a3362008-12-14 16:36:46 +00003021 _check_decode(b'\xe8', "")
3022 _check_decode(b'\xa2', "")
3023 _check_decode(b'\x88', "\u8888")
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +00003024
Antoine Pitrou180a3362008-12-14 16:36:46 +00003025 _check_decode(b'\xe8', "")
3026 _check_decode(b'\xa2', "")
3027 _check_decode(b'\x88', "\u8888")
3028
3029 _check_decode(b'\xe8', "")
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +00003030 self.assertRaises(UnicodeDecodeError, decoder.decode, b'', final=True)
3031
Antoine Pitrou180a3362008-12-14 16:36:46 +00003032 decoder.reset()
3033 _check_decode(b'\n', "\n")
3034 _check_decode(b'\r', "")
3035 _check_decode(b'', "\n", final=True)
3036 _check_decode(b'\r', "\n", final=True)
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +00003037
Antoine Pitrou180a3362008-12-14 16:36:46 +00003038 _check_decode(b'\r', "")
3039 _check_decode(b'a', "\na")
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +00003040
Antoine Pitrou180a3362008-12-14 16:36:46 +00003041 _check_decode(b'\r\r\n', "\n\n")
3042 _check_decode(b'\r', "")
3043 _check_decode(b'\r', "\n")
3044 _check_decode(b'\na', "\na")
Amaury Forgeot d'Arc1ff99102007-11-19 20:34:10 +00003045
Antoine Pitrou180a3362008-12-14 16:36:46 +00003046 _check_decode(b'\xe8\xa2\x88\r\n', "\u8888\n")
3047 _check_decode(b'\xe8\xa2\x88', "\u8888")
3048 _check_decode(b'\n', "\n")
3049 _check_decode(b'\xe8\xa2\x88\r', "\u8888")
3050 _check_decode(b'\n', "\n")
Guido van Rossum9b76da62007-04-11 01:09:03 +00003051
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003052 def check_newline_decoding(self, decoder, encoding):
Antoine Pitrou180a3362008-12-14 16:36:46 +00003053 result = []
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003054 if encoding is not None:
3055 encoder = codecs.getincrementalencoder(encoding)()
3056 def _decode_bytewise(s):
3057 # Decode one byte at a time
3058 for b in encoder.encode(s):
3059 result.append(decoder.decode(bytes([b])))
3060 else:
3061 encoder = None
3062 def _decode_bytewise(s):
3063 # Decode one char at a time
3064 for c in s:
3065 result.append(decoder.decode(c))
Ezio Melottib3aedd42010-11-20 19:04:17 +00003066 self.assertEqual(decoder.newlines, None)
Antoine Pitrou180a3362008-12-14 16:36:46 +00003067 _decode_bytewise("abc\n\r")
Ezio Melottib3aedd42010-11-20 19:04:17 +00003068 self.assertEqual(decoder.newlines, '\n')
Antoine Pitrou180a3362008-12-14 16:36:46 +00003069 _decode_bytewise("\nabc")
Ezio Melottib3aedd42010-11-20 19:04:17 +00003070 self.assertEqual(decoder.newlines, ('\n', '\r\n'))
Antoine Pitrou180a3362008-12-14 16:36:46 +00003071 _decode_bytewise("abc\r")
Ezio Melottib3aedd42010-11-20 19:04:17 +00003072 self.assertEqual(decoder.newlines, ('\n', '\r\n'))
Antoine Pitrou180a3362008-12-14 16:36:46 +00003073 _decode_bytewise("abc")
Ezio Melottib3aedd42010-11-20 19:04:17 +00003074 self.assertEqual(decoder.newlines, ('\r', '\n', '\r\n'))
Antoine Pitrou180a3362008-12-14 16:36:46 +00003075 _decode_bytewise("abc\r")
Ezio Melottib3aedd42010-11-20 19:04:17 +00003076 self.assertEqual("".join(result), "abc\n\nabcabc\nabcabc")
Antoine Pitrou180a3362008-12-14 16:36:46 +00003077 decoder.reset()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003078 input = "abc"
3079 if encoder is not None:
3080 encoder.reset()
3081 input = encoder.encode(input)
Ezio Melottib3aedd42010-11-20 19:04:17 +00003082 self.assertEqual(decoder.decode(input), "abc")
3083 self.assertEqual(decoder.newlines, None)
Antoine Pitrou180a3362008-12-14 16:36:46 +00003084
3085 def test_newline_decoder(self):
3086 encodings = (
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003087 # None meaning the IncrementalNewlineDecoder takes unicode input
3088 # rather than bytes input
3089 None, 'utf-8', 'latin-1',
Antoine Pitrou180a3362008-12-14 16:36:46 +00003090 'utf-16', 'utf-16-le', 'utf-16-be',
3091 'utf-32', 'utf-32-le', 'utf-32-be',
3092 )
3093 for enc in encodings:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003094 decoder = enc and codecs.getincrementaldecoder(enc)()
3095 decoder = self.IncrementalNewlineDecoder(decoder, translate=True)
3096 self.check_newline_decoding(decoder, enc)
Alexandre Vassalotti472f07d2008-01-06 00:34:32 +00003097 decoder = codecs.getincrementaldecoder("utf-8")()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003098 decoder = self.IncrementalNewlineDecoder(decoder, translate=True)
3099 self.check_newline_decoding_utf8(decoder)
3100
Antoine Pitrou66913e22009-03-06 23:40:56 +00003101 def test_newline_bytes(self):
3102 # Issue 5433: Excessive optimization in IncrementalNewlineDecoder
3103 def _check(dec):
Ezio Melottib3aedd42010-11-20 19:04:17 +00003104 self.assertEqual(dec.newlines, None)
3105 self.assertEqual(dec.decode("\u0D00"), "\u0D00")
3106 self.assertEqual(dec.newlines, None)
3107 self.assertEqual(dec.decode("\u0A00"), "\u0A00")
3108 self.assertEqual(dec.newlines, None)
Antoine Pitrou66913e22009-03-06 23:40:56 +00003109 dec = self.IncrementalNewlineDecoder(None, translate=False)
3110 _check(dec)
3111 dec = self.IncrementalNewlineDecoder(None, translate=True)
3112 _check(dec)
3113
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003114class CIncrementalNewlineDecoderTest(IncrementalNewlineDecoderTest):
3115 pass
3116
3117class PyIncrementalNewlineDecoderTest(IncrementalNewlineDecoderTest):
3118 pass
Antoine Pitrou180a3362008-12-14 16:36:46 +00003119
Alexandre Vassalotti472f07d2008-01-06 00:34:32 +00003120
Guido van Rossum01a27522007-03-07 01:00:12 +00003121# XXX Tests for open()
Guido van Rossum68bbcd22007-02-27 17:19:33 +00003122
Guido van Rossum5abbf752007-08-27 17:39:33 +00003123class MiscIOTest(unittest.TestCase):
3124
Barry Warsaw40e82462008-11-20 20:14:50 +00003125 def tearDown(self):
3126 support.unlink(support.TESTFN)
3127
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003128 def test___all__(self):
3129 for name in self.io.__all__:
3130 obj = getattr(self.io, name, None)
Benjamin Petersonbfb95942009-04-02 01:13:40 +00003131 self.assertTrue(obj is not None, name)
Guido van Rossum5abbf752007-08-27 17:39:33 +00003132 if name == "open":
3133 continue
Benjamin Peterson6a52a9c2009-04-29 22:00:44 +00003134 elif "error" in name.lower() or name == "UnsupportedOperation":
Benjamin Petersonbfb95942009-04-02 01:13:40 +00003135 self.assertTrue(issubclass(obj, Exception), name)
3136 elif not name.startswith("SEEK_"):
3137 self.assertTrue(issubclass(obj, self.IOBase))
Benjamin Peterson65676e42008-11-05 21:42:45 +00003138
Barry Warsaw40e82462008-11-20 20:14:50 +00003139 def test_attributes(self):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003140 f = self.open(support.TESTFN, "wb", buffering=0)
Ezio Melottib3aedd42010-11-20 19:04:17 +00003141 self.assertEqual(f.mode, "wb")
Barry Warsaw40e82462008-11-20 20:14:50 +00003142 f.close()
3143
Serhiy Storchaka2480c2e2013-11-24 23:13:26 +02003144 with support.check_warnings(('', DeprecationWarning)):
3145 f = self.open(support.TESTFN, "U")
Ezio Melottib3aedd42010-11-20 19:04:17 +00003146 self.assertEqual(f.name, support.TESTFN)
3147 self.assertEqual(f.buffer.name, support.TESTFN)
3148 self.assertEqual(f.buffer.raw.name, support.TESTFN)
3149 self.assertEqual(f.mode, "U")
3150 self.assertEqual(f.buffer.mode, "rb")
3151 self.assertEqual(f.buffer.raw.mode, "rb")
Barry Warsaw40e82462008-11-20 20:14:50 +00003152 f.close()
3153
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003154 f = self.open(support.TESTFN, "w+")
Ezio Melottib3aedd42010-11-20 19:04:17 +00003155 self.assertEqual(f.mode, "w+")
3156 self.assertEqual(f.buffer.mode, "rb+") # Does it really matter?
3157 self.assertEqual(f.buffer.raw.mode, "rb+")
Barry Warsaw40e82462008-11-20 20:14:50 +00003158
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003159 g = self.open(f.fileno(), "wb", closefd=False)
Ezio Melottib3aedd42010-11-20 19:04:17 +00003160 self.assertEqual(g.mode, "wb")
3161 self.assertEqual(g.raw.mode, "wb")
3162 self.assertEqual(g.name, f.fileno())
3163 self.assertEqual(g.raw.name, f.fileno())
Barry Warsaw40e82462008-11-20 20:14:50 +00003164 f.close()
3165 g.close()
3166
Antoine Pitrou8043cf82009-01-09 19:54:29 +00003167 def test_io_after_close(self):
3168 for kwargs in [
3169 {"mode": "w"},
3170 {"mode": "wb"},
3171 {"mode": "w", "buffering": 1},
3172 {"mode": "w", "buffering": 2},
3173 {"mode": "wb", "buffering": 0},
3174 {"mode": "r"},
3175 {"mode": "rb"},
3176 {"mode": "r", "buffering": 1},
3177 {"mode": "r", "buffering": 2},
3178 {"mode": "rb", "buffering": 0},
3179 {"mode": "w+"},
3180 {"mode": "w+b"},
3181 {"mode": "w+", "buffering": 1},
3182 {"mode": "w+", "buffering": 2},
3183 {"mode": "w+b", "buffering": 0},
3184 ]:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003185 f = self.open(support.TESTFN, **kwargs)
Antoine Pitrou8043cf82009-01-09 19:54:29 +00003186 f.close()
3187 self.assertRaises(ValueError, f.flush)
3188 self.assertRaises(ValueError, f.fileno)
3189 self.assertRaises(ValueError, f.isatty)
3190 self.assertRaises(ValueError, f.__iter__)
3191 if hasattr(f, "peek"):
3192 self.assertRaises(ValueError, f.peek, 1)
3193 self.assertRaises(ValueError, f.read)
3194 if hasattr(f, "read1"):
3195 self.assertRaises(ValueError, f.read1, 1024)
Victor Stinnerb79f28c2011-05-25 22:09:03 +02003196 if hasattr(f, "readall"):
3197 self.assertRaises(ValueError, f.readall)
Antoine Pitrou8043cf82009-01-09 19:54:29 +00003198 if hasattr(f, "readinto"):
3199 self.assertRaises(ValueError, f.readinto, bytearray(1024))
Benjamin Petersona96fea02014-06-22 14:17:44 -07003200 if hasattr(f, "readinto1"):
3201 self.assertRaises(ValueError, f.readinto1, bytearray(1024))
Antoine Pitrou8043cf82009-01-09 19:54:29 +00003202 self.assertRaises(ValueError, f.readline)
3203 self.assertRaises(ValueError, f.readlines)
3204 self.assertRaises(ValueError, f.seek, 0)
3205 self.assertRaises(ValueError, f.tell)
3206 self.assertRaises(ValueError, f.truncate)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003207 self.assertRaises(ValueError, f.write,
3208 b"" if "b" in kwargs['mode'] else "")
Antoine Pitrou8043cf82009-01-09 19:54:29 +00003209 self.assertRaises(ValueError, f.writelines, [])
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003210 self.assertRaises(ValueError, next, f)
Antoine Pitrou8043cf82009-01-09 19:54:29 +00003211
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003212 def test_blockingioerror(self):
3213 # Various BlockingIOError issues
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003214 class C(str):
3215 pass
3216 c = C("")
3217 b = self.BlockingIOError(1, c)
3218 c.b = b
3219 b.c = c
3220 wr = weakref.ref(c)
3221 del c, b
Benjamin Peterson24fb1d02009-04-24 23:26:21 +00003222 support.gc_collect()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00003223 self.assertTrue(wr() is None, wr)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003224
3225 def test_abcs(self):
3226 # Test the visible base classes are ABCs.
Ezio Melottie9615932010-01-24 19:26:24 +00003227 self.assertIsInstance(self.IOBase, abc.ABCMeta)
3228 self.assertIsInstance(self.RawIOBase, abc.ABCMeta)
3229 self.assertIsInstance(self.BufferedIOBase, abc.ABCMeta)
3230 self.assertIsInstance(self.TextIOBase, abc.ABCMeta)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003231
3232 def _check_abc_inheritance(self, abcmodule):
3233 with self.open(support.TESTFN, "wb", buffering=0) as f:
Ezio Melottie9615932010-01-24 19:26:24 +00003234 self.assertIsInstance(f, abcmodule.IOBase)
3235 self.assertIsInstance(f, abcmodule.RawIOBase)
3236 self.assertNotIsInstance(f, abcmodule.BufferedIOBase)
3237 self.assertNotIsInstance(f, abcmodule.TextIOBase)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003238 with self.open(support.TESTFN, "wb") as f:
Ezio Melottie9615932010-01-24 19:26:24 +00003239 self.assertIsInstance(f, abcmodule.IOBase)
3240 self.assertNotIsInstance(f, abcmodule.RawIOBase)
3241 self.assertIsInstance(f, abcmodule.BufferedIOBase)
3242 self.assertNotIsInstance(f, abcmodule.TextIOBase)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003243 with self.open(support.TESTFN, "w") as f:
Ezio Melottie9615932010-01-24 19:26:24 +00003244 self.assertIsInstance(f, abcmodule.IOBase)
3245 self.assertNotIsInstance(f, abcmodule.RawIOBase)
3246 self.assertNotIsInstance(f, abcmodule.BufferedIOBase)
3247 self.assertIsInstance(f, abcmodule.TextIOBase)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003248
3249 def test_abc_inheritance(self):
3250 # Test implementations inherit from their respective ABCs
3251 self._check_abc_inheritance(self)
3252
3253 def test_abc_inheritance_official(self):
3254 # Test implementations inherit from the official ABCs of the
3255 # baseline "io" module.
3256 self._check_abc_inheritance(io)
3257
Antoine Pitroue033e062010-10-29 10:38:18 +00003258 def _check_warn_on_dealloc(self, *args, **kwargs):
3259 f = open(*args, **kwargs)
3260 r = repr(f)
3261 with self.assertWarns(ResourceWarning) as cm:
3262 f = None
3263 support.gc_collect()
3264 self.assertIn(r, str(cm.warning.args[0]))
3265
3266 def test_warn_on_dealloc(self):
3267 self._check_warn_on_dealloc(support.TESTFN, "wb", buffering=0)
3268 self._check_warn_on_dealloc(support.TESTFN, "wb")
3269 self._check_warn_on_dealloc(support.TESTFN, "w")
3270
3271 def _check_warn_on_dealloc_fd(self, *args, **kwargs):
3272 fds = []
Benjamin Peterson556c7352010-10-31 01:35:43 +00003273 def cleanup_fds():
Antoine Pitroue033e062010-10-29 10:38:18 +00003274 for fd in fds:
3275 try:
3276 os.close(fd)
Andrew Svetlov3438fa42012-12-17 23:35:18 +02003277 except OSError as e:
Antoine Pitroue033e062010-10-29 10:38:18 +00003278 if e.errno != errno.EBADF:
3279 raise
Benjamin Peterson556c7352010-10-31 01:35:43 +00003280 self.addCleanup(cleanup_fds)
3281 r, w = os.pipe()
3282 fds += r, w
3283 self._check_warn_on_dealloc(r, *args, **kwargs)
3284 # When using closefd=False, there's no warning
3285 r, w = os.pipe()
3286 fds += r, w
3287 with warnings.catch_warnings(record=True) as recorded:
3288 open(r, *args, closefd=False, **kwargs)
3289 support.gc_collect()
3290 self.assertEqual(recorded, [])
Antoine Pitroue033e062010-10-29 10:38:18 +00003291
3292 def test_warn_on_dealloc_fd(self):
3293 self._check_warn_on_dealloc_fd("rb", buffering=0)
3294 self._check_warn_on_dealloc_fd("rb")
3295 self._check_warn_on_dealloc_fd("r")
3296
3297
Antoine Pitrou243757e2010-11-05 21:15:39 +00003298 def test_pickling(self):
3299 # Pickling file objects is forbidden
3300 for kwargs in [
3301 {"mode": "w"},
3302 {"mode": "wb"},
3303 {"mode": "wb", "buffering": 0},
3304 {"mode": "r"},
3305 {"mode": "rb"},
3306 {"mode": "rb", "buffering": 0},
3307 {"mode": "w+"},
3308 {"mode": "w+b"},
3309 {"mode": "w+b", "buffering": 0},
3310 ]:
3311 for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
3312 with self.open(support.TESTFN, **kwargs) as f:
3313 self.assertRaises(TypeError, pickle.dumps, f, protocol)
3314
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01003315 def test_nonblock_pipe_write_bigbuf(self):
3316 self._test_nonblock_pipe_write(16*1024)
3317
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01003318 def test_nonblock_pipe_write_smallbuf(self):
3319 self._test_nonblock_pipe_write(1024)
3320
Victor Stinner1db9e7b2014-07-29 22:32:47 +02003321 @unittest.skipUnless(hasattr(os, 'set_blocking'),
3322 'os.set_blocking() required for this test')
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01003323 def _test_nonblock_pipe_write(self, bufsize):
3324 sent = []
3325 received = []
3326 r, w = os.pipe()
Victor Stinner1db9e7b2014-07-29 22:32:47 +02003327 os.set_blocking(r, False)
3328 os.set_blocking(w, False)
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01003329
3330 # To exercise all code paths in the C implementation we need
3331 # to play with buffer sizes. For instance, if we choose a
3332 # buffer size less than or equal to _PIPE_BUF (4096 on Linux)
3333 # then we will never get a partial write of the buffer.
3334 rf = self.open(r, mode='rb', closefd=True, buffering=bufsize)
3335 wf = self.open(w, mode='wb', closefd=True, buffering=bufsize)
3336
3337 with rf, wf:
3338 for N in 9999, 73, 7574:
3339 try:
3340 i = 0
3341 while True:
3342 msg = bytes([i % 26 + 97]) * N
3343 sent.append(msg)
3344 wf.write(msg)
3345 i += 1
3346
3347 except self.BlockingIOError as e:
3348 self.assertEqual(e.args[0], errno.EAGAIN)
Antoine Pitrou7fe601c2011-11-21 20:22:01 +01003349 self.assertEqual(e.args[2], e.characters_written)
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01003350 sent[-1] = sent[-1][:e.characters_written]
3351 received.append(rf.read())
3352 msg = b'BLOCKED'
3353 wf.write(msg)
3354 sent.append(msg)
3355
3356 while True:
3357 try:
3358 wf.flush()
3359 break
3360 except self.BlockingIOError as e:
3361 self.assertEqual(e.args[0], errno.EAGAIN)
Antoine Pitrou7fe601c2011-11-21 20:22:01 +01003362 self.assertEqual(e.args[2], e.characters_written)
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01003363 self.assertEqual(e.characters_written, 0)
3364 received.append(rf.read())
3365
3366 received += iter(rf.read, None)
3367
3368 sent, received = b''.join(sent), b''.join(received)
3369 self.assertTrue(sent == received)
3370 self.assertTrue(wf.closed)
3371 self.assertTrue(rf.closed)
3372
Charles-François Natalidc3044c2012-01-09 22:40:02 +01003373 def test_create_fail(self):
3374 # 'x' mode fails if file is existing
3375 with self.open(support.TESTFN, 'w'):
3376 pass
3377 self.assertRaises(FileExistsError, self.open, support.TESTFN, 'x')
3378
3379 def test_create_writes(self):
3380 # 'x' mode opens for writing
3381 with self.open(support.TESTFN, 'xb') as f:
3382 f.write(b"spam")
3383 with self.open(support.TESTFN, 'rb') as f:
3384 self.assertEqual(b"spam", f.read())
3385
Christian Heimes7b648752012-09-10 14:48:43 +02003386 def test_open_allargs(self):
3387 # there used to be a buffer overflow in the parser for rawmode
3388 self.assertRaises(ValueError, self.open, support.TESTFN, 'rwax+')
3389
3390
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003391class CMiscIOTest(MiscIOTest):
3392 io = io
3393
Serhiy Storchaka37a79a12013-05-28 16:24:45 +03003394 def test_readinto_buffer_overflow(self):
3395 # Issue #18025
3396 class BadReader(self.io.BufferedIOBase):
3397 def read(self, n=-1):
3398 return b'x' * 10**6
3399 bufio = BadReader()
3400 b = bytearray(2)
3401 self.assertRaises(ValueError, bufio.readinto, b)
3402
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003403class PyMiscIOTest(MiscIOTest):
3404 io = pyio
Barry Warsaw40e82462008-11-20 20:14:50 +00003405
Antoine Pitroub46b9d52010-08-21 19:09:32 +00003406
3407@unittest.skipIf(os.name == 'nt', 'POSIX signals required for this test.')
3408class SignalsTest(unittest.TestCase):
3409
3410 def setUp(self):
3411 self.oldalrm = signal.signal(signal.SIGALRM, self.alarm_interrupt)
3412
3413 def tearDown(self):
3414 signal.signal(signal.SIGALRM, self.oldalrm)
3415
3416 def alarm_interrupt(self, sig, frame):
3417 1/0
3418
3419 @unittest.skipUnless(threading, 'Threading required for this test.')
3420 def check_interrupted_write(self, item, bytes, **fdopen_kwargs):
3421 """Check that a partial write, when it gets interrupted, properly
Antoine Pitrou707ce822011-02-25 21:24:11 +00003422 invokes the signal handler, and bubbles up the exception raised
3423 in the latter."""
Antoine Pitroub46b9d52010-08-21 19:09:32 +00003424 read_results = []
3425 def _read():
Victor Stinnera9293352011-04-30 15:21:58 +02003426 if hasattr(signal, 'pthread_sigmask'):
3427 signal.pthread_sigmask(signal.SIG_BLOCK, [signal.SIGALRM])
Antoine Pitroub46b9d52010-08-21 19:09:32 +00003428 s = os.read(r, 1)
3429 read_results.append(s)
3430 t = threading.Thread(target=_read)
3431 t.daemon = True
3432 r, w = os.pipe()
Benjamin Petersond8fc2e12010-10-31 01:19:53 +00003433 fdopen_kwargs["closefd"] = False
Antoine Pitroub46b9d52010-08-21 19:09:32 +00003434 try:
3435 wio = self.io.open(w, **fdopen_kwargs)
3436 t.start()
Antoine Pitroub46b9d52010-08-21 19:09:32 +00003437 # Fill the pipe enough that the write will be blocking.
3438 # It will be interrupted by the timer armed above. Since the
3439 # other thread has read one byte, the low-level write will
3440 # return with a successful (partial) result rather than an EINTR.
3441 # The buffered IO layer must check for pending signal
3442 # handlers, which in this case will invoke alarm_interrupt().
Victor Stinner775b2dd2013-07-15 19:53:13 +02003443 signal.alarm(1)
3444 try:
3445 self.assertRaises(ZeroDivisionError,
3446 wio.write, item * (support.PIPE_MAX_SIZE // len(item) + 1))
3447 finally:
3448 signal.alarm(0)
Antoine Pitroub46b9d52010-08-21 19:09:32 +00003449 t.join()
3450 # We got one byte, get another one and check that it isn't a
3451 # repeat of the first one.
3452 read_results.append(os.read(r, 1))
3453 self.assertEqual(read_results, [bytes[0:1], bytes[1:2]])
3454 finally:
3455 os.close(w)
3456 os.close(r)
3457 # This is deliberate. If we didn't close the file descriptor
3458 # before closing wio, wio would try to flush its internal
3459 # buffer, and block again.
3460 try:
3461 wio.close()
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02003462 except OSError as e:
Antoine Pitroub46b9d52010-08-21 19:09:32 +00003463 if e.errno != errno.EBADF:
3464 raise
3465
3466 def test_interrupted_write_unbuffered(self):
3467 self.check_interrupted_write(b"xy", b"xy", mode="wb", buffering=0)
3468
3469 def test_interrupted_write_buffered(self):
3470 self.check_interrupted_write(b"xy", b"xy", mode="wb")
3471
Victor Stinner6ab72862014-09-03 23:32:28 +02003472 # Issue #22331: The test hangs on FreeBSD 7.2
3473 @support.requires_freebsd_version(8)
Antoine Pitroub46b9d52010-08-21 19:09:32 +00003474 def test_interrupted_write_text(self):
3475 self.check_interrupted_write("xy", b"xy", mode="w", encoding="ascii")
3476
Brett Cannon31f59292011-02-21 19:29:56 +00003477 @support.no_tracing
Antoine Pitrouf3b68b32010-12-03 18:41:39 +00003478 def check_reentrant_write(self, data, **fdopen_kwargs):
3479 def on_alarm(*args):
3480 # Will be called reentrantly from the same thread
3481 wio.write(data)
3482 1/0
3483 signal.signal(signal.SIGALRM, on_alarm)
3484 r, w = os.pipe()
3485 wio = self.io.open(w, **fdopen_kwargs)
3486 try:
Gregory P. Smithb5ba2032012-06-25 01:13:32 -07003487 signal.alarm(1)
Antoine Pitrouf3b68b32010-12-03 18:41:39 +00003488 # Either the reentrant call to wio.write() fails with RuntimeError,
3489 # or the signal handler raises ZeroDivisionError.
3490 with self.assertRaises((ZeroDivisionError, RuntimeError)) as cm:
3491 while 1:
3492 for i in range(100):
3493 wio.write(data)
3494 wio.flush()
3495 # Make sure the buffer doesn't fill up and block further writes
3496 os.read(r, len(data) * 100)
3497 exc = cm.exception
3498 if isinstance(exc, RuntimeError):
3499 self.assertTrue(str(exc).startswith("reentrant call"), str(exc))
3500 finally:
3501 wio.close()
3502 os.close(r)
3503
3504 def test_reentrant_write_buffered(self):
3505 self.check_reentrant_write(b"xy", mode="wb")
3506
3507 def test_reentrant_write_text(self):
3508 self.check_reentrant_write("xy", mode="w", encoding="ascii")
3509
Antoine Pitrou707ce822011-02-25 21:24:11 +00003510 def check_interrupted_read_retry(self, decode, **fdopen_kwargs):
3511 """Check that a buffered read, when it gets interrupted (either
3512 returning a partial result or EINTR), properly invokes the signal
3513 handler and retries if the latter returned successfully."""
3514 r, w = os.pipe()
3515 fdopen_kwargs["closefd"] = False
3516 def alarm_handler(sig, frame):
3517 os.write(w, b"bar")
3518 signal.signal(signal.SIGALRM, alarm_handler)
3519 try:
3520 rio = self.io.open(r, **fdopen_kwargs)
3521 os.write(w, b"foo")
Gregory P. Smithb5ba2032012-06-25 01:13:32 -07003522 signal.alarm(1)
Antoine Pitrou707ce822011-02-25 21:24:11 +00003523 # Expected behaviour:
3524 # - first raw read() returns partial b"foo"
3525 # - second raw read() returns EINTR
3526 # - third raw read() returns b"bar"
3527 self.assertEqual(decode(rio.read(6)), "foobar")
3528 finally:
3529 rio.close()
3530 os.close(w)
3531 os.close(r)
3532
Antoine Pitrou20db5112011-08-19 20:32:34 +02003533 def test_interrupted_read_retry_buffered(self):
Antoine Pitrou707ce822011-02-25 21:24:11 +00003534 self.check_interrupted_read_retry(lambda x: x.decode('latin1'),
3535 mode="rb")
3536
Antoine Pitrou20db5112011-08-19 20:32:34 +02003537 def test_interrupted_read_retry_text(self):
Antoine Pitrou707ce822011-02-25 21:24:11 +00003538 self.check_interrupted_read_retry(lambda x: x,
3539 mode="r")
3540
3541 @unittest.skipUnless(threading, 'Threading required for this test.')
3542 def check_interrupted_write_retry(self, item, **fdopen_kwargs):
3543 """Check that a buffered write, when it gets interrupted (either
3544 returning a partial result or EINTR), properly invokes the signal
3545 handler and retries if the latter returned successfully."""
3546 select = support.import_module("select")
3547 # A quantity that exceeds the buffer size of an anonymous pipe's
3548 # write end.
Antoine Pitroue1a16742013-04-24 23:31:38 +02003549 N = support.PIPE_MAX_SIZE
Antoine Pitrou707ce822011-02-25 21:24:11 +00003550 r, w = os.pipe()
3551 fdopen_kwargs["closefd"] = False
3552 # We need a separate thread to read from the pipe and allow the
3553 # write() to finish. This thread is started after the SIGALRM is
3554 # received (forcing a first EINTR in write()).
3555 read_results = []
3556 write_finished = False
3557 def _read():
3558 while not write_finished:
3559 while r in select.select([r], [], [], 1.0)[0]:
3560 s = os.read(r, 1024)
3561 read_results.append(s)
3562 t = threading.Thread(target=_read)
3563 t.daemon = True
3564 def alarm1(sig, frame):
3565 signal.signal(signal.SIGALRM, alarm2)
Gregory P. Smithb5ba2032012-06-25 01:13:32 -07003566 signal.alarm(1)
Antoine Pitrou707ce822011-02-25 21:24:11 +00003567 def alarm2(sig, frame):
3568 t.start()
3569 signal.signal(signal.SIGALRM, alarm1)
3570 try:
3571 wio = self.io.open(w, **fdopen_kwargs)
Gregory P. Smithb5ba2032012-06-25 01:13:32 -07003572 signal.alarm(1)
Antoine Pitrou707ce822011-02-25 21:24:11 +00003573 # Expected behaviour:
3574 # - first raw write() is partial (because of the limited pipe buffer
3575 # and the first alarm)
3576 # - second raw write() returns EINTR (because of the second alarm)
3577 # - subsequent write()s are successful (either partial or complete)
3578 self.assertEqual(N, wio.write(item * N))
3579 wio.flush()
3580 write_finished = True
3581 t.join()
3582 self.assertEqual(N, sum(len(x) for x in read_results))
3583 finally:
3584 write_finished = True
3585 os.close(w)
3586 os.close(r)
3587 # This is deliberate. If we didn't close the file descriptor
3588 # before closing wio, wio would try to flush its internal
3589 # buffer, and could block (in case of failure).
3590 try:
3591 wio.close()
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02003592 except OSError as e:
Antoine Pitrou707ce822011-02-25 21:24:11 +00003593 if e.errno != errno.EBADF:
3594 raise
3595
Antoine Pitrou20db5112011-08-19 20:32:34 +02003596 def test_interrupted_write_retry_buffered(self):
Antoine Pitrou707ce822011-02-25 21:24:11 +00003597 self.check_interrupted_write_retry(b"x", mode="wb")
3598
Antoine Pitrou20db5112011-08-19 20:32:34 +02003599 def test_interrupted_write_retry_text(self):
Antoine Pitrou707ce822011-02-25 21:24:11 +00003600 self.check_interrupted_write_retry("x", mode="w", encoding="latin1")
3601
Antoine Pitrouf3b68b32010-12-03 18:41:39 +00003602
Antoine Pitroub46b9d52010-08-21 19:09:32 +00003603class CSignalsTest(SignalsTest):
3604 io = io
3605
3606class PySignalsTest(SignalsTest):
3607 io = pyio
3608
Antoine Pitrouf3b68b32010-12-03 18:41:39 +00003609 # Handling reentrancy issues would slow down _pyio even more, so the
3610 # tests are disabled.
3611 test_reentrant_write_buffered = None
3612 test_reentrant_write_text = None
3613
Antoine Pitroub46b9d52010-08-21 19:09:32 +00003614
Ezio Melottidaa42c72013-03-23 16:30:16 +02003615def load_tests(*args):
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003616 tests = (CIOTest, PyIOTest,
3617 CBufferedReaderTest, PyBufferedReaderTest,
3618 CBufferedWriterTest, PyBufferedWriterTest,
3619 CBufferedRWPairTest, PyBufferedRWPairTest,
3620 CBufferedRandomTest, PyBufferedRandomTest,
3621 StatefulIncrementalDecoderTest,
3622 CIncrementalNewlineDecoderTest, PyIncrementalNewlineDecoderTest,
3623 CTextIOWrapperTest, PyTextIOWrapperTest,
Antoine Pitroub46b9d52010-08-21 19:09:32 +00003624 CMiscIOTest, PyMiscIOTest,
3625 CSignalsTest, PySignalsTest,
3626 )
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003627
3628 # Put the namespaces of the IO module we are testing and some useful mock
3629 # classes in the __dict__ of each test.
3630 mocks = (MockRawIO, MisbehavedRawIO, MockFileIO, CloseFailureIO,
Antoine Pitrou328ec742010-09-14 18:37:24 +00003631 MockNonBlockWriterIO, MockUnseekableIO, MockRawIOWithoutRead)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00003632 all_members = io.__all__ + ["IncrementalNewlineDecoder"]
3633 c_io_ns = {name : getattr(io, name) for name in all_members}
3634 py_io_ns = {name : getattr(pyio, name) for name in all_members}
3635 globs = globals()
3636 c_io_ns.update((x.__name__, globs["C" + x.__name__]) for x in mocks)
3637 py_io_ns.update((x.__name__, globs["Py" + x.__name__]) for x in mocks)
3638 # Avoid turning open into a bound method.
3639 py_io_ns["open"] = pyio.OpenWrapper
3640 for test in tests:
3641 if test.__name__.startswith("C"):
3642 for name, obj in c_io_ns.items():
3643 setattr(test, name, obj)
3644 elif test.__name__.startswith("Py"):
3645 for name, obj in py_io_ns.items():
3646 setattr(test, name, obj)
3647
Ezio Melottidaa42c72013-03-23 16:30:16 +02003648 suite = unittest.TestSuite([unittest.makeSuite(test) for test in tests])
3649 return suite
Guido van Rossum28524c72007-02-27 05:47:44 +00003650
3651if __name__ == "__main__":
Ezio Melottidaa42c72013-03-23 16:30:16 +02003652 unittest.main()