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