PyXR

c:\python24\lib \ test \ test_largefile.py



0001 #!python
0002 
0003 #----------------------------------------------------------------------
0004 # test largefile support on system where this makes sense
0005 #
0006 #----------------------------------------------------------------------
0007 
0008 from test import test_support
0009 import os, struct, stat, sys
0010 
0011 try:
0012     import signal
0013     # The default handler for SIGXFSZ is to abort the process.
0014     # By ignoring it, system calls exceeding the file size resource
0015     # limit will raise IOError instead of crashing the interpreter.
0016     oldhandler = signal.signal(signal.SIGXFSZ, signal.SIG_IGN)
0017 except (ImportError, AttributeError):
0018     pass
0019 
0020 
0021 # create >2GB file (2GB = 2147483648 bytes)
0022 size = 2500000000L
0023 name = test_support.TESTFN
0024 
0025 
0026 # On Windows and Mac OSX this test comsumes large resources; It takes
0027 # a long time to build the >2GB file and takes >2GB of disk space
0028 # therefore the resource must be enabled to run this test.  If not,
0029 # nothing after this line stanza will be executed.
0030 if sys.platform[:3] == 'win' or sys.platform == 'darwin':
0031     test_support.requires(
0032         'largefile',
0033         'test requires %s bytes and a long time to run' % str(size))
0034 else:
0035     # Only run if the current filesystem supports large files.
0036     # (Skip this test on Windows, since we now always support large files.)
0037     f = open(test_support.TESTFN, 'wb')
0038     try:
0039         # 2**31 == 2147483648
0040         f.seek(2147483649L)
0041         # Seeking is not enough of a test: you must write and flush, too!
0042         f.write("x")
0043         f.flush()
0044     except (IOError, OverflowError):
0045         f.close()
0046         os.unlink(test_support.TESTFN)
0047         raise test_support.TestSkipped, \
0048               "filesystem does not have largefile support"
0049     else:
0050         f.close()
0051 
0052 
0053 def expect(got_this, expect_this):
0054     if test_support.verbose:
0055         print '%r =?= %r ...' % (got_this, expect_this),
0056     if got_this != expect_this:
0057         if test_support.verbose:
0058             print 'no'
0059         raise test_support.TestFailed, 'got %r, but expected %r' %\
0060               (got_this, expect_this)
0061     else:
0062         if test_support.verbose:
0063             print 'yes'
0064 
0065 
0066 # test that each file function works as expected for a large (i.e. >2GB, do
0067 # we have to check >4GB) files
0068 
0069 if test_support.verbose:
0070     print 'create large file via seek (may be sparse file) ...'
0071 f = open(name, 'wb')
0072 try:
0073     f.write('z')
0074     f.seek(0)
0075     f.seek(size)
0076     f.write('a')
0077     f.flush()
0078     if test_support.verbose:
0079         print 'check file size with os.fstat'
0080     expect(os.fstat(f.fileno())[stat.ST_SIZE], size+1)
0081 finally:
0082     f.close()
0083 if test_support.verbose:
0084     print 'check file size with os.stat'
0085 expect(os.stat(name)[stat.ST_SIZE], size+1)
0086 
0087 if test_support.verbose:
0088     print 'play around with seek() and read() with the built largefile'
0089 f = open(name, 'rb')
0090 try:
0091     expect(f.tell(), 0)
0092     expect(f.read(1), 'z')
0093     expect(f.tell(), 1)
0094     f.seek(0)
0095     expect(f.tell(), 0)
0096     f.seek(0, 0)
0097     expect(f.tell(), 0)
0098     f.seek(42)
0099     expect(f.tell(), 42)
0100     f.seek(42, 0)
0101     expect(f.tell(), 42)
0102     f.seek(42, 1)
0103     expect(f.tell(), 84)
0104     f.seek(0, 1)
0105     expect(f.tell(), 84)
0106     f.seek(0, 2) # seek from the end
0107     expect(f.tell(), size + 1 + 0)
0108     f.seek(-10, 2)
0109     expect(f.tell(), size + 1 - 10)
0110     f.seek(-size-1, 2)
0111     expect(f.tell(), 0)
0112     f.seek(size)
0113     expect(f.tell(), size)
0114     expect(f.read(1), 'a') # the 'a' that was written at the end of file above
0115     f.seek(-size-1, 1)
0116     expect(f.read(1), 'z')
0117     expect(f.tell(), 1)
0118 finally:
0119     f.close()
0120 
0121 if test_support.verbose:
0122     print 'play around with os.lseek() with the built largefile'
0123 f = open(name, 'rb')
0124 try:
0125     expect(os.lseek(f.fileno(), 0, 0), 0)
0126     expect(os.lseek(f.fileno(), 42, 0), 42)
0127     expect(os.lseek(f.fileno(), 42, 1), 84)
0128     expect(os.lseek(f.fileno(), 0, 1), 84)
0129     expect(os.lseek(f.fileno(), 0, 2), size+1+0)
0130     expect(os.lseek(f.fileno(), -10, 2), size+1-10)
0131     expect(os.lseek(f.fileno(), -size-1, 2), 0)
0132     expect(os.lseek(f.fileno(), size, 0), size)
0133     expect(f.read(1), 'a') # the 'a' that was written at the end of file above
0134 finally:
0135     f.close()
0136 
0137 if hasattr(f, 'truncate'):
0138     if test_support.verbose:
0139         print 'try truncate'
0140     f = open(name, 'r+b')
0141     try:
0142         f.seek(0, 2)
0143         expect(f.tell(), size+1)    # else we've lost track of the true size
0144         # Cut it back via seek + truncate with no argument.
0145         newsize = size - 10
0146         f.seek(newsize)
0147         f.truncate()
0148         expect(f.tell(), newsize)   # else pointer moved
0149         f.seek(0, 2)
0150         expect(f.tell(), newsize)   # else wasn't truncated
0151         # Ensure that truncate(smaller than true size) shrinks the file.
0152         newsize -= 1
0153         f.seek(42)
0154         f.truncate(newsize)
0155         expect(f.tell(), 42)        # else pointer moved
0156         f.seek(0, 2)
0157         expect(f.tell(), newsize)   # else wasn't truncated
0158 
0159         # XXX truncate(larger than true size) is ill-defined across platforms
0160 
0161         # cut it waaaaay back
0162         f.seek(0)
0163         f.truncate(1)
0164         expect(f.tell(), 0)         # else pointer moved
0165         expect(len(f.read()), 1)    # else wasn't truncated
0166 
0167     finally:
0168         f.close()
0169 
0170 os.unlink(name)
0171 

Generated by PyXR 0.9.4
SourceForge.net Logo