blob: 16e360f1a8bf91c302ddc8412d8e49281f066eb2 [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):
Tim Petersf733abb2007-01-30 03:03:46 +000018
Walter Dörwald9fab9a72007-01-20 17:28:31 +000019 try:
20 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
21 except AttributeError:
22 pass
23 else:
24 # RLIMIT_FSIZE should be RLIM_INFINITY, which will be a really big
25 # number on a platform with large file support. On these platforms,
26 # we need to test that the get/setrlimit functions properly convert
27 # the number to a C long long and that the conversion doesn't raise
28 # an error.
29 self.assertEqual(resource.RLIM_INFINITY, max)
30 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
Jeremy Hylton74ce77f2002-04-23 20:21:22 +000031
Walter Dörwald9fab9a72007-01-20 17:28:31 +000032 def test_fsize_enforced(self):
33 try:
34 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
35 except AttributeError:
36 pass
37 else:
38 # Check to see what happens when the RLIMIT_FSIZE is small. Some
39 # versions of Python were terminated by an uncaught SIGXFSZ, but
40 # pythonrun.c has been fixed to ignore that exception. If so, the
41 # write() should return EFBIG when the limit is exceeded.
Tim Petersf733abb2007-01-30 03:03:46 +000042
Walter Dörwald9fab9a72007-01-20 17:28:31 +000043 # At least one platform has an unlimited RLIMIT_FSIZE and attempts
44 # to change it raise ValueError instead.
45 try:
46 try:
47 resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max))
48 limit_set = True
49 except ValueError:
50 limit_set = False
51 f = open(test_support.TESTFN, "wb")
Walter Dörwald9fab9a72007-01-20 17:28:31 +000052 try:
Nick Coghlan12adef92007-07-24 14:39:23 +000053 f.write("X" * 1024)
54 try:
55 f.write("Y")
56 f.flush()
57 except IOError:
58 if not limit_set:
59 raise
60 if limit_set:
61 # Close will attempt to flush the byte we wrote
62 # Restore limit first to avoid getting a spurious error
63 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
64 finally:
65 f.close()
66 os.unlink(test_support.TESTFN)
Walter Dörwald9fab9a72007-01-20 17:28:31 +000067 finally:
Nick Coghlan12adef92007-07-24 14:39:23 +000068 if limit_set:
69 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
Jason Tishlere4a070a2002-12-12 18:13:36 +000070
Walter Dörwald9fab9a72007-01-20 17:28:31 +000071 def test_fsize_toobig(self):
72 # Be sure that setrlimit is checking for really large values
73 too_big = 10L**50
74 try:
75 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
76 except AttributeError:
77 pass
78 else:
79 try:
80 resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max))
81 except (OverflowError, ValueError):
82 pass
83 try:
84 resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big))
85 except (OverflowError, ValueError):
86 pass
Jeremy Hylton74ce77f2002-04-23 20:21:22 +000087
Walter Dörwald66262ab2007-01-20 18:19:33 +000088 def test_getrusage(self):
89 self.assertRaises(TypeError, resource.getrusage)
90 self.assertRaises(TypeError, resource.getrusage, 42, 42)
91 usageself = resource.getrusage(resource.RUSAGE_SELF)
92 usagechildren = resource.getrusage(resource.RUSAGE_CHILDREN)
93 # May not be available on all systems.
94 try:
95 usageboth = resource.getrusage(resource.RUSAGE_BOTH)
Walter Dörwaldd4143022007-01-20 19:03:17 +000096 except (ValueError, AttributeError):
Walter Dörwald66262ab2007-01-20 18:19:33 +000097 pass
98
Walter Dörwald9fab9a72007-01-20 17:28:31 +000099def test_main(verbose=None):
100 test_support.run_unittest(ResourceTest)
101
102if __name__ == "__main__":
103 test_main()