|
1 | 1 | import contextlib |
2 | 2 | import sys |
| 3 | +import textwrap |
3 | 4 | import unittest |
4 | 5 | from test import support |
5 | 6 | from test.support import import_helper |
6 | 7 | from test.support import os_helper |
| 8 | +from test.support import script_helper |
7 | 9 |
|
8 | 10 | resource = import_helper.import_module('resource') |
9 | 11 |
|
@@ -234,6 +236,35 @@ def __getitem__(self, key): |
234 | 236 | self.assertEqual(resource.prlimit(0, resource.RLIMIT_AS, BadSeq()), |
235 | 237 | limits) |
236 | 238 |
|
| 239 | + @unittest.skipUnless(sys.platform == 'linux', 'Linux only') |
| 240 | + @unittest.skipIf(support.linked_to_musl(), |
| 241 | + 'no C stack detection on musl, gh-159876') |
| 242 | + @unittest.skipUnless(hasattr(resource, 'RLIMIT_STACK'), |
| 243 | + 'requires resource.RLIMIT_STACK') |
| 244 | + def test_lowered_stack_limit(self): |
| 245 | + code = textwrap.dedent(''' |
| 246 | + import resource, sys |
| 247 | + soft, hard = resource.getrlimit(resource.RLIMIT_STACK) |
| 248 | + limits = (1024 * 1024, hard) |
| 249 | + if sys.argv[1] == 'setrlimit': |
| 250 | + resource.setrlimit(resource.RLIMIT_STACK, limits) |
| 251 | + else: |
| 252 | + resource.prlimit(0, resource.RLIMIT_STACK, limits) |
| 253 | + value = None |
| 254 | + for _ in range(100_000): |
| 255 | + value = [value] |
| 256 | + try: |
| 257 | + repr(value) |
| 258 | + except RecursionError: |
| 259 | + pass |
| 260 | + del value |
| 261 | + ''') |
| 262 | + for func in 'setrlimit', 'prlimit': |
| 263 | + with self.subTest(func=func): |
| 264 | + if not hasattr(resource, func): |
| 265 | + self.skipTest(f'requires resource.{func}') |
| 266 | + script_helper.assert_python_ok('-c', code, func) |
| 267 | + |
237 | 268 |
|
238 | 269 | if __name__ == "__main__": |
239 | 270 | unittest.main() |
0 commit comments