blob: 4a61582cfe509656719f43ef1e6c01cbda1c4e4b [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):
10 def test_fsize_ismax(self):
11
12 try:
13 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
14 except AttributeError:
15 pass
16 else:
17 # RLIMIT_FSIZE should be RLIM_INFINITY, which will be a really big
18 # number on a platform with large file support. On these platforms,
19 # we need to test that the get/setrlimit functions properly convert
20 # the number to a C long long and that the conversion doesn't raise
21 # an error.
22 self.assertEqual(resource.RLIM_INFINITY, max)
23 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
Jeremy Hylton74ce77f2002-04-23 20:21:22 +000024
Walter Dörwald9fab9a72007-01-20 17:28:31 +000025 def test_fsize_enforced(self):
26 try:
27 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
28 except AttributeError:
29 pass
30 else:
31 # Check to see what happens when the RLIMIT_FSIZE is small. Some
32 # versions of Python were terminated by an uncaught SIGXFSZ, but
33 # pythonrun.c has been fixed to ignore that exception. If so, the
34 # write() should return EFBIG when the limit is exceeded.
35
36 # At least one platform has an unlimited RLIMIT_FSIZE and attempts
37 # to change it raise ValueError instead.
38 try:
39 try:
40 resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max))
41 limit_set = True
42 except ValueError:
43 limit_set = False
44 f = open(test_support.TESTFN, "wb")
45 f.write("X" * 1024)
46 try:
47 f.write("Y")
48 f.flush()
49 except IOError:
50 if not limit_set:
51 raise
52 f.close()
53 os.unlink(test_support.TESTFN)
54 finally:
55 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
Jason Tishlere4a070a2002-12-12 18:13:36 +000056
Walter Dörwald9fab9a72007-01-20 17:28:31 +000057 def test_fsize_toobig(self):
58 # Be sure that setrlimit is checking for really large values
59 too_big = 10L**50
60 try:
61 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
62 except AttributeError:
63 pass
64 else:
65 try:
66 resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max))
67 except (OverflowError, ValueError):
68 pass
69 try:
70 resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big))
71 except (OverflowError, ValueError):
72 pass
Jeremy Hylton74ce77f2002-04-23 20:21:22 +000073
Walter Dörwald9fab9a72007-01-20 17:28:31 +000074def test_main(verbose=None):
75 test_support.run_unittest(ResourceTest)
76
77if __name__ == "__main__":
78 test_main()