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

Python code coverage for Lib/test/test_fcntl.py

#countcontent
1n/a"""Test program for the fcntl C module.
2n/a"""
3n/aimport platform
4n/aimport os
5n/aimport struct
6n/aimport sys
7n/aimport unittest
8n/afrom test.support import (verbose, TESTFN, unlink, run_unittest, import_module,
9n/a cpython_only)
10n/a
11n/a# Skip test if no fcntl module.
12n/afcntl = import_module('fcntl')
13n/a
14n/a
15n/a# TODO - Write tests for flock() and lockf().
16n/a
17n/adef get_lockdata():
18n/a try:
19n/a os.O_LARGEFILE
20n/a except AttributeError:
21n/a start_len = "ll"
22n/a else:
23n/a start_len = "qq"
24n/a
25n/a if (sys.platform.startswith(('netbsd', 'freebsd', 'openbsd', 'bsdos'))
26n/a or sys.platform == 'darwin'):
27n/a if struct.calcsize('l') == 8:
28n/a off_t = 'l'
29n/a pid_t = 'i'
30n/a else:
31n/a off_t = 'lxxxx'
32n/a pid_t = 'l'
33n/a lockdata = struct.pack(off_t + off_t + pid_t + 'hh', 0, 0, 0,
34n/a fcntl.F_WRLCK, 0)
35n/a elif sys.platform.startswith('gnukfreebsd'):
36n/a lockdata = struct.pack('qqihhi', 0, 0, 0, fcntl.F_WRLCK, 0, 0)
37n/a elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']:
38n/a lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
39n/a else:
40n/a lockdata = struct.pack('hh'+start_len+'hh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
41n/a if lockdata:
42n/a if verbose:
43n/a print('struct.pack: ', repr(lockdata))
44n/a return lockdata
45n/a
46n/alockdata = get_lockdata()
47n/a
48n/aclass BadFile:
49n/a def __init__(self, fn):
50n/a self.fn = fn
51n/a def fileno(self):
52n/a return self.fn
53n/a
54n/aclass TestFcntl(unittest.TestCase):
55n/a
56n/a def setUp(self):
57n/a self.f = None
58n/a
59n/a def tearDown(self):
60n/a if self.f and not self.f.closed:
61n/a self.f.close()
62n/a unlink(TESTFN)
63n/a
64n/a def test_fcntl_fileno(self):
65n/a # the example from the library docs
66n/a self.f = open(TESTFN, 'wb')
67n/a rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
68n/a if verbose:
69n/a print('Status from fcntl with O_NONBLOCK: ', rv)
70n/a rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETLKW, lockdata)
71n/a if verbose:
72n/a print('String from fcntl with F_SETLKW: ', repr(rv))
73n/a self.f.close()
74n/a
75n/a def test_fcntl_file_descriptor(self):
76n/a # again, but pass the file rather than numeric descriptor
77n/a self.f = open(TESTFN, 'wb')
78n/a rv = fcntl.fcntl(self.f, fcntl.F_SETFL, os.O_NONBLOCK)
79n/a if verbose:
80n/a print('Status from fcntl with O_NONBLOCK: ', rv)
81n/a rv = fcntl.fcntl(self.f, fcntl.F_SETLKW, lockdata)
82n/a if verbose:
83n/a print('String from fcntl with F_SETLKW: ', repr(rv))
84n/a self.f.close()
85n/a
86n/a def test_fcntl_bad_file(self):
87n/a with self.assertRaises(ValueError):
88n/a fcntl.fcntl(-1, fcntl.F_SETFL, os.O_NONBLOCK)
89n/a with self.assertRaises(ValueError):
90n/a fcntl.fcntl(BadFile(-1), fcntl.F_SETFL, os.O_NONBLOCK)
91n/a with self.assertRaises(TypeError):
92n/a fcntl.fcntl('spam', fcntl.F_SETFL, os.O_NONBLOCK)
93n/a with self.assertRaises(TypeError):
94n/a fcntl.fcntl(BadFile('spam'), fcntl.F_SETFL, os.O_NONBLOCK)
95n/a
96n/a @cpython_only
97n/a def test_fcntl_bad_file_overflow(self):
98n/a from _testcapi import INT_MAX, INT_MIN
99n/a # Issue 15989
100n/a with self.assertRaises(OverflowError):
101n/a fcntl.fcntl(INT_MAX + 1, fcntl.F_SETFL, os.O_NONBLOCK)
102n/a with self.assertRaises(OverflowError):
103n/a fcntl.fcntl(BadFile(INT_MAX + 1), fcntl.F_SETFL, os.O_NONBLOCK)
104n/a with self.assertRaises(OverflowError):
105n/a fcntl.fcntl(INT_MIN - 1, fcntl.F_SETFL, os.O_NONBLOCK)
106n/a with self.assertRaises(OverflowError):
107n/a fcntl.fcntl(BadFile(INT_MIN - 1), fcntl.F_SETFL, os.O_NONBLOCK)
108n/a
109n/a @unittest.skipIf(
110n/a platform.machine().startswith('arm') and platform.system() == 'Linux',
111n/a "ARM Linux returns EINVAL for F_NOTIFY DN_MULTISHOT")
112n/a def test_fcntl_64_bit(self):
113n/a # Issue #1309352: fcntl shouldn't fail when the third arg fits in a
114n/a # C 'long' but not in a C 'int'.
115n/a try:
116n/a cmd = fcntl.F_NOTIFY
117n/a # This flag is larger than 2**31 in 64-bit builds
118n/a flags = fcntl.DN_MULTISHOT
119n/a except AttributeError:
120n/a self.skipTest("F_NOTIFY or DN_MULTISHOT unavailable")
121n/a fd = os.open(os.path.dirname(os.path.abspath(TESTFN)), os.O_RDONLY)
122n/a try:
123n/a fcntl.fcntl(fd, cmd, flags)
124n/a finally:
125n/a os.close(fd)
126n/a
127n/a def test_flock(self):
128n/a # Solaris needs readable file for shared lock
129n/a self.f = open(TESTFN, 'wb+')
130n/a fileno = self.f.fileno()
131n/a fcntl.flock(fileno, fcntl.LOCK_SH)
132n/a fcntl.flock(fileno, fcntl.LOCK_UN)
133n/a fcntl.flock(self.f, fcntl.LOCK_SH | fcntl.LOCK_NB)
134n/a fcntl.flock(self.f, fcntl.LOCK_UN)
135n/a fcntl.flock(fileno, fcntl.LOCK_EX)
136n/a fcntl.flock(fileno, fcntl.LOCK_UN)
137n/a
138n/a self.assertRaises(ValueError, fcntl.flock, -1, fcntl.LOCK_SH)
139n/a self.assertRaises(TypeError, fcntl.flock, 'spam', fcntl.LOCK_SH)
140n/a
141n/a @cpython_only
142n/a def test_flock_overflow(self):
143n/a import _testcapi
144n/a self.assertRaises(OverflowError, fcntl.flock, _testcapi.INT_MAX+1,
145n/a fcntl.LOCK_SH)
146n/a
147n/a
148n/adef test_main():
149n/a run_unittest(TestFcntl)
150n/a
151n/aif __name__ == '__main__':
152n/a test_main()