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