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 |
Antoine Pitrou | 0ae29cf | 2009-03-13 22:33:17 +0000 | [diff] [blame] | 5 | import errno |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 6 | import unittest |
| 7 | from array import array |
| 8 | from weakref import proxy |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 9 | from functools import wraps |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 10 | |
Georg Brandl | 1b37e87 | 2010-03-14 10:45:50 +0000 | [diff] [blame] | 11 | from test.support import TESTFN, check_warnings, run_unittest, make_bad_fd |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 12 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 13 | from _io import FileIO as _FileIO |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 14 | |
| 15 | class AutoFileTests(unittest.TestCase): |
| 16 | # file tests for which a test file is automatically set up |
| 17 | |
| 18 | def setUp(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 19 | self.f = _FileIO(TESTFN, 'w') |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 20 | |
| 21 | def tearDown(self): |
| 22 | if self.f: |
| 23 | self.f.close() |
| 24 | os.remove(TESTFN) |
| 25 | |
| 26 | def testWeakRefs(self): |
| 27 | # verify weak references |
| 28 | p = proxy(self.f) |
| 29 | p.write(bytes(range(10))) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 30 | self.assertEqual(self.f.tell(), p.tell()) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 31 | self.f.close() |
| 32 | self.f = None |
| 33 | self.assertRaises(ReferenceError, getattr, p, 'tell') |
| 34 | |
| 35 | def testSeekTell(self): |
| 36 | self.f.write(bytes(range(20))) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 37 | self.assertEqual(self.f.tell(), 20) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 38 | self.f.seek(0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 39 | self.assertEqual(self.f.tell(), 0) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 40 | self.f.seek(10) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 41 | self.assertEqual(self.f.tell(), 10) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 42 | self.f.seek(5, 1) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 43 | self.assertEqual(self.f.tell(), 15) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 44 | self.f.seek(-5, 1) |
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, 2) |
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 | |
| 49 | def testAttributes(self): |
| 50 | # verify expected attributes exist |
| 51 | f = self.f |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 52 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 53 | self.assertEqual(f.mode, "wb") |
| 54 | self.assertEqual(f.closed, False) |
Guido van Rossum | 31c3a57 | 2007-04-12 14:51:49 +0000 | [diff] [blame] | 55 | |
| 56 | # verify the attributes are readonly |
| 57 | for attr in 'mode', 'closed': |
| 58 | self.assertRaises((AttributeError, TypeError), |
| 59 | setattr, f, attr, 'oops') |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 60 | |
| 61 | def testReadinto(self): |
| 62 | # verify readinto |
| 63 | self.f.write(bytes([1, 2])) |
| 64 | self.f.close() |
Guido van Rossum | a45ea58 | 2007-05-15 21:25:12 +0000 | [diff] [blame] | 65 | a = array('b', b'x'*10) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 66 | self.f = _FileIO(TESTFN, 'r') |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 67 | n = self.f.readinto(a) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 68 | self.assertEqual(array('b', [1, 2]), a[:n]) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 69 | |
Benjamin Peterson | bf5ff76 | 2009-12-13 19:25:34 +0000 | [diff] [blame] | 70 | def test_none_args(self): |
| 71 | self.f.write(b"hi\nbye\nabc") |
| 72 | self.f.close() |
| 73 | self.f = _FileIO(TESTFN, 'r') |
| 74 | self.assertEqual(self.f.read(None), b"hi\nbye\nabc") |
| 75 | self.f.seek(0) |
| 76 | self.assertEqual(self.f.readline(None), b"hi\n") |
| 77 | self.assertEqual(self.f.readlines(None), [b"bye\n", b"abc"]) |
| 78 | |
Benjamin Peterson | 255058f | 2010-01-27 01:47:14 +0000 | [diff] [blame] | 79 | def test_reject(self): |
| 80 | self.assertRaises(TypeError, self.f.write, "Hello!") |
| 81 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 82 | def testRepr(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 83 | self.assertEqual(repr(self.f), "<_io.FileIO name=%r mode=%r>" |
Antoine Pitrou | 716c444 | 2009-05-23 19:04:03 +0000 | [diff] [blame] | 84 | % (self.f.name, self.f.mode)) |
| 85 | del self.f.name |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 86 | self.assertEqual(repr(self.f), "<_io.FileIO fd=%r mode=%r>" |
Antoine Pitrou | 716c444 | 2009-05-23 19:04:03 +0000 | [diff] [blame] | 87 | % (self.f.fileno(), self.f.mode)) |
| 88 | self.f.close() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 89 | self.assertEqual(repr(self.f), "<_io.FileIO [closed]>") |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 90 | |
| 91 | def testErrors(self): |
| 92 | f = self.f |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 93 | self.assertTrue(not f.isatty()) |
| 94 | self.assertTrue(not f.closed) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 95 | #self.assertEqual(f.name, TESTFN) |
Guido van Rossum | 31c3a57 | 2007-04-12 14:51:49 +0000 | [diff] [blame] | 96 | self.assertRaises(ValueError, f.read, 10) # Open for reading |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 97 | f.close() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 98 | self.assertTrue(f.closed) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 99 | f = _FileIO(TESTFN, 'r') |
Guido van Rossum | 31c3a57 | 2007-04-12 14:51:49 +0000 | [diff] [blame] | 100 | self.assertRaises(TypeError, f.readinto, "") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 101 | self.assertTrue(not f.closed) |
Guido van Rossum | 31c3a57 | 2007-04-12 14:51:49 +0000 | [diff] [blame] | 102 | f.close() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 103 | self.assertTrue(f.closed) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 104 | |
| 105 | def testMethods(self): |
| 106 | methods = ['fileno', 'isatty', 'read', 'readinto', |
| 107 | 'seek', 'tell', 'truncate', 'write', 'seekable', |
| 108 | 'readable', 'writable'] |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 109 | |
Guido van Rossum | 31c3a57 | 2007-04-12 14:51:49 +0000 | [diff] [blame] | 110 | self.f.close() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 111 | self.assertTrue(self.f.closed) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 112 | |
| 113 | for methodname in methods: |
| 114 | method = getattr(self.f, methodname) |
| 115 | # should raise on closed file |
| 116 | self.assertRaises(ValueError, method) |
| 117 | |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 118 | def testOpendir(self): |
| 119 | # Issue 3703: opening a directory should fill the errno |
| 120 | # Windows always returns "[Errno 13]: Permission denied |
| 121 | # Unix calls dircheck() and returns "[Errno 21]: Is a directory" |
| 122 | try: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 123 | _FileIO('.', 'r') |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 124 | except IOError as e: |
| 125 | self.assertNotEqual(e.errno, 0) |
Benjamin Peterson | 1efc23c | 2008-12-29 18:02:28 +0000 | [diff] [blame] | 126 | self.assertEqual(e.filename, ".") |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 127 | else: |
| 128 | self.fail("Should have raised IOError") |
| 129 | |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 130 | #A set of functions testing that we get expected behaviour if someone has |
| 131 | #manually closed the internal file descriptor. First, a decorator: |
| 132 | def ClosedFD(func): |
| 133 | @wraps(func) |
| 134 | def wrapper(self): |
| 135 | #forcibly close the fd before invoking the problem function |
| 136 | f = self.f |
| 137 | os.close(f.fileno()) |
| 138 | try: |
| 139 | func(self, f) |
| 140 | finally: |
| 141 | try: |
| 142 | self.f.close() |
| 143 | except IOError: |
| 144 | pass |
| 145 | return wrapper |
Antoine Pitrou | 0ae29cf | 2009-03-13 22:33:17 +0000 | [diff] [blame] | 146 | |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 147 | def ClosedFDRaises(func): |
| 148 | @wraps(func) |
| 149 | def wrapper(self): |
| 150 | #forcibly close the fd before invoking the problem function |
| 151 | f = self.f |
| 152 | os.close(f.fileno()) |
| 153 | try: |
| 154 | func(self, f) |
| 155 | except IOError as e: |
| 156 | self.assertEqual(e.errno, errno.EBADF) |
| 157 | else: |
| 158 | self.fail("Should have raised IOError") |
| 159 | finally: |
| 160 | try: |
| 161 | self.f.close() |
| 162 | except IOError: |
| 163 | pass |
| 164 | return wrapper |
| 165 | |
| 166 | @ClosedFDRaises |
| 167 | def testErrnoOnClose(self, f): |
| 168 | f.close() |
| 169 | |
| 170 | @ClosedFDRaises |
| 171 | def testErrnoOnClosedWrite(self, f): |
Benjamin Peterson | 255058f | 2010-01-27 01:47:14 +0000 | [diff] [blame] | 172 | f.write(b'a') |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 173 | |
| 174 | @ClosedFDRaises |
| 175 | def testErrnoOnClosedSeek(self, f): |
| 176 | f.seek(0) |
| 177 | |
| 178 | @ClosedFDRaises |
| 179 | def testErrnoOnClosedTell(self, f): |
| 180 | f.tell() |
| 181 | |
| 182 | @ClosedFDRaises |
| 183 | def testErrnoOnClosedTruncate(self, f): |
| 184 | f.truncate(0) |
| 185 | |
| 186 | @ClosedFD |
| 187 | def testErrnoOnClosedSeekable(self, f): |
| 188 | f.seekable() |
| 189 | |
| 190 | @ClosedFD |
| 191 | def testErrnoOnClosedReadable(self, f): |
| 192 | f.readable() |
| 193 | |
| 194 | @ClosedFD |
| 195 | def testErrnoOnClosedWritable(self, f): |
| 196 | f.writable() |
| 197 | |
| 198 | @ClosedFD |
| 199 | def testErrnoOnClosedFileno(self, f): |
| 200 | f.fileno() |
| 201 | |
| 202 | @ClosedFD |
| 203 | def testErrnoOnClosedIsatty(self, f): |
| 204 | self.assertEqual(f.isatty(), False) |
| 205 | |
| 206 | def ReopenForRead(self): |
| 207 | try: |
| 208 | self.f.close() |
| 209 | except IOError: |
| 210 | pass |
| 211 | self.f = _FileIO(TESTFN, 'r') |
| 212 | os.close(self.f.fileno()) |
| 213 | return self.f |
| 214 | |
| 215 | @ClosedFDRaises |
| 216 | def testErrnoOnClosedRead(self, f): |
| 217 | f = self.ReopenForRead() |
| 218 | f.read(1) |
| 219 | |
| 220 | @ClosedFDRaises |
| 221 | def testErrnoOnClosedReadall(self, f): |
| 222 | f = self.ReopenForRead() |
| 223 | f.readall() |
| 224 | |
| 225 | @ClosedFDRaises |
| 226 | def testErrnoOnClosedReadinto(self, f): |
| 227 | f = self.ReopenForRead() |
| 228 | a = array('b', b'x'*10) |
| 229 | f.readinto(a) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 230 | |
| 231 | class OtherFileTests(unittest.TestCase): |
| 232 | |
| 233 | def testAbles(self): |
| 234 | try: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 235 | f = _FileIO(TESTFN, "w") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 236 | self.assertEqual(f.readable(), False) |
| 237 | self.assertEqual(f.writable(), True) |
| 238 | self.assertEqual(f.seekable(), True) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 239 | f.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 240 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 241 | f = _FileIO(TESTFN, "r") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 242 | self.assertEqual(f.readable(), True) |
| 243 | self.assertEqual(f.writable(), False) |
| 244 | self.assertEqual(f.seekable(), True) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 245 | f.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 246 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 247 | f = _FileIO(TESTFN, "a+") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 248 | self.assertEqual(f.readable(), True) |
| 249 | self.assertEqual(f.writable(), True) |
| 250 | self.assertEqual(f.seekable(), True) |
| 251 | self.assertEqual(f.isatty(), False) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 252 | f.close() |
Guido van Rossum | 682faf8 | 2007-04-12 14:56:58 +0000 | [diff] [blame] | 253 | |
Thomas Heller | b0f48ab | 2007-07-12 11:29:02 +0000 | [diff] [blame] | 254 | if sys.platform != "win32": |
| 255 | try: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 256 | f = _FileIO("/dev/tty", "a") |
Thomas Heller | b0f48ab | 2007-07-12 11:29:02 +0000 | [diff] [blame] | 257 | except EnvironmentError: |
| 258 | # When run in a cron job there just aren't any |
| 259 | # ttys, so skip the test. This also handles other |
| 260 | # OS'es that don't support /dev/tty. |
| 261 | pass |
| 262 | else: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 263 | self.assertEqual(f.readable(), False) |
| 264 | self.assertEqual(f.writable(), True) |
Hye-Shik Chang | caf871a | 2007-08-13 13:21:33 +0000 | [diff] [blame] | 265 | if sys.platform != "darwin" and \ |
Antoine Pitrou | d7b3046 | 2009-05-23 16:34:50 +0000 | [diff] [blame] | 266 | 'bsd' not in sys.platform and \ |
Benjamin Peterson | 94fe10f | 2008-07-17 23:27:26 +0000 | [diff] [blame] | 267 | not sys.platform.startswith('sunos'): |
Hye-Shik Chang | caf871a | 2007-08-13 13:21:33 +0000 | [diff] [blame] | 268 | # Somehow /dev/tty appears seekable on some BSDs |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 269 | self.assertEqual(f.seekable(), False) |
| 270 | self.assertEqual(f.isatty(), True) |
Thomas Heller | b0f48ab | 2007-07-12 11:29:02 +0000 | [diff] [blame] | 271 | f.close() |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 272 | finally: |
| 273 | os.unlink(TESTFN) |
| 274 | |
| 275 | def testModeStrings(self): |
| 276 | # check invalid mode strings |
Benjamin Peterson | 44309e6 | 2008-11-22 00:41:45 +0000 | [diff] [blame] | 277 | for mode in ("", "aU", "wU+", "rw", "rt"): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 278 | try: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 279 | f = _FileIO(TESTFN, mode) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 280 | except ValueError: |
| 281 | pass |
| 282 | else: |
| 283 | f.close() |
| 284 | self.fail('%r is an invalid file mode' % mode) |
| 285 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 286 | def testUnicodeOpen(self): |
| 287 | # verify repr works for unicode too |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 288 | f = _FileIO(str(TESTFN), "w") |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 289 | f.close() |
| 290 | os.unlink(TESTFN) |
| 291 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 292 | def testBytesOpen(self): |
| 293 | # Opening a bytes filename |
| 294 | try: |
| 295 | fn = TESTFN.encode("ascii") |
| 296 | except UnicodeEncodeError: |
| 297 | # Skip test |
| 298 | return |
| 299 | f = _FileIO(fn, "w") |
| 300 | try: |
| 301 | f.write(b"abc") |
| 302 | f.close() |
| 303 | with open(TESTFN, "rb") as f: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 304 | self.assertEqual(f.read(), b"abc") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 305 | finally: |
| 306 | os.unlink(TESTFN) |
| 307 | |
Benjamin Peterson | 806d402 | 2009-01-19 15:11:51 +0000 | [diff] [blame] | 308 | def testInvalidFd(self): |
Antoine Pitrou | 7fb111b | 2009-03-04 11:14:01 +0000 | [diff] [blame] | 309 | self.assertRaises(ValueError, _FileIO, -10) |
| 310 | self.assertRaises(OSError, _FileIO, make_bad_fd()) |
Antoine Pitrou | 0049249 | 2010-09-04 20:53:29 +0000 | [diff] [blame] | 311 | if sys.platform == 'win32': |
| 312 | import msvcrt |
| 313 | self.assertRaises(IOError, msvcrt.get_osfhandle, make_bad_fd()) |
Benjamin Peterson | 806d402 | 2009-01-19 15:11:51 +0000 | [diff] [blame] | 314 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 315 | def testBadModeArgument(self): |
| 316 | # verify that we get a sensible error message for bad mode argument |
| 317 | bad_mode = "qwerty" |
| 318 | try: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 319 | f = _FileIO(TESTFN, bad_mode) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 320 | except ValueError as msg: |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 321 | if msg.args[0] != 0: |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 322 | s = str(msg) |
Ezio Melotti | 7fb4da7 | 2010-03-18 12:29:13 +0000 | [diff] [blame] | 323 | if TESTFN in s or bad_mode not in s: |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 324 | self.fail("bad error message for invalid mode: %s" % s) |
Georg Brandl | 50da60c | 2008-01-06 21:38:54 +0000 | [diff] [blame] | 325 | # 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] | 326 | # no obvious way to discover why open() failed. |
| 327 | else: |
| 328 | f.close() |
| 329 | self.fail("no error for invalid mode: %s" % bad_mode) |
| 330 | |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 331 | def testTruncate(self): |
| 332 | f = _FileIO(TESTFN, 'w') |
| 333 | f.write(bytes(bytearray(range(10)))) |
| 334 | self.assertEqual(f.tell(), 10) |
| 335 | f.truncate(5) |
| 336 | self.assertEqual(f.tell(), 10) |
| 337 | self.assertEqual(f.seek(0, os.SEEK_END), 5) |
| 338 | f.truncate(15) |
| 339 | self.assertEqual(f.tell(), 5) |
| 340 | self.assertEqual(f.seek(0, os.SEEK_END), 15) |
Antoine Pitrou | 8d2b51b | 2010-10-30 16:19:14 +0000 | [diff] [blame] | 341 | f.close() |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 342 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 343 | def testTruncateOnWindows(self): |
| 344 | def bug801631(): |
| 345 | # SF bug <http://www.python.org/sf/801631> |
| 346 | # "file.truncate fault on windows" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 347 | f = _FileIO(TESTFN, 'w') |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 348 | f.write(bytes(range(11))) |
| 349 | f.close() |
| 350 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 351 | f = _FileIO(TESTFN,'r+') |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 352 | data = f.read(5) |
| 353 | if data != bytes(range(5)): |
| 354 | self.fail("Read on file opened for update failed %r" % data) |
| 355 | if f.tell() != 5: |
| 356 | self.fail("File pos after read wrong %d" % f.tell()) |
| 357 | |
| 358 | f.truncate() |
| 359 | if f.tell() != 5: |
| 360 | self.fail("File pos after ftruncate wrong %d" % f.tell()) |
| 361 | |
| 362 | f.close() |
| 363 | size = os.path.getsize(TESTFN) |
| 364 | if size != 5: |
| 365 | self.fail("File size after ftruncate wrong %d" % size) |
| 366 | |
| 367 | try: |
| 368 | bug801631() |
| 369 | finally: |
| 370 | os.unlink(TESTFN) |
| 371 | |
Walter Dörwald | 3a77c7a | 2007-06-06 16:31:14 +0000 | [diff] [blame] | 372 | def testAppend(self): |
| 373 | try: |
| 374 | f = open(TESTFN, 'wb') |
| 375 | f.write(b'spam') |
| 376 | f.close() |
| 377 | f = open(TESTFN, 'ab') |
| 378 | f.write(b'eggs') |
| 379 | f.close() |
| 380 | f = open(TESTFN, 'rb') |
| 381 | d = f.read() |
| 382 | f.close() |
| 383 | self.assertEqual(d, b'spameggs') |
| 384 | finally: |
| 385 | try: |
| 386 | os.unlink(TESTFN) |
| 387 | except: |
| 388 | pass |
| 389 | |
Neal Norwitz | 6e0e0e6 | 2008-08-24 22:07:28 +0000 | [diff] [blame] | 390 | def testInvalidInit(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 391 | self.assertRaises(TypeError, _FileIO, "1", 0, 0) |
Neal Norwitz | 6e0e0e6 | 2008-08-24 22:07:28 +0000 | [diff] [blame] | 392 | |
Benjamin Peterson | 65676e4 | 2008-11-05 21:42:45 +0000 | [diff] [blame] | 393 | def testWarnings(self): |
Florent Xicluna | b14930c | 2010-03-13 15:26:44 +0000 | [diff] [blame] | 394 | with check_warnings(quiet=True) as w: |
Benjamin Peterson | 65676e4 | 2008-11-05 21:42:45 +0000 | [diff] [blame] | 395 | self.assertEqual(w.warnings, []) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 396 | self.assertRaises(TypeError, _FileIO, []) |
Benjamin Peterson | 65676e4 | 2008-11-05 21:42:45 +0000 | [diff] [blame] | 397 | self.assertEqual(w.warnings, []) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 398 | self.assertRaises(ValueError, _FileIO, "/some/invalid/name", "rt") |
Benjamin Peterson | 65676e4 | 2008-11-05 21:42:45 +0000 | [diff] [blame] | 399 | self.assertEqual(w.warnings, []) |
| 400 | |
Neal Norwitz | 6e0e0e6 | 2008-08-24 22:07:28 +0000 | [diff] [blame] | 401 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 402 | def test_main(): |
| 403 | # Historically, these tests have been sloppy about removing TESTFN. |
| 404 | # So get rid of it no matter what. |
| 405 | try: |
| 406 | run_unittest(AutoFileTests, OtherFileTests) |
| 407 | finally: |
| 408 | if os.path.exists(TESTFN): |
| 409 | os.unlink(TESTFN) |
| 410 | |
| 411 | if __name__ == '__main__': |
| 412 | test_main() |