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 |
| 5 | import unittest |
| 6 | from array import array |
| 7 | from weakref import proxy |
| 8 | |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 9 | from test.support import (TESTFN, findfile, check_warnings, run_unittest, |
| 10 | make_bad_fd) |
Raymond Hettinger | 53dbe39 | 2008-02-12 20:03:09 +0000 | [diff] [blame] | 11 | from collections import UserList |
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))) |
| 30 | self.assertEquals(self.f.tell(), p.tell()) |
| 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))) |
| 37 | self.assertEquals(self.f.tell(), 20) |
| 38 | self.f.seek(0) |
| 39 | self.assertEquals(self.f.tell(), 0) |
| 40 | self.f.seek(10) |
| 41 | self.assertEquals(self.f.tell(), 10) |
| 42 | self.f.seek(5, 1) |
| 43 | self.assertEquals(self.f.tell(), 15) |
| 44 | self.f.seek(-5, 1) |
| 45 | self.assertEquals(self.f.tell(), 10) |
| 46 | self.f.seek(-5, 2) |
| 47 | self.assertEquals(self.f.tell(), 15) |
| 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 | |
Benjamin Peterson | 44309e6 | 2008-11-22 00:41:45 +0000 | [diff] [blame] | 53 | self.assertEquals(f.mode, "wb") |
Guido van Rossum | 31c3a57 | 2007-04-12 14:51:49 +0000 | [diff] [blame] | 54 | self.assertEquals(f.closed, False) |
| 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) |
| 68 | self.assertEquals(array('b', [1, 2]), a[:n]) |
| 69 | |
| 70 | def testRepr(self): |
Guido van Rossum | 682faf8 | 2007-04-12 14:56:58 +0000 | [diff] [blame] | 71 | self.assertEquals(repr(self.f), |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame^] | 72 | "io.FileIO(%d, %s)" % (self.f.fileno(), |
Guido van Rossum | 682faf8 | 2007-04-12 14:56:58 +0000 | [diff] [blame] | 73 | repr(self.f.mode))) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 74 | |
| 75 | def testErrors(self): |
| 76 | f = self.f |
| 77 | self.assert_(not f.isatty()) |
Guido van Rossum | 31c3a57 | 2007-04-12 14:51:49 +0000 | [diff] [blame] | 78 | self.assert_(not f.closed) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 79 | #self.assertEquals(f.name, TESTFN) |
Guido van Rossum | 31c3a57 | 2007-04-12 14:51:49 +0000 | [diff] [blame] | 80 | self.assertRaises(ValueError, f.read, 10) # Open for reading |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 81 | f.close() |
Guido van Rossum | 31c3a57 | 2007-04-12 14:51:49 +0000 | [diff] [blame] | 82 | self.assert_(f.closed) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame^] | 83 | f = _FileIO(TESTFN, 'r') |
Guido van Rossum | 31c3a57 | 2007-04-12 14:51:49 +0000 | [diff] [blame] | 84 | self.assertRaises(TypeError, f.readinto, "") |
| 85 | self.assert_(not f.closed) |
| 86 | f.close() |
| 87 | self.assert_(f.closed) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 88 | |
| 89 | def testMethods(self): |
| 90 | methods = ['fileno', 'isatty', 'read', 'readinto', |
| 91 | 'seek', 'tell', 'truncate', 'write', 'seekable', |
| 92 | 'readable', 'writable'] |
| 93 | if sys.platform.startswith('atheos'): |
| 94 | methods.remove('truncate') |
| 95 | |
Guido van Rossum | 31c3a57 | 2007-04-12 14:51:49 +0000 | [diff] [blame] | 96 | self.f.close() |
| 97 | self.assert_(self.f.closed) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 98 | |
| 99 | for methodname in methods: |
| 100 | method = getattr(self.f, methodname) |
| 101 | # should raise on closed file |
| 102 | self.assertRaises(ValueError, method) |
| 103 | |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 104 | def testOpendir(self): |
| 105 | # Issue 3703: opening a directory should fill the errno |
| 106 | # Windows always returns "[Errno 13]: Permission denied |
| 107 | # Unix calls dircheck() and returns "[Errno 21]: Is a directory" |
| 108 | try: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame^] | 109 | _FileIO('.', 'r') |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 110 | except IOError as e: |
| 111 | self.assertNotEqual(e.errno, 0) |
Benjamin Peterson | 1efc23c | 2008-12-29 18:02:28 +0000 | [diff] [blame] | 112 | self.assertEqual(e.filename, ".") |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 113 | else: |
| 114 | self.fail("Should have raised IOError") |
| 115 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 116 | |
| 117 | class OtherFileTests(unittest.TestCase): |
| 118 | |
| 119 | def testAbles(self): |
| 120 | try: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame^] | 121 | f = _FileIO(TESTFN, "w") |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 122 | self.assertEquals(f.readable(), False) |
| 123 | self.assertEquals(f.writable(), True) |
| 124 | self.assertEquals(f.seekable(), True) |
| 125 | f.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 126 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame^] | 127 | f = _FileIO(TESTFN, "r") |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 128 | self.assertEquals(f.readable(), True) |
| 129 | self.assertEquals(f.writable(), False) |
| 130 | self.assertEquals(f.seekable(), True) |
| 131 | f.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 132 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame^] | 133 | f = _FileIO(TESTFN, "a+") |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 134 | self.assertEquals(f.readable(), True) |
| 135 | self.assertEquals(f.writable(), True) |
| 136 | self.assertEquals(f.seekable(), True) |
| 137 | self.assertEquals(f.isatty(), False) |
| 138 | f.close() |
Guido van Rossum | 682faf8 | 2007-04-12 14:56:58 +0000 | [diff] [blame] | 139 | |
Thomas Heller | b0f48ab | 2007-07-12 11:29:02 +0000 | [diff] [blame] | 140 | if sys.platform != "win32": |
| 141 | try: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame^] | 142 | f = _FileIO("/dev/tty", "a") |
Thomas Heller | b0f48ab | 2007-07-12 11:29:02 +0000 | [diff] [blame] | 143 | except EnvironmentError: |
| 144 | # When run in a cron job there just aren't any |
| 145 | # ttys, so skip the test. This also handles other |
| 146 | # OS'es that don't support /dev/tty. |
| 147 | pass |
| 148 | else: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame^] | 149 | f = _FileIO("/dev/tty", "a") |
Thomas Heller | b0f48ab | 2007-07-12 11:29:02 +0000 | [diff] [blame] | 150 | self.assertEquals(f.readable(), False) |
| 151 | self.assertEquals(f.writable(), True) |
Hye-Shik Chang | caf871a | 2007-08-13 13:21:33 +0000 | [diff] [blame] | 152 | if sys.platform != "darwin" and \ |
Benjamin Peterson | 94fe10f | 2008-07-17 23:27:26 +0000 | [diff] [blame] | 153 | not sys.platform.startswith('freebsd') and \ |
| 154 | not sys.platform.startswith('sunos'): |
Hye-Shik Chang | caf871a | 2007-08-13 13:21:33 +0000 | [diff] [blame] | 155 | # Somehow /dev/tty appears seekable on some BSDs |
Thomas Heller | b0f48ab | 2007-07-12 11:29:02 +0000 | [diff] [blame] | 156 | self.assertEquals(f.seekable(), False) |
| 157 | self.assertEquals(f.isatty(), True) |
| 158 | f.close() |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 159 | finally: |
| 160 | os.unlink(TESTFN) |
| 161 | |
| 162 | def testModeStrings(self): |
| 163 | # check invalid mode strings |
Benjamin Peterson | 44309e6 | 2008-11-22 00:41:45 +0000 | [diff] [blame] | 164 | for mode in ("", "aU", "wU+", "rw", "rt"): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 165 | try: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame^] | 166 | f = _FileIO(TESTFN, mode) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 167 | except ValueError: |
| 168 | pass |
| 169 | else: |
| 170 | f.close() |
| 171 | self.fail('%r is an invalid file mode' % mode) |
| 172 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 173 | def testUnicodeOpen(self): |
| 174 | # verify repr works for unicode too |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame^] | 175 | f = _FileIO(str(TESTFN), "w") |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 176 | f.close() |
| 177 | os.unlink(TESTFN) |
| 178 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame^] | 179 | def testBytesOpen(self): |
| 180 | # Opening a bytes filename |
| 181 | try: |
| 182 | fn = TESTFN.encode("ascii") |
| 183 | except UnicodeEncodeError: |
| 184 | # Skip test |
| 185 | return |
| 186 | f = _FileIO(fn, "w") |
| 187 | try: |
| 188 | f.write(b"abc") |
| 189 | f.close() |
| 190 | with open(TESTFN, "rb") as f: |
| 191 | self.assertEquals(f.read(), b"abc") |
| 192 | finally: |
| 193 | os.unlink(TESTFN) |
| 194 | |
Benjamin Peterson | 806d402 | 2009-01-19 15:11:51 +0000 | [diff] [blame] | 195 | def testInvalidFd(self): |
| 196 | self.assertRaises(ValueError, _fileio._FileIO, -10) |
Benjamin Peterson | 7522c74 | 2009-01-19 21:00:09 +0000 | [diff] [blame] | 197 | self.assertRaises(OSError, _fileio._FileIO, make_bad_fd()) |
Benjamin Peterson | 806d402 | 2009-01-19 15:11:51 +0000 | [diff] [blame] | 198 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 199 | def testBadModeArgument(self): |
| 200 | # verify that we get a sensible error message for bad mode argument |
| 201 | bad_mode = "qwerty" |
| 202 | try: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame^] | 203 | f = _FileIO(TESTFN, bad_mode) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 204 | except ValueError as msg: |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 205 | if msg.args[0] != 0: |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 206 | s = str(msg) |
| 207 | if s.find(TESTFN) != -1 or s.find(bad_mode) == -1: |
| 208 | self.fail("bad error message for invalid mode: %s" % s) |
Georg Brandl | 50da60c | 2008-01-06 21:38:54 +0000 | [diff] [blame] | 209 | # 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] | 210 | # no obvious way to discover why open() failed. |
| 211 | else: |
| 212 | f.close() |
| 213 | self.fail("no error for invalid mode: %s" % bad_mode) |
| 214 | |
| 215 | def testTruncateOnWindows(self): |
| 216 | def bug801631(): |
| 217 | # SF bug <http://www.python.org/sf/801631> |
| 218 | # "file.truncate fault on windows" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame^] | 219 | f = _FileIO(TESTFN, 'w') |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 220 | f.write(bytes(range(11))) |
| 221 | f.close() |
| 222 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame^] | 223 | f = _FileIO(TESTFN,'r+') |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 224 | data = f.read(5) |
| 225 | if data != bytes(range(5)): |
| 226 | self.fail("Read on file opened for update failed %r" % data) |
| 227 | if f.tell() != 5: |
| 228 | self.fail("File pos after read wrong %d" % f.tell()) |
| 229 | |
| 230 | f.truncate() |
| 231 | if f.tell() != 5: |
| 232 | self.fail("File pos after ftruncate wrong %d" % f.tell()) |
| 233 | |
| 234 | f.close() |
| 235 | size = os.path.getsize(TESTFN) |
| 236 | if size != 5: |
| 237 | self.fail("File size after ftruncate wrong %d" % size) |
| 238 | |
| 239 | try: |
| 240 | bug801631() |
| 241 | finally: |
| 242 | os.unlink(TESTFN) |
| 243 | |
Walter Dörwald | 3a77c7a | 2007-06-06 16:31:14 +0000 | [diff] [blame] | 244 | def testAppend(self): |
| 245 | try: |
| 246 | f = open(TESTFN, 'wb') |
| 247 | f.write(b'spam') |
| 248 | f.close() |
| 249 | f = open(TESTFN, 'ab') |
| 250 | f.write(b'eggs') |
| 251 | f.close() |
| 252 | f = open(TESTFN, 'rb') |
| 253 | d = f.read() |
| 254 | f.close() |
| 255 | self.assertEqual(d, b'spameggs') |
| 256 | finally: |
| 257 | try: |
| 258 | os.unlink(TESTFN) |
| 259 | except: |
| 260 | pass |
| 261 | |
Neal Norwitz | 6e0e0e6 | 2008-08-24 22:07:28 +0000 | [diff] [blame] | 262 | def testInvalidInit(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame^] | 263 | self.assertRaises(TypeError, _FileIO, "1", 0, 0) |
Neal Norwitz | 6e0e0e6 | 2008-08-24 22:07:28 +0000 | [diff] [blame] | 264 | |
Benjamin Peterson | 65676e4 | 2008-11-05 21:42:45 +0000 | [diff] [blame] | 265 | def testWarnings(self): |
| 266 | with check_warnings() as w: |
| 267 | self.assertEqual(w.warnings, []) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame^] | 268 | self.assertRaises(TypeError, _FileIO, []) |
Benjamin Peterson | 65676e4 | 2008-11-05 21:42:45 +0000 | [diff] [blame] | 269 | self.assertEqual(w.warnings, []) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame^] | 270 | self.assertRaises(ValueError, _FileIO, "/some/invalid/name", "rt") |
Benjamin Peterson | 65676e4 | 2008-11-05 21:42:45 +0000 | [diff] [blame] | 271 | self.assertEqual(w.warnings, []) |
| 272 | |
Neal Norwitz | 6e0e0e6 | 2008-08-24 22:07:28 +0000 | [diff] [blame] | 273 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 274 | def test_main(): |
| 275 | # Historically, these tests have been sloppy about removing TESTFN. |
| 276 | # So get rid of it no matter what. |
| 277 | try: |
| 278 | run_unittest(AutoFileTests, OtherFileTests) |
| 279 | finally: |
| 280 | if os.path.exists(TESTFN): |
| 281 | os.unlink(TESTFN) |
| 282 | |
| 283 | if __name__ == '__main__': |
| 284 | test_main() |