]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | """Test largefile support on system where this makes sense.\r |
2 | """\r | |
3 | \r | |
4 | from __future__ import print_function\r | |
5 | \r | |
6 | import os\r | |
7 | import stat\r | |
8 | import sys\r | |
9 | import unittest\r | |
10 | from test.test_support import run_unittest, TESTFN, verbose, requires, \\r | |
11 | unlink\r | |
12 | import io # C implementation of io\r | |
13 | import _pyio as pyio # Python implementation of io\r | |
14 | \r | |
15 | try:\r | |
16 | import signal\r | |
17 | # The default handler for SIGXFSZ is to abort the process.\r | |
18 | # By ignoring it, system calls exceeding the file size resource\r | |
19 | # limit will raise IOError instead of crashing the interpreter.\r | |
20 | oldhandler = signal.signal(signal.SIGXFSZ, signal.SIG_IGN)\r | |
21 | except (ImportError, AttributeError):\r | |
22 | pass\r | |
23 | \r | |
24 | # create >2GB file (2GB = 2147483648 bytes)\r | |
25 | size = 2500000000\r | |
26 | \r | |
27 | \r | |
28 | class LargeFileTest(unittest.TestCase):\r | |
29 | """Test that each file function works as expected for a large\r | |
30 | (i.e. > 2GB, do we have to check > 4GB) files.\r | |
31 | \r | |
32 | NOTE: the order of execution of the test methods is important! test_seek\r | |
33 | must run first to create the test file. File cleanup must also be handled\r | |
34 | outside the test instances because of this.\r | |
35 | \r | |
36 | """\r | |
37 | \r | |
38 | def test_seek(self):\r | |
39 | if verbose:\r | |
40 | print('create large file via seek (may be sparse file) ...')\r | |
41 | with self.open(TESTFN, 'wb') as f:\r | |
42 | f.write(b'z')\r | |
43 | f.seek(0)\r | |
44 | f.seek(size)\r | |
45 | f.write(b'a')\r | |
46 | f.flush()\r | |
47 | if verbose:\r | |
48 | print('check file size with os.fstat')\r | |
49 | self.assertEqual(os.fstat(f.fileno())[stat.ST_SIZE], size+1)\r | |
50 | \r | |
51 | def test_osstat(self):\r | |
52 | if verbose:\r | |
53 | print('check file size with os.stat')\r | |
54 | self.assertEqual(os.stat(TESTFN)[stat.ST_SIZE], size+1)\r | |
55 | \r | |
56 | def test_seek_read(self):\r | |
57 | if verbose:\r | |
58 | print('play around with seek() and read() with the built largefile')\r | |
59 | with self.open(TESTFN, 'rb') as f:\r | |
60 | self.assertEqual(f.tell(), 0)\r | |
61 | self.assertEqual(f.read(1), b'z')\r | |
62 | self.assertEqual(f.tell(), 1)\r | |
63 | f.seek(0)\r | |
64 | self.assertEqual(f.tell(), 0)\r | |
65 | f.seek(0, 0)\r | |
66 | self.assertEqual(f.tell(), 0)\r | |
67 | f.seek(42)\r | |
68 | self.assertEqual(f.tell(), 42)\r | |
69 | f.seek(42, 0)\r | |
70 | self.assertEqual(f.tell(), 42)\r | |
71 | f.seek(42, 1)\r | |
72 | self.assertEqual(f.tell(), 84)\r | |
73 | f.seek(0, 1)\r | |
74 | self.assertEqual(f.tell(), 84)\r | |
75 | f.seek(0, 2) # seek from the end\r | |
76 | self.assertEqual(f.tell(), size + 1 + 0)\r | |
77 | f.seek(-10, 2)\r | |
78 | self.assertEqual(f.tell(), size + 1 - 10)\r | |
79 | f.seek(-size-1, 2)\r | |
80 | self.assertEqual(f.tell(), 0)\r | |
81 | f.seek(size)\r | |
82 | self.assertEqual(f.tell(), size)\r | |
83 | # the 'a' that was written at the end of file above\r | |
84 | self.assertEqual(f.read(1), b'a')\r | |
85 | f.seek(-size-1, 1)\r | |
86 | self.assertEqual(f.read(1), b'z')\r | |
87 | self.assertEqual(f.tell(), 1)\r | |
88 | \r | |
89 | def test_lseek(self):\r | |
90 | if verbose:\r | |
91 | print('play around with os.lseek() with the built largefile')\r | |
92 | with self.open(TESTFN, 'rb') as f:\r | |
93 | self.assertEqual(os.lseek(f.fileno(), 0, 0), 0)\r | |
94 | self.assertEqual(os.lseek(f.fileno(), 42, 0), 42)\r | |
95 | self.assertEqual(os.lseek(f.fileno(), 42, 1), 84)\r | |
96 | self.assertEqual(os.lseek(f.fileno(), 0, 1), 84)\r | |
97 | self.assertEqual(os.lseek(f.fileno(), 0, 2), size+1+0)\r | |
98 | self.assertEqual(os.lseek(f.fileno(), -10, 2), size+1-10)\r | |
99 | self.assertEqual(os.lseek(f.fileno(), -size-1, 2), 0)\r | |
100 | self.assertEqual(os.lseek(f.fileno(), size, 0), size)\r | |
101 | # the 'a' that was written at the end of file above\r | |
102 | self.assertEqual(f.read(1), b'a')\r | |
103 | \r | |
104 | def test_truncate(self):\r | |
105 | if verbose:\r | |
106 | print('try truncate')\r | |
107 | with self.open(TESTFN, 'r+b') as f:\r | |
108 | # this is already decided before start running the test suite\r | |
109 | # but we do it anyway for extra protection\r | |
110 | if not hasattr(f, 'truncate'):\r | |
111 | raise unittest.SkipTest("open().truncate() not available on this system")\r | |
112 | f.seek(0, 2)\r | |
113 | # else we've lost track of the true size\r | |
114 | self.assertEqual(f.tell(), size+1)\r | |
115 | # Cut it back via seek + truncate with no argument.\r | |
116 | newsize = size - 10\r | |
117 | f.seek(newsize)\r | |
118 | f.truncate()\r | |
119 | self.assertEqual(f.tell(), newsize) # else pointer moved\r | |
120 | f.seek(0, 2)\r | |
121 | self.assertEqual(f.tell(), newsize) # else wasn't truncated\r | |
122 | # Ensure that truncate(smaller than true size) shrinks\r | |
123 | # the file.\r | |
124 | newsize -= 1\r | |
125 | f.seek(42)\r | |
126 | f.truncate(newsize)\r | |
127 | if self.new_io:\r | |
128 | self.assertEqual(f.tell(), 42)\r | |
129 | f.seek(0, 2)\r | |
130 | self.assertEqual(f.tell(), newsize)\r | |
131 | # XXX truncate(larger than true size) is ill-defined\r | |
132 | # across platform; cut it waaaaay back\r | |
133 | f.seek(0)\r | |
134 | f.truncate(1)\r | |
135 | if self.new_io:\r | |
136 | self.assertEqual(f.tell(), 0) # else pointer moved\r | |
137 | f.seek(0)\r | |
138 | self.assertEqual(len(f.read()), 1) # else wasn't truncated\r | |
139 | \r | |
140 | def test_seekable(self):\r | |
141 | # Issue #5016; seekable() can return False when the current position\r | |
142 | # is negative when truncated to an int.\r | |
143 | if not self.new_io:\r | |
144 | self.skipTest("builtin file doesn't have seekable()")\r | |
145 | for pos in (2**31-1, 2**31, 2**31+1):\r | |
146 | with self.open(TESTFN, 'rb') as f:\r | |
147 | f.seek(pos)\r | |
148 | self.assertTrue(f.seekable())\r | |
149 | \r | |
150 | \r | |
151 | def test_main():\r | |
152 | # On Windows and Mac OSX this test comsumes large resources; It\r | |
153 | # takes a long time to build the >2GB file and takes >2GB of disk\r | |
154 | # space therefore the resource must be enabled to run this test.\r | |
155 | # If not, nothing after this line stanza will be executed.\r | |
156 | if sys.platform[:3] == 'win' or sys.platform == 'darwin':\r | |
157 | requires('largefile',\r | |
158 | 'test requires %s bytes and a long time to run' % str(size))\r | |
159 | else:\r | |
160 | # Only run if the current filesystem supports large files.\r | |
161 | # (Skip this test on Windows, since we now always support\r | |
162 | # large files.)\r | |
163 | f = open(TESTFN, 'wb', buffering=0)\r | |
164 | try:\r | |
165 | # 2**31 == 2147483648\r | |
166 | f.seek(2147483649)\r | |
167 | # Seeking is not enough of a test: you must write and\r | |
168 | # flush, too!\r | |
169 | f.write(b'x')\r | |
170 | f.flush()\r | |
171 | except (IOError, OverflowError):\r | |
172 | f.close()\r | |
173 | unlink(TESTFN)\r | |
174 | raise unittest.SkipTest("filesystem does not have largefile support")\r | |
175 | else:\r | |
176 | f.close()\r | |
177 | suite = unittest.TestSuite()\r | |
178 | for _open, prefix in [(io.open, 'C'), (pyio.open, 'Py'),\r | |
179 | (open, 'Builtin')]:\r | |
180 | class TestCase(LargeFileTest):\r | |
181 | pass\r | |
182 | TestCase.open = staticmethod(_open)\r | |
183 | TestCase.new_io = _open is not open\r | |
184 | TestCase.__name__ = prefix + LargeFileTest.__name__\r | |
185 | suite.addTest(TestCase('test_seek'))\r | |
186 | suite.addTest(TestCase('test_osstat'))\r | |
187 | suite.addTest(TestCase('test_seek_read'))\r | |
188 | suite.addTest(TestCase('test_lseek'))\r | |
189 | with _open(TESTFN, 'wb') as f:\r | |
190 | if hasattr(f, 'truncate'):\r | |
191 | suite.addTest(TestCase('test_truncate'))\r | |
192 | suite.addTest(TestCase('test_seekable'))\r | |
193 | unlink(TESTFN)\r | |
194 | try:\r | |
195 | run_unittest(suite)\r | |
196 | finally:\r | |
197 | unlink(TESTFN)\r | |
198 | \r | |
199 | if __name__ == '__main__':\r | |
200 | test_main()\r |