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