blob: bc246353b5a360f491e5512a70b03c6e1bf89fa0 [file] [log] [blame]
Trent Mickf29f47b2000-08-11 19:02:59 +00001#!python
2
3#----------------------------------------------------------------------
4# test largefile support on system where this makes sense
5#
Trent Mickf29f47b2000-08-11 19:02:59 +00006#----------------------------------------------------------------------
7
8import test_support
9import os, struct, stat, sys
10
Martin v. Löwisdf8adcd2001-12-11 17:57:26 +000011try:
12 import signal
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)
17except (ImportError, AttributeError):
18 pass
19
Trent Mickf29f47b2000-08-11 19:02:59 +000020
Guido van Rossuma31ddbb2001-09-10 15:03:18 +000021# create >2GB file (2GB = 2147483648 bytes)
22size = 2500000000L
23name = test_support.TESTFN
24
25
Barry Warsaw7fdfc382001-08-20 22:37:34 +000026# On Windows this test comsumes large resources; It takes a long time to build
27# the >2GB file and takes >2GB of disk space therefore the resource must be
28# enabled to run this test. If not, nothing after this line stanza will be
29# executed.
30if sys.platform[:3] == 'win':
31 test_support.requires(
32 'largefile',
33 'test requires %s bytes and a long time to run' % str(size))
Guido van Rossum47f40342001-09-10 13:34:12 +000034else:
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')
38 try:
39 # 2**31 == 2147483648
40 f.seek(2147483649L)
41 # Seeking is not enough of a test: you must write and flush, too!
42 f.write("x")
43 f.flush()
44 except (IOError, OverflowError):
45 f.close()
46 os.unlink(test_support.TESTFN)
47 raise test_support.TestSkipped, \
48 "filesystem does not have largefile support"
49 else:
50 f.close()
51
52
Trent Mickf29f47b2000-08-11 19:02:59 +000053def expect(got_this, expect_this):
Fred Drakedce56412000-10-23 16:38:20 +000054 if test_support.verbose:
Tim Petersb8c02302001-09-06 01:17:45 +000055 print '%r =?= %r ...' % (got_this, expect_this),
Fred Drakedce56412000-10-23 16:38:20 +000056 if got_this != expect_this:
57 if test_support.verbose:
58 print 'no'
Tim Petersb8c02302001-09-06 01:17:45 +000059 raise test_support.TestFailed, 'got %r, but expected %r' %\
60 (got_this, expect_this)
Fred Drakedce56412000-10-23 16:38:20 +000061 else:
62 if test_support.verbose:
63 print 'yes'
Trent Mickf29f47b2000-08-11 19:02:59 +000064
65
66# test that each file function works as expected for a large (i.e. >2GB, do
67# we have to check >4GB) files
68
69if test_support.verbose:
Fred Drake004d5e62000-10-23 17:22:08 +000070 print 'create large file via seek (may be sparse file) ...'
Tim Peters6e13a562001-09-06 00:32:15 +000071f = open(name, 'wb')
Tim Petersb8c02302001-09-06 01:17:45 +000072f.write('z')
73f.seek(0)
Trent Mickf29f47b2000-08-11 19:02:59 +000074f.seek(size)
75f.write('a')
76f.flush()
Trent Mickf29f47b2000-08-11 19:02:59 +000077if test_support.verbose:
Fred Drakedce56412000-10-23 16:38:20 +000078 print 'check file size with os.fstat'
Tim Peters6e13a562001-09-06 00:32:15 +000079expect(os.fstat(f.fileno())[stat.ST_SIZE], size+1)
Trent Mickf29f47b2000-08-11 19:02:59 +000080f.close()
81if test_support.verbose:
Fred Drakedce56412000-10-23 16:38:20 +000082 print 'check file size with os.stat'
Trent Mickf29f47b2000-08-11 19:02:59 +000083expect(os.stat(name)[stat.ST_SIZE], size+1)
84
85if test_support.verbose:
Fred Drakedce56412000-10-23 16:38:20 +000086 print 'play around with seek() and read() with the built largefile'
Tim Peters6e13a562001-09-06 00:32:15 +000087f = open(name, 'rb')
Trent Mickf29f47b2000-08-11 19:02:59 +000088expect(f.tell(), 0)
Tim Petersb8c02302001-09-06 01:17:45 +000089expect(f.read(1), 'z')
Trent Mickf29f47b2000-08-11 19:02:59 +000090expect(f.tell(), 1)
91f.seek(0)
92expect(f.tell(), 0)
93f.seek(0, 0)
94expect(f.tell(), 0)
95f.seek(42)
96expect(f.tell(), 42)
97f.seek(42, 0)
98expect(f.tell(), 42)
99f.seek(42, 1)
100expect(f.tell(), 84)
101f.seek(0, 1)
102expect(f.tell(), 84)
103f.seek(0, 2) # seek from the end
104expect(f.tell(), size + 1 + 0)
105f.seek(-10, 2)
106expect(f.tell(), size + 1 - 10)
107f.seek(-size-1, 2)
108expect(f.tell(), 0)
109f.seek(size)
110expect(f.tell(), size)
111expect(f.read(1), 'a') # the 'a' that was written at the end of the file above
Tim Petersb8c02302001-09-06 01:17:45 +0000112f.seek(-size-1, 1)
113expect(f.read(1), 'z')
114expect(f.tell(), 1)
Trent Mickf29f47b2000-08-11 19:02:59 +0000115f.close()
116
117if test_support.verbose:
Fred Drakedce56412000-10-23 16:38:20 +0000118 print 'play around with os.lseek() with the built largefile'
Tim Petersb8c02302001-09-06 01:17:45 +0000119f = open(name, 'rb')
Trent Mickf29f47b2000-08-11 19:02:59 +0000120expect(os.lseek(f.fileno(), 0, 0), 0)
121expect(os.lseek(f.fileno(), 42, 0), 42)
122expect(os.lseek(f.fileno(), 42, 1), 84)
123expect(os.lseek(f.fileno(), 0, 1), 84)
124expect(os.lseek(f.fileno(), 0, 2), size+1+0)
125expect(os.lseek(f.fileno(), -10, 2), size+1-10)
126expect(os.lseek(f.fileno(), -size-1, 2), 0)
127expect(os.lseek(f.fileno(), size, 0), size)
128expect(f.read(1), 'a') # the 'a' that was written at the end of the file above
129f.close()
130
Tim Petersfb05db22002-03-11 00:24:00 +0000131if hasattr(f, 'truncate'):
132 if test_support.verbose:
133 print 'try truncate'
134 f = open(name, 'r+b')
135 f.seek(0, 2)
136 expect(f.tell(), size+1)
137 # Cut it back via seek + truncate with no argument.
138 newsize = size - 10
139 f.seek(newsize)
140 f.truncate()
141 expect(f.tell(), newsize)
142 # Ensure that truncate(bigger than true size) doesn't grow the file.
143 f.truncate(size)
144 expect(f.tell(), newsize)
145 # Ensure that truncate(smaller than true size) shrinks the file.
146 newsize -= 1
147 f.seek(0)
148 f.truncate(newsize)
149 expect(f.tell(), newsize)
150 # cut it waaaaay back
151 f.truncate(1)
152 f.seek(0)
153 expect(len(f.read()), 1)
154 f.close()
Trent Mickf29f47b2000-08-11 19:02:59 +0000155
156os.unlink(name)