Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame] | 1 | import unittest |
| 2 | from test import test_support |
Neal Norwitz | 041ee5d | 2008-01-27 17:10:50 +0000 | [diff] [blame] | 3 | import time |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 4 | |
R. David Murray | 3db8a34 | 2009-03-30 23:05:48 +0000 | [diff] [blame] | 5 | resource = test_support.import_module('resource') |
| 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: |
Zachary Ware | 1f70221 | 2013-12-10 14:09:20 -0600 | [diff] [blame] | 21 | self.skipTest('RLIMIT_FSIZE not available') |
| 22 | # RLIMIT_FSIZE should be RLIM_INFINITY, which will be a really big |
| 23 | # number on a platform with large file support. On these platforms, |
| 24 | # we need to test that the get/setrlimit functions properly convert |
| 25 | # the number to a C long long and that the conversion doesn't raise |
| 26 | # an error. |
| 27 | self.assertEqual(resource.RLIM_INFINITY, max) |
| 28 | resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max)) |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 29 | |
Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame] | 30 | def test_fsize_enforced(self): |
| 31 | try: |
| 32 | (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE) |
| 33 | except AttributeError: |
Zachary Ware | 1f70221 | 2013-12-10 14:09:20 -0600 | [diff] [blame] | 34 | self.skipTest('RLIMIT_FSIZE not available') |
| 35 | # Check to see what happens when the RLIMIT_FSIZE is small. Some |
| 36 | # versions of Python were terminated by an uncaught SIGXFSZ, but |
| 37 | # pythonrun.c has been fixed to ignore that exception. If so, the |
| 38 | # write() should return EFBIG when the limit is exceeded. |
Tim Peters | f733abb | 2007-01-30 03:03:46 +0000 | [diff] [blame] | 39 | |
Zachary Ware | 1f70221 | 2013-12-10 14:09:20 -0600 | [diff] [blame] | 40 | # At least one platform has an unlimited RLIMIT_FSIZE and attempts |
| 41 | # to change it raise ValueError instead. |
| 42 | try: |
Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame] | 43 | try: |
Zachary Ware | 1f70221 | 2013-12-10 14:09:20 -0600 | [diff] [blame] | 44 | resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max)) |
| 45 | limit_set = True |
| 46 | except ValueError: |
| 47 | limit_set = False |
| 48 | f = open(test_support.TESTFN, "wb") |
| 49 | try: |
| 50 | f.write("X" * 1024) |
Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame] | 51 | try: |
Zachary Ware | 1f70221 | 2013-12-10 14:09:20 -0600 | [diff] [blame] | 52 | f.write("Y") |
| 53 | f.flush() |
| 54 | # On some systems (e.g., Ubuntu on hppa) the flush() |
| 55 | # doesn't always cause the exception, but the close() |
| 56 | # does eventually. Try flushing several times in |
| 57 | # an attempt to ensure the file is really synced and |
| 58 | # the exception raised. |
| 59 | for i in range(5): |
| 60 | time.sleep(.1) |
Nick Coghlan | 12adef9 | 2007-07-24 14:39:23 +0000 | [diff] [blame] | 61 | f.flush() |
Zachary Ware | 1f70221 | 2013-12-10 14:09:20 -0600 | [diff] [blame] | 62 | except IOError: |
| 63 | if not limit_set: |
| 64 | raise |
Nick Coghlan | 12adef9 | 2007-07-24 14:39:23 +0000 | [diff] [blame] | 65 | if limit_set: |
Zachary Ware | 1f70221 | 2013-12-10 14:09:20 -0600 | [diff] [blame] | 66 | # Close will attempt to flush the byte we wrote |
| 67 | # Restore limit first to avoid getting a spurious error |
Nick Coghlan | 12adef9 | 2007-07-24 14:39:23 +0000 | [diff] [blame] | 68 | resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max)) |
Zachary Ware | 1f70221 | 2013-12-10 14:09:20 -0600 | [diff] [blame] | 69 | finally: |
| 70 | f.close() |
| 71 | finally: |
| 72 | if limit_set: |
| 73 | resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max)) |
| 74 | test_support.unlink(test_support.TESTFN) |
Jason Tishler | e4a070a | 2002-12-12 18:13:36 +0000 | [diff] [blame] | 75 | |
Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame] | 76 | def test_fsize_toobig(self): |
| 77 | # Be sure that setrlimit is checking for really large values |
| 78 | too_big = 10L**50 |
| 79 | try: |
| 80 | (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE) |
| 81 | except AttributeError: |
Zachary Ware | 1f70221 | 2013-12-10 14:09:20 -0600 | [diff] [blame] | 82 | self.skipTest('RLIMIT_FSIZE not available') |
| 83 | try: |
| 84 | resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max)) |
| 85 | except (OverflowError, ValueError): |
Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame] | 86 | pass |
Zachary Ware | 1f70221 | 2013-12-10 14:09:20 -0600 | [diff] [blame] | 87 | try: |
| 88 | resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big)) |
| 89 | except (OverflowError, ValueError): |
| 90 | pass |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 91 | |
Walter Dörwald | 66262ab | 2007-01-20 18:19:33 +0000 | [diff] [blame] | 92 | def test_getrusage(self): |
| 93 | self.assertRaises(TypeError, resource.getrusage) |
| 94 | self.assertRaises(TypeError, resource.getrusage, 42, 42) |
| 95 | usageself = resource.getrusage(resource.RUSAGE_SELF) |
| 96 | usagechildren = resource.getrusage(resource.RUSAGE_CHILDREN) |
| 97 | # May not be available on all systems. |
| 98 | try: |
| 99 | usageboth = resource.getrusage(resource.RUSAGE_BOTH) |
Walter Dörwald | d414302 | 2007-01-20 19:03:17 +0000 | [diff] [blame] | 100 | except (ValueError, AttributeError): |
Walter Dörwald | 66262ab | 2007-01-20 18:19:33 +0000 | [diff] [blame] | 101 | pass |
| 102 | |
Serhiy Storchaka | a07a8b4 | 2013-02-04 12:45:46 +0200 | [diff] [blame] | 103 | # Issue 6083: Reference counting bug |
| 104 | def test_setrusage_refcount(self): |
| 105 | try: |
| 106 | limits = resource.getrlimit(resource.RLIMIT_CPU) |
| 107 | except AttributeError: |
Zachary Ware | 1f70221 | 2013-12-10 14:09:20 -0600 | [diff] [blame] | 108 | self.skipTest('RLIMIT_CPU not available') |
| 109 | class BadSequence: |
| 110 | def __len__(self): |
| 111 | return 2 |
| 112 | def __getitem__(self, key): |
| 113 | if key in (0, 1): |
| 114 | return len(tuple(range(1000000))) |
| 115 | raise IndexError |
Serhiy Storchaka | a07a8b4 | 2013-02-04 12:45:46 +0200 | [diff] [blame] | 116 | |
Zachary Ware | 1f70221 | 2013-12-10 14:09:20 -0600 | [diff] [blame] | 117 | resource.setrlimit(resource.RLIMIT_CPU, BadSequence()) |
Serhiy Storchaka | a07a8b4 | 2013-02-04 12:45:46 +0200 | [diff] [blame] | 118 | |
Walter Dörwald | 9fab9a7 | 2007-01-20 17:28:31 +0000 | [diff] [blame] | 119 | def test_main(verbose=None): |
| 120 | test_support.run_unittest(ResourceTest) |
| 121 | |
| 122 | if __name__ == "__main__": |
| 123 | test_main() |