Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 1 | import os |
| 2 | import resource |
| 3 | |
Neal Norwitz | 209ea39 | 2008-01-27 20:08:04 +0000 | [diff] [blame^] | 4 | from test.test_support import TESTFN, unlink |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 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 | |
Jason Tishler | e4a070a | 2002-12-12 18:13:36 +0000 | [diff] [blame] | 25 | # At least one platform has an unlimited RLIMIT_FSIZE and attempts to |
| 26 | # change it raise ValueError instead. |
| 27 | |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 28 | try: |
Jason Tishler | e4a070a | 2002-12-12 18:13:36 +0000 | [diff] [blame] | 29 | try: |
| 30 | resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max)) |
| 31 | limit_set = 1 |
| 32 | except ValueError: |
| 33 | limit_set = 0 |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 34 | f = open(TESTFN, "wb") |
| 35 | f.write("X" * 1024) |
| 36 | try: |
| 37 | f.write("Y") |
| 38 | f.flush() |
Neal Norwitz | b03528c | 2008-01-27 01:23:50 +0000 | [diff] [blame] | 39 | # On some systems (e.g., Ubuntu on hppa) the flush() |
Neal Norwitz | 6179234 | 2008-01-27 05:02:56 +0000 | [diff] [blame] | 40 | # doesn't always cause the exception, but the close() |
Neal Norwitz | 209ea39 | 2008-01-27 20:08:04 +0000 | [diff] [blame^] | 41 | # does eventually. Try flushing several times in an attempt |
Neal Norwitz | 6179234 | 2008-01-27 05:02:56 +0000 | [diff] [blame] | 42 | # to ensure the file is really synced and the exception raised. |
| 43 | for i in range(5): |
Neal Norwitz | 209ea39 | 2008-01-27 20:08:04 +0000 | [diff] [blame^] | 44 | time.sleep(.1) |
| 45 | f.flush() |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 46 | except IOError: |
Jason Tishler | e4a070a | 2002-12-12 18:13:36 +0000 | [diff] [blame] | 47 | if not limit_set: |
| 48 | raise |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 49 | f.close() |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 50 | finally: |
Neal Norwitz | 209ea39 | 2008-01-27 20:08:04 +0000 | [diff] [blame^] | 51 | if limit_set: |
| 52 | resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max)) |
| 53 | unlink(TESTFN) |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 54 | |
| 55 | # And be sure that setrlimit is checking for really large values |
| 56 | too_big = 10L**50 |
| 57 | try: |
| 58 | resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max)) |
Jason Tishler | e4a070a | 2002-12-12 18:13:36 +0000 | [diff] [blame] | 59 | except (OverflowError, ValueError): |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 60 | pass |
| 61 | try: |
| 62 | resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big)) |
Jason Tishler | e4a070a | 2002-12-12 18:13:36 +0000 | [diff] [blame] | 63 | except (OverflowError, ValueError): |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 64 | pass |