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 | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 9 | from test.support import TESTFN, findfile, 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 | |
Guido van Rossum | 31c3a57 | 2007-04-12 14:51:49 +0000 | [diff] [blame] | 52 | self.assertEquals(f.mode, "w") |
| 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 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 103 | |
| 104 | class OtherFileTests(unittest.TestCase): |
| 105 | |
| 106 | def testAbles(self): |
| 107 | try: |
| 108 | f = _fileio._FileIO(TESTFN, "w") |
| 109 | self.assertEquals(f.readable(), False) |
| 110 | self.assertEquals(f.writable(), True) |
| 111 | self.assertEquals(f.seekable(), True) |
| 112 | f.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 113 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 114 | f = _fileio._FileIO(TESTFN, "r") |
| 115 | self.assertEquals(f.readable(), True) |
| 116 | self.assertEquals(f.writable(), False) |
| 117 | self.assertEquals(f.seekable(), True) |
| 118 | f.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 119 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 120 | f = _fileio._FileIO(TESTFN, "a+") |
| 121 | self.assertEquals(f.readable(), True) |
| 122 | self.assertEquals(f.writable(), True) |
| 123 | self.assertEquals(f.seekable(), True) |
| 124 | self.assertEquals(f.isatty(), False) |
| 125 | f.close() |
Guido van Rossum | 682faf8 | 2007-04-12 14:56:58 +0000 | [diff] [blame] | 126 | |
Thomas Heller | b0f48ab | 2007-07-12 11:29:02 +0000 | [diff] [blame] | 127 | if sys.platform != "win32": |
| 128 | try: |
| 129 | f = _fileio._FileIO("/dev/tty", "a") |
| 130 | except EnvironmentError: |
| 131 | # When run in a cron job there just aren't any |
| 132 | # ttys, so skip the test. This also handles other |
| 133 | # OS'es that don't support /dev/tty. |
| 134 | pass |
| 135 | else: |
| 136 | f = _fileio._FileIO("/dev/tty", "a") |
| 137 | self.assertEquals(f.readable(), False) |
| 138 | self.assertEquals(f.writable(), True) |
Hye-Shik Chang | caf871a | 2007-08-13 13:21:33 +0000 | [diff] [blame] | 139 | if sys.platform != "darwin" and \ |
Benjamin Peterson | 94fe10f | 2008-07-17 23:27:26 +0000 | [diff] [blame] | 140 | not sys.platform.startswith('freebsd') and \ |
| 141 | not sys.platform.startswith('sunos'): |
Hye-Shik Chang | caf871a | 2007-08-13 13:21:33 +0000 | [diff] [blame] | 142 | # Somehow /dev/tty appears seekable on some BSDs |
Thomas Heller | b0f48ab | 2007-07-12 11:29:02 +0000 | [diff] [blame] | 143 | self.assertEquals(f.seekable(), False) |
| 144 | self.assertEquals(f.isatty(), True) |
| 145 | f.close() |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 146 | finally: |
| 147 | os.unlink(TESTFN) |
| 148 | |
| 149 | def testModeStrings(self): |
| 150 | # check invalid mode strings |
| 151 | for mode in ("", "aU", "wU+", "rb", "rt"): |
| 152 | try: |
| 153 | f = _fileio._FileIO(TESTFN, mode) |
| 154 | except ValueError: |
| 155 | pass |
| 156 | else: |
| 157 | f.close() |
| 158 | self.fail('%r is an invalid file mode' % mode) |
| 159 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 160 | def testUnicodeOpen(self): |
| 161 | # verify repr works for unicode too |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 162 | f = _fileio._FileIO(str(TESTFN), "w") |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 163 | f.close() |
| 164 | os.unlink(TESTFN) |
| 165 | |
| 166 | def testBadModeArgument(self): |
| 167 | # verify that we get a sensible error message for bad mode argument |
| 168 | bad_mode = "qwerty" |
| 169 | try: |
| 170 | f = _fileio._FileIO(TESTFN, bad_mode) |
| 171 | except ValueError as msg: |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 172 | if msg.args[0] != 0: |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 173 | s = str(msg) |
| 174 | if s.find(TESTFN) != -1 or s.find(bad_mode) == -1: |
| 175 | self.fail("bad error message for invalid mode: %s" % s) |
Georg Brandl | 50da60c | 2008-01-06 21:38:54 +0000 | [diff] [blame] | 176 | # 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] | 177 | # no obvious way to discover why open() failed. |
| 178 | else: |
| 179 | f.close() |
| 180 | self.fail("no error for invalid mode: %s" % bad_mode) |
| 181 | |
| 182 | def testTruncateOnWindows(self): |
| 183 | def bug801631(): |
| 184 | # SF bug <http://www.python.org/sf/801631> |
| 185 | # "file.truncate fault on windows" |
| 186 | f = _fileio._FileIO(TESTFN, 'w') |
| 187 | f.write(bytes(range(11))) |
| 188 | f.close() |
| 189 | |
| 190 | f = _fileio._FileIO(TESTFN,'r+') |
| 191 | data = f.read(5) |
| 192 | if data != bytes(range(5)): |
| 193 | self.fail("Read on file opened for update failed %r" % data) |
| 194 | if f.tell() != 5: |
| 195 | self.fail("File pos after read wrong %d" % f.tell()) |
| 196 | |
| 197 | f.truncate() |
| 198 | if f.tell() != 5: |
| 199 | self.fail("File pos after ftruncate wrong %d" % f.tell()) |
| 200 | |
| 201 | f.close() |
| 202 | size = os.path.getsize(TESTFN) |
| 203 | if size != 5: |
| 204 | self.fail("File size after ftruncate wrong %d" % size) |
| 205 | |
| 206 | try: |
| 207 | bug801631() |
| 208 | finally: |
| 209 | os.unlink(TESTFN) |
| 210 | |
Walter Dörwald | 3a77c7a | 2007-06-06 16:31:14 +0000 | [diff] [blame] | 211 | def testAppend(self): |
| 212 | try: |
| 213 | f = open(TESTFN, 'wb') |
| 214 | f.write(b'spam') |
| 215 | f.close() |
| 216 | f = open(TESTFN, 'ab') |
| 217 | f.write(b'eggs') |
| 218 | f.close() |
| 219 | f = open(TESTFN, 'rb') |
| 220 | d = f.read() |
| 221 | f.close() |
| 222 | self.assertEqual(d, b'spameggs') |
| 223 | finally: |
| 224 | try: |
| 225 | os.unlink(TESTFN) |
| 226 | except: |
| 227 | pass |
| 228 | |
Neal Norwitz | 6e0e0e6 | 2008-08-24 22:07:28 +0000 | [diff] [blame^] | 229 | def testInvalidInit(self): |
| 230 | self.assertRaises(TypeError, _fileio._FileIO, "1", 0, 0) |
| 231 | |
| 232 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 233 | def test_main(): |
| 234 | # Historically, these tests have been sloppy about removing TESTFN. |
| 235 | # So get rid of it no matter what. |
| 236 | try: |
| 237 | run_unittest(AutoFileTests, OtherFileTests) |
| 238 | finally: |
| 239 | if os.path.exists(TESTFN): |
| 240 | os.unlink(TESTFN) |
| 241 | |
| 242 | if __name__ == '__main__': |
| 243 | test_main() |