test_largefile.py revision a5af2148ee7941a033f3ecbc73bc180061416589
1#!python 2 3#---------------------------------------------------------------------- 4# test largefile support on system where this makes sense 5# 6#XXX how to only run this when support is there 7#XXX how to only optionally run this, it will take along time 8#---------------------------------------------------------------------- 9 10import test_support 11import os, struct, stat, sys 12 13 14# only run if the current system support large files 15f = open(test_support.TESTFN, 'w') 16try: 17 # 2**31 == 2147483648 18 f.seek(2147483649L) 19except (IOError, OverflowError): 20 f.close() 21 os.unlink(test_support.TESTFN) 22 raise test_support.TestSkipped, \ 23 "platform does not have largefile support" 24else: 25 f.close() 26 27 28# create >2GB file (2GB = 2147483648 bytes) 29size = 2500000000L 30name = test_support.TESTFN 31 32 33# on Windows this test comsumes large resources: 34# it takes a long time to build the >2GB file and takes >2GB of disk space 35# therefore test_support.use_large_resources must be defined to run this test 36if sys.platform[:3] == 'win' and not test_support.use_large_resources: 37 raise test_support.TestSkipped, \ 38 "test requires %s bytes and a long time to run" % str(size) 39 40 41 42def expect(got_this, expect_this): 43 if test_support.verbose: 44 print '%s =?= %s ...' % (`got_this`, `expect_this`), 45 if got_this != expect_this: 46 if test_support.verbose: 47 print 'no' 48 raise test_support.TestFailed, 'got %s, but expected %s' %\ 49 (str(got_this), str(expect_this)) 50 else: 51 if test_support.verbose: 52 print 'yes' 53 54 55# test that each file function works as expected for a large (i.e. >2GB, do 56# we have to check >4GB) files 57 58if test_support.verbose: 59 print 'create large file via seek (may be sparse file) ...' 60f = open(name, 'w') 61f.seek(size) 62f.write('a') 63f.flush() 64expect(os.fstat(f.fileno())[stat.ST_SIZE], size+1) 65if test_support.verbose: 66 print 'check file size with os.fstat' 67f.close() 68if test_support.verbose: 69 print 'check file size with os.stat' 70expect(os.stat(name)[stat.ST_SIZE], size+1) 71 72if test_support.verbose: 73 print 'play around with seek() and read() with the built largefile' 74f = open(name, 'r') 75expect(f.tell(), 0) 76expect(f.read(1), '\000') 77expect(f.tell(), 1) 78f.seek(0) 79expect(f.tell(), 0) 80f.seek(0, 0) 81expect(f.tell(), 0) 82f.seek(42) 83expect(f.tell(), 42) 84f.seek(42, 0) 85expect(f.tell(), 42) 86f.seek(42, 1) 87expect(f.tell(), 84) 88f.seek(0, 1) 89expect(f.tell(), 84) 90f.seek(0, 2) # seek from the end 91expect(f.tell(), size + 1 + 0) 92f.seek(-10, 2) 93expect(f.tell(), size + 1 - 10) 94f.seek(-size-1, 2) 95expect(f.tell(), 0) 96f.seek(size) 97expect(f.tell(), size) 98expect(f.read(1), 'a') # the 'a' that was written at the end of the file above 99f.close() 100 101if test_support.verbose: 102 print 'play around with os.lseek() with the built largefile' 103f = open(name, 'r') 104expect(os.lseek(f.fileno(), 0, 0), 0) 105expect(os.lseek(f.fileno(), 42, 0), 42) 106expect(os.lseek(f.fileno(), 42, 1), 84) 107expect(os.lseek(f.fileno(), 0, 1), 84) 108expect(os.lseek(f.fileno(), 0, 2), size+1+0) 109expect(os.lseek(f.fileno(), -10, 2), size+1-10) 110expect(os.lseek(f.fileno(), -size-1, 2), 0) 111expect(os.lseek(f.fileno(), size, 0), size) 112expect(f.read(1), 'a') # the 'a' that was written at the end of the file above 113f.close() 114 115 116# XXX add tests for truncate if it exists 117# XXX has truncate ever worked on Windows? specifically on WinNT I get: 118# "IOError: [Errno 13] Permission denied" 119##try: 120## newsize = size - 10 121## f.seek(newsize) 122## f.truncate() 123## expect(f.tell(), newsize) 124## newsize = newsize - 1 125## f.seek(0) 126## f.truncate(newsize) 127## expect(f.tell(), newsize) 128##except AttributeError: 129## pass 130 131os.unlink(name) 132print >>sys.stderr, name, "exists:", os.path.exists(name) 133