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 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 180 | self.assertTrue(not f.isatty()) |
| 181 | self.assertTrue(not 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, "") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 188 | self.assertTrue(not 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 | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 193 | methods = ['fileno', 'isatty', 'read', |
| 194 | 'tell', 'truncate', 'seekable', |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 195 | 'readable', 'writable'] |
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 | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 204 | self.assertRaises(ValueError, self.f.readinto, bytearray()) |
| 205 | self.assertRaises(ValueError, self.f.seek, 0, os.SEEK_CUR) |
| 206 | self.assertRaises(ValueError, self.f.write, b'') |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 207 | |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 208 | def testOpendir(self): |
| 209 | # Issue 3703: opening a directory should fill the errno |
| 210 | # Windows always returns "[Errno 13]: Permission denied |
Antoine Pitrou | de68722 | 2014-06-29 20:07:28 -0400 | [diff] [blame] | 211 | # Unix uses fstat and returns "[Errno 21]: Is a directory" |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 212 | try: |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 213 | self.FileIO('.', 'r') |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 214 | except OSError as e: |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 215 | self.assertNotEqual(e.errno, 0) |
Benjamin Peterson | 1efc23c | 2008-12-29 18:02:28 +0000 | [diff] [blame] | 216 | self.assertEqual(e.filename, ".") |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 217 | else: |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 218 | self.fail("Should have raised OSError") |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 219 | |
Antoine Pitrou | 9235b25 | 2012-07-06 18:48:24 +0200 | [diff] [blame] | 220 | @unittest.skipIf(os.name == 'nt', "test only works on a POSIX-like system") |
| 221 | def testOpenDirFD(self): |
| 222 | fd = os.open('.', os.O_RDONLY) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 223 | with self.assertRaises(OSError) as cm: |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 224 | self.FileIO(fd, 'r') |
Antoine Pitrou | 9235b25 | 2012-07-06 18:48:24 +0200 | [diff] [blame] | 225 | os.close(fd) |
| 226 | self.assertEqual(cm.exception.errno, errno.EISDIR) |
| 227 | |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 228 | #A set of functions testing that we get expected behaviour if someone has |
| 229 | #manually closed the internal file descriptor. First, a decorator: |
| 230 | def ClosedFD(func): |
| 231 | @wraps(func) |
| 232 | def wrapper(self): |
| 233 | #forcibly close the fd before invoking the problem function |
| 234 | f = self.f |
| 235 | os.close(f.fileno()) |
| 236 | try: |
| 237 | func(self, f) |
| 238 | finally: |
| 239 | try: |
| 240 | self.f.close() |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 241 | except OSError: |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 242 | pass |
| 243 | return wrapper |
Antoine Pitrou | 0ae29cf | 2009-03-13 22:33:17 +0000 | [diff] [blame] | 244 | |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 245 | def ClosedFDRaises(func): |
| 246 | @wraps(func) |
| 247 | def wrapper(self): |
| 248 | #forcibly close the fd before invoking the problem function |
| 249 | f = self.f |
| 250 | os.close(f.fileno()) |
| 251 | try: |
| 252 | func(self, f) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 253 | except OSError as e: |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 254 | self.assertEqual(e.errno, errno.EBADF) |
| 255 | else: |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 256 | self.fail("Should have raised OSError") |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 257 | finally: |
| 258 | try: |
| 259 | self.f.close() |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 260 | except OSError: |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 261 | pass |
| 262 | return wrapper |
| 263 | |
| 264 | @ClosedFDRaises |
| 265 | def testErrnoOnClose(self, f): |
| 266 | f.close() |
| 267 | |
| 268 | @ClosedFDRaises |
| 269 | def testErrnoOnClosedWrite(self, f): |
Benjamin Peterson | 255058f | 2010-01-27 01:47:14 +0000 | [diff] [blame] | 270 | f.write(b'a') |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 271 | |
| 272 | @ClosedFDRaises |
| 273 | def testErrnoOnClosedSeek(self, f): |
| 274 | f.seek(0) |
| 275 | |
| 276 | @ClosedFDRaises |
| 277 | def testErrnoOnClosedTell(self, f): |
| 278 | f.tell() |
| 279 | |
| 280 | @ClosedFDRaises |
| 281 | def testErrnoOnClosedTruncate(self, f): |
| 282 | f.truncate(0) |
| 283 | |
| 284 | @ClosedFD |
| 285 | def testErrnoOnClosedSeekable(self, f): |
| 286 | f.seekable() |
| 287 | |
| 288 | @ClosedFD |
| 289 | def testErrnoOnClosedReadable(self, f): |
| 290 | f.readable() |
| 291 | |
| 292 | @ClosedFD |
| 293 | def testErrnoOnClosedWritable(self, f): |
| 294 | f.writable() |
| 295 | |
| 296 | @ClosedFD |
| 297 | def testErrnoOnClosedFileno(self, f): |
| 298 | f.fileno() |
| 299 | |
| 300 | @ClosedFD |
| 301 | def testErrnoOnClosedIsatty(self, f): |
| 302 | self.assertEqual(f.isatty(), False) |
| 303 | |
| 304 | def ReopenForRead(self): |
| 305 | try: |
| 306 | self.f.close() |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 307 | except OSError: |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 308 | pass |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 309 | self.f = self.FileIO(TESTFN, 'r') |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 310 | os.close(self.f.fileno()) |
| 311 | return self.f |
| 312 | |
| 313 | @ClosedFDRaises |
| 314 | def testErrnoOnClosedRead(self, f): |
| 315 | f = self.ReopenForRead() |
| 316 | f.read(1) |
| 317 | |
| 318 | @ClosedFDRaises |
| 319 | def testErrnoOnClosedReadall(self, f): |
| 320 | f = self.ReopenForRead() |
| 321 | f.readall() |
| 322 | |
| 323 | @ClosedFDRaises |
| 324 | def testErrnoOnClosedReadinto(self, f): |
| 325 | f = self.ReopenForRead() |
| 326 | a = array('b', b'x'*10) |
| 327 | f.readinto(a) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 328 | |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 329 | class CAutoFileTests(AutoFileTests, unittest.TestCase): |
| 330 | FileIO = _io.FileIO |
| 331 | modulename = '_io' |
| 332 | |
| 333 | class PyAutoFileTests(AutoFileTests, unittest.TestCase): |
| 334 | FileIO = _pyio.FileIO |
| 335 | modulename = '_pyio' |
| 336 | |
| 337 | |
| 338 | class OtherFileTests: |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 339 | |
| 340 | def testAbles(self): |
| 341 | try: |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 342 | f = self.FileIO(TESTFN, "w") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 343 | self.assertEqual(f.readable(), False) |
| 344 | self.assertEqual(f.writable(), True) |
| 345 | self.assertEqual(f.seekable(), True) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 346 | f.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 347 | |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 348 | f = self.FileIO(TESTFN, "r") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 349 | self.assertEqual(f.readable(), True) |
| 350 | self.assertEqual(f.writable(), False) |
| 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, "a+") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 355 | self.assertEqual(f.readable(), True) |
| 356 | self.assertEqual(f.writable(), True) |
| 357 | self.assertEqual(f.seekable(), True) |
| 358 | self.assertEqual(f.isatty(), False) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 359 | f.close() |
Guido van Rossum | 682faf8 | 2007-04-12 14:56:58 +0000 | [diff] [blame] | 360 | |
Thomas Heller | b0f48ab | 2007-07-12 11:29:02 +0000 | [diff] [blame] | 361 | if sys.platform != "win32": |
| 362 | try: |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 363 | f = self.FileIO("/dev/tty", "a") |
Andrew Svetlov | 3438fa4 | 2012-12-17 23:35:18 +0200 | [diff] [blame] | 364 | except OSError: |
Thomas Heller | b0f48ab | 2007-07-12 11:29:02 +0000 | [diff] [blame] | 365 | # When run in a cron job there just aren't any |
| 366 | # ttys, so skip the test. This also handles other |
| 367 | # OS'es that don't support /dev/tty. |
| 368 | pass |
| 369 | else: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 370 | self.assertEqual(f.readable(), False) |
| 371 | self.assertEqual(f.writable(), True) |
Hye-Shik Chang | caf871a | 2007-08-13 13:21:33 +0000 | [diff] [blame] | 372 | if sys.platform != "darwin" and \ |
Antoine Pitrou | d7b3046 | 2009-05-23 16:34:50 +0000 | [diff] [blame] | 373 | 'bsd' not in sys.platform and \ |
Benjamin Peterson | 94fe10f | 2008-07-17 23:27:26 +0000 | [diff] [blame] | 374 | not sys.platform.startswith('sunos'): |
Hye-Shik Chang | caf871a | 2007-08-13 13:21:33 +0000 | [diff] [blame] | 375 | # Somehow /dev/tty appears seekable on some BSDs |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 376 | self.assertEqual(f.seekable(), False) |
| 377 | self.assertEqual(f.isatty(), True) |
Thomas Heller | b0f48ab | 2007-07-12 11:29:02 +0000 | [diff] [blame] | 378 | f.close() |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 379 | finally: |
| 380 | os.unlink(TESTFN) |
| 381 | |
Antoine Pitrou | e93b63b | 2013-09-04 20:46:33 +0200 | [diff] [blame] | 382 | def testInvalidModeStrings(self): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 383 | # check invalid mode strings |
Benjamin Peterson | 44309e6 | 2008-11-22 00:41:45 +0000 | [diff] [blame] | 384 | for mode in ("", "aU", "wU+", "rw", "rt"): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 385 | try: |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 386 | f = self.FileIO(TESTFN, mode) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 387 | except ValueError: |
| 388 | pass |
| 389 | else: |
| 390 | f.close() |
| 391 | self.fail('%r is an invalid file mode' % mode) |
| 392 | |
Antoine Pitrou | e93b63b | 2013-09-04 20:46:33 +0200 | [diff] [blame] | 393 | def testModeStrings(self): |
| 394 | # test that the mode attribute is correct for various mode strings |
| 395 | # given as init args |
| 396 | try: |
| 397 | for modes in [('w', 'wb'), ('wb', 'wb'), ('wb+', 'rb+'), |
| 398 | ('w+b', 'rb+'), ('a', 'ab'), ('ab', 'ab'), |
| 399 | ('ab+', 'ab+'), ('a+b', 'ab+'), ('r', 'rb'), |
| 400 | ('rb', 'rb'), ('rb+', 'rb+'), ('r+b', 'rb+')]: |
| 401 | # read modes are last so that TESTFN will exist first |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 402 | with self.FileIO(TESTFN, modes[0]) as f: |
Antoine Pitrou | e93b63b | 2013-09-04 20:46:33 +0200 | [diff] [blame] | 403 | self.assertEqual(f.mode, modes[1]) |
| 404 | finally: |
| 405 | if os.path.exists(TESTFN): |
| 406 | os.unlink(TESTFN) |
| 407 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 408 | def testUnicodeOpen(self): |
| 409 | # verify repr works for unicode too |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 410 | f = self.FileIO(str(TESTFN), "w") |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 411 | f.close() |
| 412 | os.unlink(TESTFN) |
| 413 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 414 | def testBytesOpen(self): |
| 415 | # Opening a bytes filename |
| 416 | try: |
| 417 | fn = TESTFN.encode("ascii") |
| 418 | except UnicodeEncodeError: |
Zachary Ware | 9fe6d86 | 2013-12-08 00:20:35 -0600 | [diff] [blame] | 419 | self.skipTest('could not encode %r to ascii' % TESTFN) |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 420 | f = self.FileIO(fn, "w") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 421 | try: |
| 422 | f.write(b"abc") |
| 423 | f.close() |
| 424 | with open(TESTFN, "rb") as f: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 425 | self.assertEqual(f.read(), b"abc") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 426 | finally: |
| 427 | os.unlink(TESTFN) |
| 428 | |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 429 | def testConstructorHandlesNULChars(self): |
| 430 | fn_with_NUL = 'foo\0bar' |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 431 | self.assertRaises(ValueError, self.FileIO, fn_with_NUL, 'w') |
| 432 | self.assertRaises(ValueError, self.FileIO, bytes(fn_with_NUL, 'ascii'), 'w') |
Antoine Pitrou | 1334884 | 2012-01-29 18:36:34 +0100 | [diff] [blame] | 433 | |
Benjamin Peterson | 806d402 | 2009-01-19 15:11:51 +0000 | [diff] [blame] | 434 | def testInvalidFd(self): |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 435 | self.assertRaises(ValueError, self.FileIO, -10) |
| 436 | self.assertRaises(OSError, self.FileIO, make_bad_fd()) |
Antoine Pitrou | 0049249 | 2010-09-04 20:53:29 +0000 | [diff] [blame] | 437 | if sys.platform == 'win32': |
| 438 | import msvcrt |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 439 | self.assertRaises(OSError, msvcrt.get_osfhandle, make_bad_fd()) |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 440 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 441 | def testBadModeArgument(self): |
| 442 | # verify that we get a sensible error message for bad mode argument |
| 443 | bad_mode = "qwerty" |
| 444 | try: |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 445 | f = self.FileIO(TESTFN, bad_mode) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 446 | except ValueError as msg: |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 447 | if msg.args[0] != 0: |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 448 | s = str(msg) |
Ezio Melotti | 7fb4da7 | 2010-03-18 12:29:13 +0000 | [diff] [blame] | 449 | if TESTFN in s or bad_mode not in s: |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 450 | self.fail("bad error message for invalid mode: %s" % s) |
Georg Brandl | 50da60c | 2008-01-06 21:38:54 +0000 | [diff] [blame] | 451 | # 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] | 452 | # no obvious way to discover why open() failed. |
| 453 | else: |
| 454 | f.close() |
| 455 | self.fail("no error for invalid mode: %s" % bad_mode) |
| 456 | |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 457 | def testTruncate(self): |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 458 | f = self.FileIO(TESTFN, 'w') |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 459 | f.write(bytes(bytearray(range(10)))) |
| 460 | self.assertEqual(f.tell(), 10) |
| 461 | f.truncate(5) |
| 462 | self.assertEqual(f.tell(), 10) |
Eli Bendersky | 74c503b | 2012-01-03 06:26:13 +0200 | [diff] [blame] | 463 | self.assertEqual(f.seek(0, io.SEEK_END), 5) |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 464 | f.truncate(15) |
| 465 | self.assertEqual(f.tell(), 5) |
Eli Bendersky | 74c503b | 2012-01-03 06:26:13 +0200 | [diff] [blame] | 466 | self.assertEqual(f.seek(0, io.SEEK_END), 15) |
Antoine Pitrou | 8d2b51b | 2010-10-30 16:19:14 +0000 | [diff] [blame] | 467 | f.close() |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 468 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 469 | def testTruncateOnWindows(self): |
| 470 | def bug801631(): |
| 471 | # SF bug <http://www.python.org/sf/801631> |
| 472 | # "file.truncate fault on windows" |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 473 | f = self.FileIO(TESTFN, 'w') |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 474 | f.write(bytes(range(11))) |
| 475 | f.close() |
| 476 | |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 477 | f = self.FileIO(TESTFN,'r+') |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 478 | data = f.read(5) |
| 479 | if data != bytes(range(5)): |
| 480 | self.fail("Read on file opened for update failed %r" % data) |
| 481 | if f.tell() != 5: |
| 482 | self.fail("File pos after read wrong %d" % f.tell()) |
| 483 | |
| 484 | f.truncate() |
| 485 | if f.tell() != 5: |
| 486 | self.fail("File pos after ftruncate wrong %d" % f.tell()) |
| 487 | |
| 488 | f.close() |
| 489 | size = os.path.getsize(TESTFN) |
| 490 | if size != 5: |
| 491 | self.fail("File size after ftruncate wrong %d" % size) |
| 492 | |
| 493 | try: |
| 494 | bug801631() |
| 495 | finally: |
| 496 | os.unlink(TESTFN) |
| 497 | |
Walter Dörwald | 3a77c7a | 2007-06-06 16:31:14 +0000 | [diff] [blame] | 498 | def testAppend(self): |
| 499 | try: |
| 500 | f = open(TESTFN, 'wb') |
| 501 | f.write(b'spam') |
| 502 | f.close() |
| 503 | f = open(TESTFN, 'ab') |
| 504 | f.write(b'eggs') |
| 505 | f.close() |
| 506 | f = open(TESTFN, 'rb') |
| 507 | d = f.read() |
| 508 | f.close() |
| 509 | self.assertEqual(d, b'spameggs') |
| 510 | finally: |
| 511 | try: |
| 512 | os.unlink(TESTFN) |
| 513 | except: |
| 514 | pass |
| 515 | |
Neal Norwitz | 6e0e0e6 | 2008-08-24 22:07:28 +0000 | [diff] [blame] | 516 | def testInvalidInit(self): |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 517 | self.assertRaises(TypeError, self.FileIO, "1", 0, 0) |
Neal Norwitz | 6e0e0e6 | 2008-08-24 22:07:28 +0000 | [diff] [blame] | 518 | |
Benjamin Peterson | 65676e4 | 2008-11-05 21:42:45 +0000 | [diff] [blame] | 519 | def testWarnings(self): |
Florent Xicluna | b14930c | 2010-03-13 15:26:44 +0000 | [diff] [blame] | 520 | with check_warnings(quiet=True) as w: |
Benjamin Peterson | 65676e4 | 2008-11-05 21:42:45 +0000 | [diff] [blame] | 521 | self.assertEqual(w.warnings, []) |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 522 | self.assertRaises(TypeError, self.FileIO, []) |
Benjamin Peterson | 65676e4 | 2008-11-05 21:42:45 +0000 | [diff] [blame] | 523 | self.assertEqual(w.warnings, []) |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 524 | self.assertRaises(ValueError, self.FileIO, "/some/invalid/name", "rt") |
Benjamin Peterson | 65676e4 | 2008-11-05 21:42:45 +0000 | [diff] [blame] | 525 | self.assertEqual(w.warnings, []) |
| 526 | |
Hynek Schlawack | 9ed8b4e | 2012-06-21 20:20:25 +0200 | [diff] [blame] | 527 | def testUnclosedFDOnException(self): |
| 528 | class MyException(Exception): pass |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 529 | class MyFileIO(self.FileIO): |
Hynek Schlawack | 9ed8b4e | 2012-06-21 20:20:25 +0200 | [diff] [blame] | 530 | def __setattr__(self, name, value): |
| 531 | if name == "name": |
| 532 | raise MyException("blocked setting name") |
| 533 | return super(MyFileIO, self).__setattr__(name, value) |
| 534 | fd = os.open(__file__, os.O_RDONLY) |
| 535 | self.assertRaises(MyException, MyFileIO, fd) |
| 536 | os.close(fd) # should not raise OSError(EBADF) |
| 537 | |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 538 | class COtherFileTests(OtherFileTests, unittest.TestCase): |
| 539 | FileIO = _io.FileIO |
| 540 | modulename = '_io' |
| 541 | |
| 542 | @cpython_only |
| 543 | def testInvalidFd_overflow(self): |
| 544 | # Issue 15989 |
| 545 | import _testcapi |
| 546 | self.assertRaises(TypeError, self.FileIO, _testcapi.INT_MAX + 1) |
| 547 | self.assertRaises(TypeError, self.FileIO, _testcapi.INT_MIN - 1) |
| 548 | |
| 549 | class PyOtherFileTests(OtherFileTests, unittest.TestCase): |
| 550 | FileIO = _pyio.FileIO |
| 551 | modulename = '_pyio' |
| 552 | |
Neal Norwitz | 6e0e0e6 | 2008-08-24 22:07:28 +0000 | [diff] [blame] | 553 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 554 | def test_main(): |
| 555 | # Historically, these tests have been sloppy about removing TESTFN. |
| 556 | # So get rid of it no matter what. |
| 557 | try: |
Serhiy Storchaka | 71fd224 | 2015-04-10 16:16:16 +0300 | [diff] [blame] | 558 | run_unittest(CAutoFileTests, PyAutoFileTests, |
| 559 | COtherFileTests, PyOtherFileTests) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 560 | finally: |
| 561 | if os.path.exists(TESTFN): |
| 562 | os.unlink(TESTFN) |
| 563 | |
| 564 | if __name__ == '__main__': |
| 565 | test_main() |