blob: 2ecae0fc450a182b8c5bf47f49f86b60f1dcfda7 [file] [log] [blame]
Brett Cannona20800d2013-10-25 15:45:25 -04001import contextlib
Christian Heimes6fc79bf2013-10-22 11:09:27 +02002import sys
Christian Heimesd885aa42013-10-22 11:45:30 +02003import os
Thomas Woutersb2137042007-02-01 18:02:27 +00004import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00005from test import support
Christian Heimes412dc9c2008-01-27 18:55:54 +00006import time
Jeremy Hylton74ce77f2002-04-23 20:21:22 +00007
R. David Murraya21e4ca2009-03-31 23:16:50 +00008resource = support.import_module('resource')
9
Thomas Woutersb2137042007-02-01 18:02:27 +000010# This test is checking a few specific problem spots with the resource module.
Jeremy Hylton74ce77f2002-04-23 20:21:22 +000011
Thomas Woutersb2137042007-02-01 18:02:27 +000012class ResourceTest(unittest.TestCase):
Jeremy Hylton74ce77f2002-04-23 20:21:22 +000013
Thomas Woutersb2137042007-02-01 18:02:27 +000014 def test_args(self):
15 self.assertRaises(TypeError, resource.getrlimit)
16 self.assertRaises(TypeError, resource.getrlimit, 42, 42)
17 self.assertRaises(TypeError, resource.setrlimit)
18 self.assertRaises(TypeError, resource.setrlimit, 42, 42, 42)
Jason Tishlere4a070a2002-12-12 18:13:36 +000019
Thomas Woutersb2137042007-02-01 18:02:27 +000020 def test_fsize_ismax(self):
Thomas Woutersb2137042007-02-01 18:02:27 +000021 try:
22 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
23 except AttributeError:
24 pass
25 else:
26 # RLIMIT_FSIZE should be RLIM_INFINITY, which will be a really big
27 # number on a platform with large file support. On these platforms,
28 # we need to test that the get/setrlimit functions properly convert
29 # the number to a C long long and that the conversion doesn't raise
30 # an error.
31 self.assertEqual(resource.RLIM_INFINITY, max)
32 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
Jeremy Hylton74ce77f2002-04-23 20:21:22 +000033
Thomas Woutersb2137042007-02-01 18:02:27 +000034 def test_fsize_enforced(self):
35 try:
36 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
37 except AttributeError:
38 pass
39 else:
40 # Check to see what happens when the RLIMIT_FSIZE is small. Some
41 # versions of Python were terminated by an uncaught SIGXFSZ, but
42 # pythonrun.c has been fixed to ignore that exception. If so, the
43 # write() should return EFBIG when the limit is exceeded.
Thomas Wouters9fe394c2007-02-05 01:24:16 +000044
Thomas Woutersb2137042007-02-01 18:02:27 +000045 # At least one platform has an unlimited RLIMIT_FSIZE and attempts
46 # to change it raise ValueError instead.
47 try:
48 try:
49 resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max))
50 limit_set = True
51 except ValueError:
52 limit_set = False
Benjamin Petersonee8712c2008-05-20 21:35:26 +000053 f = open(support.TESTFN, "wb")
Thomas Woutersb2137042007-02-01 18:02:27 +000054 try:
Guido van Rossumb644fb42007-08-27 23:36:53 +000055 f.write(b"X" * 1024)
Guido van Rossum806c2462007-08-06 23:33:07 +000056 try:
Guido van Rossumb644fb42007-08-27 23:36:53 +000057 f.write(b"Y")
Guido van Rossum806c2462007-08-06 23:33:07 +000058 f.flush()
Christian Heimesaf98da12008-01-27 15:18:18 +000059 # On some systems (e.g., Ubuntu on hppa) the flush()
60 # doesn't always cause the exception, but the close()
Christian Heimes26855632008-01-27 23:50:43 +000061 # does eventually. Try flushing several times in
Christian Heimesaf98da12008-01-27 15:18:18 +000062 # an attempt to ensure the file is really synced and
63 # the exception raised.
64 for i in range(5):
Christian Heimes412dc9c2008-01-27 18:55:54 +000065 time.sleep(.1)
66 f.flush()
Andrew Svetlovf7a17b42012-12-25 16:47:37 +020067 except OSError:
Guido van Rossum806c2462007-08-06 23:33:07 +000068 if not limit_set:
69 raise
70 if limit_set:
71 # Close will attempt to flush the byte we wrote
72 # Restore limit first to avoid getting a spurious error
73 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
74 finally:
75 f.close()
Thomas Woutersb2137042007-02-01 18:02:27 +000076 finally:
Guido van Rossum806c2462007-08-06 23:33:07 +000077 if limit_set:
78 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
Benjamin Petersonee8712c2008-05-20 21:35:26 +000079 support.unlink(support.TESTFN)
Thomas Woutersb2137042007-02-01 18:02:27 +000080
81 def test_fsize_toobig(self):
82 # Be sure that setrlimit is checking for really large values
83 too_big = 10**50
84 try:
85 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
86 except AttributeError:
87 pass
88 else:
89 try:
90 resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max))
91 except (OverflowError, ValueError):
92 pass
93 try:
94 resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big))
95 except (OverflowError, ValueError):
96 pass
97
98 def test_getrusage(self):
99 self.assertRaises(TypeError, resource.getrusage)
100 self.assertRaises(TypeError, resource.getrusage, 42, 42)
101 usageself = resource.getrusage(resource.RUSAGE_SELF)
102 usagechildren = resource.getrusage(resource.RUSAGE_CHILDREN)
103 # May not be available on all systems.
104 try:
105 usageboth = resource.getrusage(resource.RUSAGE_BOTH)
106 except (ValueError, AttributeError):
107 pass
Antoine Pitroub6d4ee52010-11-17 16:19:35 +0000108 try:
109 usage_thread = resource.getrusage(resource.RUSAGE_THREAD)
110 except (ValueError, AttributeError):
111 pass
Thomas Woutersb2137042007-02-01 18:02:27 +0000112
Serhiy Storchaka19c4e0d2013-02-04 12:47:24 +0200113 # Issue 6083: Reference counting bug
114 def test_setrusage_refcount(self):
115 try:
116 limits = resource.getrlimit(resource.RLIMIT_CPU)
117 except AttributeError:
118 pass
119 else:
120 class BadSequence:
121 def __len__(self):
122 return 2
123 def __getitem__(self, key):
124 if key in (0, 1):
125 return len(tuple(range(1000000)))
126 raise IndexError
127
128 resource.setrlimit(resource.RLIMIT_CPU, BadSequence())
129
Christian Heimes4ebc9292013-07-30 15:44:13 +0200130 def test_pagesize(self):
131 pagesize = resource.getpagesize()
132 self.assertIsInstance(pagesize, int)
133 self.assertGreaterEqual(pagesize, 0)
134
Christian Heimes6fc79bf2013-10-22 11:09:27 +0200135 @unittest.skipUnless(sys.platform == 'linux', 'test requires Linux')
136 def test_linux_constants(self):
Brett Cannona20800d2013-10-25 15:45:25 -0400137 for attr in ['MSGQUEUE', 'NICE', 'RTPRIO', 'RTTIME', 'SIGPENDING']:
138 with contextlib.suppress(AttributeError):
139 self.assertIsInstance(getattr(resource, 'RLIMIT_' + attr), int)
Christian Heimes6fc79bf2013-10-22 11:09:27 +0200140
Christian Heimes5bb414d2013-12-08 14:35:55 +0100141 @support.requires_freebsd_version(9)
142 def test_freebsd_contants(self):
143 for attr in ['SWAP', 'SBSIZE', 'NPTS']:
144 with contextlib.suppress(AttributeError):
145 self.assertIsInstance(getattr(resource, 'RLIMIT_' + attr), int)
146
Christian Heimesb7bd5df2013-10-22 11:21:54 +0200147 @unittest.skipUnless(hasattr(resource, 'prlimit'), 'no prlimit')
Christian Heimesc4a4b342013-10-25 08:31:19 +0200148 @support.requires_linux_version(2, 6, 36)
Christian Heimesb7bd5df2013-10-22 11:21:54 +0200149 def test_prlimit(self):
150 self.assertRaises(TypeError, resource.prlimit)
Christian Heimesd885aa42013-10-22 11:45:30 +0200151 if os.geteuid() != 0:
152 self.assertRaises(PermissionError, resource.prlimit,
153 1, resource.RLIMIT_AS)
Christian Heimesb7bd5df2013-10-22 11:21:54 +0200154 self.assertRaises(ProcessLookupError, resource.prlimit,
155 -1, resource.RLIMIT_AS)
Christian Heimesd885aa42013-10-22 11:45:30 +0200156 limit = resource.getrlimit(resource.RLIMIT_AS)
157 self.assertEqual(resource.prlimit(0, resource.RLIMIT_AS), limit)
158 self.assertEqual(resource.prlimit(0, resource.RLIMIT_AS, limit),
159 limit)
Christian Heimesb7bd5df2013-10-22 11:21:54 +0200160
161
Thomas Woutersb2137042007-02-01 18:02:27 +0000162def test_main(verbose=None):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000163 support.run_unittest(ResourceTest)
Thomas Woutersb2137042007-02-01 18:02:27 +0000164
165if __name__ == "__main__":
166 test_main()