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

Python code coverage for Lib/test/test_resource.py

#countcontent
1n/aimport contextlib
2n/aimport sys
3n/aimport os
4n/aimport unittest
5n/afrom test import support
6n/aimport time
7n/a
8n/aresource = support.import_module('resource')
9n/a
10n/a# This test is checking a few specific problem spots with the resource module.
11n/a
12n/aclass ResourceTest(unittest.TestCase):
13n/a
14n/a def test_args(self):
15n/a self.assertRaises(TypeError, resource.getrlimit)
16n/a self.assertRaises(TypeError, resource.getrlimit, 42, 42)
17n/a self.assertRaises(TypeError, resource.setrlimit)
18n/a self.assertRaises(TypeError, resource.setrlimit, 42, 42, 42)
19n/a
20n/a def test_fsize_ismax(self):
21n/a try:
22n/a (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
23n/a except AttributeError:
24n/a pass
25n/a else:
26n/a # RLIMIT_FSIZE should be RLIM_INFINITY, which will be a really big
27n/a # number on a platform with large file support. On these platforms,
28n/a # we need to test that the get/setrlimit functions properly convert
29n/a # the number to a C long long and that the conversion doesn't raise
30n/a # an error.
31n/a self.assertEqual(resource.RLIM_INFINITY, max)
32n/a resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
33n/a
34n/a def test_fsize_enforced(self):
35n/a try:
36n/a (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
37n/a except AttributeError:
38n/a pass
39n/a else:
40n/a # Check to see what happens when the RLIMIT_FSIZE is small. Some
41n/a # versions of Python were terminated by an uncaught SIGXFSZ, but
42n/a # pythonrun.c has been fixed to ignore that exception. If so, the
43n/a # write() should return EFBIG when the limit is exceeded.
44n/a
45n/a # At least one platform has an unlimited RLIMIT_FSIZE and attempts
46n/a # to change it raise ValueError instead.
47n/a try:
48n/a try:
49n/a resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max))
50n/a limit_set = True
51n/a except ValueError:
52n/a limit_set = False
53n/a f = open(support.TESTFN, "wb")
54n/a try:
55n/a f.write(b"X" * 1024)
56n/a try:
57n/a f.write(b"Y")
58n/a f.flush()
59n/a # On some systems (e.g., Ubuntu on hppa) the flush()
60n/a # doesn't always cause the exception, but the close()
61n/a # does eventually. Try flushing several times in
62n/a # an attempt to ensure the file is really synced and
63n/a # the exception raised.
64n/a for i in range(5):
65n/a time.sleep(.1)
66n/a f.flush()
67n/a except OSError:
68n/a if not limit_set:
69n/a raise
70n/a if limit_set:
71n/a # Close will attempt to flush the byte we wrote
72n/a # Restore limit first to avoid getting a spurious error
73n/a resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
74n/a finally:
75n/a f.close()
76n/a finally:
77n/a if limit_set:
78n/a resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
79n/a support.unlink(support.TESTFN)
80n/a
81n/a def test_fsize_toobig(self):
82n/a # Be sure that setrlimit is checking for really large values
83n/a too_big = 10**50
84n/a try:
85n/a (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
86n/a except AttributeError:
87n/a pass
88n/a else:
89n/a try:
90n/a resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max))
91n/a except (OverflowError, ValueError):
92n/a pass
93n/a try:
94n/a resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big))
95n/a except (OverflowError, ValueError):
96n/a pass
97n/a
98n/a def test_getrusage(self):
99n/a self.assertRaises(TypeError, resource.getrusage)
100n/a self.assertRaises(TypeError, resource.getrusage, 42, 42)
101n/a usageself = resource.getrusage(resource.RUSAGE_SELF)
102n/a usagechildren = resource.getrusage(resource.RUSAGE_CHILDREN)
103n/a # May not be available on all systems.
104n/a try:
105n/a usageboth = resource.getrusage(resource.RUSAGE_BOTH)
106n/a except (ValueError, AttributeError):
107n/a pass
108n/a try:
109n/a usage_thread = resource.getrusage(resource.RUSAGE_THREAD)
110n/a except (ValueError, AttributeError):
111n/a pass
112n/a
113n/a # Issue 6083: Reference counting bug
114n/a def test_setrusage_refcount(self):
115n/a try:
116n/a limits = resource.getrlimit(resource.RLIMIT_CPU)
117n/a except AttributeError:
118n/a pass
119n/a else:
120n/a class BadSequence:
121n/a def __len__(self):
122n/a return 2
123n/a def __getitem__(self, key):
124n/a if key in (0, 1):
125n/a return len(tuple(range(1000000)))
126n/a raise IndexError
127n/a
128n/a resource.setrlimit(resource.RLIMIT_CPU, BadSequence())
129n/a
130n/a def test_pagesize(self):
131n/a pagesize = resource.getpagesize()
132n/a self.assertIsInstance(pagesize, int)
133n/a self.assertGreaterEqual(pagesize, 0)
134n/a
135n/a @unittest.skipUnless(sys.platform == 'linux', 'test requires Linux')
136n/a def test_linux_constants(self):
137n/a for attr in ['MSGQUEUE', 'NICE', 'RTPRIO', 'RTTIME', 'SIGPENDING']:
138n/a with contextlib.suppress(AttributeError):
139n/a self.assertIsInstance(getattr(resource, 'RLIMIT_' + attr), int)
140n/a
141n/a @support.requires_freebsd_version(9)
142n/a def test_freebsd_contants(self):
143n/a for attr in ['SWAP', 'SBSIZE', 'NPTS']:
144n/a with contextlib.suppress(AttributeError):
145n/a self.assertIsInstance(getattr(resource, 'RLIMIT_' + attr), int)
146n/a
147n/a @unittest.skipUnless(hasattr(resource, 'prlimit'), 'no prlimit')
148n/a @support.requires_linux_version(2, 6, 36)
149n/a def test_prlimit(self):
150n/a self.assertRaises(TypeError, resource.prlimit)
151n/a if os.geteuid() != 0:
152n/a self.assertRaises(PermissionError, resource.prlimit,
153n/a 1, resource.RLIMIT_AS)
154n/a self.assertRaises(ProcessLookupError, resource.prlimit,
155n/a -1, resource.RLIMIT_AS)
156n/a limit = resource.getrlimit(resource.RLIMIT_AS)
157n/a self.assertEqual(resource.prlimit(0, resource.RLIMIT_AS), limit)
158n/a self.assertEqual(resource.prlimit(0, resource.RLIMIT_AS, limit),
159n/a limit)
160n/a
161n/a # Issue 20191: Reference counting bug
162n/a @unittest.skipUnless(hasattr(resource, 'prlimit'), 'no prlimit')
163n/a @support.requires_linux_version(2, 6, 36)
164n/a def test_prlimit_refcount(self):
165n/a class BadSeq:
166n/a def __len__(self):
167n/a return 2
168n/a def __getitem__(self, key):
169n/a return limits[key] - 1 # new reference
170n/a
171n/a limits = resource.getrlimit(resource.RLIMIT_AS)
172n/a self.assertEqual(resource.prlimit(0, resource.RLIMIT_AS, BadSeq()),
173n/a limits)
174n/a
175n/a
176n/adef test_main(verbose=None):
177n/a support.run_unittest(ResourceTest)
178n/a
179n/aif __name__ == "__main__":
180n/a test_main()