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