Martin v. Löwis | f90ae20 | 2002-06-11 06:22:31 +0000 | [diff] [blame] | 1 | import sys |
Fred Drake | 2ec80fa | 2000-10-23 16:59:35 +0000 | [diff] [blame] | 2 | import os |
Neal Norwitz | 62f5a9d | 2002-04-01 00:09:00 +0000 | [diff] [blame] | 3 | from array import array |
Raymond Hettinger | cb87bc8 | 2004-05-31 00:35:52 +0000 | [diff] [blame] | 4 | from weakref import proxy |
Fred Drake | 2ec80fa | 2000-10-23 16:59:35 +0000 | [diff] [blame] | 5 | |
Thomas Wouters | c45251a | 2006-02-12 11:53:32 +0000 | [diff] [blame] | 6 | from test.test_support import verify, TESTFN, TestFailed, findfile |
Marc-André Lemburg | fa44d79 | 2000-08-25 22:37:31 +0000 | [diff] [blame] | 7 | from UserList import UserList |
| 8 | |
Raymond Hettinger | cb87bc8 | 2004-05-31 00:35:52 +0000 | [diff] [blame] | 9 | # verify weak references |
| 10 | f = file(TESTFN, 'w') |
| 11 | p = proxy(f) |
| 12 | p.write('teststring') |
| 13 | verify(f.tell(), p.tell()) |
| 14 | f.close() |
| 15 | f = None |
| 16 | try: |
| 17 | p.tell() |
| 18 | except ReferenceError: |
| 19 | pass |
| 20 | else: |
| 21 | raise TestFailed('file proxy still exists when the file is gone') |
| 22 | |
Tim Peters | 015dd82 | 2003-05-04 04:16:52 +0000 | [diff] [blame] | 23 | # verify expected attributes exist |
| 24 | f = file(TESTFN, 'w') |
| 25 | softspace = f.softspace |
| 26 | f.name # merely shouldn't blow up |
| 27 | f.mode # ditto |
| 28 | f.closed # ditto |
| 29 | |
| 30 | # verify softspace is writable |
| 31 | f.softspace = softspace # merely shouldn't blow up |
| 32 | |
| 33 | # verify the others aren't |
| 34 | for attr in 'name', 'mode', 'closed': |
| 35 | try: |
| 36 | setattr(f, attr, 'oops') |
Barry Warsaw | b180c06 | 2005-04-20 19:41:36 +0000 | [diff] [blame] | 37 | except (AttributeError, TypeError): |
Tim Peters | 015dd82 | 2003-05-04 04:16:52 +0000 | [diff] [blame] | 38 | pass |
| 39 | else: |
Barry Warsaw | b180c06 | 2005-04-20 19:41:36 +0000 | [diff] [blame] | 40 | raise TestFailed('expected exception setting file attr %r' % attr) |
Tim Peters | 015dd82 | 2003-05-04 04:16:52 +0000 | [diff] [blame] | 41 | f.close() |
| 42 | |
Skip Montanaro | bbf12ba | 2005-05-20 03:07:06 +0000 | [diff] [blame] | 43 | # check invalid mode strings |
| 44 | for mode in ("", "aU", "wU+"): |
| 45 | try: |
| 46 | f = file(TESTFN, mode) |
| 47 | except ValueError: |
| 48 | pass |
| 49 | else: |
| 50 | f.close() |
| 51 | raise TestFailed('%r is an invalid file mode' % mode) |
| 52 | |
Marc-André Lemburg | fa44d79 | 2000-08-25 22:37:31 +0000 | [diff] [blame] | 53 | # verify writelines with instance sequence |
| 54 | l = UserList(['1', '2']) |
| 55 | f = open(TESTFN, 'wb') |
| 56 | f.writelines(l) |
| 57 | f.close() |
| 58 | f = open(TESTFN, 'rb') |
| 59 | buf = f.read() |
| 60 | f.close() |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 61 | verify(buf == '12') |
Marc-André Lemburg | fa44d79 | 2000-08-25 22:37:31 +0000 | [diff] [blame] | 62 | |
Neal Norwitz | 62f5a9d | 2002-04-01 00:09:00 +0000 | [diff] [blame] | 63 | # verify readinto |
| 64 | a = array('c', 'x'*10) |
| 65 | f = open(TESTFN, 'rb') |
| 66 | n = f.readinto(a) |
| 67 | f.close() |
| 68 | verify(buf == a.tostring()[:n]) |
| 69 | |
Marc-André Lemburg | fa44d79 | 2000-08-25 22:37:31 +0000 | [diff] [blame] | 70 | # verify writelines with integers |
| 71 | f = open(TESTFN, 'wb') |
| 72 | try: |
| 73 | f.writelines([1, 2, 3]) |
| 74 | except TypeError: |
| 75 | pass |
| 76 | else: |
| 77 | print "writelines accepted sequence of integers" |
| 78 | f.close() |
| 79 | |
| 80 | # verify writelines with integers in UserList |
| 81 | f = open(TESTFN, 'wb') |
| 82 | l = UserList([1,2,3]) |
| 83 | try: |
| 84 | f.writelines(l) |
| 85 | except TypeError: |
| 86 | pass |
| 87 | else: |
| 88 | print "writelines accepted sequence of integers" |
| 89 | f.close() |
| 90 | |
| 91 | # verify writelines with non-string object |
| 92 | class NonString: pass |
| 93 | |
| 94 | f = open(TESTFN, 'wb') |
| 95 | try: |
| 96 | f.writelines([NonString(), NonString()]) |
| 97 | except TypeError: |
| 98 | pass |
| 99 | else: |
| 100 | print "writelines accepted sequence of non-string objects" |
| 101 | f.close() |
Fred Drake | 2ec80fa | 2000-10-23 16:59:35 +0000 | [diff] [blame] | 102 | |
Neal Norwitz | fcf4435 | 2005-11-27 20:37:43 +0000 | [diff] [blame] | 103 | try: |
Neal Norwitz | a6fc397 | 2005-12-05 01:17:03 +0000 | [diff] [blame] | 104 | sys.stdin.seek(-1) |
Neal Norwitz | fcf4435 | 2005-11-27 20:37:43 +0000 | [diff] [blame] | 105 | except IOError: |
| 106 | pass |
| 107 | else: |
| 108 | print "should not be able to seek on sys.stdin" |
| 109 | |
| 110 | try: |
Neal Norwitz | fcf4435 | 2005-11-27 20:37:43 +0000 | [diff] [blame] | 111 | sys.stdin.truncate() |
| 112 | except IOError: |
| 113 | pass |
| 114 | else: |
| 115 | print "should not be able to truncate on sys.stdin" |
| 116 | |
| 117 | # verify repr works |
| 118 | f = open(TESTFN) |
| 119 | if not repr(f).startswith("<open file '" + TESTFN): |
| 120 | print "repr(file) failed" |
| 121 | f.close() |
| 122 | |
| 123 | # verify repr works for unicode too |
| 124 | f = open(unicode(TESTFN)) |
| 125 | if not repr(f).startswith("<open file u'" + TESTFN): |
| 126 | print "repr(file with unicode name) failed" |
| 127 | f.close() |
| 128 | |
Jeremy Hylton | 734c7fb | 2001-11-09 19:34:43 +0000 | [diff] [blame] | 129 | # verify that we get a sensible error message for bad mode argument |
Jeremy Hylton | 41c8321 | 2001-11-09 16:17:24 +0000 | [diff] [blame] | 130 | bad_mode = "qwerty" |
| 131 | try: |
| 132 | open(TESTFN, bad_mode) |
| 133 | except IOError, msg: |
Jeremy Hylton | 734c7fb | 2001-11-09 19:34:43 +0000 | [diff] [blame] | 134 | if msg[0] != 0: |
| 135 | s = str(msg) |
| 136 | if s.find(TESTFN) != -1 or s.find(bad_mode) == -1: |
| 137 | print "bad error message for invalid mode: %s" % s |
| 138 | # if msg[0] == 0, we're probably on Windows where there may be |
| 139 | # no obvious way to discover why open() failed. |
Jeremy Hylton | 41c8321 | 2001-11-09 16:17:24 +0000 | [diff] [blame] | 140 | else: |
| 141 | print "no error for invalid mode: %s" % bad_mode |
| 142 | |
Neal Norwitz | 653d85f | 2002-01-01 19:11:13 +0000 | [diff] [blame] | 143 | f = open(TESTFN) |
| 144 | if f.name != TESTFN: |
Neal Norwitz | b1295da | 2002-04-01 18:59:20 +0000 | [diff] [blame] | 145 | raise TestFailed, 'file.name should be "%s"' % TESTFN |
Neal Norwitz | 653d85f | 2002-01-01 19:11:13 +0000 | [diff] [blame] | 146 | if f.isatty(): |
Neal Norwitz | b1295da | 2002-04-01 18:59:20 +0000 | [diff] [blame] | 147 | raise TestFailed, 'file.isatty() should be false' |
Neal Norwitz | 653d85f | 2002-01-01 19:11:13 +0000 | [diff] [blame] | 148 | |
| 149 | if f.closed: |
Neal Norwitz | b1295da | 2002-04-01 18:59:20 +0000 | [diff] [blame] | 150 | raise TestFailed, 'file.closed should be false' |
Neal Norwitz | 653d85f | 2002-01-01 19:11:13 +0000 | [diff] [blame] | 151 | |
Neal Norwitz | 62f5a9d | 2002-04-01 00:09:00 +0000 | [diff] [blame] | 152 | try: |
| 153 | f.readinto("") |
| 154 | except TypeError: |
| 155 | pass |
| 156 | else: |
Neal Norwitz | b1295da | 2002-04-01 18:59:20 +0000 | [diff] [blame] | 157 | raise TestFailed, 'file.readinto("") should raise a TypeError' |
Neal Norwitz | 62f5a9d | 2002-04-01 00:09:00 +0000 | [diff] [blame] | 158 | |
Neal Norwitz | 653d85f | 2002-01-01 19:11:13 +0000 | [diff] [blame] | 159 | f.close() |
| 160 | if not f.closed: |
Neal Norwitz | b1295da | 2002-04-01 18:59:20 +0000 | [diff] [blame] | 161 | raise TestFailed, 'file.closed should be true' |
Neal Norwitz | 653d85f | 2002-01-01 19:11:13 +0000 | [diff] [blame] | 162 | |
Andrew MacIntyre | 4e10ed3 | 2004-04-04 07:01:35 +0000 | [diff] [blame] | 163 | # make sure that explicitly setting the buffer size doesn't cause |
| 164 | # misbehaviour especially with repeated close() calls |
| 165 | for s in (-1, 0, 1, 512): |
| 166 | try: |
| 167 | f = open(TESTFN, 'w', s) |
| 168 | f.write(str(s)) |
| 169 | f.close() |
| 170 | f.close() |
| 171 | f = open(TESTFN, 'r', s) |
| 172 | d = int(f.read()) |
| 173 | f.close() |
| 174 | f.close() |
| 175 | except IOError, msg: |
| 176 | raise TestFailed, 'error setting buffer size %d: %s' % (s, str(msg)) |
| 177 | if d != s: |
| 178 | raise TestFailed, 'readback failure using buffer size %d' |
| 179 | |
Guido van Rossum | 3c668c1 | 2002-08-06 15:58:24 +0000 | [diff] [blame] | 180 | methods = ['fileno', 'flush', 'isatty', 'next', 'read', 'readinto', |
| 181 | 'readline', 'readlines', 'seek', 'tell', 'truncate', 'write', |
| 182 | 'xreadlines', '__iter__'] |
Martin v. Löwis | f90ae20 | 2002-06-11 06:22:31 +0000 | [diff] [blame] | 183 | if sys.platform.startswith('atheos'): |
| 184 | methods.remove('truncate') |
| 185 | |
| 186 | for methodname in methods: |
Neal Norwitz | 653d85f | 2002-01-01 19:11:13 +0000 | [diff] [blame] | 187 | method = getattr(f, methodname) |
| 188 | try: |
| 189 | method() |
| 190 | except ValueError: |
| 191 | pass |
| 192 | else: |
Neal Norwitz | b1295da | 2002-04-01 18:59:20 +0000 | [diff] [blame] | 193 | raise TestFailed, 'file.%s() on a closed file should raise a ValueError' % methodname |
Neal Norwitz | 653d85f | 2002-01-01 19:11:13 +0000 | [diff] [blame] | 194 | |
| 195 | try: |
| 196 | f.writelines([]) |
| 197 | except ValueError: |
| 198 | pass |
| 199 | else: |
Neal Norwitz | b1295da | 2002-04-01 18:59:20 +0000 | [diff] [blame] | 200 | raise TestFailed, 'file.writelines([]) on a closed file should raise a ValueError' |
Neal Norwitz | 653d85f | 2002-01-01 19:11:13 +0000 | [diff] [blame] | 201 | |
Fred Drake | 2ec80fa | 2000-10-23 16:59:35 +0000 | [diff] [blame] | 202 | os.unlink(TESTFN) |
Tim Peters | f1827cf | 2003-09-07 03:30:18 +0000 | [diff] [blame] | 203 | |
| 204 | def bug801631(): |
| 205 | # SF bug <http://www.python.org/sf/801631> |
| 206 | # "file.truncate fault on windows" |
| 207 | f = file(TESTFN, 'wb') |
| 208 | f.write('12345678901') # 11 bytes |
| 209 | f.close() |
| 210 | |
| 211 | f = file(TESTFN,'rb+') |
| 212 | data = f.read(5) |
| 213 | if data != '12345': |
| 214 | raise TestFailed("Read on file opened for update failed %r" % data) |
| 215 | if f.tell() != 5: |
| 216 | raise TestFailed("File pos after read wrong %d" % f.tell()) |
| 217 | |
| 218 | f.truncate() |
| 219 | if f.tell() != 5: |
| 220 | raise TestFailed("File pos after ftruncate wrong %d" % f.tell()) |
| 221 | |
| 222 | f.close() |
| 223 | size = os.path.getsize(TESTFN) |
| 224 | if size != 5: |
| 225 | raise TestFailed("File size after ftruncate wrong %d" % size) |
| 226 | |
| 227 | try: |
| 228 | bug801631() |
| 229 | finally: |
| 230 | os.unlink(TESTFN) |
Thomas Wouters | c45251a | 2006-02-12 11:53:32 +0000 | [diff] [blame] | 231 | |
| 232 | # Test the complex interaction when mixing file-iteration and the various |
| 233 | # read* methods. Ostensibly, the mixture could just be tested to work |
| 234 | # when it should work according to the Python language, instead of fail |
| 235 | # when it should fail according to the current CPython implementation. |
| 236 | # People don't always program Python the way they should, though, and the |
| 237 | # implemenation might change in subtle ways, so we explicitly test for |
| 238 | # errors, too; the test will just have to be updated when the |
| 239 | # implementation changes. |
| 240 | dataoffset = 16384 |
| 241 | filler = "ham\n" |
| 242 | assert not dataoffset % len(filler), \ |
| 243 | "dataoffset must be multiple of len(filler)" |
| 244 | nchunks = dataoffset // len(filler) |
| 245 | testlines = [ |
| 246 | "spam, spam and eggs\n", |
| 247 | "eggs, spam, ham and spam\n", |
| 248 | "saussages, spam, spam and eggs\n", |
| 249 | "spam, ham, spam and eggs\n", |
| 250 | "spam, spam, spam, spam, spam, ham, spam\n", |
| 251 | "wonderful spaaaaaam.\n" |
| 252 | ] |
| 253 | methods = [("readline", ()), ("read", ()), ("readlines", ()), |
| 254 | ("readinto", (array("c", " "*100),))] |
| 255 | |
| 256 | try: |
| 257 | # Prepare the testfile |
| 258 | bag = open(TESTFN, "w") |
| 259 | bag.write(filler * nchunks) |
| 260 | bag.writelines(testlines) |
| 261 | bag.close() |
| 262 | # Test for appropriate errors mixing read* and iteration |
| 263 | for methodname, args in methods: |
| 264 | f = open(TESTFN) |
| 265 | if f.next() != filler: |
| 266 | raise TestFailed, "Broken testfile" |
| 267 | meth = getattr(f, methodname) |
| 268 | try: |
| 269 | meth(*args) |
| 270 | except ValueError: |
| 271 | pass |
| 272 | else: |
| 273 | raise TestFailed("%s%r after next() didn't raise ValueError" % |
| 274 | (methodname, args)) |
| 275 | f.close() |
| 276 | |
| 277 | # Test to see if harmless (by accident) mixing of read* and iteration |
| 278 | # still works. This depends on the size of the internal iteration |
| 279 | # buffer (currently 8192,) but we can test it in a flexible manner. |
| 280 | # Each line in the bag o' ham is 4 bytes ("h", "a", "m", "\n"), so |
| 281 | # 4096 lines of that should get us exactly on the buffer boundary for |
| 282 | # any power-of-2 buffersize between 4 and 16384 (inclusive). |
| 283 | f = open(TESTFN) |
| 284 | for i in range(nchunks): |
| 285 | f.next() |
| 286 | testline = testlines.pop(0) |
| 287 | try: |
| 288 | line = f.readline() |
| 289 | except ValueError: |
| 290 | raise TestFailed("readline() after next() with supposedly empty " |
| 291 | "iteration-buffer failed anyway") |
| 292 | if line != testline: |
| 293 | raise TestFailed("readline() after next() with empty buffer " |
| 294 | "failed. Got %r, expected %r" % (line, testline)) |
| 295 | testline = testlines.pop(0) |
| 296 | buf = array("c", "\x00" * len(testline)) |
| 297 | try: |
| 298 | f.readinto(buf) |
| 299 | except ValueError: |
| 300 | raise TestFailed("readinto() after next() with supposedly empty " |
| 301 | "iteration-buffer failed anyway") |
| 302 | line = buf.tostring() |
| 303 | if line != testline: |
| 304 | raise TestFailed("readinto() after next() with empty buffer " |
| 305 | "failed. Got %r, expected %r" % (line, testline)) |
| 306 | |
| 307 | testline = testlines.pop(0) |
| 308 | try: |
| 309 | line = f.read(len(testline)) |
| 310 | except ValueError: |
| 311 | raise TestFailed("read() after next() with supposedly empty " |
| 312 | "iteration-buffer failed anyway") |
| 313 | if line != testline: |
| 314 | raise TestFailed("read() after next() with empty buffer " |
| 315 | "failed. Got %r, expected %r" % (line, testline)) |
| 316 | try: |
| 317 | lines = f.readlines() |
| 318 | except ValueError: |
| 319 | raise TestFailed("readlines() after next() with supposedly empty " |
| 320 | "iteration-buffer failed anyway") |
| 321 | if lines != testlines: |
| 322 | raise TestFailed("readlines() after next() with empty buffer " |
| 323 | "failed. Got %r, expected %r" % (line, testline)) |
| 324 | # Reading after iteration hit EOF shouldn't hurt either |
| 325 | f = open(TESTFN) |
| 326 | for line in f: |
| 327 | pass |
| 328 | try: |
| 329 | f.readline() |
| 330 | f.readinto(buf) |
| 331 | f.read() |
| 332 | f.readlines() |
| 333 | except ValueError: |
| 334 | raise TestFailed("read* failed after next() consumed file") |
| 335 | finally: |
| 336 | # Bare 'except' so as not to mask errors in the test |
| 337 | try: |
| 338 | os.unlink(TESTFN) |
| 339 | except: |
| 340 | pass |