|
1 import os |
|
2 import resource |
|
3 |
|
4 from test.test_support import TESTFN, unlink |
|
5 |
|
6 # This test is checking a few specific problem spots. RLIMIT_FSIZE |
|
7 # should be RLIM_INFINITY, which will be a really big number on a |
|
8 # platform with large file support. On these platforms, we need to |
|
9 # test that the get/setrlimit functions properly convert the number to |
|
10 # a C long long and that the conversion doesn't raise an error. |
|
11 |
|
12 try: |
|
13 cur, max = resource.getrlimit(resource.RLIMIT_FSIZE) |
|
14 except AttributeError: |
|
15 pass |
|
16 else: |
|
17 print resource.RLIM_INFINITY == max |
|
18 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max)) |
|
19 |
|
20 # Now check to see what happens when the RLIMIT_FSIZE is small. Some |
|
21 # versions of Python were terminated by an uncaught SIGXFSZ, but |
|
22 # pythonrun.c has been fixed to ignore that exception. If so, the |
|
23 # write() should return EFBIG when the limit is exceeded. |
|
24 |
|
25 # At least one platform has an unlimited RLIMIT_FSIZE and attempts to |
|
26 # change it raise ValueError instead. |
|
27 |
|
28 try: |
|
29 try: |
|
30 resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max)) |
|
31 limit_set = 1 |
|
32 except ValueError: |
|
33 limit_set = 0 |
|
34 f = open(TESTFN, "wb") |
|
35 try: |
|
36 f.write("X" * 1024) |
|
37 try: |
|
38 f.write("Y") |
|
39 f.flush() |
|
40 # On some systems (e.g., Ubuntu on hppa) the flush() |
|
41 # doesn't always cause the exception, but the close() |
|
42 # does eventually. Try flushing several times in |
|
43 # an attempt to ensure the file is really synced and |
|
44 # the exception raised. |
|
45 for i in range(5): |
|
46 time.sleep(.1) |
|
47 f.flush() |
|
48 except IOError: |
|
49 if not limit_set: |
|
50 raise |
|
51 if limit_set: |
|
52 # Close will attempt to flush the byte we wrote |
|
53 # Restore limit first to avoid getting a spurious error |
|
54 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max)) |
|
55 finally: |
|
56 f.close() |
|
57 finally: |
|
58 if limit_set: |
|
59 resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max)) |
|
60 unlink(TESTFN) |
|
61 |
|
62 # And be sure that setrlimit is checking for really large values |
|
63 too_big = 10L**50 |
|
64 try: |
|
65 resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max)) |
|
66 except (OverflowError, ValueError): |
|
67 pass |
|
68 try: |
|
69 resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big)) |
|
70 except (OverflowError, ValueError): |
|
71 pass |