blob: 987c8d72fe9d92fa39990be9f20174bd8206ac36 [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 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
50 f = open(test_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()
58 # does eventually. Try closing several times in
59 # an attempt to ensure the file is really synced and
60 # the exception raised.
61 for i in range(5):
62 f.close()
Guido van Rossum806c2462007-08-06 23:33:07 +000063 except IOError:
64 if not limit_set:
65 raise
66 if limit_set:
67 # Close will attempt to flush the byte we wrote
68 # Restore limit first to avoid getting a spurious error
69 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
70 finally:
71 f.close()
Thomas Woutersb2137042007-02-01 18:02:27 +000072 finally:
Guido van Rossum806c2462007-08-06 23:33:07 +000073 if limit_set:
74 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
Christian Heimesaf98da12008-01-27 15:18:18 +000075 test_support.unlink(test_support.TESTFN)
Thomas Woutersb2137042007-02-01 18:02:27 +000076
77 def test_fsize_toobig(self):
78 # Be sure that setrlimit is checking for really large values
79 too_big = 10**50
80 try:
81 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
82 except AttributeError:
83 pass
84 else:
85 try:
86 resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max))
87 except (OverflowError, ValueError):
88 pass
89 try:
90 resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big))
91 except (OverflowError, ValueError):
92 pass
93
94 def test_getrusage(self):
95 self.assertRaises(TypeError, resource.getrusage)
96 self.assertRaises(TypeError, resource.getrusage, 42, 42)
97 usageself = resource.getrusage(resource.RUSAGE_SELF)
98 usagechildren = resource.getrusage(resource.RUSAGE_CHILDREN)
99 # May not be available on all systems.
100 try:
101 usageboth = resource.getrusage(resource.RUSAGE_BOTH)
102 except (ValueError, AttributeError):
103 pass
104
105def test_main(verbose=None):
106 test_support.run_unittest(ResourceTest)
107
108if __name__ == "__main__":
109 test_main()