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 | |
| 9 | from test.test_support import TESTFN, findfile, run_unittest |
| 10 | from UserList import UserList |
| 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 |
| 51 | # XXX do we want these? |
| 52 | #f.name # merely shouldn't blow up |
| 53 | #f.mode # ditto |
| 54 | #f.closed # ditto |
| 55 | |
| 56 | # verify the others aren't |
| 57 | for attr in 'name', 'mode', 'closed': |
| 58 | self.assertRaises((AttributeError, TypeError), setattr, f, attr, 'oops') |
| 59 | |
| 60 | def testReadinto(self): |
| 61 | # verify readinto |
| 62 | self.f.write(bytes([1, 2])) |
| 63 | self.f.close() |
| 64 | a = array('b', 'x'*10) |
| 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): |
| 70 | # verify repr works |
| 71 | return # XXX doesn't work yet |
| 72 | self.assert_(repr(self.f).startswith("<open file '" + TESTFN)) |
| 73 | |
| 74 | def testErrors(self): |
| 75 | f = self.f |
| 76 | self.assert_(not f.isatty()) |
| 77 | #self.assert_(not f.closed) # XXX Do we want to support these? |
| 78 | #self.assertEquals(f.name, TESTFN) |
| 79 | |
| 80 | self.assertRaises(TypeError, f.readinto, "") |
| 81 | f.close() |
| 82 | #self.assert_(f.closed) # XXX |
| 83 | |
| 84 | def testMethods(self): |
| 85 | methods = ['fileno', 'isatty', 'read', 'readinto', |
| 86 | 'seek', 'tell', 'truncate', 'write', 'seekable', |
| 87 | 'readable', 'writable'] |
| 88 | if sys.platform.startswith('atheos'): |
| 89 | methods.remove('truncate') |
| 90 | |
| 91 | # __exit__ should close the file |
| 92 | self.f.__exit__(None, None, None) |
| 93 | #self.assert_(self.f.closed) # XXX |
| 94 | |
| 95 | for methodname in methods: |
| 96 | method = getattr(self.f, methodname) |
| 97 | # should raise on closed file |
| 98 | self.assertRaises(ValueError, method) |
| 99 | |
| 100 | # file is closed, __exit__ shouldn't do anything |
| 101 | self.assertEquals(self.f.__exit__(None, None, None), None) |
| 102 | # it must also return None if an exception was given |
| 103 | try: |
| 104 | 1/0 |
| 105 | except: |
| 106 | self.assertEquals(self.f.__exit__(*sys.exc_info()), None) |
| 107 | |
| 108 | |
| 109 | class OtherFileTests(unittest.TestCase): |
| 110 | |
| 111 | def testAbles(self): |
| 112 | try: |
| 113 | f = _fileio._FileIO(TESTFN, "w") |
| 114 | self.assertEquals(f.readable(), False) |
| 115 | self.assertEquals(f.writable(), True) |
| 116 | self.assertEquals(f.seekable(), True) |
| 117 | f.close() |
| 118 | |
| 119 | f = _fileio._FileIO(TESTFN, "r") |
| 120 | self.assertEquals(f.readable(), True) |
| 121 | self.assertEquals(f.writable(), False) |
| 122 | self.assertEquals(f.seekable(), True) |
| 123 | f.close() |
| 124 | |
| 125 | f = _fileio._FileIO(TESTFN, "a+") |
| 126 | self.assertEquals(f.readable(), True) |
| 127 | self.assertEquals(f.writable(), True) |
| 128 | self.assertEquals(f.seekable(), True) |
| 129 | self.assertEquals(f.isatty(), False) |
| 130 | f.close() |
| 131 | |
| 132 | f = _fileio._FileIO("/dev/tty", "a") # XXX, won't work on e.g., Windows |
| 133 | self.assertEquals(f.readable(), False) |
| 134 | self.assertEquals(f.writable(), True) |
| 135 | ##self.assertEquals(f.seekable(), False) # XXX True on OSX!? |
| 136 | self.assertEquals(f.isatty(), True) |
| 137 | f.close() |
| 138 | finally: |
| 139 | os.unlink(TESTFN) |
| 140 | |
| 141 | def testModeStrings(self): |
| 142 | # check invalid mode strings |
| 143 | for mode in ("", "aU", "wU+", "rb", "rt"): |
| 144 | try: |
| 145 | f = _fileio._FileIO(TESTFN, mode) |
| 146 | except ValueError: |
| 147 | pass |
| 148 | else: |
| 149 | f.close() |
| 150 | self.fail('%r is an invalid file mode' % mode) |
| 151 | |
| 152 | def testStdin(self): |
| 153 | ## This causes the interpreter to exit on OSF1 v5.1. |
| 154 | #if sys.platform != 'osf1V5': |
| 155 | # self.assertRaises(IOError, sys.stdin.seek, -1) |
| 156 | #else: |
| 157 | # print(( |
| 158 | # ' Skipping sys.stdin.seek(-1), it may crash the interpreter.' |
| 159 | # ' Test manually.'), file=sys.__stdout__) |
| 160 | #self.assertRaises(IOError, sys.stdin.truncate) |
| 161 | # XXX Comment this out since sys.stdin is currently an old style file |
| 162 | pass |
| 163 | |
| 164 | def testUnicodeOpen(self): |
| 165 | # verify repr works for unicode too |
| 166 | f = _fileio._FileIO(unicode(TESTFN), "w") |
| 167 | # XXX doesn't work yet: |
| 168 | ##self.assert_(repr(f).startswith("<open file u'" + TESTFN)) |
| 169 | f.close() |
| 170 | os.unlink(TESTFN) |
| 171 | |
| 172 | def testBadModeArgument(self): |
| 173 | # verify that we get a sensible error message for bad mode argument |
| 174 | bad_mode = "qwerty" |
| 175 | try: |
| 176 | f = _fileio._FileIO(TESTFN, bad_mode) |
| 177 | except ValueError as msg: |
| 178 | if msg.message != 0: |
| 179 | s = str(msg) |
| 180 | if s.find(TESTFN) != -1 or s.find(bad_mode) == -1: |
| 181 | self.fail("bad error message for invalid mode: %s" % s) |
| 182 | # if msg[0] == 0, we're probably on Windows where there may be |
| 183 | # no obvious way to discover why open() failed. |
| 184 | else: |
| 185 | f.close() |
| 186 | self.fail("no error for invalid mode: %s" % bad_mode) |
| 187 | |
| 188 | def testTruncateOnWindows(self): |
| 189 | def bug801631(): |
| 190 | # SF bug <http://www.python.org/sf/801631> |
| 191 | # "file.truncate fault on windows" |
| 192 | f = _fileio._FileIO(TESTFN, 'w') |
| 193 | f.write(bytes(range(11))) |
| 194 | f.close() |
| 195 | |
| 196 | f = _fileio._FileIO(TESTFN,'r+') |
| 197 | data = f.read(5) |
| 198 | if data != bytes(range(5)): |
| 199 | self.fail("Read on file opened for update failed %r" % data) |
| 200 | if f.tell() != 5: |
| 201 | self.fail("File pos after read wrong %d" % f.tell()) |
| 202 | |
| 203 | f.truncate() |
| 204 | if f.tell() != 5: |
| 205 | self.fail("File pos after ftruncate wrong %d" % f.tell()) |
| 206 | |
| 207 | f.close() |
| 208 | size = os.path.getsize(TESTFN) |
| 209 | if size != 5: |
| 210 | self.fail("File size after ftruncate wrong %d" % size) |
| 211 | |
| 212 | try: |
| 213 | bug801631() |
| 214 | finally: |
| 215 | os.unlink(TESTFN) |
| 216 | |
| 217 | def test_main(): |
| 218 | # Historically, these tests have been sloppy about removing TESTFN. |
| 219 | # So get rid of it no matter what. |
| 220 | try: |
| 221 | run_unittest(AutoFileTests, OtherFileTests) |
| 222 | finally: |
| 223 | if os.path.exists(TESTFN): |
| 224 | os.unlink(TESTFN) |
| 225 | |
| 226 | if __name__ == '__main__': |
| 227 | test_main() |