1 | n/a | #! /usr/bin/env python3 |
---|
2 | n/a | """Find the maximum recursion limit that prevents interpreter termination. |
---|
3 | n/a | |
---|
4 | n/a | This script finds the maximum safe recursion limit on a particular |
---|
5 | n/a | platform. If you need to change the recursion limit on your system, |
---|
6 | n/a | this script will tell you a safe upper bound. To use the new limit, |
---|
7 | n/a | call sys.setrecursionlimit(). |
---|
8 | n/a | |
---|
9 | n/a | This module implements several ways to create infinite recursion in |
---|
10 | n/a | Python. Different implementations end up pushing different numbers of |
---|
11 | n/a | C stack frames, depending on how many calls through Python's abstract |
---|
12 | n/a | C API occur. |
---|
13 | n/a | |
---|
14 | n/a | After each round of tests, it prints a message: |
---|
15 | n/a | "Limit of NNNN is fine". |
---|
16 | n/a | |
---|
17 | n/a | The highest printed value of "NNNN" is therefore the highest potentially |
---|
18 | n/a | safe limit for your system (which depends on the OS, architecture, but also |
---|
19 | n/a | the compilation flags). Please note that it is practically impossible to |
---|
20 | n/a | test all possible recursion paths in the interpreter, so the results of |
---|
21 | n/a | this test should not be trusted blindly -- although they give a good hint |
---|
22 | n/a | of which values are reasonable. |
---|
23 | n/a | |
---|
24 | n/a | NOTE: When the C stack space allocated by your system is exceeded due |
---|
25 | n/a | to excessive recursion, exact behaviour depends on the platform, although |
---|
26 | n/a | the interpreter will always fail in a likely brutal way: either a |
---|
27 | n/a | segmentation fault, a MemoryError, or just a silent abort. |
---|
28 | n/a | |
---|
29 | n/a | NB: A program that does not use __methods__ can set a higher limit. |
---|
30 | n/a | """ |
---|
31 | n/a | |
---|
32 | n/a | import sys |
---|
33 | n/a | import itertools |
---|
34 | n/a | |
---|
35 | n/a | class RecursiveBlowup1: |
---|
36 | n/a | def __init__(self): |
---|
37 | n/a | self.__init__() |
---|
38 | n/a | |
---|
39 | n/a | def test_init(): |
---|
40 | n/a | return RecursiveBlowup1() |
---|
41 | n/a | |
---|
42 | n/a | class RecursiveBlowup2: |
---|
43 | n/a | def __repr__(self): |
---|
44 | n/a | return repr(self) |
---|
45 | n/a | |
---|
46 | n/a | def test_repr(): |
---|
47 | n/a | return repr(RecursiveBlowup2()) |
---|
48 | n/a | |
---|
49 | n/a | class RecursiveBlowup4: |
---|
50 | n/a | def __add__(self, x): |
---|
51 | n/a | return x + self |
---|
52 | n/a | |
---|
53 | n/a | def test_add(): |
---|
54 | n/a | return RecursiveBlowup4() + RecursiveBlowup4() |
---|
55 | n/a | |
---|
56 | n/a | class RecursiveBlowup5: |
---|
57 | n/a | def __getattr__(self, attr): |
---|
58 | n/a | return getattr(self, attr) |
---|
59 | n/a | |
---|
60 | n/a | def test_getattr(): |
---|
61 | n/a | return RecursiveBlowup5().attr |
---|
62 | n/a | |
---|
63 | n/a | class RecursiveBlowup6: |
---|
64 | n/a | def __getitem__(self, item): |
---|
65 | n/a | return self[item - 2] + self[item - 1] |
---|
66 | n/a | |
---|
67 | n/a | def test_getitem(): |
---|
68 | n/a | return RecursiveBlowup6()[5] |
---|
69 | n/a | |
---|
70 | n/a | def test_recurse(): |
---|
71 | n/a | return test_recurse() |
---|
72 | n/a | |
---|
73 | n/a | def test_cpickle(_cache={}): |
---|
74 | n/a | import io |
---|
75 | n/a | try: |
---|
76 | n/a | import _pickle |
---|
77 | n/a | except ImportError: |
---|
78 | n/a | print("cannot import _pickle, skipped!") |
---|
79 | n/a | return |
---|
80 | n/a | k, l = None, None |
---|
81 | n/a | for n in itertools.count(): |
---|
82 | n/a | try: |
---|
83 | n/a | l = _cache[n] |
---|
84 | n/a | continue # Already tried and it works, let's save some time |
---|
85 | n/a | except KeyError: |
---|
86 | n/a | for i in range(100): |
---|
87 | n/a | l = [k, l] |
---|
88 | n/a | k = {i: l} |
---|
89 | n/a | _pickle.Pickler(io.BytesIO(), protocol=-1).dump(l) |
---|
90 | n/a | _cache[n] = l |
---|
91 | n/a | |
---|
92 | n/a | def test_compiler_recursion(): |
---|
93 | n/a | # The compiler uses a scaling factor to support additional levels |
---|
94 | n/a | # of recursion. This is a sanity check of that scaling to ensure |
---|
95 | n/a | # it still raises RecursionError even at higher recursion limits |
---|
96 | n/a | compile("()" * (10 * sys.getrecursionlimit()), "<single>", "single") |
---|
97 | n/a | |
---|
98 | n/a | def check_limit(n, test_func_name): |
---|
99 | n/a | sys.setrecursionlimit(n) |
---|
100 | n/a | if test_func_name.startswith("test_"): |
---|
101 | n/a | print(test_func_name[5:]) |
---|
102 | n/a | else: |
---|
103 | n/a | print(test_func_name) |
---|
104 | n/a | test_func = globals()[test_func_name] |
---|
105 | n/a | try: |
---|
106 | n/a | test_func() |
---|
107 | n/a | # AttributeError can be raised because of the way e.g. PyDict_GetItem() |
---|
108 | n/a | # silences all exceptions and returns NULL, which is usually interpreted |
---|
109 | n/a | # as "missing attribute". |
---|
110 | n/a | except (RecursionError, AttributeError): |
---|
111 | n/a | pass |
---|
112 | n/a | else: |
---|
113 | n/a | print("Yikes!") |
---|
114 | n/a | |
---|
115 | n/a | if __name__ == '__main__': |
---|
116 | n/a | |
---|
117 | n/a | limit = 1000 |
---|
118 | n/a | while 1: |
---|
119 | n/a | check_limit(limit, "test_recurse") |
---|
120 | n/a | check_limit(limit, "test_add") |
---|
121 | n/a | check_limit(limit, "test_repr") |
---|
122 | n/a | check_limit(limit, "test_init") |
---|
123 | n/a | check_limit(limit, "test_getattr") |
---|
124 | n/a | check_limit(limit, "test_getitem") |
---|
125 | n/a | check_limit(limit, "test_cpickle") |
---|
126 | n/a | check_limit(limit, "test_compiler_recursion") |
---|
127 | n/a | print("Limit of %d is fine" % limit) |
---|
128 | n/a | limit = limit + 100 |
---|