annotate Lib/test/test_resource.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents f4c5e3a64be2
children b36cb4602e21
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
40677
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
1 import unittest
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
2 from test import test_support
52491
f4c5e3a64be2 Change more tests to use import_module for the modules that
R. David Murray <rdmurray@bitdance.com>
parents: 45468
diff changeset
3 import time
23251
405b291c321d Add tests for the recent resource module change.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
4
52491
f4c5e3a64be2 Change more tests to use import_module for the modules that
R. David Murray <rdmurray@bitdance.com>
parents: 45468
diff changeset
5 resource = test_support.import_module('resource')
40677
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
6
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
7 # This test is checking a few specific problem spots with the resource module.
23251
405b291c321d Add tests for the recent resource module change.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
8
40677
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
9 class ResourceTest(unittest.TestCase):
40678
3ae4f930bf27 Add argument tests an calls of resource.getrusage().
Walter Dörwald <walter@livinglogic.de>
parents: 40677
diff changeset
10
3ae4f930bf27 Add argument tests an calls of resource.getrusage().
Walter Dörwald <walter@livinglogic.de>
parents: 40677
diff changeset
11 def test_args(self):
3ae4f930bf27 Add argument tests an calls of resource.getrusage().
Walter Dörwald <walter@livinglogic.de>
parents: 40677
diff changeset
12 self.assertRaises(TypeError, resource.getrlimit)
3ae4f930bf27 Add argument tests an calls of resource.getrusage().
Walter Dörwald <walter@livinglogic.de>
parents: 40677
diff changeset
13 self.assertRaises(TypeError, resource.getrlimit, 42, 42)
3ae4f930bf27 Add argument tests an calls of resource.getrusage().
Walter Dörwald <walter@livinglogic.de>
parents: 40677
diff changeset
14 self.assertRaises(TypeError, resource.setrlimit)
3ae4f930bf27 Add argument tests an calls of resource.getrusage().
Walter Dörwald <walter@livinglogic.de>
parents: 40677
diff changeset
15 self.assertRaises(TypeError, resource.setrlimit, 42, 42, 42)
3ae4f930bf27 Add argument tests an calls of resource.getrusage().
Walter Dörwald <walter@livinglogic.de>
parents: 40677
diff changeset
16
40677
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
17 def test_fsize_ismax(self):
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
18 try:
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
19 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
20 except AttributeError:
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
21 pass
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
22 else:
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
23 # RLIMIT_FSIZE should be RLIM_INFINITY, which will be a really big
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
24 # number on a platform with large file support. On these platforms,
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
25 # we need to test that the get/setrlimit functions properly convert
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
26 # the number to a C long long and that the conversion doesn't raise
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
27 # an error.
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
28 self.assertEqual(resource.RLIM_INFINITY, max)
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
29 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
26618
9e751eee81f5 Patch #551960: Add check for setrlimit() support
Jason Tishler <jason@tishler.net>
parents: 24364
diff changeset
30
40677
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
31 def test_fsize_enforced(self):
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
32 try:
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
33 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
34 except AttributeError:
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
35 pass
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
36 else:
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
37 # Check to see what happens when the RLIMIT_FSIZE is small. Some
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
38 # versions of Python were terminated by an uncaught SIGXFSZ, but
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
39 # pythonrun.c has been fixed to ignore that exception. If so, the
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
40 # write() should return EFBIG when the limit is exceeded.
40732
5dae0d7f683a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 40679
diff changeset
41
40677
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
42 # At least one platform has an unlimited RLIMIT_FSIZE and attempts
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
43 # to change it raise ValueError instead.
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
44 try:
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
45 try:
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
46 resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max))
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
47 limit_set = True
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
48 except ValueError:
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
49 limit_set = False
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
50 f = open(test_support.TESTFN, "wb")
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
51 try:
42358
30e112350e20 Try to get rid of spurious failure in test_resource on the Debian buildbots by changing the file size limit before attempting to close the file
Nick Coghlan <ncoghlan@gmail.com>
parents: 40732
diff changeset
52 f.write("X" * 1024)
30e112350e20 Try to get rid of spurious failure in test_resource on the Debian buildbots by changing the file size limit before attempting to close the file
Nick Coghlan <ncoghlan@gmail.com>
parents: 40732
diff changeset
53 try:
30e112350e20 Try to get rid of spurious failure in test_resource on the Debian buildbots by changing the file size limit before attempting to close the file
Nick Coghlan <ncoghlan@gmail.com>
parents: 40732
diff changeset
54 f.write("Y")
30e112350e20 Try to get rid of spurious failure in test_resource on the Debian buildbots by changing the file size limit before attempting to close the file
Nick Coghlan <ncoghlan@gmail.com>
parents: 40732
diff changeset
55 f.flush()
44986
5f98446e0f1b On some systems (e.g., Ubuntu on hppa) the flush()
Neal Norwitz <nnorwitz@gmail.com>
parents: 44982
diff changeset
56 # On some systems (e.g., Ubuntu on hppa) the flush()
44991
2ca2b0840b35 Try to prevent this test from being flaky. We might need a sleep in here
Neal Norwitz <nnorwitz@gmail.com>
parents: 44986
diff changeset
57 # doesn't always cause the exception, but the close()
45013
85ccc7c15425 Update the comment and remove the close. If we close we can't flush anymore.
Neal Norwitz <nnorwitz@gmail.com>
parents: 45008
diff changeset
58 # does eventually. Try flushing several times in
44991
2ca2b0840b35 Try to prevent this test from being flaky. We might need a sleep in here
Neal Norwitz <nnorwitz@gmail.com>
parents: 44986
diff changeset
59 # an attempt to ensure the file is really synced and
2ca2b0840b35 Try to prevent this test from being flaky. We might need a sleep in here
Neal Norwitz <nnorwitz@gmail.com>
parents: 44986
diff changeset
60 # the exception raised.
2ca2b0840b35 Try to prevent this test from being flaky. We might need a sleep in here
Neal Norwitz <nnorwitz@gmail.com>
parents: 44986
diff changeset
61 for i in range(5):
45008
2e4dd39bf690 Add a tiny sleep and additional flush to force the file to really be synced.
Neal Norwitz <nnorwitz@gmail.com>
parents: 44991
diff changeset
62 time.sleep(.1)
2e4dd39bf690 Add a tiny sleep and additional flush to force the file to really be synced.
Neal Norwitz <nnorwitz@gmail.com>
parents: 44991
diff changeset
63 f.flush()
42358
30e112350e20 Try to get rid of spurious failure in test_resource on the Debian buildbots by changing the file size limit before attempting to close the file
Nick Coghlan <ncoghlan@gmail.com>
parents: 40732
diff changeset
64 except IOError:
30e112350e20 Try to get rid of spurious failure in test_resource on the Debian buildbots by changing the file size limit before attempting to close the file
Nick Coghlan <ncoghlan@gmail.com>
parents: 40732
diff changeset
65 if not limit_set:
30e112350e20 Try to get rid of spurious failure in test_resource on the Debian buildbots by changing the file size limit before attempting to close the file
Nick Coghlan <ncoghlan@gmail.com>
parents: 40732
diff changeset
66 raise
30e112350e20 Try to get rid of spurious failure in test_resource on the Debian buildbots by changing the file size limit before attempting to close the file
Nick Coghlan <ncoghlan@gmail.com>
parents: 40732
diff changeset
67 if limit_set:
30e112350e20 Try to get rid of spurious failure in test_resource on the Debian buildbots by changing the file size limit before attempting to close the file
Nick Coghlan <ncoghlan@gmail.com>
parents: 40732
diff changeset
68 # Close will attempt to flush the byte we wrote
30e112350e20 Try to get rid of spurious failure in test_resource on the Debian buildbots by changing the file size limit before attempting to close the file
Nick Coghlan <ncoghlan@gmail.com>
parents: 40732
diff changeset
69 # Restore limit first to avoid getting a spurious error
30e112350e20 Try to get rid of spurious failure in test_resource on the Debian buildbots by changing the file size limit before attempting to close the file
Nick Coghlan <ncoghlan@gmail.com>
parents: 40732
diff changeset
70 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
30e112350e20 Try to get rid of spurious failure in test_resource on the Debian buildbots by changing the file size limit before attempting to close the file
Nick Coghlan <ncoghlan@gmail.com>
parents: 40732
diff changeset
71 finally:
30e112350e20 Try to get rid of spurious failure in test_resource on the Debian buildbots by changing the file size limit before attempting to close the file
Nick Coghlan <ncoghlan@gmail.com>
parents: 40732
diff changeset
72 f.close()
40677
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
73 finally:
42358
30e112350e20 Try to get rid of spurious failure in test_resource on the Debian buildbots by changing the file size limit before attempting to close the file
Nick Coghlan <ncoghlan@gmail.com>
parents: 40732
diff changeset
74 if limit_set:
30e112350e20 Try to get rid of spurious failure in test_resource on the Debian buildbots by changing the file size limit before attempting to close the file
Nick Coghlan <ncoghlan@gmail.com>
parents: 40732
diff changeset
75 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
44982
5d782c8d4016 Always try to remove the test file even if close raises an exception
Neal Norwitz <nnorwitz@gmail.com>
parents: 42358
diff changeset
76 test_support.unlink(test_support.TESTFN)
23251
405b291c321d Add tests for the recent resource module change.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
77
40677
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
78 def test_fsize_toobig(self):
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
79 # Be sure that setrlimit is checking for really large values
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
80 too_big = 10L**50
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
81 try:
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
82 (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
83 except AttributeError:
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
84 pass
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
85 else:
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
86 try:
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
87 resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max))
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
88 except (OverflowError, ValueError):
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
89 pass
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
90 try:
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
91 resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big))
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
92 except (OverflowError, ValueError):
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
93 pass
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
94
40678
3ae4f930bf27 Add argument tests an calls of resource.getrusage().
Walter Dörwald <walter@livinglogic.de>
parents: 40677
diff changeset
95 def test_getrusage(self):
3ae4f930bf27 Add argument tests an calls of resource.getrusage().
Walter Dörwald <walter@livinglogic.de>
parents: 40677
diff changeset
96 self.assertRaises(TypeError, resource.getrusage)
3ae4f930bf27 Add argument tests an calls of resource.getrusage().
Walter Dörwald <walter@livinglogic.de>
parents: 40677
diff changeset
97 self.assertRaises(TypeError, resource.getrusage, 42, 42)
3ae4f930bf27 Add argument tests an calls of resource.getrusage().
Walter Dörwald <walter@livinglogic.de>
parents: 40677
diff changeset
98 usageself = resource.getrusage(resource.RUSAGE_SELF)
3ae4f930bf27 Add argument tests an calls of resource.getrusage().
Walter Dörwald <walter@livinglogic.de>
parents: 40677
diff changeset
99 usagechildren = resource.getrusage(resource.RUSAGE_CHILDREN)
3ae4f930bf27 Add argument tests an calls of resource.getrusage().
Walter Dörwald <walter@livinglogic.de>
parents: 40677
diff changeset
100 # May not be available on all systems.
3ae4f930bf27 Add argument tests an calls of resource.getrusage().
Walter Dörwald <walter@livinglogic.de>
parents: 40677
diff changeset
101 try:
3ae4f930bf27 Add argument tests an calls of resource.getrusage().
Walter Dörwald <walter@livinglogic.de>
parents: 40677
diff changeset
102 usageboth = resource.getrusage(resource.RUSAGE_BOTH)
40679
ac4c2f5c3fc0 resource.RUSAGE_BOTH might not exist.
Walter Dörwald <walter@livinglogic.de>
parents: 40678
diff changeset
103 except (ValueError, AttributeError):
40678
3ae4f930bf27 Add argument tests an calls of resource.getrusage().
Walter Dörwald <walter@livinglogic.de>
parents: 40677
diff changeset
104 pass
3ae4f930bf27 Add argument tests an calls of resource.getrusage().
Walter Dörwald <walter@livinglogic.de>
parents: 40677
diff changeset
105
40677
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
106 def test_main(verbose=None):
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
107 test_support.run_unittest(ResourceTest)
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
108
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
109 if __name__ == "__main__":
10bffd19b535 Port test_resource.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 26618
diff changeset
110 test_main()