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