blob: 48ba88b3b5e2554ec5e0e25276fcc7131fa1c3a2 [file] [log] [blame]
Facundo Batista9521f082008-02-07 16:16:29 +00001"""Test largefile support on system where this makes sense.
2"""
Trent Mickf29f47b2000-08-11 19:02:59 +00003
Facundo Batista9521f082008-02-07 16:16:29 +00004import os
5import stat
6import sys
7import unittest
8from test.test_support import run_unittest, TESTFN, verbose, requires, \
9 TestSkipped, unlink
Trent Mickf29f47b2000-08-11 19:02:59 +000010
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
Guido van Rossuma31ddbb2001-09-10 15:03:18 +000020# create >2GB file (2GB = 2147483648 bytes)
21size = 2500000000L
Guido van Rossuma31ddbb2001-09-10 15:03:18 +000022
23
Facundo Batista9521f082008-02-07 16:16:29 +000024class TestCase(unittest.TestCase):
25 """Test that each file function works as expected for a large
26 (i.e. > 2GB, do we have to check > 4GB) files.
27 """
28
29 def test_seek(self):
30 if verbose:
31 print 'create large file via seek (may be sparse file) ...'
32 f = open(TESTFN, 'wb')
33 try:
34 f.write('z')
35 f.seek(0)
36 f.seek(size)
37 f.write('a')
38 f.flush()
39 if verbose:
40 print 'check file size with os.fstat'
41 self.assertEqual(os.fstat(f.fileno())[stat.ST_SIZE], size+1)
42 finally:
43 f.close()
44
45 def test_osstat(self):
46 if verbose:
47 print 'check file size with os.stat'
48 self.assertEqual(os.stat(TESTFN)[stat.ST_SIZE], size+1)
49
50 def test_seek_read(self):
51 if verbose:
52 print 'play around with seek() and read() with the built largefile'
53 f = open(TESTFN, 'rb')
54 try:
55 self.assertEqual(f.tell(), 0)
56 self.assertEqual(f.read(1), 'z')
57 self.assertEqual(f.tell(), 1)
58 f.seek(0)
59 self.assertEqual(f.tell(), 0)
60 f.seek(0, 0)
61 self.assertEqual(f.tell(), 0)
62 f.seek(42)
63 self.assertEqual(f.tell(), 42)
64 f.seek(42, 0)
65 self.assertEqual(f.tell(), 42)
66 f.seek(42, 1)
67 self.assertEqual(f.tell(), 84)
68 f.seek(0, 1)
69 self.assertEqual(f.tell(), 84)
70 f.seek(0, 2) # seek from the end
71 self.assertEqual(f.tell(), size + 1 + 0)
72 f.seek(-10, 2)
73 self.assertEqual(f.tell(), size + 1 - 10)
74 f.seek(-size-1, 2)
75 self.assertEqual(f.tell(), 0)
76 f.seek(size)
77 self.assertEqual(f.tell(), size)
78 # the 'a' that was written at the end of file above
79 self.assertEqual(f.read(1), 'a')
80 f.seek(-size-1, 1)
81 self.assertEqual(f.read(1), 'z')
82 self.assertEqual(f.tell(), 1)
83 finally:
84 f.close()
85
86 def test_lseek(self):
87 if verbose:
88 print 'play around with os.lseek() with the built largefile'
89 f = open(TESTFN, 'rb')
90 try:
91 self.assertEqual(os.lseek(f.fileno(), 0, 0), 0)
92 self.assertEqual(os.lseek(f.fileno(), 42, 0), 42)
93 self.assertEqual(os.lseek(f.fileno(), 42, 1), 84)
94 self.assertEqual(os.lseek(f.fileno(), 0, 1), 84)
95 self.assertEqual(os.lseek(f.fileno(), 0, 2), size+1+0)
96 self.assertEqual(os.lseek(f.fileno(), -10, 2), size+1-10)
97 self.assertEqual(os.lseek(f.fileno(), -size-1, 2), 0)
98 self.assertEqual(os.lseek(f.fileno(), size, 0), size)
99 # the 'a' that was written at the end of file above
100 self.assertEqual(f.read(1), 'a')
101 finally:
102 f.close()
103
104 def test_truncate(self):
105 if verbose:
106 print 'try truncate'
107 f = open(TESTFN, 'r+b')
108 # this is already decided before start running the test suite
109 # but we do it anyway for extra protection
110 if not hasattr(f, 'truncate'):
111 raise TestSkipped, "open().truncate() not available on this system"
112 try:
113 f.seek(0, 2)
114 # else we've lost track of the true size
115 self.assertEqual(f.tell(), size+1)
116 # Cut it back via seek + truncate with no argument.
117 newsize = size - 10
118 f.seek(newsize)
119 f.truncate()
120 self.assertEqual(f.tell(), newsize) # else pointer moved
121 f.seek(0, 2)
122 self.assertEqual(f.tell(), newsize) # else wasn't truncated
123 # Ensure that truncate(smaller than true size) shrinks
124 # the file.
125 newsize -= 1
126 f.seek(42)
127 f.truncate(newsize)
128 self.assertEqual(f.tell(), 42) # else pointer moved
129 f.seek(0, 2)
130 self.assertEqual(f.tell(), newsize) # else wasn't truncated
131
132 # XXX truncate(larger than true size) is ill-defined
133 # across platform; cut it waaaaay back
134 f.seek(0)
135 f.truncate(1)
136 self.assertEqual(f.tell(), 0) # else pointer moved
137 self.assertEqual(len(f.read()), 1) # else wasn't truncated
138 finally:
139 f.close()
140
141
142def main_test():
143 # On Windows and Mac OSX this test comsumes large resources; It
144 # takes a long time to build the >2GB file and takes >2GB of disk
145 # space therefore the resource must be enabled to run this test.
146 # If not, nothing after this line stanza will be executed.
147 if sys.platform[:3] == 'win' or sys.platform == 'darwin':
148 requires('largefile',
149 'test requires %s bytes and a long time to run' % str(size))
Guido van Rossum47f40342001-09-10 13:34:12 +0000150 else:
Facundo Batista9521f082008-02-07 16:16:29 +0000151 # Only run if the current filesystem supports large files.
152 # (Skip this test on Windows, since we now always support
153 # large files.)
154 f = open(TESTFN, 'wb')
155 try:
156 # 2**31 == 2147483648
157 f.seek(2147483649L)
158 # Seeking is not enough of a test: you must write and
159 # flush, too!
160 f.write("x")
161 f.flush()
162 except (IOError, OverflowError):
163 f.close()
164 unlink(TESTFN)
165 raise TestSkipped, "filesystem does not have largefile support"
166 else:
167 f.close()
168 suite = unittest.TestSuite()
169 suite.addTest(TestCase('test_seek'))
170 suite.addTest(TestCase('test_osstat'))
171 suite.addTest(TestCase('test_seek_read'))
172 suite.addTest(TestCase('test_lseek'))
173 f = open(TESTFN, 'w')
174 if hasattr(f, 'truncate'):
175 suite.addTest(TestCase('test_truncate'))
Jason Tishler063606a2003-08-26 11:59:27 +0000176 f.close()
Facundo Batista9521f082008-02-07 16:16:29 +0000177 unlink(TESTFN)
178 run_unittest(suite)
179 unlink(TESTFN)
Trent Mickf29f47b2000-08-11 19:02:59 +0000180
Trent Mickf29f47b2000-08-11 19:02:59 +0000181
Facundo Batista9521f082008-02-07 16:16:29 +0000182if __name__ == '__main__':
183 main_test()