Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame^] | 1 | import unittest |
| 2 | from test import test_support |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 3 | |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 4 | |
Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame^] | 5 | import os, resource |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 6 | |
Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame^] | 7 | # This test is checking a few specific problem spots with the resource module. |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 8 | |
Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame^] | 9 | class ResourceTest(unittest.TestCase): |
| 10 | def test_fsize_ismax(self): |
| 11 | |
| 12 | try: |
| 13 | (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE) |
| 14 | except AttributeError: |
| 15 | pass |
| 16 | else: |
| 17 | # RLIMIT_FSIZE should be RLIM_INFINITY, which will be a really big |
| 18 | # number on a platform with large file support. On these platforms, |
| 19 | # we need to test that the get/setrlimit functions properly convert |
| 20 | # the number to a C long long and that the conversion doesn't raise |
| 21 | # an error. |
| 22 | self.assertEqual(resource.RLIM_INFINITY, max) |
| 23 | resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max)) |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 24 | |
Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame^] | 25 | def test_fsize_enforced(self): |
| 26 | try: |
| 27 | (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE) |
| 28 | except AttributeError: |
| 29 | pass |
| 30 | else: |
| 31 | # Check to see what happens when the RLIMIT_FSIZE is small. Some |
| 32 | # versions of Python were terminated by an uncaught SIGXFSZ, but |
| 33 | # pythonrun.c has been fixed to ignore that exception. If so, the |
| 34 | # write() should return EFBIG when the limit is exceeded. |
| 35 | |
| 36 | # At least one platform has an unlimited RLIMIT_FSIZE and attempts |
| 37 | # to change it raise ValueError instead. |
| 38 | try: |
| 39 | try: |
| 40 | resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max)) |
| 41 | limit_set = True |
| 42 | except ValueError: |
| 43 | limit_set = False |
| 44 | f = open(test_support.TESTFN, "wb") |
| 45 | f.write("X" * 1024) |
| 46 | try: |
| 47 | f.write("Y") |
| 48 | f.flush() |
| 49 | except IOError: |
| 50 | if not limit_set: |
| 51 | raise |
| 52 | f.close() |
| 53 | os.unlink(test_support.TESTFN) |
| 54 | finally: |
| 55 | resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max)) |
Jason Tishler | e4a070a | 2002-12-12 18:13:36 +0000 | [diff] [blame] | 56 | |
Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame^] | 57 | def test_fsize_toobig(self): |
| 58 | # Be sure that setrlimit is checking for really large values |
| 59 | too_big = 10L**50 |
| 60 | try: |
| 61 | (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE) |
| 62 | except AttributeError: |
| 63 | pass |
| 64 | else: |
| 65 | try: |
| 66 | resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max)) |
| 67 | except (OverflowError, ValueError): |
| 68 | pass |
| 69 | try: |
| 70 | resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big)) |
| 71 | except (OverflowError, ValueError): |
| 72 | pass |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 73 | |
Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame^] | 74 | def test_main(verbose=None): |
| 75 | test_support.run_unittest(ResourceTest) |
| 76 | |
| 77 | if __name__ == "__main__": |
| 78 | test_main() |