blob: bb3ff25a6a07b3f42fcb20783e65747568fc665a [file] [log] [blame]
Thomas Woutersb2137042007-02-01 18:02:27 +00001import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00002from test import support
Christian Heimes412dc9c2008-01-27 18:55:54 +00003import time
Jeremy Hylton74ce77f2002-04-23 20:21:22 +00004
R. David Murraya21e4ca2009-03-31 23:16:50 +00005resource = support.import_module('resource')
6
Thomas Woutersb2137042007-02-01 18:02:27 +00007# This test is checking a few specific problem spots with the resource module.
Jeremy Hylton74ce77f2002-04-23 20:21:22 +00008
Thomas Woutersb2137042007-02-01 18:02:27 +00009class ResourceTest(unittest.TestCase):
Jeremy Hylton74ce77f2002-04-23 20:21:22 +000010
Thomas Woutersb2137042007-02-01 18:02:27 +000011 def test_args(self):
12 self.assertRaises(TypeError, resource.getrlimit)
13 self.assertRaises(TypeError, resource.getrlimit, 42, 42)
14 self.assertRaises(TypeError, resource.setrlimit)
15 self.assertRaises(TypeError, resource.setrlimit, 42, 42, 42)
Jason Tishlere4a070a2002-12-12 18:13:36 +000016
Thomas Woutersb2137042007-02-01 18:02:27 +000017 def test_fsize_ismax(self):
Thomas Woutersb2137042007-02-01 18:02:27 +000018 try:
19 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
20 except AttributeError:
21 pass
22 else:
23 # RLIMIT_FSIZE should be RLIM_INFINITY, which will be a really big
24 # number on a platform with large file support. On these platforms,
25 # we need to test that the get/setrlimit functions properly convert
26 # the number to a C long long and that the conversion doesn't raise
27 # an error.
28 self.assertEqual(resource.RLIM_INFINITY, max)
29 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
Jeremy Hylton74ce77f2002-04-23 20:21:22 +000030
Thomas Woutersb2137042007-02-01 18:02:27 +000031 def test_fsize_enforced(self):
32 try:
33 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
34 except AttributeError:
35 pass
36 else:
37 # Check to see what happens when the RLIMIT_FSIZE is small. Some
38 # versions of Python were terminated by an uncaught SIGXFSZ, but
39 # pythonrun.c has been fixed to ignore that exception. If so, the
40 # write() should return EFBIG when the limit is exceeded.
Thomas Wouters9fe394c2007-02-05 01:24:16 +000041
Thomas Woutersb2137042007-02-01 18:02:27 +000042 # At least one platform has an unlimited RLIMIT_FSIZE and attempts
43 # to change it raise ValueError instead.
44 try:
45 try:
46 resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max))
47 limit_set = True
48 except ValueError:
49 limit_set = False
Benjamin Petersonee8712c2008-05-20 21:35:26 +000050 f = open(support.TESTFN, "wb")
Thomas Woutersb2137042007-02-01 18:02:27 +000051 try:
Guido van Rossumb644fb42007-08-27 23:36:53 +000052 f.write(b"X" * 1024)
Guido van Rossum806c2462007-08-06 23:33:07 +000053 try:
Guido van Rossumb644fb42007-08-27 23:36:53 +000054 f.write(b"Y")
Guido van Rossum806c2462007-08-06 23:33:07 +000055 f.flush()
Christian Heimesaf98da12008-01-27 15:18:18 +000056 # On some systems (e.g., Ubuntu on hppa) the flush()
57 # doesn't always cause the exception, but the close()
Christian Heimes26855632008-01-27 23:50:43 +000058 # does eventually. Try flushing several times in
Christian Heimesaf98da12008-01-27 15:18:18 +000059 # an attempt to ensure the file is really synced and
60 # the exception raised.
61 for i in range(5):
Christian Heimes412dc9c2008-01-27 18:55:54 +000062 time.sleep(.1)
63 f.flush()
Andrew Svetlovf7a17b42012-12-25 16:47:37 +020064 except OSError:
Guido van Rossum806c2462007-08-06 23:33:07 +000065 if not limit_set:
66 raise
67 if limit_set:
68 # Close will attempt to flush the byte we wrote
69 # Restore limit first to avoid getting a spurious error
70 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
71 finally:
72 f.close()
Thomas Woutersb2137042007-02-01 18:02:27 +000073 finally:
Guido van Rossum806c2462007-08-06 23:33:07 +000074 if limit_set:
75 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
Benjamin Petersonee8712c2008-05-20 21:35:26 +000076 support.unlink(support.TESTFN)
Thomas Woutersb2137042007-02-01 18:02:27 +000077
78 def test_fsize_toobig(self):
79 # Be sure that setrlimit is checking for really large values
80 too_big = 10**50
81 try:
82 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
83 except AttributeError:
84 pass
85 else:
86 try:
87 resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max))
88 except (OverflowError, ValueError):
89 pass
90 try:
91 resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big))
92 except (OverflowError, ValueError):
93 pass
94
95 def test_getrusage(self):
96 self.assertRaises(TypeError, resource.getrusage)
97 self.assertRaises(TypeError, resource.getrusage, 42, 42)
98 usageself = resource.getrusage(resource.RUSAGE_SELF)
99 usagechildren = resource.getrusage(resource.RUSAGE_CHILDREN)
100 # May not be available on all systems.
101 try:
102 usageboth = resource.getrusage(resource.RUSAGE_BOTH)
103 except (ValueError, AttributeError):
104 pass
Antoine Pitroub6d4ee52010-11-17 16:19:35 +0000105 try:
106 usage_thread = resource.getrusage(resource.RUSAGE_THREAD)
107 except (ValueError, AttributeError):
108 pass
Thomas Woutersb2137042007-02-01 18:02:27 +0000109
Serhiy Storchaka19c4e0d2013-02-04 12:47:24 +0200110 # Issue 6083: Reference counting bug
111 def test_setrusage_refcount(self):
112 try:
113 limits = resource.getrlimit(resource.RLIMIT_CPU)
114 except AttributeError:
115 pass
116 else:
117 class BadSequence:
118 def __len__(self):
119 return 2
120 def __getitem__(self, key):
121 if key in (0, 1):
122 return len(tuple(range(1000000)))
123 raise IndexError
124
125 resource.setrlimit(resource.RLIMIT_CPU, BadSequence())
126
Thomas Woutersb2137042007-02-01 18:02:27 +0000127def test_main(verbose=None):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000128 support.run_unittest(ResourceTest)
Thomas Woutersb2137042007-02-01 18:02:27 +0000129
130if __name__ == "__main__":
131 test_main()