blob: 3ff31bb33dd5be97b1c0e7fc3ee1ced32e29f0a2 [file] [log] [blame]
Walter Dörwald9fab9a72007-01-20 17:28:31 +00001import unittest
2from test import test_support
Jeremy Hylton74ce77f2002-04-23 20:21:22 +00003
Jeremy Hylton74ce77f2002-04-23 20:21:22 +00004
Walter Dörwald9fab9a72007-01-20 17:28:31 +00005import os, resource
Jeremy Hylton74ce77f2002-04-23 20:21:22 +00006
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:
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
Walter Dörwald9fab9a72007-01-20 17:28:31 +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.
Tim Petersf733abb2007-01-30 03:03:46 +000041
Walter Dörwald9fab9a72007-01-20 17:28:31 +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
50 f = open(test_support.TESTFN, "wb")
Walter Dörwald9fab9a72007-01-20 17:28:31 +000051 try:
Nick Coghlan12adef92007-07-24 14:39:23 +000052 f.write("X" * 1024)
53 try:
54 f.write("Y")
55 f.flush()
Neal Norwitz7f47d932008-01-26 23:13:46 +000056 # On some systems (e.g., Ubuntu on hppa) the flush()
57 # doesn't cause the exception, but the close() does.
58 f.close()
Nick Coghlan12adef92007-07-24 14:39:23 +000059 except IOError:
60 if not limit_set:
61 raise
62 if limit_set:
63 # Close will attempt to flush the byte we wrote
64 # Restore limit first to avoid getting a spurious error
65 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
66 finally:
67 f.close()
Walter Dörwald9fab9a72007-01-20 17:28:31 +000068 finally:
Nick Coghlan12adef92007-07-24 14:39:23 +000069 if limit_set:
70 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
Neal Norwitza1215082008-01-26 21:02:45 +000071 test_support.unlink(test_support.TESTFN)
Jason Tishlere4a070a2002-12-12 18:13:36 +000072
Walter Dörwald9fab9a72007-01-20 17:28:31 +000073 def test_fsize_toobig(self):
74 # Be sure that setrlimit is checking for really large values
75 too_big = 10L**50
76 try:
77 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
78 except AttributeError:
79 pass
80 else:
81 try:
82 resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max))
83 except (OverflowError, ValueError):
84 pass
85 try:
86 resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big))
87 except (OverflowError, ValueError):
88 pass
Jeremy Hylton74ce77f2002-04-23 20:21:22 +000089
Walter Dörwald66262ab2007-01-20 18:19:33 +000090 def test_getrusage(self):
91 self.assertRaises(TypeError, resource.getrusage)
92 self.assertRaises(TypeError, resource.getrusage, 42, 42)
93 usageself = resource.getrusage(resource.RUSAGE_SELF)
94 usagechildren = resource.getrusage(resource.RUSAGE_CHILDREN)
95 # May not be available on all systems.
96 try:
97 usageboth = resource.getrusage(resource.RUSAGE_BOTH)
Walter Dörwaldd4143022007-01-20 19:03:17 +000098 except (ValueError, AttributeError):
Walter Dörwald66262ab2007-01-20 18:19:33 +000099 pass
100
Walter Dörwald9fab9a72007-01-20 17:28:31 +0000101def test_main(verbose=None):
102 test_support.run_unittest(ResourceTest)
103
104if __name__ == "__main__":
105 test_main()