Brett Cannon | a20800d | 2013-10-25 15:45:25 -0400 | [diff] [blame] | 1 | import contextlib |
Christian Heimes | 6fc79bf | 2013-10-22 11:09:27 +0200 | [diff] [blame] | 2 | import sys |
Christian Heimes | d885aa4 | 2013-10-22 11:45:30 +0200 | [diff] [blame] | 3 | import os |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 4 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 5 | from test import support |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 6 | import time |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 7 | |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 8 | resource = support.import_module('resource') |
| 9 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 10 | # 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] | 11 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 12 | class ResourceTest(unittest.TestCase): |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 13 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 14 | def test_args(self): |
| 15 | self.assertRaises(TypeError, resource.getrlimit) |
| 16 | self.assertRaises(TypeError, resource.getrlimit, 42, 42) |
| 17 | self.assertRaises(TypeError, resource.setrlimit) |
| 18 | self.assertRaises(TypeError, resource.setrlimit, 42, 42, 42) |
Jason Tishler | e4a070a | 2002-12-12 18:13:36 +0000 | [diff] [blame] | 19 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 20 | def test_fsize_ismax(self): |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 21 | try: |
| 22 | (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE) |
| 23 | except AttributeError: |
| 24 | pass |
| 25 | else: |
| 26 | # RLIMIT_FSIZE should be RLIM_INFINITY, which will be a really big |
| 27 | # number on a platform with large file support. On these platforms, |
| 28 | # we need to test that the get/setrlimit functions properly convert |
| 29 | # the number to a C long long and that the conversion doesn't raise |
| 30 | # an error. |
| 31 | self.assertEqual(resource.RLIM_INFINITY, max) |
| 32 | resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max)) |
Jeremy Hylton | 74ce77f | 2002-04-23 20:21:22 +0000 | [diff] [blame] | 33 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 34 | def test_fsize_enforced(self): |
| 35 | try: |
| 36 | (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE) |
| 37 | except AttributeError: |
| 38 | pass |
| 39 | else: |
| 40 | # Check to see what happens when the RLIMIT_FSIZE is small. Some |
| 41 | # versions of Python were terminated by an uncaught SIGXFSZ, but |
| 42 | # pythonrun.c has been fixed to ignore that exception. If so, the |
| 43 | # write() should return EFBIG when the limit is exceeded. |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 44 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 45 | # At least one platform has an unlimited RLIMIT_FSIZE and attempts |
| 46 | # to change it raise ValueError instead. |
| 47 | try: |
| 48 | try: |
| 49 | resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max)) |
| 50 | limit_set = True |
| 51 | except ValueError: |
| 52 | limit_set = False |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 53 | f = open(support.TESTFN, "wb") |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 54 | try: |
Guido van Rossum | b644fb4 | 2007-08-27 23:36:53 +0000 | [diff] [blame] | 55 | f.write(b"X" * 1024) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 56 | try: |
Guido van Rossum | b644fb4 | 2007-08-27 23:36:53 +0000 | [diff] [blame] | 57 | f.write(b"Y") |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 58 | f.flush() |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 59 | # On some systems (e.g., Ubuntu on hppa) the flush() |
| 60 | # doesn't always cause the exception, but the close() |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 61 | # does eventually. Try flushing several times in |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 62 | # an attempt to ensure the file is really synced and |
| 63 | # the exception raised. |
| 64 | for i in range(5): |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 65 | time.sleep(.1) |
| 66 | f.flush() |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 67 | except OSError: |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 68 | if not limit_set: |
| 69 | raise |
| 70 | if limit_set: |
| 71 | # Close will attempt to flush the byte we wrote |
| 72 | # Restore limit first to avoid getting a spurious error |
| 73 | resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max)) |
| 74 | finally: |
| 75 | f.close() |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 76 | finally: |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 77 | if limit_set: |
| 78 | resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max)) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 79 | support.unlink(support.TESTFN) |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 80 | |
| 81 | def test_fsize_toobig(self): |
| 82 | # Be sure that setrlimit is checking for really large values |
| 83 | too_big = 10**50 |
| 84 | try: |
| 85 | (cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE) |
| 86 | except AttributeError: |
| 87 | pass |
| 88 | else: |
| 89 | try: |
| 90 | resource.setrlimit(resource.RLIMIT_FSIZE, (too_big, max)) |
| 91 | except (OverflowError, ValueError): |
| 92 | pass |
| 93 | try: |
| 94 | resource.setrlimit(resource.RLIMIT_FSIZE, (max, too_big)) |
| 95 | except (OverflowError, ValueError): |
| 96 | pass |
| 97 | |
| 98 | def test_getrusage(self): |
| 99 | self.assertRaises(TypeError, resource.getrusage) |
| 100 | self.assertRaises(TypeError, resource.getrusage, 42, 42) |
| 101 | usageself = resource.getrusage(resource.RUSAGE_SELF) |
| 102 | usagechildren = resource.getrusage(resource.RUSAGE_CHILDREN) |
| 103 | # May not be available on all systems. |
| 104 | try: |
| 105 | usageboth = resource.getrusage(resource.RUSAGE_BOTH) |
| 106 | except (ValueError, AttributeError): |
| 107 | pass |
Antoine Pitrou | b6d4ee5 | 2010-11-17 16:19:35 +0000 | [diff] [blame] | 108 | try: |
| 109 | usage_thread = resource.getrusage(resource.RUSAGE_THREAD) |
| 110 | except (ValueError, AttributeError): |
| 111 | pass |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 112 | |
Serhiy Storchaka | 19c4e0d | 2013-02-04 12:47:24 +0200 | [diff] [blame] | 113 | # Issue 6083: Reference counting bug |
| 114 | def test_setrusage_refcount(self): |
| 115 | try: |
| 116 | limits = resource.getrlimit(resource.RLIMIT_CPU) |
| 117 | except AttributeError: |
| 118 | pass |
| 119 | else: |
| 120 | class BadSequence: |
| 121 | def __len__(self): |
| 122 | return 2 |
| 123 | def __getitem__(self, key): |
| 124 | if key in (0, 1): |
| 125 | return len(tuple(range(1000000))) |
| 126 | raise IndexError |
| 127 | |
| 128 | resource.setrlimit(resource.RLIMIT_CPU, BadSequence()) |
| 129 | |
Christian Heimes | 4ebc929 | 2013-07-30 15:44:13 +0200 | [diff] [blame] | 130 | def test_pagesize(self): |
| 131 | pagesize = resource.getpagesize() |
| 132 | self.assertIsInstance(pagesize, int) |
| 133 | self.assertGreaterEqual(pagesize, 0) |
| 134 | |
Christian Heimes | 6fc79bf | 2013-10-22 11:09:27 +0200 | [diff] [blame] | 135 | @unittest.skipUnless(sys.platform == 'linux', 'test requires Linux') |
| 136 | def test_linux_constants(self): |
Brett Cannon | a20800d | 2013-10-25 15:45:25 -0400 | [diff] [blame] | 137 | for attr in ['MSGQUEUE', 'NICE', 'RTPRIO', 'RTTIME', 'SIGPENDING']: |
| 138 | with contextlib.suppress(AttributeError): |
| 139 | self.assertIsInstance(getattr(resource, 'RLIMIT_' + attr), int) |
Christian Heimes | 6fc79bf | 2013-10-22 11:09:27 +0200 | [diff] [blame] | 140 | |
Christian Heimes | b7bd5df | 2013-10-22 11:21:54 +0200 | [diff] [blame] | 141 | @unittest.skipUnless(hasattr(resource, 'prlimit'), 'no prlimit') |
Christian Heimes | c4a4b34 | 2013-10-25 08:31:19 +0200 | [diff] [blame] | 142 | @support.requires_linux_version(2, 6, 36) |
Christian Heimes | b7bd5df | 2013-10-22 11:21:54 +0200 | [diff] [blame] | 143 | def test_prlimit(self): |
| 144 | self.assertRaises(TypeError, resource.prlimit) |
Christian Heimes | d885aa4 | 2013-10-22 11:45:30 +0200 | [diff] [blame] | 145 | if os.geteuid() != 0: |
| 146 | self.assertRaises(PermissionError, resource.prlimit, |
| 147 | 1, resource.RLIMIT_AS) |
Christian Heimes | b7bd5df | 2013-10-22 11:21:54 +0200 | [diff] [blame] | 148 | self.assertRaises(ProcessLookupError, resource.prlimit, |
| 149 | -1, resource.RLIMIT_AS) |
Christian Heimes | d885aa4 | 2013-10-22 11:45:30 +0200 | [diff] [blame] | 150 | limit = resource.getrlimit(resource.RLIMIT_AS) |
| 151 | self.assertEqual(resource.prlimit(0, resource.RLIMIT_AS), limit) |
| 152 | self.assertEqual(resource.prlimit(0, resource.RLIMIT_AS, limit), |
| 153 | limit) |
Christian Heimes | b7bd5df | 2013-10-22 11:21:54 +0200 | [diff] [blame] | 154 | |
| 155 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 156 | def test_main(verbose=None): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 157 | support.run_unittest(ResourceTest) |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 158 | |
| 159 | if __name__ == "__main__": |
| 160 | test_main() |