ยปCore Development>Code coverage>Lib/test/test_largefile.py

Python code coverage for Lib/test/test_largefile.py

#countcontent
1n/a"""Test largefile support on system where this makes sense.
2n/a"""
3n/a
4n/aimport os
5n/aimport stat
6n/aimport sys
7n/aimport unittest
8n/afrom test.support import TESTFN, requires, unlink
9n/aimport io # C implementation of io
10n/aimport _pyio as pyio # Python implementation of io
11n/a
12n/a# size of file to create (>2GB; 2GB == 2147483648 bytes)
13n/asize = 2500000000
14n/a
15n/aclass LargeFileTest:
16n/a """Test that each file function works as expected for large
17n/a (i.e. > 2GB) files.
18n/a """
19n/a
20n/a def setUp(self):
21n/a if os.path.exists(TESTFN):
22n/a mode = 'r+b'
23n/a else:
24n/a mode = 'w+b'
25n/a
26n/a with self.open(TESTFN, mode) as f:
27n/a current_size = os.fstat(f.fileno())[stat.ST_SIZE]
28n/a if current_size == size+1:
29n/a return
30n/a
31n/a if current_size == 0:
32n/a f.write(b'z')
33n/a
34n/a f.seek(0)
35n/a f.seek(size)
36n/a f.write(b'a')
37n/a f.flush()
38n/a self.assertEqual(os.fstat(f.fileno())[stat.ST_SIZE], size+1)
39n/a
40n/a @classmethod
41n/a def tearDownClass(cls):
42n/a with cls.open(TESTFN, 'wb'):
43n/a pass
44n/a if not os.stat(TESTFN)[stat.ST_SIZE] == 0:
45n/a raise cls.failureException('File was not truncated by opening '
46n/a 'with mode "wb"')
47n/a
48n/a def test_osstat(self):
49n/a self.assertEqual(os.stat(TESTFN)[stat.ST_SIZE], size+1)
50n/a
51n/a def test_seek_read(self):
52n/a with self.open(TESTFN, 'rb') as f:
53n/a self.assertEqual(f.tell(), 0)
54n/a self.assertEqual(f.read(1), b'z')
55n/a self.assertEqual(f.tell(), 1)
56n/a f.seek(0)
57n/a self.assertEqual(f.tell(), 0)
58n/a f.seek(0, 0)
59n/a self.assertEqual(f.tell(), 0)
60n/a f.seek(42)
61n/a self.assertEqual(f.tell(), 42)
62n/a f.seek(42, 0)
63n/a self.assertEqual(f.tell(), 42)
64n/a f.seek(42, 1)
65n/a self.assertEqual(f.tell(), 84)
66n/a f.seek(0, 1)
67n/a self.assertEqual(f.tell(), 84)
68n/a f.seek(0, 2) # seek from the end
69n/a self.assertEqual(f.tell(), size + 1 + 0)
70n/a f.seek(-10, 2)
71n/a self.assertEqual(f.tell(), size + 1 - 10)
72n/a f.seek(-size-1, 2)
73n/a self.assertEqual(f.tell(), 0)
74n/a f.seek(size)
75n/a self.assertEqual(f.tell(), size)
76n/a # the 'a' that was written at the end of file above
77n/a self.assertEqual(f.read(1), b'a')
78n/a f.seek(-size-1, 1)
79n/a self.assertEqual(f.read(1), b'z')
80n/a self.assertEqual(f.tell(), 1)
81n/a
82n/a def test_lseek(self):
83n/a with self.open(TESTFN, 'rb') as f:
84n/a self.assertEqual(os.lseek(f.fileno(), 0, 0), 0)
85n/a self.assertEqual(os.lseek(f.fileno(), 42, 0), 42)
86n/a self.assertEqual(os.lseek(f.fileno(), 42, 1), 84)
87n/a self.assertEqual(os.lseek(f.fileno(), 0, 1), 84)
88n/a self.assertEqual(os.lseek(f.fileno(), 0, 2), size+1+0)
89n/a self.assertEqual(os.lseek(f.fileno(), -10, 2), size+1-10)
90n/a self.assertEqual(os.lseek(f.fileno(), -size-1, 2), 0)
91n/a self.assertEqual(os.lseek(f.fileno(), size, 0), size)
92n/a # the 'a' that was written at the end of file above
93n/a self.assertEqual(f.read(1), b'a')
94n/a
95n/a def test_truncate(self):
96n/a with self.open(TESTFN, 'r+b') as f:
97n/a if not hasattr(f, 'truncate'):
98n/a raise unittest.SkipTest("open().truncate() not available "
99n/a "on this system")
100n/a f.seek(0, 2)
101n/a # else we've lost track of the true size
102n/a self.assertEqual(f.tell(), size+1)
103n/a # Cut it back via seek + truncate with no argument.
104n/a newsize = size - 10
105n/a f.seek(newsize)
106n/a f.truncate()
107n/a self.assertEqual(f.tell(), newsize) # else pointer moved
108n/a f.seek(0, 2)
109n/a self.assertEqual(f.tell(), newsize) # else wasn't truncated
110n/a # Ensure that truncate(smaller than true size) shrinks
111n/a # the file.
112n/a newsize -= 1
113n/a f.seek(42)
114n/a f.truncate(newsize)
115n/a self.assertEqual(f.tell(), 42)
116n/a f.seek(0, 2)
117n/a self.assertEqual(f.tell(), newsize)
118n/a # XXX truncate(larger than true size) is ill-defined
119n/a # across platform; cut it waaaaay back
120n/a f.seek(0)
121n/a f.truncate(1)
122n/a self.assertEqual(f.tell(), 0) # else pointer moved
123n/a f.seek(0)
124n/a self.assertEqual(len(f.read()), 1) # else wasn't truncated
125n/a
126n/a def test_seekable(self):
127n/a # Issue #5016; seekable() can return False when the current position
128n/a # is negative when truncated to an int.
129n/a for pos in (2**31-1, 2**31, 2**31+1):
130n/a with self.open(TESTFN, 'rb') as f:
131n/a f.seek(pos)
132n/a self.assertTrue(f.seekable())
133n/a
134n/adef setUpModule():
135n/a try:
136n/a import signal
137n/a # The default handler for SIGXFSZ is to abort the process.
138n/a # By ignoring it, system calls exceeding the file size resource
139n/a # limit will raise OSError instead of crashing the interpreter.
140n/a signal.signal(signal.SIGXFSZ, signal.SIG_IGN)
141n/a except (ImportError, AttributeError):
142n/a pass
143n/a
144n/a # On Windows and Mac OSX this test comsumes large resources; It
145n/a # takes a long time to build the >2GB file and takes >2GB of disk
146n/a # space therefore the resource must be enabled to run this test.
147n/a # If not, nothing after this line stanza will be executed.
148n/a if sys.platform[:3] == 'win' or sys.platform == 'darwin':
149n/a requires('largefile',
150n/a 'test requires %s bytes and a long time to run' % str(size))
151n/a else:
152n/a # Only run if the current filesystem supports large files.
153n/a # (Skip this test on Windows, since we now always support
154n/a # large files.)
155n/a f = open(TESTFN, 'wb', buffering=0)
156n/a try:
157n/a # 2**31 == 2147483648
158n/a f.seek(2147483649)
159n/a # Seeking is not enough of a test: you must write and flush, too!
160n/a f.write(b'x')
161n/a f.flush()
162n/a except (OSError, OverflowError):
163n/a raise unittest.SkipTest("filesystem does not have "
164n/a "largefile support")
165n/a finally:
166n/a f.close()
167n/a unlink(TESTFN)
168n/a
169n/a
170n/aclass CLargeFileTest(LargeFileTest, unittest.TestCase):
171n/a open = staticmethod(io.open)
172n/a
173n/aclass PyLargeFileTest(LargeFileTest, unittest.TestCase):
174n/a open = staticmethod(pyio.open)
175n/a
176n/adef tearDownModule():
177n/a unlink(TESTFN)
178n/a
179n/aif __name__ == '__main__':
180n/a unittest.main()