Jeremy Hylton | c18b7d9 | 2000-08-31 19:24:17 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | """Find the maximum recursion limit that prevents core dumps |
| 3 | |
| 4 | This script finds the maximum safe recursion limit on a particular |
| 5 | platform. If you need to change the recursion limit on your system, |
| 6 | this script will tell you a safe upper bound. To use the new limit, |
| 7 | call sys.setrecursionlimit. |
| 8 | |
| 9 | This module implements several ways to create infinite recursion in |
| 10 | Python. Different implementations end up pushing different numbers of |
| 11 | C stack frames, depending on how many calls through Python's abstract |
| 12 | C API occur. |
| 13 | |
| 14 | After each round of tests, it prints a message |
| 15 | Limit of NNNN is fine. |
| 16 | |
| 17 | It ends when Python causes a segmentation fault because the limit is |
| 18 | too high. On platforms like Mac and Windows, it should exit with a |
| 19 | MemoryError. |
Jeremy Hylton | c18b7d9 | 2000-08-31 19:24:17 +0000 | [diff] [blame] | 20 | """ |
| 21 | |
| 22 | import sys |
| 23 | |
| 24 | class RecursiveBlowup1: |
| 25 | def __init__(self): |
| 26 | self.__init__() |
| 27 | |
| 28 | def test_init(): |
| 29 | return RecursiveBlowup1() |
| 30 | |
| 31 | class RecursiveBlowup2: |
| 32 | def __repr__(self): |
| 33 | return repr(self) |
| 34 | |
| 35 | def test_repr(): |
| 36 | return repr(RecursiveBlowup2()) |
| 37 | |
| 38 | class RecursiveBlowup4: |
| 39 | def __add__(self, x): |
| 40 | return x + self |
| 41 | |
| 42 | def test_add(): |
| 43 | return RecursiveBlowup4() + RecursiveBlowup4() |
| 44 | |
| 45 | class RecursiveBlowup5: |
| 46 | def __getattr__(self, attr): |
| 47 | return getattr(self, attr) |
| 48 | |
| 49 | def test_getattr(): |
| 50 | return RecursiveBlowup5().attr |
| 51 | |
| 52 | class RecursiveBlowup6: |
| 53 | def __getitem__(self, item): |
| 54 | return self[item - 2] + self[item - 1] |
| 55 | |
| 56 | def test_getitem(): |
| 57 | return RecursiveBlowup6()[5] |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 58 | |
Jeremy Hylton | c18b7d9 | 2000-08-31 19:24:17 +0000 | [diff] [blame] | 59 | def test_recurse(): |
| 60 | return test_recurse() |
| 61 | |
| 62 | def check_limit(n, test_func_name): |
| 63 | sys.setrecursionlimit(n) |
| 64 | if test_func_name.startswith("test_"): |
Collin Winter | e7bf59f | 2007-08-30 18:39:28 +0000 | [diff] [blame] | 65 | print(test_func_name[5:]) |
Jeremy Hylton | c18b7d9 | 2000-08-31 19:24:17 +0000 | [diff] [blame] | 66 | else: |
Collin Winter | e7bf59f | 2007-08-30 18:39:28 +0000 | [diff] [blame] | 67 | print(test_func_name) |
Jeremy Hylton | c18b7d9 | 2000-08-31 19:24:17 +0000 | [diff] [blame] | 68 | test_func = globals()[test_func_name] |
| 69 | try: |
| 70 | test_func() |
| 71 | except RuntimeError: |
| 72 | pass |
| 73 | else: |
Collin Winter | e7bf59f | 2007-08-30 18:39:28 +0000 | [diff] [blame] | 74 | print("Yikes!") |
Jeremy Hylton | c18b7d9 | 2000-08-31 19:24:17 +0000 | [diff] [blame] | 75 | |
| 76 | limit = 1000 |
| 77 | while 1: |
| 78 | check_limit(limit, "test_recurse") |
| 79 | check_limit(limit, "test_add") |
| 80 | check_limit(limit, "test_repr") |
| 81 | check_limit(limit, "test_init") |
| 82 | check_limit(limit, "test_getattr") |
| 83 | check_limit(limit, "test_getitem") |
Collin Winter | e7bf59f | 2007-08-30 18:39:28 +0000 | [diff] [blame] | 84 | print("Limit of %d is fine" % limit) |
Jeremy Hylton | c18b7d9 | 2000-08-31 19:24:17 +0000 | [diff] [blame] | 85 | limit = limit + 100 |