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): |
Walter Dörwald | 66262ab | 2007-01-20 18:19:33 +0000 | [diff] [blame] | 10 | |
| 11 | def test_args(self): |
| 12 | self.assertRaises(TypeError, resource.getrlimit) |
| 13 | self.assertRaises(TypeError, resource.getrlimit, 42, 42) |
| 14 | self.assertRaises(TypeError, resource.setrlimit) |
| 15 | self.assertRaises(TypeError, resource.setrlimit, 42, 42, 42) |
| 16 | |
Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame] | 17 | def test_fsize_ismax(self): |
Tim Peters | f733abb | 2007-01-30 03:03:46 +0000 | [diff] [blame] | 18 | |
Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame] | 19 | try: |
| 20 | (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE) |
| 21 | except AttributeError: |
| 22 | pass |
| 23 | else: |
| 24 | # RLIMIT_FSIZE should be RLIM_INFINITY, which will be a really big |
| 25 | # number on a platform with large file support. On these platforms, |
| 26 | # we need to test that the get/setrlimit functions properly convert |
| 27 | # the number to a C long long and that the conversion doesn't raise |
| 28 | # an error. |
| 29 | self.assertEqual(resource.RLIM_INFINITY, max) |
| 30 | resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max)) |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 31 | |
Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame] | 32 | def test_fsize_enforced(self): |
| 33 | try: |
| 34 | (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE) |
| 35 | except AttributeError: |
| 36 | pass |
| 37 | else: |
| 38 | # Check to see what happens when the RLIMIT_FSIZE is small. Some |
| 39 | # versions of Python were terminated by an uncaught SIGXFSZ, but |
| 40 | # pythonrun.c has been fixed to ignore that exception. If so, the |
| 41 | # write() should return EFBIG when the limit is exceeded. |
Tim Peters | f733abb | 2007-01-30 03:03:46 +0000 | [diff] [blame] | 42 | |
Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame] | 43 | # At least one platform has an unlimited RLIMIT_FSIZE and attempts |
| 44 | # to change it raise ValueError instead. |
| 45 | try: |
| 46 | try: |
| 47 | resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max)) |
| 48 | limit_set = True |
| 49 | except ValueError: |
| 50 | limit_set = False |
| 51 | f = open(test_support.TESTFN, "wb") |
Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame] | 52 | try: |
Nick Coghlan | 12adef9 | 2007-07-24 14:39:23 +0000 | [diff] [blame] | 53 | f.write("X" * 1024) |
| 54 | try: |
| 55 | f.write("Y") |
| 56 | f.flush() |
| 57 | except IOError: |
| 58 | if not limit_set: |
| 59 | raise |
| 60 | if limit_set: |
| 61 | # Close will attempt to flush the byte we wrote |
| 62 | # Restore limit first to avoid getting a spurious error |
| 63 | resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max)) |
| 64 | finally: |
| 65 | f.close() |
| 66 | os.unlink(test_support.TESTFN) |
Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame] | 67 | finally: |
Nick Coghlan | 12adef9 | 2007-07-24 14:39:23 +0000 | [diff] [blame] | 68 | if limit_set: |
| 69 | resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max)) |
Jason Tishler | e4a070a | 2002-12-12 18:13:36 +0000 | [diff] [blame] | 70 | |
Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame] | 71 | def test_fsize_toobig(self): |
| 72 | # Be sure that setrlimit is checking for really large values |
| 73 | too_big = 10L**50 |
| 74 | try: |
| 75 | (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE) |
| 76 | except AttributeError: |
| 77 | pass |
| 78 | else: |
| 79 | try: |
| 80 | resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max)) |
| 81 | except (OverflowError, ValueError): |
| 82 | pass |
| 83 | try: |
| 84 | resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big)) |
| 85 | except (OverflowError, ValueError): |
| 86 | pass |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 87 | |
Walter Dörwald | 66262ab | 2007-01-20 18:19:33 +0000 | [diff] [blame] | 88 | def test_getrusage(self): |
| 89 | self.assertRaises(TypeError, resource.getrusage) |
| 90 | self.assertRaises(TypeError, resource.getrusage, 42, 42) |
| 91 | usageself = resource.getrusage(resource.RUSAGE_SELF) |
| 92 | usagechildren = resource.getrusage(resource.RUSAGE_CHILDREN) |
| 93 | # May not be available on all systems. |
| 94 | try: |
| 95 | usageboth = resource.getrusage(resource.RUSAGE_BOTH) |
Walter Dörwald | d414302 | 2007-01-20 19:03:17 +0000 | [diff] [blame] | 96 | except (ValueError, AttributeError): |
Walter Dörwald | 66262ab | 2007-01-20 18:19:33 +0000 | [diff] [blame] | 97 | pass |
| 98 | |
Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame] | 99 | def test_main(verbose=None): |
| 100 | test_support.run_unittest(ResourceTest) |
| 101 | |
| 102 | if __name__ == "__main__": |
| 103 | test_main() |