2 from test
import test_support
5 resource
= test_support
.import_module('resource')
7 # This test is checking a few specific problem spots with the resource module.
9 class ResourceTest(unittest
.TestCase
):
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)
17 def test_fsize_ismax(self
):
19 (cur
, max) = resource
.getrlimit(resource
.RLIMIT_FSIZE
)
20 except AttributeError:
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
28 self
.assertEqual(resource
.RLIM_INFINITY
, max)
29 resource
.setrlimit(resource
.RLIMIT_FSIZE
, (cur
, max))
31 def test_fsize_enforced(self
):
33 (cur
, max) = resource
.getrlimit(resource
.RLIMIT_FSIZE
)
34 except AttributeError:
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.
42 # At least one platform has an unlimited RLIMIT_FSIZE and attempts
43 # to change it raise ValueError instead.
46 resource
.setrlimit(resource
.RLIMIT_FSIZE
, (1024, max))
50 f
= open(test_support
.TESTFN
, "wb")
56 # On some systems (e.g., Ubuntu on hppa) the flush()
57 # doesn't always cause the exception, but the close()
58 # does eventually. Try flushing several times in
59 # an attempt to ensure the file is really synced and
60 # the exception raised.
68 # Close will attempt to flush the byte we wrote
69 # Restore limit first to avoid getting a spurious error
70 resource
.setrlimit(resource
.RLIMIT_FSIZE
, (cur
, max))
75 resource
.setrlimit(resource
.RLIMIT_FSIZE
, (cur
, max))
76 test_support
.unlink(test_support
.TESTFN
)
78 def test_fsize_toobig(self
):
79 # Be sure that setrlimit is checking for really large values
82 (cur
, max) = resource
.getrlimit(resource
.RLIMIT_FSIZE
)
83 except AttributeError:
87 resource
.setrlimit(resource
.RLIMIT_FSIZE
, (too_big
, max))
88 except (OverflowError, ValueError):
91 resource
.setrlimit(resource
.RLIMIT_FSIZE
, (max, too_big
))
92 except (OverflowError, ValueError):
95 def test_getrusage(self
):
96 self
.assertRaises(TypeError, resource
.getrusage
)
97 self
.assertRaises(TypeError, resource
.getrusage
, 42, 42)
98 usageself
= resource
.getrusage(resource
.RUSAGE_SELF
)
99 usagechildren
= resource
.getrusage(resource
.RUSAGE_CHILDREN
)
100 # May not be available on all systems.
102 usageboth
= resource
.getrusage(resource
.RUSAGE_BOTH
)
103 except (ValueError, AttributeError):
106 def test_main(verbose
=None):
107 test_support
.run_unittest(ResourceTest
)
109 if __name__
== "__main__":