]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | import unittest\r |
2 | from test import test_support\r | |
3 | import time\r | |
4 | \r | |
5 | resource = test_support.import_module('resource')\r | |
6 | \r | |
7 | # This test is checking a few specific problem spots with the resource module.\r | |
8 | \r | |
9 | class ResourceTest(unittest.TestCase):\r | |
10 | \r | |
11 | def test_args(self):\r | |
12 | self.assertRaises(TypeError, resource.getrlimit)\r | |
13 | self.assertRaises(TypeError, resource.getrlimit, 42, 42)\r | |
14 | self.assertRaises(TypeError, resource.setrlimit)\r | |
15 | self.assertRaises(TypeError, resource.setrlimit, 42, 42, 42)\r | |
16 | \r | |
17 | def test_fsize_ismax(self):\r | |
18 | try:\r | |
19 | (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)\r | |
20 | except AttributeError:\r | |
21 | pass\r | |
22 | else:\r | |
23 | # RLIMIT_FSIZE should be RLIM_INFINITY, which will be a really big\r | |
24 | # number on a platform with large file support. On these platforms,\r | |
25 | # we need to test that the get/setrlimit functions properly convert\r | |
26 | # the number to a C long long and that the conversion doesn't raise\r | |
27 | # an error.\r | |
28 | self.assertEqual(resource.RLIM_INFINITY, max)\r | |
29 | resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))\r | |
30 | \r | |
31 | def test_fsize_enforced(self):\r | |
32 | try:\r | |
33 | (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)\r | |
34 | except AttributeError:\r | |
35 | pass\r | |
36 | else:\r | |
37 | # Check to see what happens when the RLIMIT_FSIZE is small. Some\r | |
38 | # versions of Python were terminated by an uncaught SIGXFSZ, but\r | |
39 | # pythonrun.c has been fixed to ignore that exception. If so, the\r | |
40 | # write() should return EFBIG when the limit is exceeded.\r | |
41 | \r | |
42 | # At least one platform has an unlimited RLIMIT_FSIZE and attempts\r | |
43 | # to change it raise ValueError instead.\r | |
44 | try:\r | |
45 | try:\r | |
46 | resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max))\r | |
47 | limit_set = True\r | |
48 | except ValueError:\r | |
49 | limit_set = False\r | |
50 | f = open(test_support.TESTFN, "wb")\r | |
51 | try:\r | |
52 | f.write("X" * 1024)\r | |
53 | try:\r | |
54 | f.write("Y")\r | |
55 | f.flush()\r | |
56 | # On some systems (e.g., Ubuntu on hppa) the flush()\r | |
57 | # doesn't always cause the exception, but the close()\r | |
58 | # does eventually. Try flushing several times in\r | |
59 | # an attempt to ensure the file is really synced and\r | |
60 | # the exception raised.\r | |
61 | for i in range(5):\r | |
62 | time.sleep(.1)\r | |
63 | f.flush()\r | |
64 | except IOError:\r | |
65 | if not limit_set:\r | |
66 | raise\r | |
67 | if limit_set:\r | |
68 | # Close will attempt to flush the byte we wrote\r | |
69 | # Restore limit first to avoid getting a spurious error\r | |
70 | resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))\r | |
71 | finally:\r | |
72 | f.close()\r | |
73 | finally:\r | |
74 | if limit_set:\r | |
75 | resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))\r | |
76 | test_support.unlink(test_support.TESTFN)\r | |
77 | \r | |
78 | def test_fsize_toobig(self):\r | |
79 | # Be sure that setrlimit is checking for really large values\r | |
80 | too_big = 10L**50\r | |
81 | try:\r | |
82 | (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)\r | |
83 | except AttributeError:\r | |
84 | pass\r | |
85 | else:\r | |
86 | try:\r | |
87 | resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max))\r | |
88 | except (OverflowError, ValueError):\r | |
89 | pass\r | |
90 | try:\r | |
91 | resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big))\r | |
92 | except (OverflowError, ValueError):\r | |
93 | pass\r | |
94 | \r | |
95 | def test_getrusage(self):\r | |
96 | self.assertRaises(TypeError, resource.getrusage)\r | |
97 | self.assertRaises(TypeError, resource.getrusage, 42, 42)\r | |
98 | usageself = resource.getrusage(resource.RUSAGE_SELF)\r | |
99 | usagechildren = resource.getrusage(resource.RUSAGE_CHILDREN)\r | |
100 | # May not be available on all systems.\r | |
101 | try:\r | |
102 | usageboth = resource.getrusage(resource.RUSAGE_BOTH)\r | |
103 | except (ValueError, AttributeError):\r | |
104 | pass\r | |
105 | \r | |
106 | def test_main(verbose=None):\r | |
107 | test_support.run_unittest(ResourceTest)\r | |
108 | \r | |
109 | if __name__ == "__main__":\r | |
110 | test_main()\r |