blob: 2450e782f6d1655729af82e21cfd0e0dd2c71201 [file] [log] [blame]
Thomas Woutersb2137042007-02-01 18:02:27 +00001import unittest
2from test import test_support
Jeremy Hylton74ce77f2002-04-23 20:21:22 +00003
Jeremy Hylton74ce77f2002-04-23 20:21:22 +00004
Thomas Woutersb2137042007-02-01 18:02:27 +00005import os, resource
Jeremy Hylton74ce77f2002-04-23 20:21:22 +00006
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 Wouters9fe394c2007-02-05 01:24:16 +000018
Thomas Woutersb2137042007-02-01 18:02:27 +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
Thomas Woutersb2137042007-02-01 18:02:27 +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.
Thomas Wouters9fe394c2007-02-05 01:24:16 +000042
Thomas Woutersb2137042007-02-01 18:02:27 +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")
52 f.write("X" * 1024)
53 try:
54 f.write("Y")
55 f.flush()
56 except IOError:
57 if not limit_set:
58 raise
59 f.close()
60 os.unlink(test_support.TESTFN)
61 finally:
62 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
63
64 def test_fsize_toobig(self):
65 # Be sure that setrlimit is checking for really large values
66 too_big = 10**50
67 try:
68 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
69 except AttributeError:
70 pass
71 else:
72 try:
73 resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max))
74 except (OverflowError, ValueError):
75 pass
76 try:
77 resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big))
78 except (OverflowError, ValueError):
79 pass
80
81 def test_getrusage(self):
82 self.assertRaises(TypeError, resource.getrusage)
83 self.assertRaises(TypeError, resource.getrusage, 42, 42)
84 usageself = resource.getrusage(resource.RUSAGE_SELF)
85 usagechildren = resource.getrusage(resource.RUSAGE_CHILDREN)
86 # May not be available on all systems.
87 try:
88 usageboth = resource.getrusage(resource.RUSAGE_BOTH)
89 except (ValueError, AttributeError):
90 pass
91
92def test_main(verbose=None):
93 test_support.run_unittest(ResourceTest)
94
95if __name__ == "__main__":
96 test_main()