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.
|
---|
20 |
|
---|
21 | NB: A program that does not use __methods__ can set a higher limit.
|
---|
22 | """
|
---|
23 |
|
---|
24 | import sys
|
---|
25 |
|
---|
26 | class RecursiveBlowup1:
|
---|
27 | def __init__(self):
|
---|
28 | self.__init__()
|
---|
29 |
|
---|
30 | def test_init():
|
---|
31 | return RecursiveBlowup1()
|
---|
32 |
|
---|
33 | class RecursiveBlowup2:
|
---|
34 | def __repr__(self):
|
---|
35 | return repr(self)
|
---|
36 |
|
---|
37 | def test_repr():
|
---|
38 | return repr(RecursiveBlowup2())
|
---|
39 |
|
---|
40 | class RecursiveBlowup4:
|
---|
41 | def __add__(self, x):
|
---|
42 | return x + self
|
---|
43 |
|
---|
44 | def test_add():
|
---|
45 | return RecursiveBlowup4() + RecursiveBlowup4()
|
---|
46 |
|
---|
47 | class RecursiveBlowup5:
|
---|
48 | def __getattr__(self, attr):
|
---|
49 | return getattr(self, attr)
|
---|
50 |
|
---|
51 | def test_getattr():
|
---|
52 | return RecursiveBlowup5().attr
|
---|
53 |
|
---|
54 | class RecursiveBlowup6:
|
---|
55 | def __getitem__(self, item):
|
---|
56 | return self[item - 2] + self[item - 1]
|
---|
57 |
|
---|
58 | def test_getitem():
|
---|
59 | return RecursiveBlowup6()[5]
|
---|
60 |
|
---|
61 | def test_recurse():
|
---|
62 | return test_recurse()
|
---|
63 |
|
---|
64 | def check_limit(n, test_func_name):
|
---|
65 | sys.setrecursionlimit(n)
|
---|
66 | if test_func_name.startswith("test_"):
|
---|
67 | print test_func_name[5:]
|
---|
68 | else:
|
---|
69 | print test_func_name
|
---|
70 | test_func = globals()[test_func_name]
|
---|
71 | try:
|
---|
72 | test_func()
|
---|
73 | except RuntimeError:
|
---|
74 | pass
|
---|
75 | else:
|
---|
76 | print "Yikes!"
|
---|
77 |
|
---|
78 | limit = 1000
|
---|
79 | while 1:
|
---|
80 | check_limit(limit, "test_recurse")
|
---|
81 | check_limit(limit, "test_add")
|
---|
82 | check_limit(limit, "test_repr")
|
---|
83 | check_limit(limit, "test_init")
|
---|
84 | check_limit(limit, "test_getattr")
|
---|
85 | check_limit(limit, "test_getitem")
|
---|
86 | print "Limit of %d is fine" % limit
|
---|
87 | limit = limit + 100
|
---|