3 #----------------------------------------------------------------------
4 # test largefile support on system where this makes sense
6 #----------------------------------------------------------------------
8 from test
import test_support
9 import os
, struct
, stat
, sys
13 # The default handler for SIGXFSZ is to abort the process.
14 # By ignoring it, system calls exceeding the file size resource
15 # limit will raise IOError instead of crashing the interpreter.
16 oldhandler
= signal
.signal(signal
.SIGXFSZ
, signal
.SIG_IGN
)
17 except (ImportError, AttributeError):
21 # create >2GB file (2GB = 2147483648 bytes)
23 name
= test_support
.TESTFN
26 # On Windows and Mac OSX this test comsumes large resources; It takes
27 # a long time to build the >2GB file and takes >2GB of disk space
28 # therefore the resource must be enabled to run this test. If not,
29 # nothing after this line stanza will be executed.
30 if sys
.platform
[:3] == 'win' or sys
.platform
== 'darwin':
31 test_support
.requires(
33 'test requires %s bytes and a long time to run' % str(size
))
35 # Only run if the current filesystem supports large files.
36 # (Skip this test on Windows, since we now always support large files.)
37 f
= open(test_support
.TESTFN
, 'wb')
41 # Seeking is not enough of a test: you must write and flush, too!
44 except (IOError, OverflowError):
46 os
.unlink(test_support
.TESTFN
)
47 raise test_support
.TestSkipped
, \
48 "filesystem does not have largefile support"
53 def expect(got_this
, expect_this
):
54 if test_support
.verbose
:
55 print '%r =?= %r ...' % (got_this
, expect_this
),
56 if got_this
!= expect_this
:
57 if test_support
.verbose
:
59 raise test_support
.TestFailed
, 'got %r, but expected %r' %\
60 (got_this
, expect_this
)
62 if test_support
.verbose
:
66 # test that each file function works as expected for a large (i.e. >2GB, do
67 # we have to check >4GB) files
69 if test_support
.verbose
:
70 print 'create large file via seek (may be sparse file) ...'
78 if test_support
.verbose
:
79 print 'check file size with os.fstat'
80 expect(os
.fstat(f
.fileno())[stat
.ST_SIZE
], size
+1)
83 if test_support
.verbose
:
84 print 'check file size with os.stat'
85 expect(os
.stat(name
)[stat
.ST_SIZE
], size
+1)
87 if test_support
.verbose
:
88 print 'play around with seek() and read() with the built largefile'
92 expect(f
.read(1), 'z')
106 f
.seek(0, 2) # seek from the end
107 expect(f
.tell(), size
+ 1 + 0)
109 expect(f
.tell(), size
+ 1 - 10)
113 expect(f
.tell(), size
)
114 expect(f
.read(1), 'a') # the 'a' that was written at the end of file above
116 expect(f
.read(1), 'z')
121 if test_support
.verbose
:
122 print 'play around with os.lseek() with the built largefile'
125 expect(os
.lseek(f
.fileno(), 0, 0), 0)
126 expect(os
.lseek(f
.fileno(), 42, 0), 42)
127 expect(os
.lseek(f
.fileno(), 42, 1), 84)
128 expect(os
.lseek(f
.fileno(), 0, 1), 84)
129 expect(os
.lseek(f
.fileno(), 0, 2), size
+1+0)
130 expect(os
.lseek(f
.fileno(), -10, 2), size
+1-10)
131 expect(os
.lseek(f
.fileno(), -size
-1, 2), 0)
132 expect(os
.lseek(f
.fileno(), size
, 0), size
)
133 expect(f
.read(1), 'a') # the 'a' that was written at the end of file above
137 if hasattr(f
, 'truncate'):
138 if test_support
.verbose
:
140 f
= open(name
, 'r+b')
143 expect(f
.tell(), size
+1) # else we've lost track of the true size
144 # Cut it back via seek + truncate with no argument.
148 expect(f
.tell(), newsize
) # else pointer moved
150 expect(f
.tell(), newsize
) # else wasn't truncated
151 # Ensure that truncate(smaller than true size) shrinks the file.
155 expect(f
.tell(), 42) # else pointer moved
157 expect(f
.tell(), newsize
) # else wasn't truncated
159 # XXX truncate(larger than true size) is ill-defined across platforms
161 # cut it waaaaay back
164 expect(f
.tell(), 0) # else pointer moved
165 expect(len(f
.read()), 1) # else wasn't truncated