blob: de29d3b68b87ad036abde98691491aa03f2f7124 [file] [log] [blame]
Walter Dörwald9fab9a72007-01-20 17:28:31 +00001import unittest
2from test import test_support
Neal Norwitz041ee5d2008-01-27 17:10:50 +00003import time
Jeremy Hylton74ce77f2002-04-23 20:21:22 +00004
R. David Murray3db8a342009-03-30 23:05:48 +00005resource = test_support.import_module('resource')
6
Walter Dörwald9fab9a72007-01-20 17:28:31 +00007# This test is checking a few specific problem spots with the resource module.
Jeremy Hylton74ce77f2002-04-23 20:21:22 +00008
Walter Dörwald9fab9a72007-01-20 17:28:31 +00009class ResourceTest(unittest.TestCase):
Walter Dörwald66262ab2007-01-20 18:19:33 +000010
11 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)
16
Walter Dörwald9fab9a72007-01-20 17:28:31 +000017 def test_fsize_ismax(self):
Walter Dörwald9fab9a72007-01-20 17:28:31 +000018 try:
19 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
20 except AttributeError:
Zachary Ware1f702212013-12-10 14:09:20 -060021 self.skipTest('RLIMIT_FSIZE not available')
22 # RLIMIT_FSIZE should be RLIM_INFINITY, which will be a really big
23 # number on a platform with large file support. On these platforms,
24 # we need to test that the get/setrlimit functions properly convert
25 # the number to a C long long and that the conversion doesn't raise
26 # an error.
27 self.assertEqual(resource.RLIM_INFINITY, max)
28 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
Jeremy Hylton74ce77f2002-04-23 20:21:22 +000029
Walter Dörwald9fab9a72007-01-20 17:28:31 +000030 def test_fsize_enforced(self):
31 try:
32 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
33 except AttributeError:
Zachary Ware1f702212013-12-10 14:09:20 -060034 self.skipTest('RLIMIT_FSIZE not available')
35 # Check to see what happens when the RLIMIT_FSIZE is small. Some
36 # versions of Python were terminated by an uncaught SIGXFSZ, but
37 # pythonrun.c has been fixed to ignore that exception. If so, the
38 # write() should return EFBIG when the limit is exceeded.
Tim Petersf733abb2007-01-30 03:03:46 +000039
Zachary Ware1f702212013-12-10 14:09:20 -060040 # At least one platform has an unlimited RLIMIT_FSIZE and attempts
41 # to change it raise ValueError instead.
42 try:
Walter Dörwald9fab9a72007-01-20 17:28:31 +000043 try:
Zachary Ware1f702212013-12-10 14:09:20 -060044 resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max))
45 limit_set = True
46 except ValueError:
47 limit_set = False
48 f = open(test_support.TESTFN, "wb")
49 try:
50 f.write("X" * 1024)
Walter Dörwald9fab9a72007-01-20 17:28:31 +000051 try:
Zachary Ware1f702212013-12-10 14:09:20 -060052 f.write("Y")
53 f.flush()
54 # On some systems (e.g., Ubuntu on hppa) the flush()
55 # doesn't always cause the exception, but the close()
56 # does eventually. Try flushing several times in
57 # an attempt to ensure the file is really synced and
58 # the exception raised.
59 for i in range(5):
60 time.sleep(.1)
Nick Coghlan12adef92007-07-24 14:39:23 +000061 f.flush()
Zachary Ware1f702212013-12-10 14:09:20 -060062 except IOError:
63 if not limit_set:
64 raise
Nick Coghlan12adef92007-07-24 14:39:23 +000065 if limit_set:
Zachary Ware1f702212013-12-10 14:09:20 -060066 # Close will attempt to flush the byte we wrote
67 # Restore limit first to avoid getting a spurious error
Nick Coghlan12adef92007-07-24 14:39:23 +000068 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
Zachary Ware1f702212013-12-10 14:09:20 -060069 finally:
70 f.close()
71 finally:
72 if limit_set:
73 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
74 test_support.unlink(test_support.TESTFN)
Jason Tishlere4a070a2002-12-12 18:13:36 +000075
Walter Dörwald9fab9a72007-01-20 17:28:31 +000076 def test_fsize_toobig(self):
77 # Be sure that setrlimit is checking for really large values
78 too_big = 10L**50
79 try:
80 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
81 except AttributeError:
Zachary Ware1f702212013-12-10 14:09:20 -060082 self.skipTest('RLIMIT_FSIZE not available')
83 try:
84 resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max))
85 except (OverflowError, ValueError):
Walter Dörwald9fab9a72007-01-20 17:28:31 +000086 pass
Zachary Ware1f702212013-12-10 14:09:20 -060087 try:
88 resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big))
89 except (OverflowError, ValueError):
90 pass
Jeremy Hylton74ce77f2002-04-23 20:21:22 +000091
Walter Dörwald66262ab2007-01-20 18:19:33 +000092 def test_getrusage(self):
93 self.assertRaises(TypeError, resource.getrusage)
94 self.assertRaises(TypeError, resource.getrusage, 42, 42)
95 usageself = resource.getrusage(resource.RUSAGE_SELF)
96 usagechildren = resource.getrusage(resource.RUSAGE_CHILDREN)
97 # May not be available on all systems.
98 try:
99 usageboth = resource.getrusage(resource.RUSAGE_BOTH)
Walter Dörwaldd4143022007-01-20 19:03:17 +0000100 except (ValueError, AttributeError):
Walter Dörwald66262ab2007-01-20 18:19:33 +0000101 pass
102
Serhiy Storchakaa07a8b42013-02-04 12:45:46 +0200103 # Issue 6083: Reference counting bug
104 def test_setrusage_refcount(self):
105 try:
106 limits = resource.getrlimit(resource.RLIMIT_CPU)
107 except AttributeError:
Zachary Ware1f702212013-12-10 14:09:20 -0600108 self.skipTest('RLIMIT_CPU not available')
109 class BadSequence:
110 def __len__(self):
111 return 2
112 def __getitem__(self, key):
113 if key in (0, 1):
114 return len(tuple(range(1000000)))
115 raise IndexError
Serhiy Storchakaa07a8b42013-02-04 12:45:46 +0200116
Zachary Ware1f702212013-12-10 14:09:20 -0600117 resource.setrlimit(resource.RLIMIT_CPU, BadSequence())
Serhiy Storchakaa07a8b42013-02-04 12:45:46 +0200118
Walter Dörwald9fab9a72007-01-20 17:28:31 +0000119def test_main(verbose=None):
120 test_support.run_unittest(ResourceTest)
121
122if __name__ == "__main__":
123 test_main()