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 |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 23 | import itertools |
Jeremy Hylton | c18b7d9 | 2000-08-31 19:24:17 +0000 | [diff] [blame] | 24 | |
| 25 | class RecursiveBlowup1: |
| 26 | def __init__(self): |
| 27 | self.__init__() |
| 28 | |
| 29 | def test_init(): |
| 30 | return RecursiveBlowup1() |
| 31 | |
| 32 | class RecursiveBlowup2: |
| 33 | def __repr__(self): |
| 34 | return repr(self) |
| 35 | |
| 36 | def test_repr(): |
| 37 | return repr(RecursiveBlowup2()) |
| 38 | |
| 39 | class RecursiveBlowup4: |
| 40 | def __add__(self, x): |
| 41 | return x + self |
| 42 | |
| 43 | def test_add(): |
| 44 | return RecursiveBlowup4() + RecursiveBlowup4() |
| 45 | |
| 46 | class RecursiveBlowup5: |
| 47 | def __getattr__(self, attr): |
| 48 | return getattr(self, attr) |
| 49 | |
| 50 | def test_getattr(): |
| 51 | return RecursiveBlowup5().attr |
| 52 | |
| 53 | class RecursiveBlowup6: |
| 54 | def __getitem__(self, item): |
| 55 | return self[item - 2] + self[item - 1] |
| 56 | |
| 57 | def test_getitem(): |
| 58 | return RecursiveBlowup6()[5] |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 59 | |
Jeremy Hylton | c18b7d9 | 2000-08-31 19:24:17 +0000 | [diff] [blame] | 60 | def test_recurse(): |
| 61 | return test_recurse() |
| 62 | |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 63 | def test_cpickle(_cache={}): |
| 64 | import io |
| 65 | try: |
| 66 | import _pickle |
| 67 | except ImportError: |
| 68 | print("cannot import _pickle, skipped!") |
| 69 | return |
| 70 | l = None |
| 71 | for n in itertools.count(): |
| 72 | try: |
| 73 | l = _cache[n] |
| 74 | continue # Already tried and it works, let's save some time |
| 75 | except KeyError: |
| 76 | for i in range(100): |
| 77 | l = [l] |
| 78 | _pickle.Pickler(io.BytesIO(), protocol=-1).dump(l) |
| 79 | _cache[n] = l |
| 80 | |
Jeremy Hylton | c18b7d9 | 2000-08-31 19:24:17 +0000 | [diff] [blame] | 81 | def check_limit(n, test_func_name): |
| 82 | sys.setrecursionlimit(n) |
| 83 | if test_func_name.startswith("test_"): |
Collin Winter | e7bf59f | 2007-08-30 18:39:28 +0000 | [diff] [blame] | 84 | print(test_func_name[5:]) |
Jeremy Hylton | c18b7d9 | 2000-08-31 19:24:17 +0000 | [diff] [blame] | 85 | else: |
Collin Winter | e7bf59f | 2007-08-30 18:39:28 +0000 | [diff] [blame] | 86 | print(test_func_name) |
Jeremy Hylton | c18b7d9 | 2000-08-31 19:24:17 +0000 | [diff] [blame] | 87 | test_func = globals()[test_func_name] |
| 88 | try: |
| 89 | test_func() |
| 90 | except RuntimeError: |
| 91 | pass |
| 92 | else: |
Collin Winter | e7bf59f | 2007-08-30 18:39:28 +0000 | [diff] [blame] | 93 | print("Yikes!") |
Jeremy Hylton | c18b7d9 | 2000-08-31 19:24:17 +0000 | [diff] [blame] | 94 | |
| 95 | limit = 1000 |
| 96 | while 1: |
| 97 | check_limit(limit, "test_recurse") |
| 98 | check_limit(limit, "test_add") |
| 99 | check_limit(limit, "test_repr") |
| 100 | check_limit(limit, "test_init") |
| 101 | check_limit(limit, "test_getattr") |
| 102 | check_limit(limit, "test_getitem") |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 103 | check_limit(limit, "test_cpickle") |
Collin Winter | e7bf59f | 2007-08-30 18:39:28 +0000 | [diff] [blame] | 104 | print("Limit of %d is fine" % limit) |
Jeremy Hylton | c18b7d9 | 2000-08-31 19:24:17 +0000 | [diff] [blame] | 105 | limit = limit + 100 |