Guido van Rossum | de3bc7c | 2007-03-08 01:02:00 +0000 | [diff] [blame] | 1 | # Adapted from test_file.py by Daniel Stutzbach |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 2 | |
| 3 | import sys |
| 4 | import os |
Eli Bendersky | 74c503b | 2012-01-03 06:26:13 +0200 | [diff] [blame] | 5 | import io |
Antoine Pitrou | 0ae29cf | 2009-03-13 22:33:17 +0000 | [diff] [blame] | 6 | import errno |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 7 | import unittest |
| 8 | from array import array |
| 9 | from weakref import proxy |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 10 | from functools import wraps |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 11 | |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 12 | from test.support import TESTFN, check_warnings, run_unittest, make_bad_fd, cpython_only |
Antoine Pitrou | 131a489 | 2012-10-16 22:57:11 +0200 | [diff] [blame] | 13 | from collections import UserList |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 14 | |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 15 | import _io # C implementation of io |
| 16 | import _pyio # Python implementation of io |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 17 | |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 18 | |
| 19 | class AutoFileTests: |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 20 | # file tests for which a test file is automatically set up |
| 21 | |
| 22 | def setUp(self): |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 23 | self.f = self.FileIO(TESTFN, 'w') |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 24 | |
| 25 | def tearDown(self): |
| 26 | if self.f: |
| 27 | self.f.close() |
| 28 | os.remove(TESTFN) |
| 29 | |
| 30 | def testWeakRefs(self): |
| 31 | # verify weak references |
| 32 | p = proxy(self.f) |
| 33 | p.write(bytes(range(10))) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 34 | self.assertEqual(self.f.tell(), p.tell()) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 35 | self.f.close() |
| 36 | self.f = None |
| 37 | self.assertRaises(ReferenceError, getattr, p, 'tell') |
| 38 | |
| 39 | def testSeekTell(self): |
| 40 | self.f.write(bytes(range(20))) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 41 | self.assertEqual(self.f.tell(), 20) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 42 | self.f.seek(0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 43 | self.assertEqual(self.f.tell(), 0) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 44 | self.f.seek(10) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 45 | self.assertEqual(self.f.tell(), 10) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 46 | self.f.seek(5, 1) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 47 | self.assertEqual(self.f.tell(), 15) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 48 | self.f.seek(-5, 1) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 49 | self.assertEqual(self.f.tell(), 10) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 50 | self.f.seek(-5, 2) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 51 | self.assertEqual(self.f.tell(), 15) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 52 | |
| 53 | def testAttributes(self): |
| 54 | # verify expected attributes exist |
| 55 | f = self.f |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 56 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 57 | self.assertEqual(f.mode, "wb") |
| 58 | self.assertEqual(f.closed, False) |
Guido van Rossum | 31c3a57 | 2007-04-12 14:51:49 +0000 | [diff] [blame] | 59 | |
| 60 | # verify the attributes are readonly |
| 61 | for attr in 'mode', 'closed': |
| 62 | self.assertRaises((AttributeError, TypeError), |
| 63 | setattr, f, attr, 'oops') |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 64 | |
Antoine Pitrou | de68722 | 2014-06-29 20:07:28 -0400 | [diff] [blame] | 65 | def testBlksize(self): |
| 66 | # test private _blksize attribute |
| 67 | blksize = io.DEFAULT_BUFFER_SIZE |
| 68 | # try to get preferred blksize from stat.st_blksize, if available |
| 69 | if hasattr(os, 'fstat'): |
| 70 | fst = os.fstat(self.f.fileno()) |
| 71 | blksize = getattr(fst, 'st_blksize', blksize) |
| 72 | self.assertEqual(self.f._blksize, blksize) |
| 73 | |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 74 | # verify readinto |
| 75 | def testReadintoByteArray(self): |
| 76 | self.f.write(bytes([1, 2, 0, 255])) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 77 | self.f.close() |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 78 | |
| 79 | ba = bytearray(b'abcdefgh') |
| 80 | with self.FileIO(TESTFN, 'r') as f: |
| 81 | n = f.readinto(ba) |
| 82 | self.assertEqual(ba, b'\x01\x02\x00\xffefgh') |
| 83 | self.assertEqual(n, 4) |
| 84 | |
| 85 | def _testReadintoMemoryview(self): |
| 86 | self.f.write(bytes([1, 2, 0, 255])) |
| 87 | self.f.close() |
| 88 | |
| 89 | m = memoryview(bytearray(b'abcdefgh')) |
| 90 | with self.FileIO(TESTFN, 'r') as f: |
| 91 | n = f.readinto(m) |
| 92 | self.assertEqual(m, b'\x01\x02\x00\xffefgh') |
| 93 | self.assertEqual(n, 4) |
| 94 | |
| 95 | m = memoryview(bytearray(b'abcdefgh')).cast('H', shape=[2, 2]) |
| 96 | with self.FileIO(TESTFN, 'r') as f: |
| 97 | n = f.readinto(m) |
| 98 | self.assertEqual(bytes(m), b'\x01\x02\x00\xffefgh') |
| 99 | self.assertEqual(n, 4) |
| 100 | |
| 101 | def _testReadintoArray(self): |
| 102 | self.f.write(bytes([1, 2, 0, 255])) |
| 103 | self.f.close() |
| 104 | |
| 105 | a = array('B', b'abcdefgh') |
| 106 | with self.FileIO(TESTFN, 'r') as f: |
| 107 | n = f.readinto(a) |
| 108 | self.assertEqual(a, array('B', [1, 2, 0, 255, 101, 102, 103, 104])) |
| 109 | self.assertEqual(n, 4) |
| 110 | |
| 111 | a = array('b', b'abcdefgh') |
| 112 | with self.FileIO(TESTFN, 'r') as f: |
| 113 | n = f.readinto(a) |
| 114 | self.assertEqual(a, array('b', [1, 2, 0, -1, 101, 102, 103, 104])) |
| 115 | self.assertEqual(n, 4) |
| 116 | |
| 117 | a = array('I', b'abcdefgh') |
| 118 | with self.FileIO(TESTFN, 'r') as f: |
| 119 | n = f.readinto(a) |
| 120 | self.assertEqual(a, array('I', b'\x01\x02\x00\xffefgh')) |
| 121 | self.assertEqual(n, 4) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 122 | |
Antoine Pitrou | 131a489 | 2012-10-16 22:57:11 +0200 | [diff] [blame] | 123 | def testWritelinesList(self): |
| 124 | l = [b'123', b'456'] |
| 125 | self.f.writelines(l) |
| 126 | self.f.close() |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 127 | self.f = self.FileIO(TESTFN, 'rb') |
Antoine Pitrou | 131a489 | 2012-10-16 22:57:11 +0200 | [diff] [blame] | 128 | buf = self.f.read() |
| 129 | self.assertEqual(buf, b'123456') |
| 130 | |
| 131 | def testWritelinesUserList(self): |
| 132 | l = UserList([b'123', b'456']) |
| 133 | self.f.writelines(l) |
| 134 | self.f.close() |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 135 | self.f = self.FileIO(TESTFN, 'rb') |
Antoine Pitrou | 131a489 | 2012-10-16 22:57:11 +0200 | [diff] [blame] | 136 | buf = self.f.read() |
| 137 | self.assertEqual(buf, b'123456') |
| 138 | |
| 139 | def testWritelinesError(self): |
| 140 | self.assertRaises(TypeError, self.f.writelines, [1, 2, 3]) |
| 141 | self.assertRaises(TypeError, self.f.writelines, None) |
| 142 | self.assertRaises(TypeError, self.f.writelines, "abc") |
| 143 | |
Benjamin Peterson | bf5ff76 | 2009-12-13 19:25:34 +0000 | [diff] [blame] | 144 | def test_none_args(self): |
| 145 | self.f.write(b"hi\nbye\nabc") |
| 146 | self.f.close() |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 147 | self.f = self.FileIO(TESTFN, 'r') |
Benjamin Peterson | bf5ff76 | 2009-12-13 19:25:34 +0000 | [diff] [blame] | 148 | self.assertEqual(self.f.read(None), b"hi\nbye\nabc") |
| 149 | self.f.seek(0) |
| 150 | self.assertEqual(self.f.readline(None), b"hi\n") |
| 151 | self.assertEqual(self.f.readlines(None), [b"bye\n", b"abc"]) |
| 152 | |
Benjamin Peterson | 255058f | 2010-01-27 01:47:14 +0000 | [diff] [blame] | 153 | def test_reject(self): |
| 154 | self.assertRaises(TypeError, self.f.write, "Hello!") |
| 155 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 156 | def testRepr(self): |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 157 | self.assertEqual(repr(self.f), |
| 158 | "<%s.FileIO name=%r mode=%r closefd=True>" % |
| 159 | (self.modulename, self.f.name, self.f.mode)) |
Antoine Pitrou | 716c444 | 2009-05-23 19:04:03 +0000 | [diff] [blame] | 160 | del self.f.name |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 161 | self.assertEqual(repr(self.f), |
| 162 | "<%s.FileIO fd=%r mode=%r closefd=True>" % |
| 163 | (self.modulename, self.f.fileno(), self.f.mode)) |
Antoine Pitrou | 716c444 | 2009-05-23 19:04:03 +0000 | [diff] [blame] | 164 | self.f.close() |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 165 | self.assertEqual(repr(self.f), |
| 166 | "<%s.FileIO [closed]>" % (self.modulename,)) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 167 | |
Serhiy Storchaka | 4954f9f | 2014-12-02 23:39:56 +0200 | [diff] [blame] | 168 | def testReprNoCloseFD(self): |
| 169 | fd = os.open(TESTFN, os.O_RDONLY) |
| 170 | try: |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 171 | with self.FileIO(fd, 'r', closefd=False) as f: |
Serhiy Storchaka | 4954f9f | 2014-12-02 23:39:56 +0200 | [diff] [blame] | 172 | self.assertEqual(repr(f), |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 173 | "<%s.FileIO name=%r mode=%r closefd=False>" % |
| 174 | (self.modulename, f.name, f.mode)) |
Serhiy Storchaka | 4954f9f | 2014-12-02 23:39:56 +0200 | [diff] [blame] | 175 | finally: |
| 176 | os.close(fd) |
| 177 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 178 | def testErrors(self): |
| 179 | f = self.f |
Serhiy Storchaka | 0dcd80a | 2015-08-02 15:17:49 +0300 | [diff] [blame] | 180 | self.assertFalse(f.isatty()) |
| 181 | self.assertFalse(f.closed) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 182 | #self.assertEqual(f.name, TESTFN) |
Guido van Rossum | 31c3a57 | 2007-04-12 14:51:49 +0000 | [diff] [blame] | 183 | self.assertRaises(ValueError, f.read, 10) # Open for reading |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 184 | f.close() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 185 | self.assertTrue(f.closed) |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 186 | f = self.FileIO(TESTFN, 'r') |
Guido van Rossum | 31c3a57 | 2007-04-12 14:51:49 +0000 | [diff] [blame] | 187 | self.assertRaises(TypeError, f.readinto, "") |
Serhiy Storchaka | 0dcd80a | 2015-08-02 15:17:49 +0300 | [diff] [blame] | 188 | self.assertFalse(f.closed) |
Guido van Rossum | 31c3a57 | 2007-04-12 14:51:49 +0000 | [diff] [blame] | 189 | f.close() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 190 | self.assertTrue(f.closed) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 191 | |
| 192 | def testMethods(self): |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 193 | methods = ['fileno', 'isatty', 'seekable', 'readable', 'writable', |
| 194 | 'read', 'readall', 'readline', 'readlines', |
| 195 | 'tell', 'truncate', 'flush'] |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 196 | |
Guido van Rossum | 31c3a57 | 2007-04-12 14:51:49 +0000 | [diff] [blame] | 197 | self.f.close() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 198 | self.assertTrue(self.f.closed) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 199 | |
| 200 | for methodname in methods: |
| 201 | method = getattr(self.f, methodname) |
| 202 | # should raise on closed file |
| 203 | self.assertRaises(ValueError, method) |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 204 | |
| 205 | self.assertRaises(TypeError, self.f.readinto) |
| 206 | self.assertRaises(ValueError, self.f.readinto, bytearray(1)) |
| 207 | self.assertRaises(TypeError, self.f.seek) |
| 208 | self.assertRaises(ValueError, self.f.seek, 0) |
| 209 | self.assertRaises(TypeError, self.f.write) |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 210 | self.assertRaises(ValueError, self.f.write, b'') |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 211 | self.assertRaises(TypeError, self.f.writelines) |
| 212 | self.assertRaises(ValueError, self.f.writelines, b'') |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 213 | |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 214 | def testOpendir(self): |
| 215 | # Issue 3703: opening a directory should fill the errno |
| 216 | # Windows always returns "[Errno 13]: Permission denied |
Antoine Pitrou | de68722 | 2014-06-29 20:07:28 -0400 | [diff] [blame] | 217 | # Unix uses fstat and returns "[Errno 21]: Is a directory" |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 218 | try: |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 219 | self.FileIO('.', 'r') |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 220 | except OSError as e: |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 221 | self.assertNotEqual(e.errno, 0) |
Benjamin Peterson | 1efc23c | 2008-12-29 18:02:28 +0000 | [diff] [blame] | 222 | self.assertEqual(e.filename, ".") |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 223 | else: |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 224 | self.fail("Should have raised OSError") |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 225 | |
Antoine Pitrou | 9235b25 | 2012-07-06 18:48:24 +0200 | [diff] [blame] | 226 | @unittest.skipIf(os.name == 'nt', "test only works on a POSIX-like system") |
| 227 | def testOpenDirFD(self): |
| 228 | fd = os.open('.', os.O_RDONLY) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 229 | with self.assertRaises(OSError) as cm: |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 230 | self.FileIO(fd, 'r') |
Antoine Pitrou | 9235b25 | 2012-07-06 18:48:24 +0200 | [diff] [blame] | 231 | os.close(fd) |
| 232 | self.assertEqual(cm.exception.errno, errno.EISDIR) |
| 233 | |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 234 | #A set of functions testing that we get expected behaviour if someone has |
| 235 | #manually closed the internal file descriptor. First, a decorator: |
| 236 | def ClosedFD(func): |
| 237 | @wraps(func) |
| 238 | def wrapper(self): |
| 239 | #forcibly close the fd before invoking the problem function |
| 240 | f = self.f |
| 241 | os.close(f.fileno()) |
| 242 | try: |
| 243 | func(self, f) |
| 244 | finally: |
| 245 | try: |
| 246 | self.f.close() |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 247 | except OSError: |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 248 | pass |
| 249 | return wrapper |
Antoine Pitrou | 0ae29cf | 2009-03-13 22:33:17 +0000 | [diff] [blame] | 250 | |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 251 | def ClosedFDRaises(func): |
| 252 | @wraps(func) |
| 253 | def wrapper(self): |
| 254 | #forcibly close the fd before invoking the problem function |
| 255 | f = self.f |
| 256 | os.close(f.fileno()) |
| 257 | try: |
| 258 | func(self, f) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 259 | except OSError as e: |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 260 | self.assertEqual(e.errno, errno.EBADF) |
| 261 | else: |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 262 | self.fail("Should have raised OSError") |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 263 | finally: |
| 264 | try: |
| 265 | self.f.close() |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 266 | except OSError: |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 267 | pass |
| 268 | return wrapper |
| 269 | |
| 270 | @ClosedFDRaises |
| 271 | def testErrnoOnClose(self, f): |
| 272 | f.close() |
| 273 | |
| 274 | @ClosedFDRaises |
| 275 | def testErrnoOnClosedWrite(self, f): |
Benjamin Peterson | 255058f | 2010-01-27 01:47:14 +0000 | [diff] [blame] | 276 | f.write(b'a') |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 277 | |
| 278 | @ClosedFDRaises |
| 279 | def testErrnoOnClosedSeek(self, f): |
| 280 | f.seek(0) |
| 281 | |
| 282 | @ClosedFDRaises |
| 283 | def testErrnoOnClosedTell(self, f): |
| 284 | f.tell() |
| 285 | |
| 286 | @ClosedFDRaises |
| 287 | def testErrnoOnClosedTruncate(self, f): |
| 288 | f.truncate(0) |
| 289 | |
| 290 | @ClosedFD |
| 291 | def testErrnoOnClosedSeekable(self, f): |
| 292 | f.seekable() |
| 293 | |
| 294 | @ClosedFD |
| 295 | def testErrnoOnClosedReadable(self, f): |
| 296 | f.readable() |
| 297 | |
| 298 | @ClosedFD |
| 299 | def testErrnoOnClosedWritable(self, f): |
| 300 | f.writable() |
| 301 | |
| 302 | @ClosedFD |
| 303 | def testErrnoOnClosedFileno(self, f): |
| 304 | f.fileno() |
| 305 | |
| 306 | @ClosedFD |
| 307 | def testErrnoOnClosedIsatty(self, f): |
| 308 | self.assertEqual(f.isatty(), False) |
| 309 | |
| 310 | def ReopenForRead(self): |
| 311 | try: |
| 312 | self.f.close() |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 313 | except OSError: |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 314 | pass |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 315 | self.f = self.FileIO(TESTFN, 'r') |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 316 | os.close(self.f.fileno()) |
| 317 | return self.f |
| 318 | |
| 319 | @ClosedFDRaises |
| 320 | def testErrnoOnClosedRead(self, f): |
| 321 | f = self.ReopenForRead() |
| 322 | f.read(1) |
| 323 | |
| 324 | @ClosedFDRaises |
| 325 | def testErrnoOnClosedReadall(self, f): |
| 326 | f = self.ReopenForRead() |
| 327 | f.readall() |
| 328 | |
| 329 | @ClosedFDRaises |
| 330 | def testErrnoOnClosedReadinto(self, f): |
| 331 | f = self.ReopenForRead() |
| 332 | a = array('b', b'x'*10) |
| 333 | f.readinto(a) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 334 | |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 335 | class CAutoFileTests(AutoFileTests, unittest.TestCase): |
| 336 | FileIO = _io.FileIO |
| 337 | modulename = '_io' |
| 338 | |
| 339 | class PyAutoFileTests(AutoFileTests, unittest.TestCase): |
| 340 | FileIO = _pyio.FileIO |
| 341 | modulename = '_pyio' |
| 342 | |
| 343 | |
| 344 | class OtherFileTests: |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 345 | |
| 346 | def testAbles(self): |
| 347 | try: |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 348 | f = self.FileIO(TESTFN, "w") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 349 | self.assertEqual(f.readable(), False) |
| 350 | self.assertEqual(f.writable(), True) |
| 351 | self.assertEqual(f.seekable(), True) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 352 | f.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 353 | |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 354 | f = self.FileIO(TESTFN, "r") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 355 | self.assertEqual(f.readable(), True) |
| 356 | self.assertEqual(f.writable(), False) |
| 357 | self.assertEqual(f.seekable(), True) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 358 | f.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 359 | |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 360 | f = self.FileIO(TESTFN, "a+") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 361 | self.assertEqual(f.readable(), True) |
| 362 | self.assertEqual(f.writable(), True) |
| 363 | self.assertEqual(f.seekable(), True) |
| 364 | self.assertEqual(f.isatty(), False) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 365 | f.close() |
Guido van Rossum | 682faf8 | 2007-04-12 14:56:58 +0000 | [diff] [blame] | 366 | |
Thomas Heller | b0f48ab | 2007-07-12 11:29:02 +0000 | [diff] [blame] | 367 | if sys.platform != "win32": |
| 368 | try: |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 369 | f = self.FileIO("/dev/tty", "a") |
Andrew Svetlov | 3438fa4 | 2012-12-17 23:35:18 +0200 | [diff] [blame] | 370 | except OSError: |
Thomas Heller | b0f48ab | 2007-07-12 11:29:02 +0000 | [diff] [blame] | 371 | # When run in a cron job there just aren't any |
| 372 | # ttys, so skip the test. This also handles other |
| 373 | # OS'es that don't support /dev/tty. |
| 374 | pass |
| 375 | else: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 376 | self.assertEqual(f.readable(), False) |
| 377 | self.assertEqual(f.writable(), True) |
Hye-Shik Chang | caf871a | 2007-08-13 13:21:33 +0000 | [diff] [blame] | 378 | if sys.platform != "darwin" and \ |
Antoine Pitrou | d7b3046 | 2009-05-23 16:34:50 +0000 | [diff] [blame] | 379 | 'bsd' not in sys.platform and \ |
Benjamin Peterson | 94fe10f | 2008-07-17 23:27:26 +0000 | [diff] [blame] | 380 | not sys.platform.startswith('sunos'): |
Hye-Shik Chang | caf871a | 2007-08-13 13:21:33 +0000 | [diff] [blame] | 381 | # Somehow /dev/tty appears seekable on some BSDs |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 382 | self.assertEqual(f.seekable(), False) |
| 383 | self.assertEqual(f.isatty(), True) |
Thomas Heller | b0f48ab | 2007-07-12 11:29:02 +0000 | [diff] [blame] | 384 | f.close() |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 385 | finally: |
| 386 | os.unlink(TESTFN) |
| 387 | |
Antoine Pitrou | e93b63b | 2013-09-04 20:46:33 +0200 | [diff] [blame] | 388 | def testInvalidModeStrings(self): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 389 | # check invalid mode strings |
Benjamin Peterson | 44309e6 | 2008-11-22 00:41:45 +0000 | [diff] [blame] | 390 | for mode in ("", "aU", "wU+", "rw", "rt"): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 391 | try: |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 392 | f = self.FileIO(TESTFN, mode) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 393 | except ValueError: |
| 394 | pass |
| 395 | else: |
| 396 | f.close() |
| 397 | self.fail('%r is an invalid file mode' % mode) |
| 398 | |
Antoine Pitrou | e93b63b | 2013-09-04 20:46:33 +0200 | [diff] [blame] | 399 | def testModeStrings(self): |
| 400 | # test that the mode attribute is correct for various mode strings |
| 401 | # given as init args |
| 402 | try: |
| 403 | for modes in [('w', 'wb'), ('wb', 'wb'), ('wb+', 'rb+'), |
| 404 | ('w+b', 'rb+'), ('a', 'ab'), ('ab', 'ab'), |
| 405 | ('ab+', 'ab+'), ('a+b', 'ab+'), ('r', 'rb'), |
| 406 | ('rb', 'rb'), ('rb+', 'rb+'), ('r+b', 'rb+')]: |
| 407 | # read modes are last so that TESTFN will exist first |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 408 | with self.FileIO(TESTFN, modes[0]) as f: |
Antoine Pitrou | e93b63b | 2013-09-04 20:46:33 +0200 | [diff] [blame] | 409 | self.assertEqual(f.mode, modes[1]) |
| 410 | finally: |
| 411 | if os.path.exists(TESTFN): |
| 412 | os.unlink(TESTFN) |
| 413 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 414 | def testUnicodeOpen(self): |
| 415 | # verify repr works for unicode too |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 416 | f = self.FileIO(str(TESTFN), "w") |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 417 | f.close() |
| 418 | os.unlink(TESTFN) |
| 419 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 420 | def testBytesOpen(self): |
| 421 | # Opening a bytes filename |
| 422 | try: |
| 423 | fn = TESTFN.encode("ascii") |
| 424 | except UnicodeEncodeError: |
Zachary Ware | 9fe6d86 | 2013-12-08 00:20:35 -0600 | [diff] [blame] | 425 | self.skipTest('could not encode %r to ascii' % TESTFN) |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 426 | f = self.FileIO(fn, "w") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 427 | try: |
| 428 | f.write(b"abc") |
| 429 | f.close() |
| 430 | with open(TESTFN, "rb") as f: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 431 | self.assertEqual(f.read(), b"abc") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 432 | finally: |
| 433 | os.unlink(TESTFN) |
| 434 | |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 435 | def testConstructorHandlesNULChars(self): |
| 436 | fn_with_NUL = 'foo\0bar' |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 437 | self.assertRaises(ValueError, self.FileIO, fn_with_NUL, 'w') |
| 438 | self.assertRaises(ValueError, self.FileIO, bytes(fn_with_NUL, 'ascii'), 'w') |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 439 | |
Benjamin Peterson | 806d402 | 2009-01-19 15:11:51 +0000 | [diff] [blame] | 440 | def testInvalidFd(self): |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 441 | self.assertRaises(ValueError, self.FileIO, -10) |
| 442 | self.assertRaises(OSError, self.FileIO, make_bad_fd()) |
Antoine Pitrou | 0049249 | 2010-09-04 20:53:29 +0000 | [diff] [blame] | 443 | if sys.platform == 'win32': |
| 444 | import msvcrt |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 445 | self.assertRaises(OSError, msvcrt.get_osfhandle, make_bad_fd()) |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 446 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 447 | def testBadModeArgument(self): |
| 448 | # verify that we get a sensible error message for bad mode argument |
| 449 | bad_mode = "qwerty" |
| 450 | try: |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 451 | f = self.FileIO(TESTFN, bad_mode) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 452 | except ValueError as msg: |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 453 | if msg.args[0] != 0: |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 454 | s = str(msg) |
Ezio Melotti | 7fb4da7 | 2010-03-18 12:29:13 +0000 | [diff] [blame] | 455 | if TESTFN in s or bad_mode not in s: |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 456 | self.fail("bad error message for invalid mode: %s" % s) |
Georg Brandl | 50da60c | 2008-01-06 21:38:54 +0000 | [diff] [blame] | 457 | # if msg.args[0] == 0, we're probably on Windows where there may be |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 458 | # no obvious way to discover why open() failed. |
| 459 | else: |
| 460 | f.close() |
| 461 | self.fail("no error for invalid mode: %s" % bad_mode) |
| 462 | |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 463 | def testTruncate(self): |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 464 | f = self.FileIO(TESTFN, 'w') |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 465 | f.write(bytes(bytearray(range(10)))) |
| 466 | self.assertEqual(f.tell(), 10) |
| 467 | f.truncate(5) |
| 468 | self.assertEqual(f.tell(), 10) |
Eli Bendersky | 74c503b | 2012-01-03 06:26:13 +0200 | [diff] [blame] | 469 | self.assertEqual(f.seek(0, io.SEEK_END), 5) |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 470 | f.truncate(15) |
| 471 | self.assertEqual(f.tell(), 5) |
Eli Bendersky | 74c503b | 2012-01-03 06:26:13 +0200 | [diff] [blame] | 472 | self.assertEqual(f.seek(0, io.SEEK_END), 15) |
Antoine Pitrou | 8d2b51b | 2010-10-30 16:19:14 +0000 | [diff] [blame] | 473 | f.close() |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 474 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 475 | def testTruncateOnWindows(self): |
| 476 | def bug801631(): |
| 477 | # SF bug <http://www.python.org/sf/801631> |
| 478 | # "file.truncate fault on windows" |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 479 | f = self.FileIO(TESTFN, 'w') |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 480 | f.write(bytes(range(11))) |
| 481 | f.close() |
| 482 | |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 483 | f = self.FileIO(TESTFN,'r+') |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 484 | data = f.read(5) |
| 485 | if data != bytes(range(5)): |
| 486 | self.fail("Read on file opened for update failed %r" % data) |
| 487 | if f.tell() != 5: |
| 488 | self.fail("File pos after read wrong %d" % f.tell()) |
| 489 | |
| 490 | f.truncate() |
| 491 | if f.tell() != 5: |
| 492 | self.fail("File pos after ftruncate wrong %d" % f.tell()) |
| 493 | |
| 494 | f.close() |
| 495 | size = os.path.getsize(TESTFN) |
| 496 | if size != 5: |
| 497 | self.fail("File size after ftruncate wrong %d" % size) |
| 498 | |
| 499 | try: |
| 500 | bug801631() |
| 501 | finally: |
| 502 | os.unlink(TESTFN) |
| 503 | |
Walter Dörwald | 3a77c7a | 2007-06-06 16:31:14 +0000 | [diff] [blame] | 504 | def testAppend(self): |
| 505 | try: |
| 506 | f = open(TESTFN, 'wb') |
| 507 | f.write(b'spam') |
| 508 | f.close() |
| 509 | f = open(TESTFN, 'ab') |
| 510 | f.write(b'eggs') |
| 511 | f.close() |
| 512 | f = open(TESTFN, 'rb') |
| 513 | d = f.read() |
| 514 | f.close() |
| 515 | self.assertEqual(d, b'spameggs') |
| 516 | finally: |
| 517 | try: |
| 518 | os.unlink(TESTFN) |
| 519 | except: |
| 520 | pass |
| 521 | |
Neal Norwitz | 6e0e0e6 | 2008-08-24 22:07:28 +0000 | [diff] [blame] | 522 | def testInvalidInit(self): |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 523 | self.assertRaises(TypeError, self.FileIO, "1", 0, 0) |
Neal Norwitz | 6e0e0e6 | 2008-08-24 22:07:28 +0000 | [diff] [blame] | 524 | |
Benjamin Peterson | 65676e4 | 2008-11-05 21:42:45 +0000 | [diff] [blame] | 525 | def testWarnings(self): |
Florent Xicluna | b14930c | 2010-03-13 15:26:44 +0000 | [diff] [blame] | 526 | with check_warnings(quiet=True) as w: |
Benjamin Peterson | 65676e4 | 2008-11-05 21:42:45 +0000 | [diff] [blame] | 527 | self.assertEqual(w.warnings, []) |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 528 | self.assertRaises(TypeError, self.FileIO, []) |
Benjamin Peterson | 65676e4 | 2008-11-05 21:42:45 +0000 | [diff] [blame] | 529 | self.assertEqual(w.warnings, []) |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 530 | self.assertRaises(ValueError, self.FileIO, "/some/invalid/name", "rt") |
Benjamin Peterson | 65676e4 | 2008-11-05 21:42:45 +0000 | [diff] [blame] | 531 | self.assertEqual(w.warnings, []) |
| 532 | |
Hynek Schlawack | 9ed8b4e | 2012-06-21 20:20:25 +0200 | [diff] [blame] | 533 | def testUnclosedFDOnException(self): |
| 534 | class MyException(Exception): pass |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 535 | class MyFileIO(self.FileIO): |
Hynek Schlawack | 9ed8b4e | 2012-06-21 20:20:25 +0200 | [diff] [blame] | 536 | def __setattr__(self, name, value): |
| 537 | if name == "name": |
| 538 | raise MyException("blocked setting name") |
| 539 | return super(MyFileIO, self).__setattr__(name, value) |
| 540 | fd = os.open(__file__, os.O_RDONLY) |
| 541 | self.assertRaises(MyException, MyFileIO, fd) |
| 542 | os.close(fd) # should not raise OSError(EBADF) |
| 543 | |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 544 | class COtherFileTests(OtherFileTests, unittest.TestCase): |
| 545 | FileIO = _io.FileIO |
| 546 | modulename = '_io' |
| 547 | |
| 548 | @cpython_only |
| 549 | def testInvalidFd_overflow(self): |
| 550 | # Issue 15989 |
| 551 | import _testcapi |
| 552 | self.assertRaises(TypeError, self.FileIO, _testcapi.INT_MAX + 1) |
| 553 | self.assertRaises(TypeError, self.FileIO, _testcapi.INT_MIN - 1) |
| 554 | |
| 555 | class PyOtherFileTests(OtherFileTests, unittest.TestCase): |
| 556 | FileIO = _pyio.FileIO |
| 557 | modulename = '_pyio' |
| 558 | |
Neal Norwitz | 6e0e0e6 | 2008-08-24 22:07:28 +0000 | [diff] [blame] | 559 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 560 | def test_main(): |
| 561 | # Historically, these tests have been sloppy about removing TESTFN. |
| 562 | # So get rid of it no matter what. |
| 563 | try: |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 564 | run_unittest(CAutoFileTests, PyAutoFileTests, |
| 565 | COtherFileTests, PyOtherFileTests) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 566 | finally: |
| 567 | if os.path.exists(TESTFN): |
| 568 | os.unlink(TESTFN) |
| 569 | |
| 570 | if __name__ == '__main__': |
| 571 | test_main() |