Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 1 | #!python |
| 2 | |
| 3 | #---------------------------------------------------------------------- |
| 4 | # test largefile support on system where this makes sense |
| 5 | # |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 6 | #---------------------------------------------------------------------- |
| 7 | |
| 8 | import test_support |
| 9 | import os, struct, stat, sys |
| 10 | |
| 11 | |
Guido van Rossum | a31ddbb | 2001-09-10 15:03:18 +0000 | [diff] [blame] | 12 | # create >2GB file (2GB = 2147483648 bytes) |
| 13 | size = 2500000000L |
| 14 | name = test_support.TESTFN |
| 15 | |
| 16 | |
Barry Warsaw | 7fdfc38 | 2001-08-20 22:37:34 +0000 | [diff] [blame] | 17 | # On Windows this test comsumes large resources; It takes a long time to build |
| 18 | # the >2GB file and takes >2GB of disk space therefore the resource must be |
| 19 | # enabled to run this test. If not, nothing after this line stanza will be |
| 20 | # executed. |
| 21 | if sys.platform[:3] == 'win': |
| 22 | test_support.requires( |
| 23 | 'largefile', |
| 24 | 'test requires %s bytes and a long time to run' % str(size)) |
Guido van Rossum | 47f4034 | 2001-09-10 13:34:12 +0000 | [diff] [blame] | 25 | else: |
| 26 | # Only run if the current filesystem supports large files. |
| 27 | # (Skip this test on Windows, since we now always support large files.) |
| 28 | f = open(test_support.TESTFN, 'wb') |
| 29 | try: |
| 30 | # 2**31 == 2147483648 |
| 31 | f.seek(2147483649L) |
| 32 | # Seeking is not enough of a test: you must write and flush, too! |
| 33 | f.write("x") |
| 34 | f.flush() |
| 35 | except (IOError, OverflowError): |
| 36 | f.close() |
| 37 | os.unlink(test_support.TESTFN) |
| 38 | raise test_support.TestSkipped, \ |
| 39 | "filesystem does not have largefile support" |
| 40 | else: |
| 41 | f.close() |
| 42 | |
| 43 | |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 44 | def expect(got_this, expect_this): |
Fred Drake | dce5641 | 2000-10-23 16:38:20 +0000 | [diff] [blame] | 45 | if test_support.verbose: |
Tim Peters | b8c0230 | 2001-09-06 01:17:45 +0000 | [diff] [blame] | 46 | print '%r =?= %r ...' % (got_this, expect_this), |
Fred Drake | dce5641 | 2000-10-23 16:38:20 +0000 | [diff] [blame] | 47 | if got_this != expect_this: |
| 48 | if test_support.verbose: |
| 49 | print 'no' |
Tim Peters | b8c0230 | 2001-09-06 01:17:45 +0000 | [diff] [blame] | 50 | raise test_support.TestFailed, 'got %r, but expected %r' %\ |
| 51 | (got_this, expect_this) |
Fred Drake | dce5641 | 2000-10-23 16:38:20 +0000 | [diff] [blame] | 52 | else: |
| 53 | if test_support.verbose: |
| 54 | print 'yes' |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 55 | |
| 56 | |
| 57 | # test that each file function works as expected for a large (i.e. >2GB, do |
| 58 | # we have to check >4GB) files |
| 59 | |
| 60 | if test_support.verbose: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 61 | print 'create large file via seek (may be sparse file) ...' |
Tim Peters | 6e13a56 | 2001-09-06 00:32:15 +0000 | [diff] [blame] | 62 | f = open(name, 'wb') |
Tim Peters | b8c0230 | 2001-09-06 01:17:45 +0000 | [diff] [blame] | 63 | f.write('z') |
| 64 | f.seek(0) |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 65 | f.seek(size) |
| 66 | f.write('a') |
| 67 | f.flush() |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 68 | if test_support.verbose: |
Fred Drake | dce5641 | 2000-10-23 16:38:20 +0000 | [diff] [blame] | 69 | print 'check file size with os.fstat' |
Tim Peters | 6e13a56 | 2001-09-06 00:32:15 +0000 | [diff] [blame] | 70 | expect(os.fstat(f.fileno())[stat.ST_SIZE], size+1) |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 71 | f.close() |
| 72 | if test_support.verbose: |
Fred Drake | dce5641 | 2000-10-23 16:38:20 +0000 | [diff] [blame] | 73 | print 'check file size with os.stat' |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 74 | expect(os.stat(name)[stat.ST_SIZE], size+1) |
| 75 | |
| 76 | if test_support.verbose: |
Fred Drake | dce5641 | 2000-10-23 16:38:20 +0000 | [diff] [blame] | 77 | print 'play around with seek() and read() with the built largefile' |
Tim Peters | 6e13a56 | 2001-09-06 00:32:15 +0000 | [diff] [blame] | 78 | f = open(name, 'rb') |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 79 | expect(f.tell(), 0) |
Tim Peters | b8c0230 | 2001-09-06 01:17:45 +0000 | [diff] [blame] | 80 | expect(f.read(1), 'z') |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 81 | expect(f.tell(), 1) |
| 82 | f.seek(0) |
| 83 | expect(f.tell(), 0) |
| 84 | f.seek(0, 0) |
| 85 | expect(f.tell(), 0) |
| 86 | f.seek(42) |
| 87 | expect(f.tell(), 42) |
| 88 | f.seek(42, 0) |
| 89 | expect(f.tell(), 42) |
| 90 | f.seek(42, 1) |
| 91 | expect(f.tell(), 84) |
| 92 | f.seek(0, 1) |
| 93 | expect(f.tell(), 84) |
| 94 | f.seek(0, 2) # seek from the end |
| 95 | expect(f.tell(), size + 1 + 0) |
| 96 | f.seek(-10, 2) |
| 97 | expect(f.tell(), size + 1 - 10) |
| 98 | f.seek(-size-1, 2) |
| 99 | expect(f.tell(), 0) |
| 100 | f.seek(size) |
| 101 | expect(f.tell(), size) |
| 102 | expect(f.read(1), 'a') # the 'a' that was written at the end of the file above |
Tim Peters | b8c0230 | 2001-09-06 01:17:45 +0000 | [diff] [blame] | 103 | f.seek(-size-1, 1) |
| 104 | expect(f.read(1), 'z') |
| 105 | expect(f.tell(), 1) |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 106 | f.close() |
| 107 | |
| 108 | if test_support.verbose: |
Fred Drake | dce5641 | 2000-10-23 16:38:20 +0000 | [diff] [blame] | 109 | print 'play around with os.lseek() with the built largefile' |
Tim Peters | b8c0230 | 2001-09-06 01:17:45 +0000 | [diff] [blame] | 110 | f = open(name, 'rb') |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 111 | expect(os.lseek(f.fileno(), 0, 0), 0) |
| 112 | expect(os.lseek(f.fileno(), 42, 0), 42) |
| 113 | expect(os.lseek(f.fileno(), 42, 1), 84) |
| 114 | expect(os.lseek(f.fileno(), 0, 1), 84) |
| 115 | expect(os.lseek(f.fileno(), 0, 2), size+1+0) |
| 116 | expect(os.lseek(f.fileno(), -10, 2), size+1-10) |
| 117 | expect(os.lseek(f.fileno(), -size-1, 2), 0) |
| 118 | expect(os.lseek(f.fileno(), size, 0), size) |
| 119 | expect(f.read(1), 'a') # the 'a' that was written at the end of the file above |
| 120 | f.close() |
| 121 | |
| 122 | |
| 123 | # XXX add tests for truncate if it exists |
| 124 | # XXX has truncate ever worked on Windows? specifically on WinNT I get: |
| 125 | # "IOError: [Errno 13] Permission denied" |
| 126 | ##try: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 127 | ## newsize = size - 10 |
| 128 | ## f.seek(newsize) |
| 129 | ## f.truncate() |
| 130 | ## expect(f.tell(), newsize) |
| 131 | ## newsize = newsize - 1 |
| 132 | ## f.seek(0) |
| 133 | ## f.truncate(newsize) |
| 134 | ## expect(f.tell(), newsize) |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 135 | ##except AttributeError: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 136 | ## pass |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 137 | |
| 138 | os.unlink(name) |