Debian Codegraph @

Lib/test/test_resource.py

raw
1 import contextlib
2 import sys
3 import unittest
4 from test import support
5 from test.support import import_helper
6 from test.support import os_helper
7 import time
8
9 resource = import_helper.import_module('resource')
10
11 # This test is checking a few specific problem spots with the resource module.
12
13 class ResourceTest(unittest.TestCase):
14
15 def test_args(self):
16 self.assertRaises(TypeError, resource.getrlimit)
17 self.assertRaises(TypeError, resource.getrlimit, 0, 42)
18 self.assertRaises(OverflowError, resource.getrlimit, 2**1000)
19 self.assertRaises(OverflowError, resource.getrlimit, -2**1000)
20 self.assertRaises(TypeError, resource.getrlimit, '0')
21 self.assertRaises(TypeError, resource.setrlimit)
22 self.assertRaises(TypeError, resource.setrlimit, 0)
23 self.assertRaises(TypeError, resource.setrlimit, 0, 42)
24 self.assertRaises(TypeError, resource.setrlimit, 0, 42, 42)
25 self.assertRaises(OverflowError, resource.setrlimit, 2**1000, (42, 42))
26 self.assertRaises(OverflowError, resource.setrlimit, -2**1000, (42, 42))
27 self.assertRaises(ValueError, resource.setrlimit, 0, (42,))
28 self.assertRaises(ValueError, resource.setrlimit, 0, (42, 42, 42))
29 self.assertRaises(TypeError, resource.setrlimit, '0', (42, 42))
30 self.assertRaises(TypeError, resource.setrlimit, 0, ('42', 42))
31 self.assertRaises(TypeError, resource.setrlimit, 0, (42, '42'))
32
33 @unittest.skipIf(sys.platform == "vxworks",
34 "setting RLIMIT_FSIZE is not supported on VxWorks")
35 @unittest.skipUnless(hasattr(resource, 'RLIMIT_FSIZE'), 'requires resource.RLIMIT_FSIZE')
36 def test_fsize_ismax(self):
37 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
38 # RLIMIT_FSIZE should be RLIM_INFINITY, which will be a really big
39 # number on a platform with large file support. On these platforms,
40 # we need to test that the get/setrlimit functions properly convert
41 # the number to a C long long and that the conversion doesn't raise
42 # an error.
43 self.assertEqual(resource.RLIM_INFINITY, max)
44 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
45
46 @unittest.skipIf(sys.platform == "vxworks",
47 "setting RLIMIT_FSIZE is not supported on VxWorks")
48 @unittest.skipUnless(hasattr(resource, 'RLIMIT_FSIZE'), 'requires resource.RLIMIT_FSIZE')
49 def test_fsize_enforced(self):
50 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
51 # Check to see what happens when the RLIMIT_FSIZE is small. Some
52 # versions of Python were terminated by an uncaught SIGXFSZ, but
53 # pythonrun.c has been fixed to ignore that exception. If so, the
54 # write() should return EFBIG when the limit is exceeded.
55
56 # At least one platform has an unlimited RLIMIT_FSIZE and attempts
57 # to change it raise ValueError instead.
58 try:
59 try:
60 resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max))
61 limit_set = True
62 except ValueError:
63 limit_set = False
64 f = open(os_helper.TESTFN, "wb")
65 try:
66 f.write(b"X" * 1024)
67 try:
68 f.write(b"Y")
69 f.flush()
70 # On some systems (e.g., Ubuntu on hppa) the flush()
71 # doesn't always cause the exception, but the close()
72 # does eventually. Try flushing several times in
73 # an attempt to ensure the file is really synced and
74 # the exception raised.
75 for i in range(5):
76 time.sleep(.1)
77 f.flush()
78 except OSError:
79 if not limit_set:
80 raise
81 if limit_set:
82 # Close will attempt to flush the byte we wrote
83 # Restore limit first to avoid getting a spurious error
84 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
85 finally:
86 f.close()
87 finally:
88 if limit_set:
89 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
90 os_helper.unlink(os_helper.TESTFN)
91
92 @unittest.skipIf(sys.platform == "vxworks",
93 "setting RLIMIT_FSIZE is not supported on VxWorks")
94 @unittest.skipUnless(hasattr(resource, 'RLIMIT_FSIZE'), 'requires resource.RLIMIT_FSIZE')
95 def test_fsize_too_big(self):
96 # Be sure that setrlimit is checking for really large values
97 too_big = 10**50
98 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
99 try:
100 resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max))
101 except (OverflowError, ValueError):
102 pass
103 try:
104 resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big))
105 except (OverflowError, ValueError):
106 pass
107
108 @unittest.skipIf(sys.platform == "vxworks",
109 "setting RLIMIT_FSIZE is not supported on VxWorks")
110 @unittest.skipUnless(hasattr(resource, 'RLIMIT_FSIZE'), 'requires resource.RLIMIT_FSIZE')
111 def test_fsize_not_too_big(self):
112 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
113 self.addCleanup(resource.setrlimit, resource.RLIMIT_FSIZE, (cur, max))
114
115 def expected(cur):
116 if resource.RLIM_INFINITY < 0:
117 return [(cur, max), (resource.RLIM_INFINITY, max)]
118 elif resource.RLIM_INFINITY < cur:
119 return [(resource.RLIM_INFINITY, max)]
120 else:
121 return [(cur, max)]
122
123 resource.setrlimit(resource.RLIMIT_FSIZE, (2**31-5, max))
124 self.assertEqual(resource.getrlimit(resource.RLIMIT_FSIZE), (2**31-5, max))
125
126 try:
127 resource.setrlimit(resource.RLIMIT_FSIZE, (2**32, max))
128 except OverflowError:
129 resource.setrlimit(resource.RLIMIT_FSIZE, (2**31, max))
130 self.assertIn(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**31))
131 resource.setrlimit(resource.RLIMIT_FSIZE, (2**32-5, max))
132 self.assertIn(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**32-5))
133 else:
134 self.assertIn(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**32))
135 resource.setrlimit(resource.RLIMIT_FSIZE, (2**31, max))
136 self.assertEqual(resource.getrlimit(resource.RLIMIT_FSIZE), (2**31, max))
137 resource.setrlimit(resource.RLIMIT_FSIZE, (2**32-5, max))
138 self.assertEqual(resource.getrlimit(resource.RLIMIT_FSIZE), (2**32-5, max))
139
140 resource.setrlimit(resource.RLIMIT_FSIZE, (2**63-5, max))
141 self.assertIn(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**63-5))
142 try:
143 resource.setrlimit(resource.RLIMIT_FSIZE, (2**63, max))
144 except ValueError:
145 # There is a hard limit on macOS.
146 pass
147 else:
148 self.assertIn(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**63))
149 resource.setrlimit(resource.RLIMIT_FSIZE, (2**64-5, max))
150 self.assertIn(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**64-5))
151
152 @unittest.skipIf(sys.platform == "vxworks",
153 "setting RLIMIT_FSIZE is not supported on VxWorks")
154 @unittest.skipUnless(hasattr(resource, 'RLIMIT_FSIZE'), 'requires resource.RLIMIT_FSIZE')
155 def test_fsize_negative(self):
156 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
157 for value in -5, -2**31, -2**32-5, -2**63, -2**64-5, -2**1000:
158 with self.subTest(value=value):
159 # This test assumes that the values don't map to RLIM_INFINITY,
160 # though Posix doesn't guarantee it.
161 self.assertNotEqual(value, resource.RLIM_INFINITY)
162
163 self.assertRaises(ValueError, resource.setrlimit, resource.RLIMIT_FSIZE, (value, max))
164 self.assertRaises(ValueError, resource.setrlimit, resource.RLIMIT_FSIZE, (cur, value))
165
166 @unittest.skipUnless(hasattr(resource, "getrusage"), "needs getrusage")
167 def test_getrusage(self):
168 self.assertRaises(TypeError, resource.getrusage)
169 self.assertRaises(TypeError, resource.getrusage, 42, 42)
170 usageself = resource.getrusage(resource.RUSAGE_SELF)
171 usagechildren = resource.getrusage(resource.RUSAGE_CHILDREN)
172 # May not be available on all systems.
173 try:
174 usageboth = resource.getrusage(resource.RUSAGE_BOTH)
175 except (ValueError, AttributeError):
176 pass
177 try:
178 usage_thread = resource.getrusage(resource.RUSAGE_THREAD)
179 except (ValueError, AttributeError):
180 pass
181
182 # Issue 6083: Reference counting bug
183 @unittest.skipIf(sys.platform == "vxworks",
184 "setting RLIMIT_CPU is not supported on VxWorks")
185 @unittest.skipUnless(hasattr(resource, 'RLIMIT_CPU'), 'requires resource.RLIMIT_CPU')
186 def test_setrusage_refcount(self):
187 limits = resource.getrlimit(resource.RLIMIT_CPU)
188 class BadSequence:
189 def __len__(self):
190 return 2
191 def __getitem__(self, key):
192 if key in (0, 1):
193 return len(tuple(range(1000000)))
194 raise IndexError
195
196 resource.setrlimit(resource.RLIMIT_CPU, BadSequence())
197
198 def test_pagesize(self):
199 pagesize = resource.getpagesize()
200 self.assertIsInstance(pagesize, int)
201 self.assertGreaterEqual(pagesize, 0)
202
203 @unittest.skipUnless(sys.platform in ('linux', 'android'), 'Linux only')
204 def test_linux_constants(self):
205 for attr in ['MSGQUEUE', 'NICE', 'RTPRIO', 'RTTIME', 'SIGPENDING']:
206 with contextlib.suppress(AttributeError):
207 self.assertIsInstance(getattr(resource, 'RLIMIT_' + attr), int)
208
209 def test_freebsd_contants(self):
210 for attr in ['SWAP', 'SBSIZE', 'NPTS']:
211 with contextlib.suppress(AttributeError):
212 self.assertIsInstance(getattr(resource, 'RLIMIT_' + attr), int)
213
214 @unittest.skipUnless(hasattr(resource, 'prlimit'), 'no prlimit')
215 @support.requires_linux_version(2, 6, 36)
216 def test_prlimit(self):
217 self.assertRaises(TypeError, resource.prlimit)
218 self.assertRaises(ProcessLookupError, resource.prlimit,
219 -1, resource.RLIMIT_AS)
220 limit = resource.getrlimit(resource.RLIMIT_AS)
221 self.assertEqual(resource.prlimit(0, resource.RLIMIT_AS), limit)
222 self.assertEqual(resource.prlimit(0, resource.RLIMIT_AS, limit),
223 limit)
224
225 # Issue 20191: Reference counting bug
226 @unittest.skipUnless(hasattr(resource, 'prlimit'), 'no prlimit')
227 @support.requires_linux_version(2, 6, 36)
228 def test_prlimit_refcount(self):
229 class BadSeq:
230 def __len__(self):
231 return 2
232 def __getitem__(self, key):
233 lim = limits[key]
234 return lim - 1 if lim > 0 else lim + sys.maxsize*2 # new reference
235
236 limits = resource.getrlimit(resource.RLIMIT_AS)
237 self.assertEqual(resource.prlimit(0, resource.RLIMIT_AS, BadSeq()),
238 limits)
239
240
241 if __name__ == "__main__":
242 unittest.main()